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