github.com/klaytn/klaytn@v1.12.1/crypto/bls/blst/signature_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 testSignatureBytes = common.FromHex("0xae82747ddeefe4fd64cf9cedb9b04ae3e8a43420cd255e3c7cd06a8d88b7c7f8638543719981c5d16fa3527c468c25f0026704a6951bde891360c7e8d12ddee0559004ccdbe6046b55bae1b257ee97f7cdb955773d7cf29adf3ccbb9975e4eb9") 31 // deserialization_G2/deserialization_fails_not_in_curve.json 32 testBadSignatureBytes = common.FromHex("0x8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde0") 33 ) 34 35 func TestSignatureCopy(t *testing.T) { 36 sig, _ := SignatureFromBytes(testSignatureBytes) 37 assert.Equal(t, sig.Marshal(), sig.Copy().Marshal()) 38 } 39 40 func TestSignatureFromBytes(t *testing.T) { 41 b := testSignatureBytes 42 s, err := SignatureFromBytes(b) 43 assert.Nil(t, err) 44 assert.Equal(t, b, s.Marshal()) 45 46 _, err = SignatureFromBytes([]byte{1, 2, 3, 4}) // len? 47 assert.NotNil(t, err) 48 49 zero := make([]byte, types.SignatureLength) 50 _, err = SignatureFromBytes(zero) // is_inf? 51 assert.Equal(t, types.ErrSignatureUnmarshal, err) 52 53 _, err = SignatureFromBytes(testBadSignatureBytes) // in_g2? 54 assert.Equal(t, types.ErrSignatureUnmarshal, err) 55 } 56 57 func TestMultipleSignaturesFromBytes(t *testing.T) { 58 L := benchAggregateLen 59 tc := generateBenchmarkMaterial(L) 60 bs := tc.sigbs 61 62 // all uncached 63 sigs, err := MultipleSignaturesFromBytes(tc.sigbs[:L/2]) 64 assert.Nil(t, err) 65 for i := 0; i < L/2; i++ { 66 assert.Equal(t, tc.sigs[i].Marshal(), sigs[i].Marshal()) 67 } 68 69 // [0:L/2] are cached and [L/2:L] are uncached 70 sigs, err = MultipleSignaturesFromBytes(tc.sigbs) 71 assert.Nil(t, err) 72 for i := 0; i < L; i++ { 73 assert.Equal(t, tc.sigs[i].Marshal(), sigs[i].Marshal()) 74 } 75 76 sigs, err = MultipleSignaturesFromBytes(nil) // empty 77 assert.Nil(t, err) 78 assert.Nil(t, sigs) 79 80 bs[0] = []byte{1, 2, 3, 4} // len? 81 _, err = MultipleSignaturesFromBytes(bs) 82 assert.NotNil(t, err) 83 84 bs[0] = make([]byte, types.SignatureLength) // is_inf? 85 _, err = MultipleSignaturesFromBytes(bs) 86 assert.Equal(t, types.ErrSignatureUnmarshal, err) 87 88 bs[0] = testBadSignatureBytes // in_g2? 89 _, err = MultipleSignaturesFromBytes(bs) 90 assert.Equal(t, types.ErrSignatureUnmarshal, err) 91 } 92 93 func TestAggregateSignatures(t *testing.T) { 94 var ( 95 // https://github.com/ethereum/bls12-381-tests 96 // aggregate/aggregate_0xabababababababababababababababababababababababababababababababab.json 97 sigb1 = common.FromHex("0x91347bccf740d859038fcdcaf233eeceb2a436bcaaee9b2aa3bfb70efe29dfb2677562ccbea1c8e061fb9971b0753c240622fab78489ce96768259fc01360346da5b9f579e5da0d941e4c6ba18a0e64906082375394f337fa1af2b7127b0d121") 98 sigb2 = common.FromHex("0x9674e2228034527f4c083206032b020310face156d4a4685e2fcaec2f6f3665aa635d90347b6ce124eb879266b1e801d185de36a0a289b85e9039662634f2eea1e02e670bc7ab849d006a70b2f93b84597558a05b879c8d445f387a5d5b653df") 99 sigb3 = common.FromHex("0xae82747ddeefe4fd64cf9cedb9b04ae3e8a43420cd255e3c7cd06a8d88b7c7f8638543719981c5d16fa3527c468c25f0026704a6951bde891360c7e8d12ddee0559004ccdbe6046b55bae1b257ee97f7cdb955773d7cf29adf3ccbb9975e4eb9") 100 asigb = common.FromHex("0x9712c3edd73a209c742b8250759db12549b3eaf43b5ca61376d9f30e2747dbcf842d8b2ac0901d2a093713e20284a7670fcf6954e9ab93de991bb9b313e664785a075fc285806fa5224c82bde146561b446ccfc706a64b8579513cfc4ff1d930") 101 102 sig1, _ = SignatureFromBytes(sigb1) 103 sig2, _ = SignatureFromBytes(sigb2) 104 sig3, _ = SignatureFromBytes(sigb3) 105 ) 106 107 asig, err := AggregateSignatures([]types.Signature{sig1, sig2, sig3}) 108 assert.Nil(t, err) 109 assert.Equal(t, asigb, asig.Marshal()) 110 111 _, err = AggregateSignatures(nil) // empty 112 assert.Equal(t, types.ErrEmptyArray, err) 113 } 114 115 func TestAggregateSignaturesFromBytes(t *testing.T) { 116 var ( 117 // https://github.com/ethereum/bls12-381-tests 118 // aggregate/aggregate_0xabababababababababababababababababababababababababababababababab.json 119 sigb1 = common.FromHex("0x91347bccf740d859038fcdcaf233eeceb2a436bcaaee9b2aa3bfb70efe29dfb2677562ccbea1c8e061fb9971b0753c240622fab78489ce96768259fc01360346da5b9f579e5da0d941e4c6ba18a0e64906082375394f337fa1af2b7127b0d121") 120 sigb2 = common.FromHex("0x9674e2228034527f4c083206032b020310face156d4a4685e2fcaec2f6f3665aa635d90347b6ce124eb879266b1e801d185de36a0a289b85e9039662634f2eea1e02e670bc7ab849d006a70b2f93b84597558a05b879c8d445f387a5d5b653df") 121 sigb3 = common.FromHex("0xae82747ddeefe4fd64cf9cedb9b04ae3e8a43420cd255e3c7cd06a8d88b7c7f8638543719981c5d16fa3527c468c25f0026704a6951bde891360c7e8d12ddee0559004ccdbe6046b55bae1b257ee97f7cdb955773d7cf29adf3ccbb9975e4eb9") 122 asigb = common.FromHex("0x9712c3edd73a209c742b8250759db12549b3eaf43b5ca61376d9f30e2747dbcf842d8b2ac0901d2a093713e20284a7670fcf6954e9ab93de991bb9b313e664785a075fc285806fa5224c82bde146561b446ccfc706a64b8579513cfc4ff1d930") 123 ) 124 125 asig, err := AggregateSignaturesFromBytes([][]byte{sigb1, sigb2, sigb3}) 126 assert.Nil(t, err) 127 assert.Equal(t, asigb, asig.Marshal()) 128 129 _, err = AggregateSignaturesFromBytes(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 = AggregateSignaturesFromBytes(bs) 135 assert.NotNil(t, err) 136 137 bs[0] = make([]byte, types.SignatureLength) // is_inf? 138 _, err = AggregateSignaturesFromBytes(bs) 139 assert.Equal(t, types.ErrSignatureUnmarshal, err) 140 141 bs[0] = testBadSignatureBytes // in_g2? 142 _, err = AggregateSignaturesFromBytes(bs) 143 assert.Equal(t, types.ErrSignatureUnmarshal, err) 144 } 145 146 func TestSignVerify(t *testing.T) { 147 var ( 148 // https://github.com/ethereum/bls12-381-tests 149 // sign/sign_case_84d45c9c7cca6b92.json 150 // verify/verify_valid_case_195246ee3bd3b6ec.json 151 skb = common.FromHex("0x328388aff0d4a5b7dc9205abd374e7e98f3cd9f3418edb4eafda5fb16473d216") 152 msg = common.FromHex("0xabababababababababababababababababababababababababababababababab") 153 pkb = common.FromHex("0xb53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f") 154 sigb = common.FromHex("0xae82747ddeefe4fd64cf9cedb9b04ae3e8a43420cd255e3c7cd06a8d88b7c7f8638543719981c5d16fa3527c468c25f0026704a6951bde891360c7e8d12ddee0559004ccdbe6046b55bae1b257ee97f7cdb955773d7cf29adf3ccbb9975e4eb9") 155 156 sk, _ = SecretKeyFromBytes(skb) 157 pk, _ = PublicKeyFromBytes(pkb) 158 sig, _ = SignatureFromBytes(sigb) 159 ) 160 161 assert.Equal(t, sigb, Sign(sk, msg).Marshal()) 162 assert.True(t, Verify(sig, msg, pk)) 163 164 sk2, _ := RandKey() 165 pk2 := sk2.PublicKey() 166 msg2 := []byte("test message") 167 assert.True(t, Verify(Sign(sk2, msg2), msg2, pk2)) 168 assert.False(t, Verify(Sign(sk2, msg), msg, pk)) 169 assert.False(t, Verify(Sign(sk, msg2), msg, pk)) 170 assert.False(t, Verify(Sign(sk, msg), msg2, pk)) 171 assert.False(t, Verify(Sign(sk, msg), msg, pk2)) 172 } 173 174 func TestPopProveVerify(t *testing.T) { 175 var ( 176 // https://github.com/Chia-Network/bls-signatures/blob/2.0.2/src/test.cpp 177 // "Chia test vector 3 (PoP)" 178 // Note: `skb` is the result of PopSchemeMPL().KeyGen(seed1) in the testcase 179 skb = common.FromHex("0x258787ef728c898e43bc76244d70f468c9c7e1338a107b18b42da0d86b663c26") 180 popb = common.FromHex("0x84f709159435f0dc73b3e8bf6c78d85282d19231555a8ee3b6e2573aaf66872d9203fefa1ef700e34e7c3f3fb28210100558c6871c53f1ef6055b9f06b0d1abe22ad584ad3b957f3018a8f58227c6c716b1e15791459850f2289168fa0cf9115") 181 182 sk, _ = SecretKeyFromBytes(skb) 183 pk = sk.PublicKey() 184 pop = PopProve(sk) 185 ) 186 assert.Equal(t, popb, pop.Marshal()) 187 assert.True(t, PopVerify(pk, pop)) 188 189 sk2, _ := RandKey() 190 pk2 := sk2.PublicKey() 191 assert.True(t, PopVerify(pk2, PopProve(sk2))) 192 assert.False(t, PopVerify(pk2, PopProve(sk))) 193 assert.False(t, PopVerify(pk, PopProve(sk2))) 194 } 195 196 func TestAggregateVerify(t *testing.T) { 197 var ( 198 // https://github.com/ethereum/bls12-381-tests 199 // fast_aggregate_verify/fast_aggregate_verify_valid_3d7576f3c0e3570a.json 200 pkb1 = common.FromHex("0xa491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a") 201 pkb2 = common.FromHex("0xb301803f8b5ac4a1133581fc676dfedc60d891dd5fa99028805e5ea5b08d3491af75d0707adab3b70c6a6a580217bf81") 202 pkb3 = common.FromHex("0xb53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f") 203 msg = common.FromHex("0xabababababababababababababababababababababababababababababababab") 204 asigb = common.FromHex("0x9712c3edd73a209c742b8250759db12549b3eaf43b5ca61376d9f30e2747dbcf842d8b2ac0901d2a093713e20284a7670fcf6954e9ab93de991bb9b313e664785a075fc285806fa5224c82bde146561b446ccfc706a64b8579513cfc4ff1d930") 205 206 sig, _ = SignatureFromBytes(asigb) 207 ) 208 209 apk, _ := AggregatePublicKeysFromBytes([][]byte{pkb1, pkb2, pkb3}) 210 assert.True(t, Verify(sig, msg, apk)) 211 } 212 213 func TestVerifyMultiple(t *testing.T) { 214 var ( 215 // https://github.com/ethereum/bls12-381-tests 216 // batch_verify/batch_verify_valid_simple_signature_set.json 217 pkb1 = common.FromHex("0xa491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a") 218 pkb2 = common.FromHex("0xb301803f8b5ac4a1133581fc676dfedc60d891dd5fa99028805e5ea5b08d3491af75d0707adab3b70c6a6a580217bf81") 219 pkb3 = common.FromHex("0xb53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f") 220 msg1 = common.BytesToHash(common.FromHex("0x0000000000000000000000000000000000000000000000000000000000000000")) 221 msg2 = common.BytesToHash(common.FromHex("0x5656565656565656565656565656565656565656565656565656565656565656")) 222 msg3 = common.BytesToHash(common.FromHex("0xabababababababababababababababababababababababababababababababab")) 223 sigb1 = common.FromHex("0xb6ed936746e01f8ecf281f020953fbf1f01debd5657c4a383940b020b26507f6076334f91e2366c96e9ab279fb5158090352ea1c5b0c9274504f4f0e7053af24802e51e4568d164fe986834f41e55c8e850ce1f98458c0cfc9ab380b55285a55") 224 sigb2 = common.FromHex("0xaf1390c3c47acdb37131a51216da683c509fce0e954328a59f93aebda7e4ff974ba208d9a4a2a2389f892a9d418d618418dd7f7a6bc7aa0da999a9d3a5b815bc085e14fd001f6a1948768a3f4afefc8b8240dda329f984cb345c6363272ba4fe") 225 sigb3 = common.FromHex("0xae82747ddeefe4fd64cf9cedb9b04ae3e8a43420cd255e3c7cd06a8d88b7c7f8638543719981c5d16fa3527c468c25f0026704a6951bde891360c7e8d12ddee0559004ccdbe6046b55bae1b257ee97f7cdb955773d7cf29adf3ccbb9975e4eb9") 226 227 sigbs = [][]byte{sigb1, sigb2, sigb3} 228 msgs = [][32]byte{msg1, msg2, msg3} 229 pks, _ = MultiplePublicKeysFromBytes([][]byte{pkb1, pkb2, pkb3}) 230 sigs, _ = MultipleSignaturesFromBytes(sigbs) 231 ) 232 233 ok, err := VerifyMultipleSignatures(sigbs, msgs, pks) 234 assert.Nil(t, err) 235 assert.True(t, ok) 236 237 // Verify individually 238 assert.True(t, Verify(sigs[0], msgs[0][:], pks[0])) 239 assert.True(t, Verify(sigs[1], msgs[1][:], pks[1])) 240 assert.True(t, Verify(sigs[2], msgs[2][:], pks[2])) 241 }