1
1
import unittest
2
2
3
- from ethsnarks .shamirspoly import lagrange , shamirs_poly , randq , randn , shamirs_poly_n
3
+ from ethsnarks .shamirspoly import lagrange , shamirs_poly
4
+ from ethsnarks .field import FQ
4
5
from ethsnarks .r1cs import r1cs_constraint
5
6
6
7
from scipy .interpolate import lagrange as scipy_lagrange
@@ -14,39 +15,41 @@ class ShamirPolyTests(unittest.TestCase):
14
15
def test_fromdocs (self ):
15
16
p = 100003
16
17
k = 4
17
- a = [6257 , 85026 , 44499 , 14701 ]
18
+ a = [FQ ( 6257 , p ), FQ ( 85026 , p ), FQ ( 44499 , p ), FQ ( 14701 , p ) ]
18
19
F = lambda i , x : a [i ] * (x ** i )
19
20
X = lambda x : a [0 ] + F (1 , x ) + F (2 , x ) + F (3 , x )
20
21
# Create the shares
21
- Sx = range (1 , 5 )
22
+ Sx = [ _ for _ in range (1 , 5 )]
22
23
Sy = [X (_ ) for _ in Sx ]
23
24
for x , y in zip (Sx , Sy ):
24
- print (x , y )
25
- z = shamirs_poly_n (x , a , p )
26
- assert z == y % p
25
+ z = shamirs_poly (FQ (x , p ), a )
26
+ assert z == y
27
27
# Then recover secret
28
- assert a [0 ] == int (scipy_lagrange (Sx , Sy ).c [- 1 ])
28
+ result = int (scipy_lagrange (Sx , [_ .n for _ in Sy ]).c [- 1 ]) % p
29
+ assert a [0 ] == result
30
+
29
31
30
32
def test_fromdocs2 (self ):
31
33
p = 100003
32
34
k = 4
33
- a = [randn (p ) for _ in range (0 , k )] # [6257, 85026, 44499, 14701]
35
+ a = [FQ . random (p ) for _ in range (0 , k )] # [6257, 85026, 44499, 14701]
34
36
F = lambda i , x : a [i ] * (x ** i )
35
37
X = lambda x : a [0 ] + F (1 , x ) + F (2 , x ) + F (3 , x )
36
38
# Create the shares
37
39
Sx = range (1 , 5 )
38
40
Sy = [X (_ ) for _ in Sx ]
39
41
for x , y in zip (Sx , Sy ):
40
- z = shamirs_poly_n ( x , a , p )
41
- assert z == y % p
42
+ z = shamirs_poly ( FQ ( x , p ), a )
43
+ assert z == y
42
44
# Then recover secret
43
- assert a [0 ] == int (scipy_lagrange (Sx , Sy ).c [- 1 ])
45
+ result = int (scipy_lagrange (Sx , [_ .n for _ in Sy ]).c [- 1 ]) % p
46
+ assert a [0 ] == result
44
47
45
48
def test_random (self ):
46
49
# Randomized tests
47
50
for _ in range (0 , 10 ):
48
- alpha = [randq () for _ in range (0 , 4 )]
49
- points = [(i , shamirs_poly (i , alpha ))
51
+ alpha = [FQ . random () for _ in range (0 , 4 )]
52
+ points = [(FQ ( i ) , shamirs_poly (FQ ( i ) , alpha ))
50
53
for i in range (0 , len (alpha ))]
51
54
assert alpha [0 ] == lagrange (points , 0 )
52
55
assert alpha [0 ] != lagrange (points [1 :], 0 )
@@ -55,57 +58,58 @@ def test_random(self):
55
58
def test_random_small (self ):
56
59
q = 100003
57
60
for _ in range (0 , 10 ):
58
- alpha = [randn (q ) for _ in range (0 , 4 )]
59
- points = [(i , shamirs_poly_n ( i , alpha , q ))
61
+ alpha = [FQ . random (q ) for _ in range (0 , 4 )]
62
+ points = [(FQ ( i , q ), shamirs_poly ( FQ ( i , q ), alpha ))
60
63
for i in range (0 , len (alpha ))]
61
- assert alpha [0 ] == lagrange (points , 0 , q )
62
- assert alpha [0 ] != lagrange (points [1 :], 0 , q )
63
- assert alpha [0 ] != lagrange (points [2 :], 0 , q )
64
+ assert alpha [0 ] == lagrange (points , 0 )
65
+ assert alpha [0 ] != lagrange (points [1 :], 0 )
66
+ assert alpha [0 ] != lagrange (points [2 :], 0 )
64
67
65
68
# XXX: scipy's lagrange has floating point precision for large numbers
66
69
points_x , points_y = unzip (points )
67
- interpolation = scipy_lagrange (points_x , points_y )
70
+ interpolation = scipy_lagrange ([ _ . n for _ in points_x ], [ _ . n for _ in points_y ] )
68
71
assert int (interpolation .c [- 1 ]) == alpha [0 ]
69
72
70
73
def test_static (self ):
71
74
# Verify against static test vectors
72
- alpha = [6808181831819141657160280673506432691407806061837762993142662373500430825792 ,
73
- 4138536697521448323155976179625860582331141320072618244300034508091478437877 ,
74
- 20259243729221075783953642258755031830946498253783650311586175820530608751936 ,
75
- 11227115470523445882235139084890542822660569362938710556861479160600812964997 ]
76
- points = [(i , shamirs_poly (i , alpha )) for i in range (0 , len (alpha ))]
77
- test_points = [(0 , 6808181831819141657160280673506432691407806061837762993142662373500430825792 ),
78
- (1 , 20544834857245836424258632451520592838797650598216707762192147676147522484985 ),
79
- (2 , 10833210933219706719196668784844423052753721417299010433393634464005858464330 ),
80
- (3 , 1259517139202877390892412692306630092142705895884865660519589327528699562575 )]
75
+ alpha = [FQ ( 6808181831819141657160280673506432691407806061837762993142662373500430825792 ) ,
76
+ FQ ( 4138536697521448323155976179625860582331141320072618244300034508091478437877 ) ,
77
+ FQ ( 20259243729221075783953642258755031830946498253783650311586175820530608751936 ) ,
78
+ FQ ( 11227115470523445882235139084890542822660569362938710556861479160600812964997 ) ]
79
+ points = [(FQ ( i ) , shamirs_poly (FQ ( i ) , alpha )) for i in range (0 , len (alpha ))]
80
+ test_points = [(FQ ( 0 ), FQ ( 6808181831819141657160280673506432691407806061837762993142662373500430825792 ) ),
81
+ (FQ ( 1 ), FQ ( 20544834857245836424258632451520592838797650598216707762192147676147522484985 ) ),
82
+ (FQ ( 2 ), FQ ( 10833210933219706719196668784844423052753721417299010433393634464005858464330 ) ),
83
+ (FQ ( 3 ), FQ ( 1259517139202877390892412692306630092142705895884865660519589327528699562575 ) )]
81
84
assert points == test_points
82
85
assert alpha [0 ] == lagrange (points , 0 )
83
86
84
87
def test_poly_constraints (self ):
85
- I = 14107816444829002666153088737167870815199986768206359534115449255516606414458
88
+ I = FQ ( 14107816444829002666153088737167870815199986768206359534115449255516606414458 )
86
89
87
90
A = [
88
- 17167899297711346731111134130539302906398306115120667361324654931060817769652 ,
89
- 20692971555607562002131076139518972969954851288604992618106076572794460197154 ,
90
- 11669314840495787582492056085422064523948780040073924653117253046363337959489 ,
91
- 15735493254427804666818698471807230275586165984790760178845725782084556556535
91
+ FQ ( 17167899297711346731111134130539302906398306115120667361324654931060817769652 ) ,
92
+ FQ ( 20692971555607562002131076139518972969954851288604992618106076572794460197154 ) ,
93
+ FQ ( 11669314840495787582492056085422064523948780040073924653117253046363337959489 ) ,
94
+ FQ ( 15735493254427804666818698471807230275586165984790760178845725782084556556535 )
92
95
]
93
96
94
97
S = [
95
- 1 ,
96
- 14107816444829002666153088737167870815199986768206359534115449255516606414458 ,
97
- 5067932324814081810415131337916762410698113547031307360925810907599032394672 ,
98
- 13201723662860499519704937914604513230538757359894370890481138582074383924053
98
+ FQ ( 1 ) ,
99
+ FQ ( 14107816444829002666153088737167870815199986768206359534115449255516606414458 ) ,
100
+ FQ ( 5067932324814081810415131337916762410698113547031307360925810907599032394672 ) ,
101
+ FQ ( 13201723662860499519704937914604513230538757359894370890481138582074383924053 )
99
102
]
100
103
101
104
T = [
102
- 17167899297711346731111134130539302906398306115120667361324654931060817769652 ,
103
- 13947767308050706262790594447202101821284419559667543268525874072511409211876 ,
104
- 12611939459072476081893406313696501745859572890147944797422704733972764295239 ,
105
- 1657398546220101661314129991806057074896482894269175421846876999103942041527
105
+ FQ ( 17167899297711346731111134130539302906398306115120667361324654931060817769652 ) ,
106
+ FQ ( 13947767308050706262790594447202101821284419559667543268525874072511409211876 ) ,
107
+ FQ ( 12611939459072476081893406313696501745859572890147944797422704733972764295239 ) ,
108
+ FQ ( 1657398546220101661314129991806057074896482894269175421846876999103942041527 )
106
109
]
107
110
108
- assert shamirs_poly (I , A ) == T [- 1 ]
111
+ result = shamirs_poly (I , A )
112
+ assert result == T [- 1 ]
109
113
110
114
for i in range (0 , len (A )):
111
115
if i == 0 :
0 commit comments