Skip to content

Commit 21ed1c0

Browse files
authored
Perf: Optimize Sqrt in Fp2 for all fields (#757)
1 parent eb61127 commit 21ed1c0

File tree

102 files changed

+3823
-730
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+3823
-730
lines changed

ecc/bls12-377/fp/element.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/fp/element_exp.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/fp/element_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/fp/element_utils.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,140 @@ func (z *Element) MulByNonResidueInv(x *Element) *Element {
1616
z.Mul(x, &qnrInv)
1717
return z
1818
}
19+
20+
// SqrtAndInverse computes both the square root and the inverse of x in E2 by
21+
// doing a single exponentiation to the power (p-2^e-1)/2^(e+1) where e is the
22+
// field 2-adicity.
23+
func (z *Element) SqrtAndInverse(x, inv *Element) (*Element, *Element) {
24+
// q ≡ 1 (mod 4)
25+
// see modSqrtTonelliShanks in math/big/int.go
26+
27+
var y, b, t, w Element
28+
r := uint64(46)
29+
30+
// w = x^((s-1)/2))
31+
w.ExpBySqrtExp(*x)
32+
33+
inv.expByC1(x)
34+
35+
y.Square(&w).
36+
Square(&y).
37+
Mul(&y, x)
38+
39+
for i := uint64(0); i < r-1; i++ {
40+
y.Square(&y)
41+
}
42+
inv.Mul(inv, &y)
43+
44+
// y = x^((s+1)/2)) = w * x
45+
y.Mul(x, &w)
46+
47+
// b = xˢ = w * w * x = y * x
48+
b.Mul(&w, &y)
49+
50+
// g = nonResidue ^ s
51+
var g = Element{
52+
7563926049028936178,
53+
2688164645460651601,
54+
12112688591437172399,
55+
3177973240564633687,
56+
14764383749841851163,
57+
52487407124055189,
58+
}
59+
60+
// compute legendre symbol
61+
// t = x^((q-1)/2) = r-1 squaring of xˢ
62+
t = b
63+
for i := uint64(0); i < r-1; i++ {
64+
t.Square(&t)
65+
}
66+
if t.IsZero() {
67+
return z.SetZero(), inv
68+
}
69+
if !t.IsOne() {
70+
// t != 1, we don't have a square root
71+
return nil, inv
72+
}
73+
for {
74+
var m uint64
75+
t = b
76+
77+
// for t != 1
78+
for !t.IsOne() {
79+
t.Square(&t)
80+
m++
81+
}
82+
83+
if m == 0 {
84+
return z.Set(&y), inv
85+
}
86+
// t = g^(2^(r-m-1)) (mod q)
87+
ge := int(r - m - 1)
88+
t = g
89+
for ge > 0 {
90+
t.Square(&t)
91+
ge--
92+
}
93+
94+
g.Square(&t)
95+
y.Mul(&y, &t)
96+
b.Mul(&b, &g)
97+
r = m
98+
}
99+
}
100+
101+
// expByC1 set z to x^c1 and return z
102+
// where c1 = 2^(e-1)-1 and e = 46 is the 2-adicity of Fp.
103+
func (z *Element) expByC1(x *Element) *Element {
104+
// addition chain:
105+
//
106+
// _10 = 2*1
107+
// _11 = 1 + _10
108+
// _1100 = _11 << 2
109+
// _1111 = _11 + _1100
110+
// _11110 = 2*_1111
111+
// _11111 = 1 + _11110
112+
// x10 = _11111 << 5 + _11111
113+
// x20 = x10 << 10 + x10
114+
// x40 = x20 << 20 + x20
115+
// return x40 << 5 + _11111
116+
//
117+
// Operations: 44 squares 7 multiplies
118+
//
119+
// Generated by github.com/mmcloughlin/addchain v0.4.0.
120+
121+
var (
122+
t0 = new(Element)
123+
t1 = new(Element)
124+
)
125+
z.Square(x)
126+
z.Mul(x, z)
127+
t0.Square(z)
128+
for s := 1; s < 2; s++ {
129+
t0.Square(t0)
130+
}
131+
z.Mul(z, t0)
132+
z.Square(z)
133+
z.Mul(x, z)
134+
t0.Square(z)
135+
for s := 1; s < 5; s++ {
136+
t0.Square(t0)
137+
}
138+
t0.Mul(z, t0)
139+
t1.Square(t0)
140+
for s := 1; s < 10; s++ {
141+
t1.Square(t1)
142+
}
143+
t0.Mul(t0, t1)
144+
t1.Square(t0)
145+
for s := 1; s < 20; s++ {
146+
t1.Square(t1)
147+
}
148+
t0.Mul(t0, t1)
149+
for s := 0; s < 5; s++ {
150+
t0.Square(t0)
151+
}
152+
z.Mul(z, t0)
153+
154+
return z
155+
}

ecc/bls12-377/fr/element.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/fr/element_exp.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/fr/element_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/internal/fptower/e2.go

Lines changed: 16 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-377/internal/fptower/e2_test.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecc/bls12-381/fp/element.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)