github.com/klaytn/klaytn@v1.12.1/crypto/bls/blst/public_key_test.go (about) 1 // Copyright 2023 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package blst 18 19 import ( 20 "testing" 21 22 "github.com/klaytn/klaytn/common" 23 "github.com/klaytn/klaytn/crypto/bls/types" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 var ( 28 // https://github.com/ethereum/bls12-381-tests 29 // verify/verify_valid_case_195246ee3bd3b6ec.json 30 testPublicKeyBytes = common.FromHex("0xb53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f") 31 // deserialization_G1/deserialization_fails_not_in_G1.json 32 testBadPublicKeyBytes = common.FromHex("0x8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") 33 ) 34 35 func TestPublicKeyCopy(t *testing.T) { 36 pk, _ := PublicKeyFromBytes(testPublicKeyBytes) 37 assert.Equal(t, pk.Marshal(), pk.Copy().Marshal()) 38 } 39 40 func TestPublicKeyFromBytes(t *testing.T) { 41 b := testPublicKeyBytes 42 pk, err := PublicKeyFromBytes(b) 43 assert.Nil(t, err) 44 assert.Equal(t, b, pk.Marshal()) 45 46 _, err = PublicKeyFromBytes([]byte{1, 2, 3, 4}) 47 assert.NotNil(t, err) 48 49 zero := make([]byte, types.PublicKeyLength) 50 _, err = PublicKeyFromBytes(zero) 51 assert.Equal(t, types.ErrPublicKeyUnmarshal, err) 52 53 _, err = PublicKeyFromBytes(testBadPublicKeyBytes) 54 assert.Equal(t, types.ErrPublicKeyUnmarshal, err) 55 } 56 57 func TestMultiplePublicKeysFromBytes(t *testing.T) { 58 L := benchAggregateLen 59 tc := generateBenchmarkMaterial(L) 60 bs := tc.pkbs 61 62 // all uncached 63 pks, err := MultiplePublicKeysFromBytes(tc.pkbs[:L/2]) 64 assert.Nil(t, err) 65 for i := 0; i < L/2; i++ { 66 assert.Equal(t, tc.pks[i].Marshal(), pks[i].Marshal()) 67 } 68 69 // [0:L/2] are cached and [L/2:L] are uncached 70 pks, err = MultiplePublicKeysFromBytes(tc.pkbs) 71 assert.Nil(t, err) 72 for i := 0; i < L; i++ { 73 assert.Equal(t, tc.pks[i].Marshal(), pks[i].Marshal()) 74 } 75 76 bs[0] = []byte{1, 2, 3, 4} // len? 77 _, err = MultiplePublicKeysFromBytes(bs) 78 assert.NotNil(t, err) 79 80 bs[0] = make([]byte, types.PublicKeyLength) // is_inf? 81 _, err = MultiplePublicKeysFromBytes(bs) 82 assert.Equal(t, types.ErrPublicKeyUnmarshal, err) 83 84 bs[0] = testBadPublicKeyBytes // in_g2? 85 _, err = MultiplePublicKeysFromBytes(bs) 86 assert.Equal(t, types.ErrPublicKeyUnmarshal, err) 87 } 88 89 func TestAggregatePublicKeys(t *testing.T) { 90 var ( 91 // import milagro_bls_binding as bls 92 // from binascii import unhexlify as unhex 93 // pk1 = bls.SkToPk(unhex('263dbd792f5b1be47ed85f8938c0f29586af0d3ac7b977f21c278fe1462040e3')) 94 // pk2 = bls.SkToPk(unhex('47b8192d77bf871b62e87859d653922725724a5c031afeabc60bcef5ff665138')) 95 // pk3 = bls.SkToPk(unhex('328388aff0d4a5b7dc9205abd374e7e98f3cd9f3418edb4eafda5fb16473d216')) 96 // apk = bls._AggregatePKs([pk1,pk2,pk3]) 97 // print('\n'.join(map(lambda k: k.hex(), [pk1,pk2,pk3,apk]))) 98 pkb1 = common.FromHex("a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a") 99 pkb2 = common.FromHex("b301803f8b5ac4a1133581fc676dfedc60d891dd5fa99028805e5ea5b08d3491af75d0707adab3b70c6a6a580217bf81") 100 pkb3 = common.FromHex("b53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f") 101 apkb = common.FromHex("a095608b35495ca05002b7b5966729dd1ed096568cf2ff24f3318468e0f3495361414a78ebc09574489bc79e48fca969") 102 103 pk1, _ = PublicKeyFromBytes(pkb1) 104 pk2, _ = PublicKeyFromBytes(pkb2) 105 pk3, _ = PublicKeyFromBytes(pkb3) 106 ) 107 108 apk, err := AggregatePublicKeys([]types.PublicKey{pk1, pk2, pk3}) 109 assert.Nil(t, err) 110 assert.Equal(t, apkb, apk.Marshal()) 111 112 _, err = AggregatePublicKeys(nil) // empty 113 assert.Equal(t, types.ErrEmptyArray, err) 114 } 115 116 func TestAggregatePublicKeysFromBytes(t *testing.T) { 117 var ( 118 // See TestAggregatePublicKeys for the data source 119 pkb1 = common.FromHex("a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a") 120 pkb2 = common.FromHex("b301803f8b5ac4a1133581fc676dfedc60d891dd5fa99028805e5ea5b08d3491af75d0707adab3b70c6a6a580217bf81") 121 pkb3 = common.FromHex("b53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f") 122 apkb = common.FromHex("a095608b35495ca05002b7b5966729dd1ed096568cf2ff24f3318468e0f3495361414a78ebc09574489bc79e48fca969") 123 ) 124 125 apk, err := AggregatePublicKeysFromBytes([][]byte{pkb1, pkb2, pkb3}) 126 assert.Nil(t, err) 127 assert.Equal(t, apkb, apk.Marshal()) 128 129 _, err = AggregatePublicKeysFromBytes(nil) // empty 130 assert.Equal(t, types.ErrEmptyArray, err) 131 132 bs := make([][]byte, 1) 133 bs[0] = []byte{1, 2, 3, 4} // len? 134 _, err = AggregatePublicKeysFromBytes(bs) 135 assert.NotNil(t, err) 136 137 bs[0] = make([]byte, types.PublicKeyLength) // is_inf? 138 _, err = AggregatePublicKeysFromBytes(bs) 139 assert.Equal(t, types.ErrPublicKeyUnmarshal, err) 140 141 bs[0] = testBadPublicKeyBytes // in_g2? 142 _, err = AggregatePublicKeysFromBytes(bs) 143 assert.Equal(t, types.ErrPublicKeyUnmarshal, err) 144 }