forked from aszepieniec/stark-anatomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_univariate.py
98 lines (71 loc) · 3.03 KB
/
test_univariate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from univariate import *
import os
def test_distributivity():
field = Field.main()
zero = field.zero()
one = field.one()
two = FieldElement(2, field)
five = FieldElement(5, field)
a = Polynomial([one, zero, five, two])
b = Polynomial([two, two, one])
c = Polynomial([zero, five, two, five, five, one])
lhs = a * (b + c)
rhs = a * b + a * c
assert(lhs == rhs), "distributivity fails for polynomials: {} =/= {}".format(lhs.__str__(), rhs.__str__())
print("univariate polynomial distributivity success \\o/")
def test_division():
field = Field.main()
zero = field.zero()
one = field.one()
two = FieldElement(2, field)
five = FieldElement(5, field)
a = Polynomial([one, zero, five, two])
b = Polynomial([two, two, one])
c = Polynomial([zero, five, two, five, five, one])
# a should divide a*b, quotient should be b
quo, rem = Polynomial.divide(a*b, a)
assert(rem.is_zero()), "fail division test 1"
assert(quo == b), "fail division test 2"
# b should divide a*b, quotient should be a
quo, rem = Polynomial.divide(a*b, b)
assert(rem.is_zero()), "fail division test 3"
assert(quo == a), "fail division test 4"
# c should not divide a*b
quo, rem = Polynomial.divide(a*b, c)
assert(not rem.is_zero()), "fail division test 5"
# ... but quo * c + rem == a*b
assert(quo * c + rem == a*b), "fail division test 6"
print("univariate polynomial division success \\o/")
def test_interpolate():
field = Field.main()
zero = field.zero()
one = field.one()
two = FieldElement(2, field)
five = FieldElement(5, field)
values = [five, two, two, one, five, zero]
domain = [FieldElement(i, field) for i in range(1, 6)]
poly = Polynomial.interpolate_domain(domain, values)
for i in range(len(domain)):
assert(poly.evaluate(domain[i]) == values[i]), "fail interpolate test 1"
# evaluation in random point is nonzero with high probability
assert(poly.evaluate(FieldElement(363, field)) != zero), "fail interpolate test 2"
assert(poly.degree() == len(domain)-1), "fail interpolate test 3"
print("univariate polynomial interpolate success \\o/")
def test_zerofier( ):
field = Field.main()
for trial in range(0, 100):
degree = int(os.urandom(1)[0])
domain = []
while len(domain) != degree:
new = field.sample(os.urandom(17))
if not new in domain:
domain += [new]
zerofier = Polynomial.zerofier_domain(domain)
assert(zerofier.degree() == degree), "zerofier has degree unequal to size of domain"
for d in domain:
assert(zerofier.evaluate(d) == field.zero()), "zerofier does not evaluate to zero where it should"
random = field.sample(os.urandom(17))
while random in domain:
random = field.sample(os.urandom(17))
assert(zerofier.evaluate(random) != field.zero()), "zerofier evaluates to zero where it should not"
print("univariate zerofier test success \\o/")