github.com/emmansun/gmsm@v0.29.1/sm9/bn256/twist_test.go (about)

     1  package bn256
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestIsOnCurve(t *testing.T) {
     8  	if !twistGen.IsOnCurve() {
     9  		t.Errorf("twist gen point should be on curve")
    10  	}
    11  	a := &twistPoint{}
    12  	a.SetInfinity()
    13  	if !a.IsOnCurve() {
    14  		t.Errorf("infinity zero point should be on curve")
    15  	}
    16  }
    17  
    18  func TestAddNeg(t *testing.T) {
    19  	neg := &twistPoint{}
    20  	neg.Neg(twistGen)
    21  	res := &twistPoint{}
    22  	res.Add(twistGen, neg)
    23  	if !res.IsInfinity() {
    24  		t.Errorf("a add its neg should be zero")
    25  	}
    26  }
    27  
    28  func Test_TwistFrobeniusP(t *testing.T) {
    29  	ret1, ret2 := &twistPoint{}, &twistPoint{}
    30  	ret1.Frobenius(twistGen)
    31  	ret1.AffineFromJacobian()
    32  
    33  	ret2.x.Conjugate(&twistGen.x)
    34  	ret2.x.MulScalar(&ret2.x, betaToNegPPlus1Over3)
    35  
    36  	ret2.y.Conjugate(&twistGen.y)
    37  	ret2.y.MulScalar(&ret2.y, betaToNegPPlus1Over2)
    38  	ret2.z.SetOne()
    39  	ret2.t.SetOne()
    40  	if !ret2.IsOnCurve() {
    41  		t.Errorf("point should be on curve")
    42  	}
    43  
    44  	if ret1.x != ret2.x || ret1.y != ret2.y || ret1.z != ret2.z || ret1.t != ret2.t {
    45  		t.Errorf("not same")
    46  	}
    47  }
    48  
    49  func Test_TwistFrobeniusP2(t *testing.T) {
    50  	ret1, ret2 := &twistPoint{}, &twistPoint{}
    51  	ret1.Frobenius(twistGen)
    52  	ret1.AffineFromJacobian()
    53  	ret1.Frobenius(ret1)
    54  	ret1.AffineFromJacobian()
    55  	if !ret1.IsOnCurve() {
    56  		t.Errorf("point should be on curve")
    57  	}
    58  
    59  	ret2.FrobeniusP2(twistGen)
    60  	ret2.AffineFromJacobian()
    61  	if !ret2.IsOnCurve() {
    62  		t.Errorf("point should be on curve")
    63  	}
    64  	if ret1.x != ret2.x || ret1.y != ret2.y || ret1.z != ret2.z || ret1.t != ret2.t {
    65  		t.Errorf("not same")
    66  	}
    67  }
    68  
    69  func Test_TwistFrobeniusP2_Case2(t *testing.T) {
    70  	ret1, ret2 := &twistPoint{}, &twistPoint{}
    71  	ret1.x.Set(&twistGen.x)
    72  	ret1.x.MulScalar(&ret1.x, betaToNegP2Plus1Over3)
    73  
    74  	ret1.y.Set(&twistGen.y)
    75  	ret1.y.MulScalar(&ret1.y, betaToNegP2Plus1Over2)
    76  	ret1.z.SetOne()
    77  	ret1.t.SetOne()
    78  	if !ret1.IsOnCurve() {
    79  		t.Errorf("point should be on curve")
    80  	}
    81  
    82  	ret2.FrobeniusP2(twistGen)
    83  	ret2.AffineFromJacobian()
    84  	if !ret2.IsOnCurve() {
    85  		t.Errorf("point should be on curve")
    86  	}
    87  	if ret1.x != ret2.x || ret1.y != ret2.y || ret1.z != ret2.z || ret1.t != ret2.t {
    88  		t.Errorf("not same")
    89  	}
    90  }
    91  
    92  func Test_TwistNegFrobeniusP2_Case2(t *testing.T) {
    93  	ret1, ret2 := &twistPoint{}, &twistPoint{}
    94  	ret1.x.Set(&twistGen.x)
    95  	ret1.x.MulScalar(&ret1.x, betaToNegP2Plus1Over3)
    96  
    97  	ret1.y.Neg(&twistGen.y)
    98  	ret1.y.MulScalar(&ret1.y, betaToNegP2Plus1Over2)
    99  	ret1.z.SetOne()
   100  	ret1.t.SetOne()
   101  	if !ret1.IsOnCurve() {
   102  		t.Errorf("point should be on curve")
   103  	}
   104  
   105  	ret2.NegFrobeniusP2(twistGen)
   106  	ret2.AffineFromJacobian()
   107  	if !ret2.IsOnCurve() {
   108  		t.Errorf("point should be on curve")
   109  	}
   110  	if ret1.x != ret2.x || ret1.y != ret2.y || ret1.z != ret2.z || ret1.t != ret2.t {
   111  		t.Errorf("not same")
   112  	}
   113  }