From a5f9732ba00df15025ca6c95909c8bcc432eb07f Mon Sep 17 00:00:00 2001 From: Daniel Cason Date: Tue, 19 Apr 2022 14:54:29 +0200 Subject: [PATCH] BLS: simple sign and verify using ostracon library * FIXME: when a signature verification fails, an error is printed. --- crypto/bls/ostracon/bls_test.go | 2 +- crypto/bls/simple_sign_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crypto/bls/ostracon/bls_test.go b/crypto/bls/ostracon/bls_test.go index 5ad9b95ab..9945df614 100644 --- a/crypto/bls/ostracon/bls_test.go +++ b/crypto/bls/ostracon/bls_test.go @@ -359,7 +359,7 @@ func TestSignAndValidateBLS12(t *testing.T) { msg := crypto.CRandBytes(128) sig, err := privKey.Sign(msg) require.Nil(t, err) - fmt.Printf("restoring signature: %x\n", sig) + //fmt.Printf("restoring signature: %x\n", sig) // Test the signature assert.True(t, pubKey.VerifySignature(msg, sig)) diff --git a/crypto/bls/simple_sign_test.go b/crypto/bls/simple_sign_test.go index b604cb90e..09003cfa3 100644 --- a/crypto/bls/simple_sign_test.go +++ b/crypto/bls/simple_sign_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/tendermint/tendermint/crypto/bls/blst" + "github.com/tendermint/tendermint/crypto/bls/ostracon" "github.com/tendermint/tendermint/crypto/ed25519" ) @@ -47,6 +48,26 @@ func TestBlstSignVerify(t *testing.T) { } } +func TestOstraconSignVerify(t *testing.T) { + m := []byte("a test message to be signed") + privKey := ostracon.GenPrivKey() + pubKey := privKey.PubKey() + + sig, err := privKey.Sign(m) + if err != nil { + t.Error("Unexpected nil signature or error", m, sig, err) + } + if !pubKey.VerifySignature(m, sig) { + t.Error("Failed to verify signature produced by the key", m, sig) + } + if pubKey.VerifySignature(scrambleBytes(m), sig) { + t.Error("Unexpected to verify scrambled message", scrambleBytes(m), sig) + } + if pubKey.VerifySignature(m, scrambleBytes(sig)) { + t.Error("Unexpected to verify scrambled signature", m, scrambleBytes(sig)) + } +} + // Scrambles a byte array, currently just flipping a bit. // TODO: implement a more complex scrambling method. func scrambleBytes(b []byte) []byte {