go-snark/r1csqap/r1csqap_test.go
r1csqap/r1csqap_test.goRaw
1package r1csqap
2
3import (
4 "bytes"
5 "fmt"
6 "math/big"
7 "testing"
8
9 "github.com/arnaucube/go-snark/fields"
10 "github.com/stretchr/testify/assert"
11)
12
13func TestTranspose(t *testing.T) {
14 b0 := big.NewInt(int64(0))
15 b1 := big.NewInt(int64(1))
16 bFive := big.NewInt(int64(5))
17 a := [][]*big.Int{
18 []*big.Int{b0, b1, b0, b0, b0, b0},
19 []*big.Int{b0, b0, b0, b1, b0, b0},
20 []*big.Int{b0, b1, b0, b0, b1, b0},
21 []*big.Int{bFive, b0, b0, b0, b0, b1},
22 }
23 aT := Transpose(a)
24 assert.Equal(t, aT, [][]*big.Int{
25 []*big.Int{b0, b0, b0, bFive},
26 []*big.Int{b1, b0, b1, b0},
27 []*big.Int{b0, b0, b0, b0},
28 []*big.Int{b0, b1, b0, b0},
29 []*big.Int{b0, b0, b1, b0},
30 []*big.Int{b0, b0, b0, b1},
31 })
32}
33
34func TestPol(t *testing.T) {
35 b0 := big.NewInt(int64(0))
36 b1 := big.NewInt(int64(1))
37 b3 := big.NewInt(int64(3))
38 b4 := big.NewInt(int64(4))
39 b5 := big.NewInt(int64(5))
40 b6 := big.NewInt(int64(6))
41 b16 := big.NewInt(int64(16))
42
43 a := []*big.Int{b1, b0, b5}
44 b := []*big.Int{b3, b0, b1}
45
46 // new Finite Field
47 r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
48 assert.True(nil, ok)
49 f := fields.NewFq(r)
50
51 // new Polynomial Field
52 pf := NewPolynomialField(f)
53
54 // polynomial multiplication
55 o := pf.Mul(a, b)
56 assert.Equal(t, o, []*big.Int{b3, b0, b16, b0, b5})
57
58 // polynomial addition
59 o = pf.Add(a, b)
60 assert.Equal(t, o, []*big.Int{b4, b0, b6})
61
62 // polynomial subtraction
63 o1 := pf.Sub(a, b)
64 o2 := pf.Sub(b, a)
65 o = pf.Add(o1, o2)
66 assert.True(t, bytes.Equal(b0.Bytes(), o[0].Bytes()))
67 assert.True(t, bytes.Equal(b0.Bytes(), o[1].Bytes()))
68 assert.True(t, bytes.Equal(b0.Bytes(), o[2].Bytes()))
69
70 c := []*big.Int{b5, b6, b1}
71 d := []*big.Int{b1, b3}
72 o = pf.Sub(c, d)
73 assert.Equal(t, o, []*big.Int{b4, b3, b1})
74
75 // NewPolZeroAt
76 o = pf.NewPolZeroAt(3, 4, b4)
77 assert.Equal(t, pf.Eval(o, big.NewInt(3)), b4)
78 o = pf.NewPolZeroAt(2, 4, b3)
79 assert.Equal(t, pf.Eval(o, big.NewInt(2)), b3)
80}
81
82func TestLagrangeInterpolation(t *testing.T) {
83 // new Finite Field
84 r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
85 assert.True(nil, ok)
86 f := fields.NewFq(r)
87 // new Polynomial Field
88 pf := NewPolynomialField(f)
89
90 b0 := big.NewInt(int64(0))
91 b5 := big.NewInt(int64(5))
92 a := []*big.Int{b0, b0, b0, b5}
93 alpha := pf.LagrangeInterpolation(a)
94
95 assert.Equal(t, pf.Eval(alpha, big.NewInt(int64(4))), b5)
96 aux := pf.Eval(alpha, big.NewInt(int64(3))).Int64()
97 assert.Equal(t, aux, int64(0))
98
99}
100
101func TestR1CSToQAP(t *testing.T) {
102 // new Finite Field
103 r, ok := new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
104 assert.True(nil, ok)
105 f := fields.NewFq(r)
106 // new Polynomial Field
107 pf := NewPolynomialField(f)
108
109 b0 := big.NewInt(int64(0))
110 b1 := big.NewInt(int64(1))
111 b3 := big.NewInt(int64(3))
112 b5 := big.NewInt(int64(5))
113 b9 := big.NewInt(int64(9))
114 b27 := big.NewInt(int64(27))
115 b30 := big.NewInt(int64(30))
116 b35 := big.NewInt(int64(35))
117 a := [][]*big.Int{
118 []*big.Int{b0, b1, b0, b0, b0, b0},
119 []*big.Int{b0, b0, b0, b1, b0, b0},
120 []*big.Int{b0, b1, b0, b0, b1, b0},
121 []*big.Int{b5, b0, b0, b0, b0, b1},
122 }
123 b := [][]*big.Int{
124 []*big.Int{b0, b1, b0, b0, b0, b0},
125 []*big.Int{b0, b1, b0, b0, b0, b0},
126 []*big.Int{b1, b0, b0, b0, b0, b0},
127 []*big.Int{b1, b0, b0, b0, b0, b0},
128 }
129 c := [][]*big.Int{
130 []*big.Int{b0, b0, b0, b1, b0, b0},
131 []*big.Int{b0, b0, b0, b0, b1, b0},
132 []*big.Int{b0, b0, b0, b0, b0, b1},
133 []*big.Int{b0, b0, b1, b0, b0, b0},
134 }
135 ap, bp, cp, z := pf.R1CSToQAP(a, b, c)
136 fmt.Println(ap)
137 fmt.Println(bp)
138 fmt.Println(cp)
139 fmt.Println(z)
140
141 w := []*big.Int{b1, b3, b35, b9, b27, b30}
142 alpha, beta, gamma, px := pf.SolPolynomials(w, ap, bp, cp)
143 fmt.Println(alpha)
144 fmt.Println(beta)
145 fmt.Println(gamma)
146 fmt.Println(px)
147
148 h := pf.DivisorPolinomial(px, z)
149 fmt.Println(h)
150
151 // h==px/z so px==h*z
152 assert.Equal(t, px, pf.Mul(h, z))
153
154 // a(x) * b(x) - c(x) == h * z(x)
155 abc := pf.Sub(pf.Mul(alpha, beta), gamma)
156 hz := pf.Mul(h, z)
157 assert.Equal(t, abc, hz)
158}