Skip to content
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

Dealing with float numbers #75

Open
SpBossGui opened this issue Apr 21, 2019 · 3 comments
Open

Dealing with float numbers #75

SpBossGui opened this issue Apr 21, 2019 · 3 comments
Labels

Comments

@SpBossGui
Copy link

SpBossGui commented Apr 21, 2019

I wonder whether it has the ability to deal with float numbers. Actually I run alternative_base.py in examples folder and changed the values of a and b to float numbers and it turned to be a failure to deal with it. Did I make any mistakes or could it really not? Thank you for answering my questions!

def math_example():
    print("Encoding two large positive numbers. BASE={}".format(ExampleEncodedNumber.BASE))

    a = 0.5
    b = 0.2

    encoded_a = ExampleEncodedNumber.encode(public_key, a)
    encoded_b = ExampleEncodedNumber.encode(public_key, b)

    print("Checking that decoding gives the same number...")
    assert a == encoded_a.decode()
    assert b == encoded_b.decode()

    print("Encrypting the encoded numbers")
    encrypted_a = public_key.encrypt(encoded_a)
    encrypted_b = public_key.encrypt(encoded_b)

    print("Adding the encrypted numbers")
    encrypted_c = encrypted_a + encrypted_b

    print("Decrypting the one encrypted sum")
    decrypted_but_encoded = \
        private_key.decrypt_encoded(encrypted_c, ExampleEncodedNumber)

    print("Checking the decrypted number is what we started with")

    print("Decrypted: {}".format(decrypted_but_encoded.decode()))

Generating paillier keypair
Encoding a large positive number. With a BASE 64 encoding scheme
Checking that decoding gives the same number...
Encrypting the encoded number
Decrypting...
Checking the decrypted number is what we started with
Encoding two large positive numbers. BASE=64
Checking that decoding gives the same number...
Encrypting the encoded numbers
Adding the encrypted numbers
Decrypting the one encrypted sum
Checking the decrypted number is what we started with
Decrypted: 0.325

@wilko77
Copy link
Collaborator

wilko77 commented Apr 29, 2019

You discovered a bug. Congratulations.
In general, arithmetic with floating point numbers is fine. We have tests for that...
However, changing the BASE changes everything. There is bug in how an encrypted number handles non-standard bases which leads to the problem you are seeing.

If you run you code again with the default BASE=16, then the results will be correct.
I've also created issue #76 for the bug with the custom base.

@SpBossGui
Copy link
Author

It really helps, thank you very much!

@LittleDonkey7
Copy link

Hello, I would like to ask a question. Is it possible to get the result of operations with a determined number of decimal places during the float numbers operation? For example, in calculating E(10) / 3 = a, I want a result to be accurate to three decimal places, that is, D(a)=3.333 instead of 3.33333333333.

pk, sk = paillier.generate_paillier_keypair(n_length=128)
a = pk.encrypt(10)
print("a/3: ",sk.decrypt(a/3))

a/3: 3.333333333333333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants