pull/21515/head
Alex Vlasov 4 years ago
parent 8d5020a5e5
commit 40f5708f3a
  1. 6
      crypto/bn256/cloudflare/bn256_test.go
  2. 54
      crypto/bn256/cloudflare/gfp.go

@ -108,13 +108,11 @@ func TestBinaryEAA(t *testing.T) {
tmpBinaryEAASelfSet.Set(&Ga.p.x) tmpBinaryEAASelfSet.Set(&Ga.p.x)
tmpBinaryEAASelfSet.InvertVariableTime(tmpBinaryEAASelfSet) tmpBinaryEAASelfSet.InvertVariableTime(tmpBinaryEAASelfSet)
eq := equals(tmpLittleFermat, tmpBinaryEAA) if *tmpLittleFermat != *tmpBinaryEAA {
if eq == false {
t.Fatalf("results of different inversion do not agree") t.Fatalf("results of different inversion do not agree")
} }
eq = equals(tmpLittleFermat, tmpBinaryEAASelfSet) if *tmpLittleFermat != *tmpBinaryEAASelfSet {
if eq == false {
t.Fatalf("self-assigned inversion is invalid") t.Fatalf("self-assigned inversion is invalid")
} }
} }

@ -86,7 +86,7 @@ func isZero(a *gfP) bool {
} }
func isEven(a *gfP) bool { func isEven(a *gfP) bool {
return bits.TrailingZeros64((a[0])) > 0 return a[0]&1 == 0
} }
func div2(a *gfP) { func div2(a *gfP) {
@ -123,10 +123,6 @@ func gte(a, b *gfP) bool {
return borrow == 0 return borrow == 0
} }
func equals(a, b *gfP) bool {
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]
}
// Performs inversion of the field element using binary EEA. // Performs inversion of the field element using binary EEA.
// If element is zero (no inverse exists) then set `e` to zero // If element is zero (no inverse exists) then set `e` to zero
func (e *gfP) InvertVariableTime(f *gfP) { func (e *gfP) InvertVariableTime(f *gfP) {
@ -145,63 +141,47 @@ func (e *gfP) InvertVariableTime(f *gfP) {
u.Set(f) u.Set(f)
b.Set(r2) b.Set(r2)
v := gfP{p2[0], p2[1], p2[2], p2[3]} v := gfP(p2)
c := gfP{0, 0, 0, 0} c := gfP{0, 0, 0, 0}
modulus := gfP{p2[0], p2[1], p2[2], p2[3]} modulus := gfP(p2)
for {
if equals(&u, &one) || equals(&v, &one) {
break
}
for u != one && v != one {
// while u is even // while u is even
for { for isEven(&u) {
if !isEven(&u) {
break
}
div2(&u) div2(&u)
if isEven(&b) { if !isEven(&b) {
div2(&b)
} else {
// we will not overflow a modulus here, // we will not overflow a modulus here,
// so we can use specialized function // so we can use specialized function
// do perform addition without reduction // do perform addition without reduction
b.addNocarry(&modulus) b.addNocarry(&modulus)
div2(&b)
} }
div2(&b)
} }
// while v is even // while v is even
for { for isEven(&v) {
if !isEven(&v) {
break
}
div2(&v) div2(&v)
if isEven(&c) { if !isEven(&c) {
div2(&c)
} else {
// we will not overflow a modulus here, // we will not overflow a modulus here,
// so we can use specialized function // so we can use specialized function
// do perform addition without reduction // do perform addition without reduction
c.addNocarry(&modulus) c.addNocarry(&modulus)
div2(&c) } else {
} }
div2(&c)
} }
if gte(&v, &u) { if gte(&u, &v) {
// v >= u
v.subNoborrow(&u)
gfpSub(&c, &c, &b)
} else {
// if v < u
u.subNoborrow(&v) u.subNoborrow(&v)
gfpSub(&b, &b, &c) gfpSub(&b, &b, &c)
} else {
v.subNoborrow(&u)
gfpSub(&c, &c, &b)
} }
} }
if equals(&u, &one) { if u == one {
e.Set(&b) e.Set(&b)
} else { } else {
e.Set(&c) e.Set(&c)

Loading…
Cancel
Save