schnorr_test.mx raw

   1  package secp256k1
   2  
   3  import (
   4  	"smesh.lol/web/common/helpers"
   5  	"testing"
   6  )
   7  
   8  func TestPubKeyFromSecKey(t *testing.T) {
   9  	// BIP-340 test vector 0.
  10  	seckey, _ := helpers.HexDecode32("0000000000000000000000000000000000000000000000000000000000000003")
  11  	expected := "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"
  12  
  13  	pubkey, ok := PubKeyFromSecKey(seckey)
  14  	if !ok {
  15  		t.Fatal("PubKeyFromSecKey failed")
  16  	}
  17  	got := helpers.HexEncode(pubkey[:])
  18  
  19  	if got != expected {
  20  		t.Errorf("PubKeyFromSecKey:\ngot  %s\nwant %s", got, expected)
  21  	}
  22  }
  23  
  24  func TestSignVerify(t *testing.T) {
  25  	seckey, _ := helpers.HexDecode32("0000000000000000000000000000000000000000000000000000000000000003")
  26  	pubkey, _ := PubKeyFromSecKey(seckey)
  27  	msg, _ := helpers.HexDecode32("0000000000000000000000000000000000000000000000000000000000000000")
  28  	aux, _ := helpers.HexDecode32("0000000000000000000000000000000000000000000000000000000000000000")
  29  
  30  	sig, ok := SignSchnorr(seckey, msg, aux)
  31  	if !ok {
  32  		t.Fatal("SignSchnorr failed")
  33  	}
  34  
  35  	if !VerifySchnorr(pubkey, msg, sig) {
  36  		t.Error("VerifySchnorr failed on own signature")
  37  	}
  38  
  39  	// Tamper with message.
  40  	msg[0] ^= 1
  41  	if VerifySchnorr(pubkey, msg, sig) {
  42  		t.Error("VerifySchnorr should fail on tampered message")
  43  	}
  44  }
  45  
  46  func TestBIP340Vector1(t *testing.T) {
  47  	// Test vector 1 from BIP-340.
  48  	seckey, _ := helpers.HexDecode32("0000000000000000000000000000000000000000000000000000000000000001")
  49  	pubkey, ok := PubKeyFromSecKey(seckey)
  50  	if !ok {
  51  		t.Fatal("PubKeyFromSecKey failed")
  52  	}
  53  	expectedPubkey := "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
  54  
  55  	got := helpers.HexEncode(pubkey[:])
  56  	if got != expectedPubkey {
  57  		t.Errorf("pubkey mismatch:\ngot  %s\nwant %s", got, expectedPubkey)
  58  	}
  59  }
  60