def save_load(path): testcase = sealapi.BigUInt(16, "123B") testcase.save(path) save_test = sealapi.BigUInt() save_test.load(path) assert save_test.bit_count() == 16 assert save_test == 0x123B
def test_biguint_sanity(): testcase = sealapi.BigUInt() assert testcase.is_zero() testcase = sealapi.BigUInt(16) assert testcase.bit_count() == 16 testcase = sealapi.BigUInt("123A") assert testcase == 0x123A testcase = sealapi.BigUInt(16, "123B") assert testcase.bit_count() == 16 assert testcase == 0x123B testcase = sealapi.BigUInt(16, 45) assert testcase.bit_count() == 16 assert testcase == 45 testcase = sealapi.BigUInt(128, 456) testcase2 = sealapi.BigUInt(testcase) assert testcase2.bit_count() == 128 assert testcase2 == 456 testcase = sealapi.BigUInt(8, 23) assert testcase.bit_count() == 8 assert testcase.byte_count() == 1 testcase = sealapi.BigUInt(128, 34) assert testcase.uint64_count() == 2 assert testcase.significant_bit_count() == 6 assert testcase.to_double() == 34 assert testcase.to_string() == "22" # hex string assert testcase.to_dec_string() == "34" assert testcase.data() == 34 assert not testcase.is_zero() testcase.set_zero() assert testcase.is_zero() testcase = sealapi.BigUInt(128) assert testcase.bit_count() == 128 testcase.resize(16) assert testcase.bit_count() == 16 def save_load(path): testcase = sealapi.BigUInt(16, "123B") testcase.save(path) save_test = sealapi.BigUInt() save_test.load(path) assert save_test.bit_count() == 16 assert save_test == 0x123B tmp_file(save_load)
def test_intencoder(ctx): encoder = sealapi.IntegerEncoder(ctx) enc = encoder.encode(5) assert enc.to_string() == "1x^2 + 1" assert enc.int_array()[2] == 1 assert encoder.decode_uint32(enc) == 5 assert encoder.decode_int32(enc) == 5 assert encoder.decode_uint64(enc) == 5 assert encoder.decode_int32(enc) == 5 enc = sealapi.Plaintext() encoder.encode(7, enc) assert enc.to_string() == "1x^2 + 1x^1 + 1" big = sealapi.BigUInt("5555555555") enc = encoder.encode(big) assert enc.int_array()[0] == 1 assert encoder.decode_biguint(enc) == 0x5555555555 enc = sealapi.Plaintext() encoder.encode(big + 1, enc) assert encoder.decode_biguint(enc) == 0x5555555556
def test_biguint_ops(): numerator = 555555555555555555 denominator = 77777777777 num = sealapi.BigUInt(128, numerator) den = sealapi.BigUInt(128, denominator) rem = sealapi.BigUInt(128) assert sealapi.BigUInt.of(7).bit_count() == 3 assert num.divrem(den, rem) == int(numerator / denominator) assert rem == numerator % denominator def expected_modinv(a, m): a = a % m for x in range(1, m): if (a * x) % m == 1: return x return 1 value = 66 modulus = 101 testcase = sealapi.BigUInt(128, value) assert testcase.modinv(sealapi.BigUInt(128, modulus)) == expected_modinv(value, modulus) assert testcase.trymodinv(sealapi.BigUInt(128, modulus), sealapi.BigUInt(128)) is True assert testcase.trymodinv(sealapi.BigUInt(128, 99), sealapi.BigUInt(128)) is False testcase = sealapi.BigUInt(128, 34) testcase = sealapi.BigUInt(128, 555) testcase2 = sealapi.BigUInt(2, 10) testcase.duplicate_to(testcase2) assert testcase2.bit_count() == 128 assert testcase2 == 555 testcase = sealapi.BigUInt(128, 777) testcase2 = sealapi.BigUInt(2, 10) testcase2.duplicate_from(testcase) assert testcase2.bit_count() == 128 assert testcase2 == 777
def test_biguint_operators(): left = sealapi.BigUInt(32, 27) right = sealapi.BigUInt(32, 13) assert left - right == 14 assert left - 13 == 14 assert left + right == 40 assert left + 13 == 40 assert left * right == 351 assert left * 13 == 351 assert left / right == 2 assert left / 13 == 2 left = sealapi.BigUInt(32, 555) right = sealapi.BigUInt(32, 777) assert left & right == 555 & 777 assert left & 777 == 555 & 777 assert left | right == 555 | 777 assert left | 777 == 555 | 777 assert right > left assert right > 555 assert right >= left assert right >= 555 assert left < right assert left < 777 assert left <= right assert left <= 777 assert left != right assert left >> 2 == 555 >> 2 left = sealapi.BigUInt(32, 27) right = sealapi.BigUInt(32, 13) left -= right assert left == 14 left = sealapi.BigUInt(32, 27) left -= 13 assert left == 14 left = sealapi.BigUInt(32, 27) right = sealapi.BigUInt(32, 13) left += right assert left == 40 left = sealapi.BigUInt(32, 27) left += 13 assert left == 40 left = sealapi.BigUInt(32, 27) right = sealapi.BigUInt(32, 13) left *= right assert left == 351 left = sealapi.BigUInt(32, 27) left *= 13 assert left == 351 left = sealapi.BigUInt(32, 27) right = sealapi.BigUInt(32, 13) left /= right assert left == 2 left = sealapi.BigUInt(32, 27) left /= 13 assert left == 2