github.com/chain5j/chain5j-pkg@v1.0.7/crypto/bn256/google/main_test.go (about)

     1  package bn256
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  )
     7  
     8  func TestRandomG2Marshal(t *testing.T) {
     9  	for i := 0; i < 10; i++ {
    10  		n, g2, err := RandomG2(rand.Reader)
    11  		if err != nil {
    12  			t.Error(err)
    13  			continue
    14  		}
    15  		t.Logf("%v: %x\n", n, g2.Marshal())
    16  	}
    17  }
    18  
    19  func TestPairings(t *testing.T) {
    20  	a1 := new(G1).ScalarBaseMult(bigFromBase10("1"))
    21  	a2 := new(G1).ScalarBaseMult(bigFromBase10("2"))
    22  	a37 := new(G1).ScalarBaseMult(bigFromBase10("37"))
    23  	an1 := new(G1).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
    24  
    25  	b0 := new(G2).ScalarBaseMult(bigFromBase10("0"))
    26  	b1 := new(G2).ScalarBaseMult(bigFromBase10("1"))
    27  	b2 := new(G2).ScalarBaseMult(bigFromBase10("2"))
    28  	b27 := new(G2).ScalarBaseMult(bigFromBase10("27"))
    29  	b999 := new(G2).ScalarBaseMult(bigFromBase10("999"))
    30  	bn1 := new(G2).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
    31  
    32  	p1 := Pair(a1, b1)
    33  	pn1 := Pair(a1, bn1)
    34  	np1 := Pair(an1, b1)
    35  	if pn1.String() != np1.String() {
    36  		t.Error("Pairing mismatch: e(a, -b) != e(-a, b)")
    37  	}
    38  	if !PairingCheck([]*G1{a1, an1}, []*G2{b1, b1}) {
    39  		t.Error("MultiAte check gave false negative!")
    40  	}
    41  	p0 := new(GT).Add(p1, pn1)
    42  	p0_2 := Pair(a1, b0)
    43  	if p0.String() != p0_2.String() {
    44  		t.Error("Pairing mismatch: e(a, b) * e(a, -b) != 1")
    45  	}
    46  	p0_3 := new(GT).ScalarMult(p1, bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495617"))
    47  	if p0.String() != p0_3.String() {
    48  		t.Error("Pairing mismatch: e(a, b) has wrong order")
    49  	}
    50  	p2 := Pair(a2, b1)
    51  	p2_2 := Pair(a1, b2)
    52  	p2_3 := new(GT).ScalarMult(p1, bigFromBase10("2"))
    53  	if p2.String() != p2_2.String() {
    54  		t.Error("Pairing mismatch: e(a, b * 2) != e(a * 2, b)")
    55  	}
    56  	if p2.String() != p2_3.String() {
    57  		t.Error("Pairing mismatch: e(a, b * 2) != e(a, b) ** 2")
    58  	}
    59  	if p2.String() == p1.String() {
    60  		t.Error("Pairing is degenerate!")
    61  	}
    62  	if PairingCheck([]*G1{a1, a1}, []*G2{b1, b1}) {
    63  		t.Error("MultiAte check gave false positive!")
    64  	}
    65  	p999 := Pair(a37, b27)
    66  	p999_2 := Pair(a1, b999)
    67  	if p999.String() != p999_2.String() {
    68  		t.Error("Pairing mismatch: e(a * 37, b * 27) != e(a, b * 999)")
    69  	}
    70  }