andre@0: /* andre@0: * mplogic.c andre@0: * andre@0: * Bitwise logical operations on MPI values andre@0: * andre@0: * This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "mpi-priv.h" andre@0: #include "mplogic.h" andre@0: andre@0: /* {{{ Lookup table for population count */ andre@0: andre@0: static unsigned char bitc[] = { andre@0: 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, andre@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, andre@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, andre@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, andre@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, andre@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, andre@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, andre@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, andre@0: 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, andre@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, andre@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, andre@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, andre@0: 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, andre@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, andre@0: 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, andre@0: 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 andre@0: }; andre@0: andre@0: /* }}} */ andre@0: andre@0: /*------------------------------------------------------------------------*/ andre@0: /* andre@0: mpl_not(a, b) - compute b = ~a andre@0: mpl_and(a, b, c) - compute c = a & b andre@0: mpl_or(a, b, c) - compute c = a | b andre@0: mpl_xor(a, b, c) - compute c = a ^ b andre@0: */ andre@0: andre@0: /* {{{ mpl_not(a, b) */ andre@0: andre@0: mp_err mpl_not(mp_int *a, mp_int *b) andre@0: { andre@0: mp_err res; andre@0: unsigned int ix; andre@0: andre@0: ARGCHK(a != NULL && b != NULL, MP_BADARG); andre@0: andre@0: if((res = mp_copy(a, b)) != MP_OKAY) andre@0: return res; andre@0: andre@0: /* This relies on the fact that the digit type is unsigned */ andre@0: for(ix = 0; ix < USED(b); ix++) andre@0: DIGIT(b, ix) = ~DIGIT(b, ix); andre@0: andre@0: s_mp_clamp(b); andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpl_not() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpl_and(a, b, c) */ andre@0: andre@0: mp_err mpl_and(mp_int *a, mp_int *b, mp_int *c) andre@0: { andre@0: mp_int *which, *other; andre@0: mp_err res; andre@0: unsigned int ix; andre@0: andre@0: ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); andre@0: andre@0: if(USED(a) <= USED(b)) { andre@0: which = a; andre@0: other = b; andre@0: } else { andre@0: which = b; andre@0: other = a; andre@0: } andre@0: andre@0: if((res = mp_copy(which, c)) != MP_OKAY) andre@0: return res; andre@0: andre@0: for(ix = 0; ix < USED(which); ix++) andre@0: DIGIT(c, ix) &= DIGIT(other, ix); andre@0: andre@0: s_mp_clamp(c); andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpl_and() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpl_or(a, b, c) */ andre@0: andre@0: mp_err mpl_or(mp_int *a, mp_int *b, mp_int *c) andre@0: { andre@0: mp_int *which, *other; andre@0: mp_err res; andre@0: unsigned int ix; andre@0: andre@0: ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); andre@0: andre@0: if(USED(a) >= USED(b)) { andre@0: which = a; andre@0: other = b; andre@0: } else { andre@0: which = b; andre@0: other = a; andre@0: } andre@0: andre@0: if((res = mp_copy(which, c)) != MP_OKAY) andre@0: return res; andre@0: andre@0: for(ix = 0; ix < USED(which); ix++) andre@0: DIGIT(c, ix) |= DIGIT(other, ix); andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpl_or() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpl_xor(a, b, c) */ andre@0: andre@0: mp_err mpl_xor(mp_int *a, mp_int *b, mp_int *c) andre@0: { andre@0: mp_int *which, *other; andre@0: mp_err res; andre@0: unsigned int ix; andre@0: andre@0: ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); andre@0: andre@0: if(USED(a) >= USED(b)) { andre@0: which = a; andre@0: other = b; andre@0: } else { andre@0: which = b; andre@0: other = a; andre@0: } andre@0: andre@0: if((res = mp_copy(which, c)) != MP_OKAY) andre@0: return res; andre@0: andre@0: for(ix = 0; ix < USED(which); ix++) andre@0: DIGIT(c, ix) ^= DIGIT(other, ix); andre@0: andre@0: s_mp_clamp(c); andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpl_xor() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /*------------------------------------------------------------------------*/ andre@0: /* andre@0: mpl_rsh(a, b, d) - b = a >> d andre@0: mpl_lsh(a, b, d) - b = a << d andre@0: */ andre@0: andre@0: /* {{{ mpl_rsh(a, b, d) */ andre@0: andre@0: mp_err mpl_rsh(const mp_int *a, mp_int *b, mp_digit d) andre@0: { andre@0: mp_err res; andre@0: andre@0: ARGCHK(a != NULL && b != NULL, MP_BADARG); andre@0: andre@0: if((res = mp_copy(a, b)) != MP_OKAY) andre@0: return res; andre@0: andre@0: s_mp_div_2d(b, d); andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpl_rsh() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpl_lsh(a, b, d) */ andre@0: andre@0: mp_err mpl_lsh(const mp_int *a, mp_int *b, mp_digit d) andre@0: { andre@0: mp_err res; andre@0: andre@0: ARGCHK(a != NULL && b != NULL, MP_BADARG); andre@0: andre@0: if((res = mp_copy(a, b)) != MP_OKAY) andre@0: return res; andre@0: andre@0: return s_mp_mul_2d(b, d); andre@0: andre@0: } /* end mpl_lsh() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /*------------------------------------------------------------------------*/ andre@0: /* andre@0: mpl_num_set(a, num) andre@0: andre@0: Count the number of set bits in the binary representation of a. andre@0: Returns MP_OKAY and sets 'num' to be the number of such bits, if andre@0: possible. If num is NULL, the result is thrown away, but it is andre@0: not considered an error. andre@0: andre@0: mpl_num_clear() does basically the same thing for clear bits. andre@0: */ andre@0: andre@0: /* {{{ mpl_num_set(a, num) */ andre@0: andre@0: mp_err mpl_num_set(mp_int *a, int *num) andre@0: { andre@0: unsigned int ix; andre@0: int db, nset = 0; andre@0: mp_digit cur; andre@0: unsigned char reg; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: for(ix = 0; ix < USED(a); ix++) { andre@0: cur = DIGIT(a, ix); andre@0: andre@0: for(db = 0; db < sizeof(mp_digit); db++) { andre@0: reg = (unsigned char)(cur >> (CHAR_BIT * db)); andre@0: andre@0: nset += bitc[reg]; andre@0: } andre@0: } andre@0: andre@0: if(num) andre@0: *num = nset; andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpl_num_set() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpl_num_clear(a, num) */ andre@0: andre@0: mp_err mpl_num_clear(mp_int *a, int *num) andre@0: { andre@0: unsigned int ix; andre@0: int db, nset = 0; andre@0: mp_digit cur; andre@0: unsigned char reg; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: for(ix = 0; ix < USED(a); ix++) { andre@0: cur = DIGIT(a, ix); andre@0: andre@0: for(db = 0; db < sizeof(mp_digit); db++) { andre@0: reg = (unsigned char)(cur >> (CHAR_BIT * db)); andre@0: andre@0: nset += bitc[UCHAR_MAX - reg]; andre@0: } andre@0: } andre@0: andre@0: if(num) andre@0: *num = nset; andre@0: andre@0: return MP_OKAY; andre@0: andre@0: andre@0: } /* end mpl_num_clear() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /*------------------------------------------------------------------------*/ andre@0: /* andre@0: mpl_parity(a) andre@0: andre@0: Determines the bitwise parity of the value given. Returns MP_EVEN andre@0: if an even number of digits are set, MP_ODD if an odd number are andre@0: set. andre@0: */ andre@0: andre@0: /* {{{ mpl_parity(a) */ andre@0: andre@0: mp_err mpl_parity(mp_int *a) andre@0: { andre@0: unsigned int ix; andre@0: int par = 0; andre@0: mp_digit cur; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: for(ix = 0; ix < USED(a); ix++) { andre@0: int shft = (sizeof(mp_digit) * CHAR_BIT) / 2; andre@0: andre@0: cur = DIGIT(a, ix); andre@0: andre@0: /* Compute parity for current digit */ andre@0: while(shft != 0) { andre@0: cur ^= (cur >> shft); andre@0: shft >>= 1; andre@0: } andre@0: cur &= 1; andre@0: andre@0: /* XOR with running parity so far */ andre@0: par ^= cur; andre@0: } andre@0: andre@0: if(par) andre@0: return MP_ODD; andre@0: else andre@0: return MP_EVEN; andre@0: andre@0: } /* end mpl_parity() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* andre@0: mpl_set_bit andre@0: andre@0: Returns MP_OKAY or some error code. andre@0: Grows a if needed to set a bit to 1. andre@0: */ andre@0: mp_err mpl_set_bit(mp_int *a, mp_size bitNum, mp_size value) andre@0: { andre@0: mp_size ix; andre@0: mp_err rv; andre@0: mp_digit mask; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: ix = bitNum / MP_DIGIT_BIT; andre@0: if (ix + 1 > MP_USED(a)) { andre@0: rv = s_mp_pad(a, ix + 1); andre@0: if (rv != MP_OKAY) andre@0: return rv; andre@0: } andre@0: andre@0: bitNum = bitNum % MP_DIGIT_BIT; andre@0: mask = (mp_digit)1 << bitNum; andre@0: if (value) andre@0: MP_DIGIT(a,ix) |= mask; andre@0: else andre@0: MP_DIGIT(a,ix) &= ~mask; andre@0: s_mp_clamp(a); andre@0: return MP_OKAY; andre@0: } andre@0: andre@0: /* andre@0: mpl_get_bit andre@0: andre@0: returns 0 or 1 or some (negative) error code. andre@0: */ andre@0: mp_err mpl_get_bit(const mp_int *a, mp_size bitNum) andre@0: { andre@0: mp_size bit, ix; andre@0: mp_err rv; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: ix = bitNum / MP_DIGIT_BIT; andre@0: ARGCHK(ix <= MP_USED(a) - 1, MP_RANGE); andre@0: andre@0: bit = bitNum % MP_DIGIT_BIT; andre@0: rv = (mp_err)(MP_DIGIT(a, ix) >> bit) & 1; andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: mpl_get_bits andre@0: - Extracts numBits bits from a, where the least significant extracted bit andre@0: is bit lsbNum. Returns a negative value if error occurs. andre@0: - Because sign bit is used to indicate error, maximum number of bits to andre@0: be returned is the lesser of (a) the number of bits in an mp_digit, or andre@0: (b) one less than the number of bits in an mp_err. andre@0: - lsbNum + numbits can be greater than the number of significant bits in andre@0: integer a, as long as bit lsbNum is in the high order digit of a. andre@0: */ andre@0: mp_err mpl_get_bits(const mp_int *a, mp_size lsbNum, mp_size numBits) andre@0: { andre@0: mp_size rshift = (lsbNum % MP_DIGIT_BIT); andre@0: mp_size lsWndx = (lsbNum / MP_DIGIT_BIT); andre@0: mp_digit * digit = MP_DIGITS(a) + lsWndx; andre@0: mp_digit mask = ((1 << numBits) - 1); andre@0: andre@0: ARGCHK(numBits < CHAR_BIT * sizeof mask, MP_BADARG); andre@0: ARGCHK(MP_HOWMANY(lsbNum, MP_DIGIT_BIT) <= MP_USED(a), MP_RANGE); andre@0: andre@0: if ((numBits + lsbNum % MP_DIGIT_BIT <= MP_DIGIT_BIT) || andre@0: (lsWndx + 1 >= MP_USED(a))) { andre@0: mask &= (digit[0] >> rshift); andre@0: } else { andre@0: mask &= ((digit[0] >> rshift) | (digit[1] << (MP_DIGIT_BIT - rshift))); andre@0: } andre@0: return (mp_err)mask; andre@0: } andre@0: andre@0: /* andre@0: mpl_significant_bits andre@0: returns number of significnant bits in abs(a). andre@0: returns 1 if value is zero. andre@0: */ andre@0: mp_err mpl_significant_bits(const mp_int *a) andre@0: { andre@0: mp_err bits = 0; andre@0: int ix; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: ix = MP_USED(a); andre@0: for (ix = MP_USED(a); ix > 0; ) { andre@0: mp_digit d; andre@0: d = MP_DIGIT(a, --ix); andre@0: if (d) { andre@0: while (d) { andre@0: ++bits; andre@0: d >>= 1; andre@0: } andre@0: break; andre@0: } andre@0: } andre@0: bits += ix * MP_DIGIT_BIT; andre@0: if (!bits) andre@0: bits = 1; andre@0: return bits; andre@0: } andre@0: andre@0: /*------------------------------------------------------------------------*/ andre@0: /* HERE THERE BE DRAGONS */