github.com/cloudflare/circl@v1.5.0/sign/bls/vectors_test.go (about)

     1  package bls_test
     2  
     3  import (
     4  	"archive/zip"
     5  	"bufio"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/cloudflare/circl/internal/test"
    12  	"github.com/cloudflare/circl/sign/bls"
    13  )
    14  
    15  func TestVectors(t *testing.T) {
    16  	// Test vectors taken from:
    17  	// Repository:  https://github.com/kwantam/bls_sigs_ref/tree/sgn0_fix/test-vectors
    18  	// Branch: sig0_fix
    19  	// Path: /test-vectors/sig_[g1|g2]_basic/[name]
    20  	// Compression: zip
    21  
    22  	for _, name := range []string{"P256", "P521"} {
    23  		t.Run(name+"/G1", func(t *testing.T) { testVector[bls.KeyG2SigG1](t, "g1", name) })
    24  		t.Run(name+"/G2", func(t *testing.T) { testVector[bls.KeyG1SigG2](t, "g2", name) })
    25  	}
    26  }
    27  
    28  func testVector[K bls.KeyGroup](t *testing.T, group, name string) {
    29  	nameFile := fmt.Sprintf("./testdata/sig_%v_basic_%v.txt.zip", group, name)
    30  	zipFile, err := zip.OpenReader(nameFile)
    31  	test.CheckNoErr(t, err, "error opening zipped file")
    32  
    33  	for _, f := range zipFile.File {
    34  		unzipped, err := f.Open()
    35  		test.CheckNoErr(t, err, "error opening unzipped file")
    36  
    37  		scanner := bufio.NewScanner(unzipped)
    38  		for scanner.Scan() {
    39  			line := scanner.Text()
    40  			inputs := strings.Split(line, " ")
    41  			if len(inputs) != 3 {
    42  				t.Fatalf("bad input length")
    43  			}
    44  
    45  			msg, err := hex.DecodeString(inputs[0])
    46  			test.CheckNoErr(t, err, "error decoding msg")
    47  			seed, err := hex.DecodeString(inputs[1])
    48  			test.CheckNoErr(t, err, "error decoding sk")
    49  			wantSig := inputs[2]
    50  
    51  			salt := []byte("BLS-SIG-KEYGEN-SALT-")
    52  			keyInfo := []byte("")
    53  			priv, err := bls.KeyGen[K](seed, salt, keyInfo)
    54  			test.CheckNoErr(t, err, "error generating priv key")
    55  
    56  			sig := bls.Sign(priv, msg)
    57  			gotSig := hex.EncodeToString(sig)
    58  
    59  			if gotSig != wantSig {
    60  				test.ReportError(t, gotSig, wantSig, msg)
    61  			}
    62  
    63  			pub := priv.PublicKey()
    64  			test.CheckOk(bls.Verify(pub, msg, sig), "cannot verify", t)
    65  		}
    66  	}
    67  }