Skip to content

Fix overflow when encoding non-python data types #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions phe/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions phe/tests/paillier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import unittest
import sys
import math
import numpy

from phe import paillier

Expand Down Expand Up @@ -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()
Expand Down
Loading