github.com/klaytn/klaytn@v1.12.1/crypto/bls/bls_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 bls
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  // Test single signature verification.
    26  // Usage example: validating one IBFT Commit message in handleCommit()
    27  func TestVerify(t *testing.T) {
    28  	sk, _ := RandKey()
    29  	pk := sk.PublicKey()
    30  
    31  	var msg [32]byte
    32  	sigb := Sign(sk, msg[:]).Marshal()
    33  
    34  	ok, err := VerifySignature(sigb, msg, pk)
    35  	assert.Nil(t, err)
    36  	assert.True(t, ok)
    37  }
    38  
    39  // Test aggregated signature verification for the same message.
    40  // Usage example: validating aggregated block signature in VerifyHeader()
    41  func TestAggregateVerify(t *testing.T) {
    42  	var (
    43  		sk1, _ = RandKey()
    44  		sk2, _ = RandKey()
    45  
    46  		pkb1 = sk1.PublicKey().Marshal()
    47  		pkb2 = sk2.PublicKey().Marshal()
    48  		pkbs = [][]byte{pkb1, pkb2}
    49  
    50  		msg = [32]byte{'a', 'b', 'c'}
    51  
    52  		sigb1 = Sign(sk1, msg[:]).Marshal()
    53  		sigb2 = Sign(sk2, msg[:]).Marshal()
    54  		sigbs = [][]byte{sigb1, sigb2}
    55  	)
    56  
    57  	apk, _ := AggregatePublicKeys(pkbs)
    58  	asig, _ := AggregateCompressedSignatures(sigbs)
    59  	asigb := asig.Marshal()
    60  	ok, err := VerifySignature(asigb, msg, apk)
    61  	assert.Nil(t, err)
    62  	assert.True(t, ok)
    63  }
    64  
    65  // Test aggregated signatrue verification for distinct messages.
    66  // Usage example: validating BLS registry contract
    67  func TestMultipleVerify(t *testing.T) {
    68  	var (
    69  		sk1, _ = RandKey()
    70  		sk2, _ = RandKey()
    71  
    72  		pkb1 = sk1.PublicKey().Marshal()
    73  		pkb2 = sk2.PublicKey().Marshal()
    74  		pkbs = [][]byte{pkb1, pkb2}
    75  
    76  		msg1 = [32]byte{'1', '2', '3'}
    77  		msg2 = [32]byte{'4', '5', '6'}
    78  		msgs = [][32]byte{msg1, msg2}
    79  
    80  		sigb1 = Sign(sk1, msg1[:]).Marshal()
    81  		sigb2 = Sign(sk2, msg2[:]).Marshal()
    82  		sigbs = [][]byte{sigb1, sigb2}
    83  	)
    84  
    85  	pks, _ := MultiplePublicKeysFromBytes(pkbs)
    86  	ok, err := VerifyMultipleSignatures(sigbs, msgs, pks)
    87  	assert.Nil(t, err)
    88  	assert.True(t, ok)
    89  }
    90  
    91  func TestPop(t *testing.T) {
    92  	var (
    93  		sk1, _ = RandKey()
    94  		sk2, _ = RandKey()
    95  
    96  		pk1 = sk1.PublicKey()
    97  		pk2 = sk2.PublicKey()
    98  
    99  		pop1 = PopProve(sk1)
   100  		pop2 = PopProve(sk2)
   101  	)
   102  
   103  	assert.True(t, PopVerify(pk1, pop1))
   104  	assert.True(t, PopVerify(pk2, pop2))
   105  	assert.False(t, PopVerify(pk1, pop2))
   106  	assert.False(t, PopVerify(pk2, pop1))
   107  }