diff --git a/mpint.c b/mpint.c index 34805d81..3e0ab530 100644 --- a/mpint.c +++ b/mpint.c @@ -758,7 +758,7 @@ static BignumCarry mp_add_masked_integer_into( for (size_t i = 0; i < rw; i++) { BignumInt aword = mp_word(a, i); size_t shift = i * BIGNUM_INT_BITS; - BignumInt bword = shift < BIGNUM_INT_BYTES ? b >> shift : 0; + BignumInt bword = shift < CHAR_BIT*sizeof(b) ? b >> shift : 0; BignumInt out; bword = (bword ^ b_xor) & b_and; BignumADC(out, carry, aword, bword, carry); @@ -799,7 +799,8 @@ static void mp_add_integer_into_shifted_by_words( * shift n down. If it's 0, we add zero bits into r, and * leave n alone. */ BignumInt bword = n & -(BignumInt)indicator; - uintmax_t new_n = (BIGNUM_INT_BITS < 64 ? n >> BIGNUM_INT_BITS : 0); + uintmax_t new_n = (BIGNUM_INT_BYTES < sizeof(n) ? + n >> BIGNUM_INT_BITS : 0); n ^= (n ^ new_n) & -(uintmax_t)indicator; BignumInt aword = mp_word(a, i); diff --git a/test/cryptsuite.py b/test/cryptsuite.py index 60fc8cc5..7638d346 100755 --- a/test/cryptsuite.py +++ b/test/cryptsuite.py @@ -335,6 +335,22 @@ class mpint(MyTestBase): bm = mp_copy(bi) self.assertEqual(int(mp_mul(am, bm)), ai * bi) + def testAddInteger(self): + initial = mp_copy(4444444444444444444444444) + + x = mp_new(mp_max_bits(initial) + 64) + + # mp_{add,sub}_integer_into should be able to cope with any + # uintmax_t. Test a number that requires more than 32 bits. + mp_add_integer_into(x, initial, 123123123123123) + self.assertEqual(int(x), 4444444444567567567567567) + mp_sub_integer_into(x, initial, 123123123123123) + self.assertEqual(int(x), 4444444444321321321321321) + + # mp_mul_integer_into only takes a uint16_t integer input + mp_mul_integer_into(x, initial, 10001) + self.assertEqual(int(x), 44448888888888888888888884444) + def testDivision(self): divisors = [1, 2, 3, 2**16+1, 2**32-1, 2**32+1, 2**128-159, 141421356237309504880168872420969807856967187537694807]