2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2004-10-29 22:07:18 +00:00
|
|
|
#ifdef BN_MP_XOR_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2019-05-20 17:11:25 +00:00
|
|
|
/* two complement xor */
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2019-05-20 17:11:25 +00:00
|
|
|
int used = MP_MAX(a->used, b->used) + 1, i;
|
|
|
|
mp_err err;
|
|
|
|
mp_digit ac = 1, bc = 1, cc = 1;
|
|
|
|
mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2019-05-20 17:11:25 +00:00
|
|
|
if (c->alloc < used) {
|
|
|
|
if ((err = mp_grow(c, used)) != MP_OKAY) {
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2017-08-30 17:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2019-05-20 17:11:25 +00:00
|
|
|
for (i = 0; i < used; i++) {
|
|
|
|
mp_digit x, y;
|
|
|
|
|
|
|
|
/* convert to two complement if negative */
|
|
|
|
if (a->sign == MP_NEG) {
|
2019-05-22 06:16:38 +00:00
|
|
|
ac += (i >= a->used) ? MP_MASK : (~a->dp[i] & MP_MASK);
|
2019-05-20 17:11:25 +00:00
|
|
|
x = ac & MP_MASK;
|
|
|
|
ac >>= MP_DIGIT_BIT;
|
|
|
|
} else {
|
2019-05-21 19:11:56 +00:00
|
|
|
x = (i >= a->used) ? 0uL : a->dp[i];
|
2019-05-20 17:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* convert to two complement if negative */
|
|
|
|
if (b->sign == MP_NEG) {
|
2019-05-22 06:16:38 +00:00
|
|
|
bc += (i >= b->used) ? MP_MASK : (~b->dp[i] & MP_MASK);
|
2019-05-20 17:11:25 +00:00
|
|
|
y = bc & MP_MASK;
|
|
|
|
bc >>= MP_DIGIT_BIT;
|
|
|
|
} else {
|
2019-05-21 19:11:56 +00:00
|
|
|
y = (i >= b->used) ? 0uL : b->dp[i];
|
2019-05-20 17:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c->dp[i] = x ^ y;
|
|
|
|
|
|
|
|
/* convert to to sign-magnitude if negative */
|
|
|
|
if (csign == MP_NEG) {
|
|
|
|
cc += ~c->dp[i] & MP_MASK;
|
|
|
|
c->dp[i] = cc & MP_MASK;
|
|
|
|
cc >>= MP_DIGIT_BIT;
|
|
|
|
}
|
2017-08-30 17:15:27 +00:00
|
|
|
}
|
2019-05-20 17:11:25 +00:00
|
|
|
|
|
|
|
c->used = used;
|
|
|
|
c->sign = csign;
|
|
|
|
mp_clamp(c);
|
2017-08-30 17:15:27 +00:00
|
|
|
return MP_OKAY;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|