github.com/amazechain/amc@v0.1.3/common/crypto/bn256/cloudflare/main_test.go (about)

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