Skip to content

Commit 7cbff05

Browse files
committed
Using exponentiation of inverting scalars.
1 parent a35566c commit 7cbff05

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

group/decaf448/decaf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (e *Elt) IsIdentity() bool {
8484
b0 := fp.IsZero(&e.p.X)
8585
b1 := 1 - fp.IsZero(&e.p.Y)
8686
b2 := 1 - fp.IsZero(&e.p.Z)
87-
return subtle.ConstantTimeEq(int32(4*b2+2*b1+b0), 0x7) == 1
87+
return (b0 & b1 & b2) == 1
8888
}
8989

9090
// IsEqual returns True if e=a, where = is an equivalence relation.

internal/ted448/constants.go

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ var (
4444
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4545
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f,
4646
}
47+
orderMinusTwo = Scalar{
48+
0xf1, 0x44, 0x58, 0xab, 0x92, 0xc2, 0x78, 0x23,
49+
0x55, 0x8f, 0xc5, 0x8d, 0x72, 0xc2, 0x6c, 0x21,
50+
0x90, 0x36, 0xd6, 0xae, 0x49, 0xdb, 0x4e, 0xc4,
51+
0xe9, 0x23, 0xca, 0x7c, 0xff, 0xff, 0xff, 0xff,
52+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
53+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f,
55+
}
4756
// residue448 is 2^448 mod order.
4857
residue448 = [4]uint64{
4958
0x721cf5b5529eec34, 0x7a4cf635c8e9c2ab, 0xeec492d944a725bf, 0x20cd77058,

internal/ted448/point.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ func (P *Point) Double() {
4646
fp.Add(c, c, c) // C = 2*z^2
4747
fp.Add(h, a, b) // H = A+B
4848
fp.Sqr(e, e) // (x+y)^2
49-
fp.Sub(e, e, h) // E = (x+y)^2-A-B
49+
fp.Sub(e, e, h) // E = (x+y)^2-A-B = 2xy
5050
fp.Sub(g, b, a) // G = B-A
51-
fp.Sub(f, c, g) // F = C-G
52-
fp.Mul(Pz, f, g) // Z = F * G
53-
fp.Mul(Px, e, f) // X = E * F
54-
fp.Mul(Py, g, h) // Y = G * H, T = E * H
51+
fp.Sub(f, c, g) // F = C-G = 2z^2-y^2+x^2
52+
fp.Mul(Pz, f, g) // Z = F * G = (y^2-x^2)(2z^2-y^2+x^2)
53+
fp.Mul(Px, e, f) // X = E * F = 2xy(2z^2-y^2+x^2)
54+
fp.Mul(Py, g, h) // Y = G * H = (x^2-y^2)(x^2+y^2)
55+
// T = E * H = 2xy(x^2+y^2)
5556
}
5657

5758
// mixAdd calulates P= P+Q, where Q is a precomputed448 point with Z_Q = 1.

internal/ted448/scalar.go

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ted448
22

33
import (
4-
"crypto/rand"
54
"encoding/binary"
65
"math/bits"
76

@@ -202,32 +201,41 @@ func (z *Scalar) Sub(x, y *Scalar) {
202201
// Mul calculates z = x*y mod order.
203202
func (z *Scalar) Mul(x, y *Scalar) {
204203
var z64, x64, y64 scalar64
205-
prod := (&[_N + 1]uint64{})[:]
206204
x64.fromScalar(x)
207205
y64.fromScalar(y)
206+
coremul(&z64, &x64, &y64)
207+
z64.modOrder()
208+
z64.toScalar(z)
209+
}
210+
211+
func coremul(z64, x64, y64 *scalar64) {
212+
var p64 scalar64
213+
prod := (&[_N + 1]uint64{})[:]
208214
mulWord(prod, x64[:], y64[_N-1])
209-
copy(z64[:], prod[:_N])
210-
z64.reduceOneWord(prod[_N])
215+
copy(p64[:], prod[:_N])
216+
p64.reduceOneWord(prod[_N])
211217
for i := _N - 2; i >= 0; i-- {
212-
h := z64.leftShift(0)
213-
z64.reduceOneWord(h)
218+
h := p64.leftShift(0)
219+
p64.reduceOneWord(h)
214220
mulWord(prod, x64[:], y64[i])
215-
c := add(z64[:], z64[:], prod[:_N])
216-
z64.reduceOneWord(prod[_N] + c)
221+
c := add(p64[:], p64[:], prod[:_N])
222+
p64.reduceOneWord(prod[_N] + c)
217223
}
218-
z64.modOrder()
219-
z64.toScalar(z)
224+
*z64 = p64
220225
}
221226

222227
// Inv calculates z = 1/x mod order.
223228
func (z *Scalar) Inv(x *Scalar) {
224-
var t, r Scalar
225-
_, _ = rand.Read(r[:])
226-
r.Red()
227-
t.Mul(x, &r)
228-
bigT := conv.BytesLe2BigInt(t[:])
229-
bigOrder := conv.BytesLe2BigInt(order[:])
230-
bigT.ModInverse(bigT, bigOrder)
231-
conv.BigInt2BytesLe(z[:], bigT)
232-
z.Mul(z, &r)
229+
var x64 scalar64
230+
x64.fromScalar(x)
231+
t := &scalar64{1}
232+
for i := 8*len(orderMinusTwo) - 1; i >= 0; i-- {
233+
coremul(t, t, t)
234+
b := (orderMinusTwo[i/8] >> uint(i%8)) & 1
235+
if b != 0 {
236+
coremul(t, t, &x64)
237+
}
238+
}
239+
t.modOrder()
240+
t.toScalar(z)
233241
}

0 commit comments

Comments
 (0)