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