HarryR / ethsnarks

A toolkit for viable zk-SNARKS on Ethereum, Web, Mobile and Desktop
GNU Lesser General Public License v3.0
240 stars 57 forks source link

Add Fr field class to the field.py #154

Closed wanseob closed 4 years ago

wanseob commented 4 years ago

Added Fr field for jubjub curve. FR class inherits FQ and the example code is below

from ethsnarks.field import FQ, FR 
from ethsnarks.jubjub import Point

G = Point.from_y(FQ(20819045374670962167435360035096875258406992893633759881276124905556507972311))

def test_homomorphic_encryption():
    a = FR(3)
    b = FR(-1)
    P = G*(a+b)
    Q = G*a + G*b

    print("a:                       ", a)      # a: 3
    print("b:                       ", b)      # b: 21888242871839275222246405745257275088614511777268538073601725287587578984327
    print("G*(a+b):                 ", P)      # x: 173245..., y: 200221...
    print("G*(a) + G(b):            ", Q)      # x: 173245..., y: 200221...
    print("G*(a+b) == G*(a) + G(b): ", P == Q) # True

if __name__ == "__main__":
    test_homomorphic_encryption()
HarryR commented 4 years ago

Thanks, this is really clean and simple and will definitely be useful.

wanseob commented 4 years ago

@HarryR Thanks for the quick review!