github.com/shopperooofficial/ethereum-protocol@v1.9.7/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  	a1 := new(G1).ScalarBaseMult(bigFromBase10("1"))
    22  	a2 := new(G1).ScalarBaseMult(bigFromBase10("2"))
    23  	a37 := new(G1).ScalarBaseMult(bigFromBase10("37"))
    24  	an1 := new(G1).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
    25  
    26  	b0 := new(G2).ScalarBaseMult(bigFromBase10("0"))
    27  	b1 := new(G2).ScalarBaseMult(bigFromBase10("1"))
    28  	b2 := new(G2).ScalarBaseMult(bigFromBase10("2"))
    29  	b27 := new(G2).ScalarBaseMult(bigFromBase10("27"))
    30  	b999 := new(G2).ScalarBaseMult(bigFromBase10("999"))
    31  	bn1 := new(G2).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
    32  
    33  	p1 := Pair(a1, b1)
    34  	pn1 := Pair(a1, bn1)
    35  	np1 := Pair(an1, b1)
    36  	if pn1.String() != np1.String() {
    37  		t.Error("Pairing mismatch: e(a, -b) != e(-a, b)")
    38  	}
    39  	if !PairingCheck([]*G1{a1, an1}, []*G2{b1, b1}) {
    40  		t.Error("MultiAte check gave false negative!")
    41  	}
    42  	p0 := new(GT).Add(p1, pn1)
    43  	p0_2 := Pair(a1, b0)
    44  	if p0.String() != p0_2.String() {
    45  		t.Error("Pairing mismatch: e(a, b) * e(a, -b) != 1")
    46  	}
    47  	p0_3 := new(GT).ScalarMult(p1, bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495617"))
    48  	if p0.String() != p0_3.String() {
    49  		t.Error("Pairing mismatch: e(a, b) has wrong order")
    50  	}
    51  	p2 := Pair(a2, b1)
    52  	p2_2 := Pair(a1, b2)
    53  	p2_3 := new(GT).ScalarMult(p1, bigFromBase10("2"))
    54  	if p2.String() != p2_2.String() {
    55  		t.Error("Pairing mismatch: e(a, b * 2) != e(a * 2, b)")
    56  	}
    57  	if p2.String() != p2_3.String() {
    58  		t.Error("Pairing mismatch: e(a, b * 2) != e(a, b) ** 2")
    59  	}
    60  	if p2.String() == p1.String() {
    61  		t.Error("Pairing is degenerate!")
    62  	}
    63  	if PairingCheck([]*G1{a1, a1}, []*G2{b1, b1}) {
    64  		t.Error("MultiAte check gave false positive!")
    65  	}
    66  	p999 := Pair(a37, b27)
    67  	p999_2 := Pair(a1, b999)
    68  	if p999.String() != p999_2.String() {
    69  		t.Error("Pairing mismatch: e(a * 37, b * 27) != e(a, b * 999)")
    70  	}
    71  }