github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/bbs/subtle/bls12381g2_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package subtle
     8  
     9  import (
    10  	"crypto/rand"
    11  	"crypto/sha256"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  	bbs "github.com/trustbloc/bbs-signature-go/bbs12381g2pub"
    16  )
    17  
    18  func TestBBSG2_Sign(t *testing.T) {
    19  	pubKey, privKey, err := generateKeyPairRandom()
    20  	require.NoError(t, err)
    21  
    22  	privKeyBytes, err := privKey.Marshal()
    23  	require.NoError(t, err)
    24  
    25  	pubKeyBytes, err := pubKey.Marshal()
    26  	require.NoError(t, err)
    27  
    28  	blsSigner := NewBLS12381G2Signer(privKeyBytes)
    29  	blsVerifier := NewBLS12381G2Verifier(pubKeyBytes)
    30  
    31  	messagesBytes := [][]byte{[]byte("message1"), []byte("message2")}
    32  
    33  	signatureBytes, err := blsSigner.Sign(messagesBytes)
    34  	require.NoError(t, err)
    35  	require.NotEmpty(t, signatureBytes)
    36  	require.Len(t, signatureBytes, 112)
    37  
    38  	require.NoError(t, blsVerifier.Verify(messagesBytes, signatureBytes))
    39  
    40  	// at least one message must be passed
    41  	signatureBytes, err = blsSigner.Sign([][]byte{})
    42  	require.Error(t, err)
    43  	require.EqualError(t, err, "messages are not defined")
    44  	require.Nil(t, signatureBytes)
    45  }
    46  
    47  func TestBBSG2_DeriveProof(t *testing.T) {
    48  	pubKey, privKey, err := generateKeyPairRandom()
    49  	require.NoError(t, err)
    50  
    51  	privKeyBytes, err := privKey.Marshal()
    52  	require.NoError(t, err)
    53  
    54  	pubKeyBytes, err := pubKey.Marshal()
    55  	require.NoError(t, err)
    56  
    57  	messagesBytes := [][]byte{
    58  		[]byte("message1"),
    59  		[]byte("message2"),
    60  		[]byte("message3"),
    61  		[]byte("message4"),
    62  	}
    63  
    64  	blsSigner := NewBLS12381G2Signer(privKeyBytes)
    65  	blsVerifier := NewBLS12381G2Verifier(pubKeyBytes)
    66  
    67  	signatureBytes, err := blsSigner.Sign(messagesBytes)
    68  	require.NoError(t, err)
    69  
    70  	require.NoError(t, blsVerifier.Verify(messagesBytes, signatureBytes))
    71  
    72  	nonce := []byte("nonce")
    73  	revealedIndexes := []int{0, 2}
    74  	proofBytes, err := blsVerifier.DeriveProof(messagesBytes, signatureBytes, nonce, revealedIndexes)
    75  	require.NoError(t, err)
    76  	require.NotEmpty(t, proofBytes)
    77  
    78  	revealedMessages := make([][]byte, len(revealedIndexes))
    79  	for i, ind := range revealedIndexes {
    80  		revealedMessages[i] = messagesBytes[ind]
    81  	}
    82  
    83  	require.NoError(t, blsVerifier.VerifyProof(revealedMessages, proofBytes, nonce))
    84  }
    85  
    86  func generateKeyPairRandom() (*bbs.PublicKey, *bbs.PrivateKey, error) {
    87  	seed := make([]byte, 32)
    88  
    89  	_, err := rand.Read(seed)
    90  	if err != nil {
    91  		panic(err)
    92  	}
    93  
    94  	return bbs.GenerateKeyPair(sha256.New, seed)
    95  }