package secp256k1 import ( "encoding/hex" "testing" ) func TestECDHSymmetry(t *testing.T) { var a, b [32]byte a[31] = 1 b[31] = 2 pubA, ok := PubKey(a) if !ok { t.Fatal("pub a") } pubB, ok := PubKey(b) if !ok { t.Fatal("pub b") } x1, ok := ECDH(a, pubB) if !ok { t.Fatal("ecdh a,b") } x2, ok := ECDH(b, pubA) if !ok { t.Fatal("ecdh b,a") } if x1 != x2 { t.Fatalf("shared secrets differ: %x %x", x1, x2) } // 2*G has this x coordinate. const want = "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5" if got := hex.EncodeToString(x1[:]); got != want { t.Fatalf("x = %s, want %s", got, want) } } func TestECDHRejectsBadInput(t *testing.T) { var zero, one [32]byte one[31] = 1 if _, ok := ECDH(zero, one); ok { t.Fatal("accepted zero secret") } badPub := [32]byte{0xff} if _, ok := ECDH(one, badPub); ok { t.Fatal("accepted off-curve public key") } } func TestValidatePubKey(t *testing.T) { var one [32]byte one[31] = 1 pub, _ := PubKey(one) if !ValidatePubKey(pub) { t.Fatal("rejected a real public key") } if ValidatePubKey([32]byte{0xff}) { t.Fatal("accepted an off-curve key") } }