|
| 1 | +package usigverifier |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ed25519" |
| 5 | + "encoding/hex" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// Fixed test vectors locking in the two distinct signing conventions exposed |
| 12 | +// by the precompile (F-2026-17043 remediation). |
| 13 | +// |
| 14 | +// - verifyEd25519: signature must be over `"0x" + hex(msgDigest)` (66 ASCII bytes) |
| 15 | +// - verifyEd25519RawMessage: signature must be over the raw message bytes |
| 16 | +// |
| 17 | +// A signature produced for one convention MUST NOT verify under the other. |
| 18 | + |
| 19 | +// Deterministic seed so the test vectors below are reproducible and inspectable. |
| 20 | +// Anyone can re-derive these by running ed25519.NewKeyFromSeed on this 32-byte seed. |
| 21 | +var testSeed = mustHex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") |
| 22 | + |
| 23 | +// A 32-byte digest used as the input to both methods. Same input, different |
| 24 | +// signing semantics — that's the whole point of the two methods. |
| 25 | +var testDigest32 = mustHex("deadbeef00112233445566778899aabbccddeeff0123456789abcdef00ff00ff") |
| 26 | + |
| 27 | +func TestVerifyEd25519_AcceptsHexAsciiSignature(t *testing.T) { |
| 28 | + priv := ed25519.NewKeyFromSeed(testSeed) |
| 29 | + pub := priv.Public().(ed25519.PublicKey) |
| 30 | + |
| 31 | + // What verifyEd25519 expects the signer to have signed. |
| 32 | + hexAsciiBytes := []byte("0x" + hex.EncodeToString(testDigest32)) |
| 33 | + require.Len(t, hexAsciiBytes, 66, "ASCII hex form must be 66 bytes (0x + 64 hex chars)") |
| 34 | + |
| 35 | + sig := ed25519.Sign(priv, hexAsciiBytes) |
| 36 | + require.True(t, ed25519.Verify(pub, hexAsciiBytes, sig), |
| 37 | + "sanity: signature must verify against the bytes that were signed") |
| 38 | + |
| 39 | + // What verifyEd25519 actually verifies internally: |
| 40 | + verified := ed25519.Verify(pub, []byte("0x"+hex.EncodeToString(testDigest32)), sig) |
| 41 | + require.True(t, verified, "verifyEd25519 must accept signature over hex-ASCII form of digest") |
| 42 | +} |
| 43 | + |
| 44 | +func TestVerifyEd25519_RejectsRawDigestSignature(t *testing.T) { |
| 45 | + priv := ed25519.NewKeyFromSeed(testSeed) |
| 46 | + pub := priv.Public().(ed25519.PublicKey) |
| 47 | + |
| 48 | + // Signer mistakenly signs the raw 32-byte digest (the "natural" thing). |
| 49 | + rawDigestSig := ed25519.Sign(priv, testDigest32) |
| 50 | + |
| 51 | + // What verifyEd25519 actually verifies internally: |
| 52 | + verified := ed25519.Verify(pub, []byte("0x"+hex.EncodeToString(testDigest32)), rawDigestSig) |
| 53 | + require.False(t, verified, "verifyEd25519 must reject signature over raw digest bytes") |
| 54 | +} |
| 55 | + |
| 56 | +func TestVerifyEd25519RawMessage_AcceptsRawSignature(t *testing.T) { |
| 57 | + priv := ed25519.NewKeyFromSeed(testSeed) |
| 58 | + pub := priv.Public().(ed25519.PublicKey) |
| 59 | + |
| 60 | + // What verifyEd25519RawMessage expects: signature over the raw message bytes. |
| 61 | + rawSig := ed25519.Sign(priv, testDigest32) |
| 62 | + |
| 63 | + // What verifyEd25519RawMessage actually verifies internally: |
| 64 | + verified := ed25519.Verify(pub, testDigest32, rawSig) |
| 65 | + require.True(t, verified, "verifyEd25519RawMessage must accept signature over raw bytes") |
| 66 | +} |
| 67 | + |
| 68 | +func TestVerifyEd25519RawMessage_RejectsHexAsciiSignature(t *testing.T) { |
| 69 | + priv := ed25519.NewKeyFromSeed(testSeed) |
| 70 | + pub := priv.Public().(ed25519.PublicKey) |
| 71 | + |
| 72 | + // Signer (using legacy convention) signs the hex-ASCII form. |
| 73 | + hexAsciiSig := ed25519.Sign(priv, []byte("0x"+hex.EncodeToString(testDigest32))) |
| 74 | + |
| 75 | + // What verifyEd25519RawMessage actually verifies internally: |
| 76 | + verified := ed25519.Verify(pub, testDigest32, hexAsciiSig) |
| 77 | + require.False(t, verified, "verifyEd25519RawMessage must reject signature over hex-ASCII form") |
| 78 | +} |
| 79 | + |
| 80 | +// TestVerifyEd25519RawMessage_ArbitraryMessageLength sanity-checks that the raw |
| 81 | +// method works for messages other than 32-byte digests (its whole point — |
| 82 | +// no implicit assumption that the message is a digest). |
| 83 | +func TestVerifyEd25519RawMessage_ArbitraryMessageLength(t *testing.T) { |
| 84 | + priv := ed25519.NewKeyFromSeed(testSeed) |
| 85 | + pub := priv.Public().(ed25519.PublicKey) |
| 86 | + |
| 87 | + for _, msg := range [][]byte{ |
| 88 | + []byte("hello"), |
| 89 | + make([]byte, 0), // empty |
| 90 | + make([]byte, 1024), // 1 KiB |
| 91 | + []byte{0x00, 0x01, 0x02, 0x03, 0xff}, // arbitrary short |
| 92 | + } { |
| 93 | + sig := ed25519.Sign(priv, msg) |
| 94 | + require.True(t, ed25519.Verify(pub, msg, sig), |
| 95 | + "verifyEd25519RawMessage must work for messages of any length (len=%d)", len(msg)) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func mustHex(s string) []byte { |
| 100 | + b, err := hex.DecodeString(s) |
| 101 | + if err != nil { |
| 102 | + panic(err) |
| 103 | + } |
| 104 | + return b |
| 105 | +} |
0 commit comments