forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_compute.go
159 lines (151 loc) · 4.31 KB
/
params_compute.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sw_emulated
import (
"crypto/elliptic"
"math/big"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/consensys/gnark-crypto/ecc/bn254"
bw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761"
"github.com/consensys/gnark-crypto/ecc/secp256k1"
)
func computeSecp256k1Table() [][2]*big.Int {
Gjac, _ := secp256k1.Generators()
table := make([][2]*big.Int, 256)
tmp := new(secp256k1.G1Jac).Set(&Gjac)
aff := new(secp256k1.G1Affine)
jac := new(secp256k1.G1Jac)
for i := 1; i < 256; i++ {
tmp = tmp.Double(tmp)
switch i {
case 1, 2:
jac.Set(tmp).AddAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
case 3:
jac.Set(tmp).SubAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
fallthrough
default:
aff.FromJacobian(tmp)
table[i] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
}
}
return table[:]
}
func computeBN254Table() [][2]*big.Int {
Gjac, _, _, _ := bn254.Generators()
table := make([][2]*big.Int, 256)
tmp := new(bn254.G1Jac).Set(&Gjac)
aff := new(bn254.G1Affine)
jac := new(bn254.G1Jac)
for i := 1; i < 256; i++ {
tmp = tmp.Double(tmp)
switch i {
case 1, 2:
jac.Set(tmp).AddAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
case 3:
jac.Set(tmp).SubAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
fallthrough
default:
aff.FromJacobian(tmp)
table[i] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
}
}
return table
}
func computeBLS12381Table() [][2]*big.Int {
Gjac, _, _, _ := bls12381.Generators()
table := make([][2]*big.Int, 256)
tmp := new(bls12381.G1Jac).Set(&Gjac)
aff := new(bls12381.G1Affine)
jac := new(bls12381.G1Jac)
for i := 1; i < 256; i++ {
tmp = tmp.Double(tmp)
switch i {
case 1, 2:
jac.Set(tmp).AddAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
case 3:
jac.Set(tmp).SubAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
fallthrough
default:
aff.FromJacobian(tmp)
table[i] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
}
}
return table
}
func computeP256Table() [][2]*big.Int {
table := make([][2]*big.Int, 256)
p256 := elliptic.P256()
gx, gy := p256.Params().Gx, p256.Params().Gy
tmpx, tmpy := new(big.Int).Set(gx), new(big.Int).Set(gy)
for i := 1; i < 256; i++ {
tmpx, tmpy = p256.Double(tmpx, tmpy)
switch i {
case 1, 2:
xx, yy := p256.Add(tmpx, tmpy, gx, gy)
table[i-1] = [2]*big.Int{xx, yy}
case 3:
xx, yy := p256.Add(tmpx, tmpy, gx, new(big.Int).Sub(p256.Params().P, gy))
table[i-1] = [2]*big.Int{xx, yy}
fallthrough
default:
table[i] = [2]*big.Int{tmpx, tmpy}
}
}
return table
}
func computeP384Table() [][2]*big.Int {
table := make([][2]*big.Int, 384)
p384 := elliptic.P384()
gx, gy := p384.Params().Gx, p384.Params().Gy
tmpx, tmpy := new(big.Int).Set(gx), new(big.Int).Set(gy)
for i := 1; i < 384; i++ {
tmpx, tmpy = p384.Double(tmpx, tmpy)
switch i {
case 1, 2:
xx, yy := p384.Add(tmpx, tmpy, gx, gy)
table[i-1] = [2]*big.Int{xx, yy}
case 3:
xx, yy := p384.Add(tmpx, tmpy, gx, new(big.Int).Sub(p384.Params().P, gy))
table[i-1] = [2]*big.Int{xx, yy}
fallthrough
default:
table[i] = [2]*big.Int{tmpx, tmpy}
}
}
return table
}
func computeBW6761Table() [][2]*big.Int {
Gjac, _, _, _ := bw6761.Generators()
table := make([][2]*big.Int, 377)
tmp := new(bw6761.G1Jac).Set(&Gjac)
aff := new(bw6761.G1Affine)
jac := new(bw6761.G1Jac)
for i := 1; i < 377; i++ {
tmp = tmp.Double(tmp)
switch i {
case 1, 2:
jac.Set(tmp).AddAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
case 3:
jac.Set(tmp).SubAssign(&Gjac)
aff.FromJacobian(jac)
table[i-1] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
fallthrough
default:
aff.FromJacobian(tmp)
table[i] = [2]*big.Int{aff.X.BigInt(new(big.Int)), aff.Y.BigInt(new(big.Int))}
}
}
return table
}