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

     1  package bn256
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"math/big"
     7  	"testing"
     8  )
     9  
    10  func TestToGfP12_1(t *testing.T) {
    11  	x := &gfP12b6{}
    12  	x.SetGfP12(expected1)
    13  	fmt.Printf("%v\n", gfP12b6Decode(x))
    14  	x.SetGfP12(expected_b2)
    15  	fmt.Printf("%v\n", gfP12b6Decode(x))
    16  	x.SetGfP12(expected_b2_2)
    17  	fmt.Printf("%v\n", gfP12b6Decode(x))
    18  }
    19  
    20  func Test_finalExponentiationB6(t *testing.T) {
    21  	x := &gfP12b6{
    22  		p6,
    23  		p6,
    24  	}
    25  	got := finalExponentiationB6(x)
    26  
    27  	exp := new(big.Int).Exp(p, big.NewInt(12), nil)
    28  	exp.Sub(exp, big.NewInt(1))
    29  	exp.Div(exp, Order)
    30  	expected := (&gfP12b6{}).Exp(x, exp)
    31  
    32  	if *got != *expected {
    33  		t.Errorf("got %v, expected %v\n", got, expected)
    34  	}
    35  }
    36  
    37  func Test_PairingB6_A2(t *testing.T) {
    38  	pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4")
    39  	g2 := &G2{}
    40  	_, err := g2.ScalarBaseMult(NormalizeScalar(pk.Bytes()))
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	ret := pairingB6(g2.p, curveGen)
    45  	if *ret != *expected1 {
    46  		t.Errorf("not expected")
    47  	}
    48  }
    49  
    50  func Test_PairingB6_B2(t *testing.T) {
    51  	deB := &twistPoint{}
    52  	deB.x.x = *fromBigInt(bigFromHex("74CCC3AC9C383C60AF083972B96D05C75F12C8907D128A17ADAFBAB8C5A4ACF7"))
    53  	deB.x.y = *fromBigInt(bigFromHex("01092FF4DE89362670C21711B6DBE52DCD5F8E40C6654B3DECE573C2AB3D29B2"))
    54  	deB.y.x = *fromBigInt(bigFromHex("44B0294AA04290E1524FF3E3DA8CFD432BB64DE3A8040B5B88D1B5FC86A4EBC1"))
    55  	deB.y.y = *fromBigInt(bigFromHex("8CFC48FB4FF37F1E27727464F3C34E2153861AD08E972D1625FC1A7BD18D5539"))
    56  	deB.z.SetOne()
    57  	deB.t.SetOne()
    58  
    59  	rA := &curvePoint{}
    60  	rA.x = *fromBigInt(bigFromHex("7CBA5B19069EE66AA79D490413D11846B9BA76DD22567F809CF23B6D964BB265"))
    61  	rA.y = *fromBigInt(bigFromHex("A9760C99CB6F706343FED05637085864958D6C90902ABA7D405FBEDF7B781599"))
    62  	rA.z = *one
    63  	rA.t = *one
    64  
    65  	ret := pairingB6(deB, rA)
    66  	if ret.x != expected_b2.x || ret.y != expected_b2.y || ret.z != expected_b2.z {
    67  		t.Errorf("not expected")
    68  	}
    69  }
    70  
    71  func Test_PairingB6_B2_2(t *testing.T) {
    72  	pubE := &curvePoint{}
    73  	pubE.x = *fromBigInt(bigFromHex("9174542668E8F14AB273C0945C3690C66E5DD09678B86F734C4350567ED06283"))
    74  	pubE.y = *fromBigInt(bigFromHex("54E598C6BF749A3DACC9FFFEDD9DB6866C50457CFC7AA2A4AD65C3168FF74210"))
    75  	pubE.z = *one
    76  	pubE.t = *one
    77  
    78  	ret := pairingB6(twistGen, pubE)
    79  	ret.Exp(ret, bigFromHex("00018B98C44BEF9F8537FB7D071B2C928B3BC65BD3D69E1EEE213564905634FE"))
    80  	if ret.x != expected_b2_2.x || ret.y != expected_b2_2.y || ret.z != expected_b2_2.z {
    81  		t.Errorf("not expected")
    82  	}
    83  }
    84  
    85  func TestBilinearityB6(t *testing.T) {
    86  	for i := 0; i < 2; i++ {
    87  		a, p1, _ := RandomG1(rand.Reader)
    88  		b, p2, _ := RandomG2(rand.Reader)
    89  		e1 := pairingB6(p2.p, p1.p)
    90  
    91  		e2 := pairingB6(twistGen, curveGen)
    92  		e2.Exp(e2, a)
    93  		e2.Exp(e2, b)
    94  
    95  		if *e1 != *e2 {
    96  			t.Fatalf("bad pairing result: %s", e1)
    97  		}
    98  	}
    99  }
   100  
   101  func BenchmarkFinalExponentiationB6(b *testing.B) {
   102  	x := &gfP12b6{
   103  		p6,
   104  		p6,
   105  	}
   106  	exp := new(big.Int).Exp(p, big.NewInt(12), nil)
   107  	exp.Sub(exp, big.NewInt(1))
   108  	exp.Div(exp, Order)
   109  	expected := (&gfP12b6{}).Exp(x, exp)
   110  
   111  	b.ReportAllocs()
   112  	b.ResetTimer()
   113  	for i := 0; i < b.N; i++ {
   114  		got := finalExponentiationB6(x)
   115  		if *got != *expected {
   116  			b.Errorf("got %v, expected %v\n", got, expected)
   117  		}
   118  	}
   119  }
   120  
   121  func BenchmarkPairingB6(b *testing.B) {
   122  	pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4")
   123  	g2 := &G2{}
   124  	_, err := g2.ScalarBaseMult(NormalizeScalar(pk.Bytes()))
   125  	if err != nil {
   126  		b.Fatal(err)
   127  	}
   128  	b.ReportAllocs()
   129  	b.ResetTimer()
   130  	for i := 0; i < b.N; i++ {
   131  		ret := pairingB6(g2.p, curveGen)
   132  		if *ret != *expected1 {
   133  			b.Errorf("not expected")
   134  		}
   135  	}
   136  }