From 4ebb504eaa33a085fa7f5b4d98d9fac23183adbc Mon Sep 17 00:00:00 2001 From: Arthur Khodaverdian Date: Sun, 2 Mar 2025 23:03:43 -0800 Subject: [PATCH 1/2] Added Failing UnitTest --- phe/tests/paillier_test.py | 5 +++++ 1 file changed, 5 insertions(+) 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() From cdfe7e239a456d99d97b0ccdb0b03de88cfc1cba Mon Sep 17 00:00:00 2001 From: Arthur Khodaverdian Date: Sun, 2 Mar 2025 23:08:52 -0800 Subject: [PATCH 2/2] Fixed overflow by converting int_rep into python int --- phe/encoding.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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'