Skip to content

Commit

Permalink
Fix FR inverse function and add test case (#8)
Browse files Browse the repository at this point in the history
* fix inverse()

* fix FR inverse method and add tc
  • Loading branch information
boohyunsik authored and meyer9 committed Jun 21, 2019
1 parent b495094 commit e008a26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func (f FR) Inverse() *FR {
b := &FR{FRR2.Copy()}
c := bigZeroFR.Copy()

for u.Cmp(frOne) != 0 && v.Cmp(frOne) != 0 {
one := &FRRepr{1, 0, 0, 0}
for u.Cmp(one) != 0 && v.Cmp(one) != 0 {
for u.IsEven() {
u.Div2()
if b.n.IsEven() {
Expand Down Expand Up @@ -254,7 +255,7 @@ func (f FR) Inverse() *FR {
c.SubAssign(b)
}
}
if u.IsZero() {
if u.Equals(one) {
return b
}
return c
Expand Down
18 changes: 18 additions & 0 deletions fr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package bls

import (
"crypto/rand"
"testing"
)

func TestFRInverse(t *testing.T) {
one := FRReprToFR(&FRRepr{1, 0, 0, 0})
for i := 0; i < 10; i++ {
newFR, _ := RandFR(rand.Reader)
inverse := newFR.Inverse()
newFR.MulAssign(inverse)
if !one.Equals(newFR) {
t.Errorf("Multiplication with inverse must be one.")
}
}
}

0 comments on commit e008a26

Please sign in to comment.