comparison libtommath/bn_mp_xor.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h" 1 #include "tommath_private.h"
2 #ifdef BN_MP_XOR_C 2 #ifdef BN_MP_XOR_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* XOR two ints together */ 6 /* two complement xor */
16 int mp_xor(const mp_int *a, const mp_int *b, mp_int *c) 7 mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
17 { 8 {
18 int res, ix, px; 9 int used = MP_MAX(a->used, b->used) + 1, i;
19 mp_int t; 10 mp_err err;
20 const mp_int *x; 11 mp_digit ac = 1, bc = 1, cc = 1;
12 mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
21 13
22 if (a->used > b->used) { 14 if (c->alloc < used) {
23 if ((res = mp_init_copy(&t, a)) != MP_OKAY) { 15 if ((err = mp_grow(c, used)) != MP_OKAY) {
24 return res; 16 return err;
25 } 17 }
26 px = b->used;
27 x = b;
28 } else {
29 if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
30 return res;
31 }
32 px = a->used;
33 x = a;
34 } 18 }
35 19
36 for (ix = 0; ix < px; ix++) { 20 for (i = 0; i < used; i++) {
37 t.dp[ix] ^= x->dp[ix]; 21 mp_digit x, y;
22
23 /* convert to two complement if negative */
24 if (a->sign == MP_NEG) {
25 ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
26 x = ac & MP_MASK;
27 ac >>= MP_DIGIT_BIT;
28 } else {
29 x = (i >= a->used) ? 0uL : a->dp[i];
30 }
31
32 /* convert to two complement if negative */
33 if (b->sign == MP_NEG) {
34 bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
35 y = bc & MP_MASK;
36 bc >>= MP_DIGIT_BIT;
37 } else {
38 y = (i >= b->used) ? 0uL : b->dp[i];
39 }
40
41 c->dp[i] = x ^ y;
42
43 /* convert to to sign-magnitude if negative */
44 if (csign == MP_NEG) {
45 cc += ~c->dp[i] & MP_MASK;
46 c->dp[i] = cc & MP_MASK;
47 cc >>= MP_DIGIT_BIT;
48 }
38 } 49 }
39 mp_clamp(&t); 50
40 mp_exch(c, &t); 51 c->used = used;
41 mp_clear(&t); 52 c->sign = csign;
53 mp_clamp(c);
42 return MP_OKAY; 54 return MP_OKAY;
43 } 55 }
44 #endif 56 #endif
45
46 /* ref: HEAD -> master, tag: v1.1.0 */
47 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
48 /* commit time: 2019-01-28 20:32:32 +0100 */