github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/crypto/bn256/cloudflare/main_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:36</date> 10 //</624342625482182656> 11 12 package bn256 13 14 import ( 15 "testing" 16 17 "crypto/rand" 18 ) 19 20 func TestRandomG2Marshal(t *testing.T) { 21 for i := 0; i < 10; i++ { 22 n, g2, err := RandomG2(rand.Reader) 23 if err != nil { 24 t.Error(err) 25 continue 26 } 27 t.Logf("%d: %x\n", n, g2.Marshal()) 28 } 29 } 30 31 func TestPairings(t *testing.T) { 32 a1 := new(G1).ScalarBaseMult(bigFromBase10("1")) 33 a2 := new(G1).ScalarBaseMult(bigFromBase10("2")) 34 a37 := new(G1).ScalarBaseMult(bigFromBase10("37")) 35 an1 := new(G1).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616")) 36 37 b0 := new(G2).ScalarBaseMult(bigFromBase10("0")) 38 b1 := new(G2).ScalarBaseMult(bigFromBase10("1")) 39 b2 := new(G2).ScalarBaseMult(bigFromBase10("2")) 40 b27 := new(G2).ScalarBaseMult(bigFromBase10("27")) 41 b999 := new(G2).ScalarBaseMult(bigFromBase10("999")) 42 bn1 := new(G2).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616")) 43 44 p1 := Pair(a1, b1) 45 pn1 := Pair(a1, bn1) 46 np1 := Pair(an1, b1) 47 if pn1.String() != np1.String() { 48 t.Error("Pairing mismatch: e(a, -b) != e(-a, b)") 49 } 50 if !PairingCheck([]*G1{a1, an1}, []*G2{b1, b1}) { 51 t.Error("MultiAte check gave false negative!") 52 } 53 p0 := new(GT).Add(p1, pn1) 54 p0_2 := Pair(a1, b0) 55 if p0.String() != p0_2.String() { 56 t.Error("Pairing mismatch: e(a, b) * e(a, -b) != 1") 57 } 58 p0_3 := new(GT).ScalarMult(p1, bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495617")) 59 if p0.String() != p0_3.String() { 60 t.Error("Pairing mismatch: e(a, b) has wrong order") 61 } 62 p2 := Pair(a2, b1) 63 p2_2 := Pair(a1, b2) 64 p2_3 := new(GT).ScalarMult(p1, bigFromBase10("2")) 65 if p2.String() != p2_2.String() { 66 t.Error("Pairing mismatch: e(a, b * 2) != e(a * 2, b)") 67 } 68 if p2.String() != p2_3.String() { 69 t.Error("Pairing mismatch: e(a, b * 2) != e(a, b) ** 2") 70 } 71 if p2.String() == p1.String() { 72 t.Error("Pairing is degenerate!") 73 } 74 if PairingCheck([]*G1{a1, a1}, []*G2{b1, b1}) { 75 t.Error("MultiAte check gave false positive!") 76 } 77 p999 := Pair(a37, b27) 78 p999_2 := Pair(a1, b999) 79 if p999.String() != p999_2.String() { 80 t.Error("Pairing mismatch: e(a * 37, b * 27) != e(a, b * 999)") 81 } 82 } 83