sm9/bn256: fix twist Frobenius bug due to #144, will further review those functions usage

This commit is contained in:
Sun Yimin 2023-07-21 17:51:25 +08:00 committed by GitHub
parent 16b2a43dc3
commit 5b5b26c095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -208,6 +208,29 @@ func (c *twistPoint) MakeAffine() {
c.t.SetOne() c.t.SetOne()
} }
// MakeAffine reverses the Jacobian transform.
// the Jacobian coordinates are (x1, y1, z1)
// where x = x1/z1² and y = y1/z1³.
func (c *twistPoint) AffineFromJacobian() {
if c.z.IsOne() {
return
} else if c.z.IsZero() {
c.x.SetZero()
c.y.SetOne()
c.t.SetZero()
return
}
zInv := (&gfP2{}).Invert(&c.z)
t := (&gfP2{}).Mul(&c.y, zInv)
zInv2 := (&gfP2{}).Square(zInv)
c.y.Mul(t, zInv2)
t.Mul(&c.x, zInv2)
c.x.Set(t)
c.z.SetOne()
c.t.SetOne()
}
func (c *twistPoint) Neg(a *twistPoint) { func (c *twistPoint) Neg(a *twistPoint) {
c.x.Set(&a.x) c.x.Set(&a.x)
c.y.Neg(&a.y) c.y.Neg(&a.y)

View File

@ -28,7 +28,7 @@ func TestAddNeg(t *testing.T) {
func Test_TwistFrobeniusP(t *testing.T) { func Test_TwistFrobeniusP(t *testing.T) {
ret1, ret2 := &twistPoint{}, &twistPoint{} ret1, ret2 := &twistPoint{}, &twistPoint{}
ret1.Frobenius(twistGen) ret1.Frobenius(twistGen)
ret1.MakeAffine() ret1.AffineFromJacobian()
ret2.x.Conjugate(&twistGen.x) ret2.x.Conjugate(&twistGen.x)
ret2.x.MulScalar(&ret2.x, betaToNegPPlus1Over3) ret2.x.MulScalar(&ret2.x, betaToNegPPlus1Over3)
@ -49,12 +49,15 @@ func Test_TwistFrobeniusP(t *testing.T) {
func Test_TwistFrobeniusP2(t *testing.T) { func Test_TwistFrobeniusP2(t *testing.T) {
ret1, ret2 := &twistPoint{}, &twistPoint{} ret1, ret2 := &twistPoint{}, &twistPoint{}
ret1.Frobenius(twistGen) ret1.Frobenius(twistGen)
ret1.AffineFromJacobian()
ret1.Frobenius(ret1) ret1.Frobenius(ret1)
ret1.AffineFromJacobian()
if !ret1.IsOnCurve() { if !ret1.IsOnCurve() {
t.Errorf("point should be on curve") t.Errorf("point should be on curve")
} }
ret2.FrobeniusP2(twistGen) ret2.FrobeniusP2(twistGen)
ret2.AffineFromJacobian()
if !ret2.IsOnCurve() { if !ret2.IsOnCurve() {
t.Errorf("point should be on curve") t.Errorf("point should be on curve")
} }
@ -77,7 +80,7 @@ func Test_TwistFrobeniusP2_Case2(t *testing.T) {
} }
ret2.FrobeniusP2(twistGen) ret2.FrobeniusP2(twistGen)
ret2.MakeAffine() ret2.AffineFromJacobian()
if !ret2.IsOnCurve() { if !ret2.IsOnCurve() {
t.Errorf("point should be on curve") t.Errorf("point should be on curve")
} }
@ -100,7 +103,7 @@ func Test_TwistNegFrobeniusP2_Case2(t *testing.T) {
} }
ret2.NegFrobeniusP2(twistGen) ret2.NegFrobeniusP2(twistGen)
ret2.MakeAffine() ret2.AffineFromJacobian()
if !ret2.IsOnCurve() { if !ret2.IsOnCurve() {
t.Errorf("point should be on curve") t.Errorf("point should be on curve")
} }