diff --git a/phe/encoding.py b/phe/encoding.py index 785b11c..bbcf4cd 100644 --- a/phe/encoding.py +++ b/phe/encoding.py @@ -188,8 +188,8 @@ def encode(cls, public_key, scalar, precision=None, max_exponent=None): exponent = min(max_exponent, prec_exponent) # Use rationals instead of floats to avoid overflow. - int_rep = round(fractions.Fraction(scalar) - * fractions.Fraction(cls.BASE) ** -exponent) + int_rep = int(round(fractions.Fraction(scalar) + * fractions.Fraction(cls.BASE) ** -exponent)) if abs(int_rep) > public_key.max_int: raise ValueError('Integer needs to be within +/- %d but got %d' diff --git a/phe/tests/paillier_test.py b/phe/tests/paillier_test.py index 81c59d9..97414c2 100644 --- a/phe/tests/paillier_test.py +++ b/phe/tests/paillier_test.py @@ -25,6 +25,7 @@ import unittest import sys import math +import numpy from phe import paillier @@ -1094,6 +1095,10 @@ def testIssue62(self): # This will raise OverflowError without bugfix #73. priv.decrypt(a + b) +class TestNumpyOverflow(unittest.TestCase): + def testNumpyOverflow(self): + public_key, private_key = paillier.generate_paillier_keypair() + private_key.decrypt(public_key.encrypt(numpy.int64(0),precision=2**-12)) def main(): unittest.main()