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

     1  package bn256
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  )
     7  
     8  var p6 = gfP6{
     9  	gfP2{
    10  		*fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141")),
    11  		*fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B")),
    12  	},
    13  	gfP2{
    14  		*fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")),
    15  		*fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")),
    16  	},
    17  	gfP2{
    18  		*fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")),
    19  		*fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")),
    20  	},
    21  }
    22  
    23  func testGfP12b6Square(t *testing.T, x *gfP12b6) {
    24  	xmulx := &gfP12b6{}
    25  	xmulx.Mul(x, x)
    26  	xmulx = gfP12b6Decode(xmulx)
    27  
    28  	x2 := &gfP12b6{}
    29  	x2.Square(x)
    30  	x2 = gfP12b6Decode(x2)
    31  
    32  	if *xmulx != *x2 {
    33  		t.Errorf("xmulx=%v, x2=%v", xmulx, x2)
    34  	}
    35  }
    36  
    37  func Test_gfP12b6Square(t *testing.T) {
    38  	x := &gfP12b6{
    39  		p6,
    40  		p6,
    41  	}
    42  	testGfP12b6Square(t, x)
    43  
    44  	x = &gfP12b6{
    45  		p6,
    46  		*(&gfP6{}).SetOne(),
    47  	}
    48  	testGfP12b6Square(t, x)
    49  
    50  	x = &gfP12b6{
    51  		*(&gfP6{}).SetOne(),
    52  		p6,
    53  	}
    54  	testGfP12b6Square(t, x)
    55  
    56  	x = &gfP12b6{
    57  		*(&gfP6{}).SetZero(),
    58  		p6,
    59  	}
    60  	testGfP12b6Square(t, x)
    61  
    62  	x = &gfP12b6{
    63  		p6,
    64  		*(&gfP6{}).SetZero(),
    65  	}
    66  	testGfP12b6Square(t, x)
    67  }
    68  
    69  func testGfP12b6Invert(t *testing.T, x *gfP12b6) {
    70  	xInv := &gfP12b6{}
    71  	xInv.Invert(x)
    72  
    73  	y := &gfP12b6{}
    74  	y.Mul(x, xInv)
    75  	if !y.IsOne() {
    76  		t.Fail()
    77  	}
    78  }
    79  
    80  func TestToGfP12(t *testing.T) {
    81  	x := &gfP12b6{
    82  		p6,
    83  		p6,
    84  	}
    85  
    86  	p12 := x.ToGfP12()
    87  
    88  	x1 := &gfP12b6{}
    89  	x1.SetGfP12(p12)
    90  
    91  	if *x1 != *x {
    92  		t.Errorf("not same")
    93  	}
    94  
    95  	// after add
    96  	x2 := (&gfP12b6{}).Add(x, x)
    97  	p12_1 := (&gfP12{}).Add(p12, p12)
    98  	x3 := (&gfP12b6{}).SetGfP12(p12_1)
    99  	if *x2 != *x3 {
   100  		x3 = gfP12b6Decode(x3)
   101  		x2 = gfP12b6Decode(x2)
   102  		t.Errorf("not same after double, %v\n%v\n", x3, x2)
   103  	}
   104  
   105  	// after sub
   106  	x2 = (&gfP12b6{}).Sub(x, x)
   107  	p12_1 = (&gfP12{}).Sub(p12, p12)
   108  	x3 = (&gfP12b6{}).SetGfP12(p12_1)
   109  	if *x2 != *x3 {
   110  		x3 = gfP12b6Decode(x3)
   111  		x2 = gfP12b6Decode(x2)
   112  		t.Errorf("not same after double, %v\n%v\n", x3, x2)
   113  	}
   114  
   115  	// after neg
   116  	x2 = (&gfP12b6{}).Neg(x)
   117  	p12_1 = (&gfP12{}).Neg(p12)
   118  	x3 = (&gfP12b6{}).SetGfP12(p12_1)
   119  	if *x2 != *x3 {
   120  		x3 = gfP12b6Decode(x3)
   121  		x2 = gfP12b6Decode(x2)
   122  		t.Errorf("not same after double, %v\n%v\n", x3, x2)
   123  	}
   124  
   125  	// after mul gfp
   126  	x2.MulGfP(x, fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")))
   127  	p12_1.MulGFP(p12, fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")))
   128  	x3.SetGfP12(p12_1)
   129  	if *x2 != *x3 {
   130  		x3 = gfP12b6Decode(x3)
   131  		x2 = gfP12b6Decode(x2)
   132  		t.Errorf("not same after mul gfp, %v\n%v\n", x3, x2)
   133  	}
   134  
   135  	// after mul gfp2
   136  	gfp2 := &gfP2{
   137  		*fromBigInt(bigFromHex("17509B092E845C1266BA0D262CBEE6ED0736A96FA347C8BD856DC76B84EBEB96")),
   138  		*fromBigInt(bigFromHex("A7CF28D519BE3DA65F3170153D278FF247EFBA98A71A08116215BBA5C999A7C7")),
   139  	}
   140  
   141  	x2.MulGfP2(x, gfp2)
   142  	p12_1.MulGFP2(p12, gfp2)
   143  	x3.SetGfP12(p12_1)
   144  	if *x2 != *x3 {
   145  		x3 = gfP12b6Decode(x3)
   146  		x2 = gfP12b6Decode(x2)
   147  		t.Errorf("not same after mul gfp2, %v\n%v\n", x3, x2)
   148  	}
   149  
   150  	// after squre
   151  	x2.Square(x)
   152  	p12_1.Square(p12)
   153  	x3.SetGfP12(p12_1)
   154  	if *x2 != *x3 {
   155  		x3 = gfP12b6Decode(x3)
   156  		x2 = gfP12b6Decode(x2)
   157  		t.Errorf("not same after square, %v\n%v\n", x3, x2)
   158  	}
   159  
   160  	// after mul
   161  	x2.Mul(x, x)
   162  	p12_1.Mul(p12, p12)
   163  	x3.SetGfP12(p12_1)
   164  	if *x2 != *x3 {
   165  		x3 = gfP12b6Decode(x3)
   166  		x2 = gfP12b6Decode(x2)
   167  		t.Errorf("not same after mul, %v\n%v\n", x3, x2)
   168  	}
   169  }
   170  
   171  func Test_gfP12b6Invert(t *testing.T) {
   172  	x := &gfP12b6{
   173  		*(&gfP6{}).SetZero(),
   174  		p6,
   175  	}
   176  	testGfP12b6Invert(t, x)
   177  	x = &gfP12b6{
   178  		*(&gfP6{}).SetOne(),
   179  		p6,
   180  	}
   181  	testGfP12b6Invert(t, x)
   182  }
   183  
   184  func TestSToPMinus1Over2(t *testing.T) {
   185  	expected := &gfP2{}
   186  	expected.y.Set(fromBigInt(bigFromHex("3f23ea58e5720bdb843c6cfa9c08674947c5c86e0ddd04eda91d8354377b698b")))
   187  	expected.x.Set(zero)
   188  
   189  	s := &gfP6{}
   190  	s.SetS()
   191  	s.Exp(s, pMinus1Over2Big)
   192  	if !(s.x.IsZero() && s.y.IsZero() && s.z == *expected) {
   193  		s = gfP6Decode(s)
   194  		t.Errorf("not same as expected %v\n", s)
   195  	}
   196  }
   197  
   198  func Test_gfP12b6Frobenius(t *testing.T) {
   199  	x := &gfP12b6{
   200  		p6,
   201  		p6,
   202  	}
   203  	expected := &gfP12b6{}
   204  	expected.Exp(x, p)
   205  	got := &gfP12b6{}
   206  	got.Frobenius(x)
   207  	if *expected != *got {
   208  		t.Errorf("got %v, expected %v", got, expected)
   209  	}
   210  }
   211  
   212  func TestSToPSquaredMinus1Over2(t *testing.T) {
   213  	s := &gfP6{}
   214  	s.SetS()
   215  	p2 := new(big.Int).Mul(p, p)
   216  	p2 = new(big.Int).Sub(p2, big.NewInt(1))
   217  	p2.Rsh(p2, 1)
   218  	s.Exp(s, p2)
   219  
   220  	expected := &gfP2{}
   221  	expected.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65334")))
   222  	expected.x.Set(zero)
   223  
   224  	if !(s.x.IsZero() && s.y.IsZero() && s.z == *expected) {
   225  		s = gfP6Decode(s)
   226  		t.Errorf("not same as expected %v\n", s)
   227  	}
   228  }
   229  
   230  func Test_gfP12b6FrobeniusP2(t *testing.T) {
   231  	x := &gfP12b6{
   232  		p6,
   233  		p6,
   234  	}
   235  	expected := &gfP12b6{}
   236  	p2 := new(big.Int).Mul(p, p)
   237  	expected.Exp(x, p2)
   238  	got := &gfP12b6{}
   239  	got.FrobeniusP2(x)
   240  	if *expected != *got {
   241  		t.Errorf("got %v, expected %v", got, expected)
   242  	}
   243  }
   244  
   245  func TestSToP4Minus1Over2(t *testing.T) {
   246  	s := &gfP6{}
   247  	s.SetS()
   248  	p4 := new(big.Int).Mul(p, p)
   249  	p4.Mul(p4, p4)
   250  	p4 = new(big.Int).Sub(p4, big.NewInt(1))
   251  	p4.Rsh(p4, 1)
   252  	s.Exp(s, p4)
   253  
   254  	expected := &gfP2{}
   255  	expected.y.Set(fromBigInt(bigFromHex("0000000000000000f300000002a3a6f2780272354f8b78f4d5fc11967be65333")))
   256  	expected.x.Set(zero)
   257  
   258  	if !(s.x.IsZero() && s.y.IsZero() && s.z == *expected) {
   259  		s = gfP6Decode(s)
   260  		t.Errorf("not same as expected %v\n", s)
   261  	}
   262  }
   263  
   264  func Test_gfP12b6FrobeniusP4(t *testing.T) {
   265  	x := &gfP12b6{
   266  		p6,
   267  		p6,
   268  	}
   269  	expected := &gfP12b6{}
   270  	p4 := new(big.Int).Mul(p, p)
   271  	p4.Mul(p4, p4)
   272  	expected.Exp(x, p4)
   273  	got := &gfP12b6{}
   274  	got.FrobeniusP4(x)
   275  	if *expected != *got {
   276  		t.Errorf("got %v, expected %v", got, expected)
   277  	}
   278  }
   279  
   280  func Test_gfP12b6FrobeniusP6(t *testing.T) {
   281  	x := &gfP12b6{
   282  		p6,
   283  		p6,
   284  	}
   285  	expected := &gfP12b6{}
   286  	p6 := new(big.Int).Mul(p, p)
   287  	p6.Mul(p6, p)
   288  	p6.Mul(p6, p6)
   289  	expected.Exp(x, p6)
   290  	got := &gfP12b6{}
   291  	got.FrobeniusP6(x)
   292  	if *expected != *got {
   293  		t.Errorf("got %v, expected %v", got, expected)
   294  	}
   295  }
   296  
   297  func BenchmarkGfP12b6Frobenius(b *testing.B) {
   298  	x := &gfP12b6{
   299  		p6,
   300  		p6,
   301  	}
   302  	expected := &gfP12b6{}
   303  	expected.Exp(x, p)
   304  	got := &gfP12b6{}
   305  	b.ReportAllocs()
   306  	b.ResetTimer()
   307  	for i := 0; i < b.N; i++ {
   308  		got.Frobenius(x)
   309  		if *expected != *got {
   310  			b.Errorf("got %v, expected %v", got, expected)
   311  		}
   312  	}
   313  }
   314  
   315  func TestGfP12b6SpecialSquare(t *testing.T) {
   316  	in := &gfP12b6{
   317  		p6,
   318  		p6,
   319  	}
   320  	t1 := &gfP12b6{}
   321  	t1.x.Neg(&in.x)
   322  	t1.y.Set(&in.y)
   323  
   324  	inv := &gfP12b6{}
   325  	inv.Invert(in)
   326  	t1.Mul(t1, inv)
   327  
   328  	t2 := (&gfP12b6{}).FrobeniusP2(t1)
   329  	t1.Mul(t1, t2)
   330  
   331  	got := &gfP12b6{}
   332  	expected := &gfP12b6{}
   333  	got.Cyclo6Square(t1)
   334  	expected.Square(t1)
   335  	if *got != *expected {
   336  		t.Errorf("not same got=%v, expected=%v", got, expected)
   337  	}
   338  }