github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/cloudflare/bn256_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package bn256 19 20 import ( 21 "bytes" 22 "crypto/rand" 23 "testing" 24 ) 25 26 func TestG1Marshal(t *testing.T) { 27 _, Ga, err := RandomG1(rand.Reader) 28 if err != nil { 29 t.Fatal(err) 30 } 31 ma := Ga.Marshal() 32 33 Gb := new(G1) 34 _, err = Gb.Unmarshal(ma) 35 if err != nil { 36 t.Fatal(err) 37 } 38 mb := Gb.Marshal() 39 40 if !bytes.Equal(ma, mb) { 41 t.Fatal("bytes are different") 42 } 43 } 44 45 func TestG2Marshal(t *testing.T) { 46 _, Ga, err := RandomG2(rand.Reader) 47 if err != nil { 48 t.Fatal(err) 49 } 50 ma := Ga.Marshal() 51 52 Gb := new(G2) 53 _, err = Gb.Unmarshal(ma) 54 if err != nil { 55 t.Fatal(err) 56 } 57 mb := Gb.Marshal() 58 59 if !bytes.Equal(ma, mb) { 60 t.Fatal("bytes are different") 61 } 62 } 63 64 func TestBilinearity(t *testing.T) { 65 for i := 0; i < 2; i++ { 66 a, p1, _ := RandomG1(rand.Reader) 67 b, p2, _ := RandomG2(rand.Reader) 68 e1 := Pair(p1, p2) 69 70 e2 := Pair(&G1{curveGen}, &G2{twistGen}) 71 e2.ScalarMult(e2, a) 72 e2.ScalarMult(e2, b) 73 74 if *e1.p != *e2.p { 75 t.Fatalf("bad pairing result: %s", e1) 76 } 77 } 78 } 79 80 func TestTripartiteDiffieHellman(t *testing.T) { 81 a, _ := rand.Int(rand.Reader, Order) 82 b, _ := rand.Int(rand.Reader, Order) 83 c, _ := rand.Int(rand.Reader, Order) 84 85 pa, pb, pc := new(G1), new(G1), new(G1) 86 qa, qb, qc := new(G2), new(G2), new(G2) 87 88 pa.Unmarshal(new(G1).ScalarBaseMult(a).Marshal()) 89 qa.Unmarshal(new(G2).ScalarBaseMult(a).Marshal()) 90 pb.Unmarshal(new(G1).ScalarBaseMult(b).Marshal()) 91 qb.Unmarshal(new(G2).ScalarBaseMult(b).Marshal()) 92 pc.Unmarshal(new(G1).ScalarBaseMult(c).Marshal()) 93 qc.Unmarshal(new(G2).ScalarBaseMult(c).Marshal()) 94 95 k1 := Pair(pb, qc) 96 k1.ScalarMult(k1, a) 97 k1Bytes := k1.Marshal() 98 99 k2 := Pair(pc, qa) 100 k2.ScalarMult(k2, b) 101 k2Bytes := k2.Marshal() 102 103 k3 := Pair(pa, qb) 104 k3.ScalarMult(k3, c) 105 k3Bytes := k3.Marshal() 106 107 if !bytes.Equal(k1Bytes, k2Bytes) || !bytes.Equal(k2Bytes, k3Bytes) { 108 t.Errorf("keys didn't agree") 109 } 110 } 111 112 func BenchmarkG1(b *testing.B) { 113 x, _ := rand.Int(rand.Reader, Order) 114 b.ResetTimer() 115 116 for i := 0; i < b.N; i++ { 117 new(G1).ScalarBaseMult(x) 118 } 119 } 120 121 func BenchmarkG2(b *testing.B) { 122 x, _ := rand.Int(rand.Reader, Order) 123 b.ResetTimer() 124 125 for i := 0; i < b.N; i++ { 126 new(G2).ScalarBaseMult(x) 127 } 128 } 129 func BenchmarkPairing(b *testing.B) { 130 for i := 0; i < b.N; i++ { 131 Pair(&G1{curveGen}, &G2{twistGen}) 132 } 133 }