This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathglv_test.go
134 lines (124 loc) · 2.86 KB
/
glv_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package bls12381
import (
"crypto/rand"
"math/big"
"testing"
)
func TestGLVConstruction(t *testing.T) {
t.Run("Parameters", func(t *testing.T) {
t0, t1 := new(Fr), new(Fr)
one := new(Fr).setUint64(1)
t0.Square(glvLambda)
t0.Add(t0, glvLambda)
t1.Sub(&q, one)
if !t0.Equal(t1) {
t.Fatal("lambda1^2 + lambda1 + 1 = 0")
}
c0 := new(fe)
square(c0, glvPhi1)
mul(c0, c0, glvPhi1)
if !c0.isOne() {
t.Fatal("phi1^3 = 1")
}
square(c0, glvPhi2)
mul(c0, c0, glvPhi2)
if !c0.isOne() {
t.Fatal("phi2^3 = 1")
}
})
t.Run("Endomorphism G1", func(t *testing.T) {
g := NewG1()
{
p0, p1 := g.randAffine(), g.New()
g.MulScalar(p1, p0, glvLambda)
g.Affine(p1)
r := g.New()
g.glvEndomorphism(r, p0)
if !g.Equal(r, p1) {
t.Fatal("f(x, y) = (phi * x, y)")
}
}
})
t.Run("Endomorphism G2", func(t *testing.T) {
g := NewG2()
{
p0, p1 := g.randAffine(), g.New()
g.MulScalar(p1, p0, glvLambda)
g.Affine(p1)
r := g.New()
g.glvEndomorphism(r, p0)
if !g.Equal(r, p1) {
t.Fatal("f(x, y) = (phi * x, y)")
}
}
})
t.Run("Scalar Decomposition", func(t *testing.T) {
for i := 0; i < fuz; i++ {
m, err := new(Fr).Rand(rand.Reader)
if err != nil {
t.Fatal(err)
}
mBig := m.ToBig()
var vFr *glvVectorFr
var vBig *glvVectorBig
{
vFr = new(glvVectorFr).new(m)
v := vFr
if v.k1.Cmp(r128) >= 0 {
t.Fatal("bad scalar component, k1")
}
if v.k2.Cmp(r128) >= 0 {
t.Fatal("bad scalar component, k2")
}
k := new(Fr)
if v.neg1 && v.neg2 {
k.Mul(glvLambda, v.k2)
k.Sub(k, v.k1)
} else if v.neg1 {
k.Mul(glvLambda, v.k2)
k.Add(k, v.k1)
k.Neg(k)
} else if v.neg2 {
k.Mul(glvLambda, v.k2)
k.Add(v.k1, k)
} else {
k.Mul(glvLambda, v.k2)
k.Sub(v.k1, k)
}
if !k.Equal(m) {
t.Fatal("scalar decomposing failed")
}
}
r128Big := r128.ToBig()
{
vBig = new(glvVectorBig).new(mBig)
if new(big.Int).Abs(vBig.k1).Cmp(r128Big) >= 0 {
t.Fatal("bad scalar component, big k1")
}
if new(big.Int).Abs(vBig.k2).Cmp(r128Big) >= 0 {
t.Fatal("bad scalar component, big k2")
}
k := new(big.Int)
k.Mul(glvLambdaBig, vBig.k2)
k.Sub(vBig.k1, k).Mod(k, qBig)
if k.Cmp(mBig) != 0 {
t.Fatal("scalar decomposing with big.Int failed", i)
}
}
zeroBig := new(big.Int)
k1Abs, k2Abs := new(big.Int).Abs(vBig.k1), new(big.Int).Abs(vBig.k2)
if vFr.neg1 != (vBig.k1.Cmp(zeroBig) == -1) {
t.Fatal("cross: scalar decomposing with failed neg1")
}
if vFr.neg2 != (vBig.k2.Cmp(zeroBig) == -1) {
t.Fatal("cross: scalar decomposing with failed neg2")
}
if k1Abs.Cmp(vFr.k1.ToBig()) != 0 {
t.Fatal("cross: scalar decomposing with failed k1", i)
}
if k2Abs.Cmp(vFr.k2.ToBig()) != 0 {
t.Fatal("cross: scalar decomposing with failed k2", i)
}
}
})
}