package qrc import ( "crypto/merkle" "encoding/hex" "testing" ) func TestSizes(t *testing.T) { for _, tc := range []struct { name string got, want int }{ {"HashSize", HashSize, 32}, {"DigestBits", DigestBits, 256}, {"PublicKeySize", PublicKeySize, 16384}, {"SignatureSize", SignatureSize, 16384}, {"MinHeight", MinHeight, 6}, {"MaxHeight", MaxHeight, 16}, {"Leaves(MinHeight)", Leaves(MinHeight), 64}, {"Leaves(MaxHeight)", Leaves(MaxHeight), 65536}, {"ProofSize(MinHeight)", ProofSize(MinHeight), 16576}, {"ProofSize(MaxHeight)", ProofSize(MaxHeight), 16896}, } { if tc.got != tc.want { t.Errorf("%s = %d, want %d", tc.name, tc.got, tc.want) } } } // TestHashes pins down which functions the two names stand for. Keccak256 is // the original Keccak with its 0x01 padding, not the SHA3-256 of FIPS 202, // whose digest of the empty string starts with a7ffc6f8. func TestHashes(t *testing.T) { for _, tc := range []struct { h Hash name string empty string abc string }{ { SHA256, "sha256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", }, { Keccak256, "keccak256", "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45", }, } { if !tc.h.Valid() { t.Errorf("%s is not valid", tc.name) } if tc.h.String() != tc.name { t.Errorf("String() = %q, want %q", tc.h.String(), tc.name) } empty := tc.h.Sum(nil) if got := hex.EncodeToString(empty[:]); got != tc.empty { t.Errorf("%s of nothing = %s", tc.name, got) } abc := tc.h.Sum([]byte("abc")) if got := hex.EncodeToString(abc[:]); got != tc.abc { t.Errorf("%s of abc = %s", tc.name, got) } } for _, h := range []Hash{0, 3, 255} { if h.Valid() { t.Errorf("hash %d is valid", h) } if h.String() != "unknown" { t.Errorf("hash %d is called %q", h, h.String()) } } } func TestVerify(t *testing.T) { msg := []byte("bind this key to my account") for _, h := range bothHashes { for _, tc := range []struct { height int leaves []int }{ {6, []int{0, 1, 2, 31, 32, 37, 62, 63}}, {7, []int{0, 64, 127}}, {11, []int{1365, 682, 2047}}, {16, []int{0, 21845, 43690, 65535}}, } { keys := map[int]testKey{} for _, leaf := range tc.leaves { keys[leaf] = testKey{h, byte(leaf)} } tree := newTestTree(h, tc.height, keys) for _, leaf := range tc.leaves { proof := append(keys[leaf].sign(msg), tree.path(leaf)...) if len(proof) != ProofSize(tc.height) { t.Fatalf("%s, height %d: proof of %d bytes", h, tc.height, len(proof)) } if err := Verify(h, tree.root(), tc.height, leaf, msg, proof); err != nil { t.Errorf("%s, height %d, leaf %d: %v", h, tc.height, leaf, err) } } } } } // TestVerifyMessages signs messages of very different sizes: the empty one, // single bytes and a long one. func TestVerifyMessages(t *testing.T) { long := make([]byte, 70000) for i := range long { long[i] = byte(i * 7) } for _, h := range bothHashes { for i, msg := range [][]byte{nil, {0}, {0xff}, []byte("a"), []byte("b"), long} { root, proof := testProof(h, MinHeight, 5+i, msg) if err := Verify(h, root, MinHeight, 5+i, msg, proof); err != nil { t.Errorf("%s, message %d: %v", h, i, err) } } } } func TestVerifyRejectsChangedProof(t *testing.T) { const height, leaf = MinHeight, 37 msg := []byte("bind this key to my account") for _, h := range bothHashes { root, proof := testProof(h, height, leaf, msg) if err := Verify(h, root, height, leaf, msg, proof); err != nil { t.Fatalf("%s: %v", h, err) } // One flipped bit anywhere breaks the proof: in a secret, in the hash // of a secret that stays private, in a sibling of the path. positions := []int{ 0, 31, 32, 63, 64, // first pair and the start of the second SignatureSize - 64, SignatureSize - 33, SignatureSize - 32, SignatureSize - 1, SignatureSize, SignatureSize + 31, SignatureSize + 32, // path len(proof) - 32, len(proof) - 1, } for at := 101; at < len(proof); at += 211 { positions = append(positions, at) } for _, at := range positions { for _, mask := range []byte{0x01, 0x80} { changed := append([]byte(nil), proof...) changed[at] ^= mask if err := Verify(h, root, height, leaf, msg, changed); err != ErrMismatch { t.Fatalf("%s: byte %d changed, got %v", h, at, err) } } } // Two pairs swapped, two siblings swapped. swapped := append([]byte(nil), proof...) copy(swapped[0:64], proof[64:128]) copy(swapped[64:128], proof[0:64]) if err := Verify(h, root, height, leaf, msg, swapped); err != ErrMismatch { t.Errorf("%s: pairs swapped, got %v", h, err) } swapped = append([]byte(nil), proof...) copy(swapped[SignatureSize:SignatureSize+32], proof[SignatureSize+32:SignatureSize+64]) copy(swapped[SignatureSize+32:SignatureSize+64], proof[SignatureSize:SignatureSize+32]) if err := Verify(h, root, height, leaf, msg, swapped); err != ErrMismatch { t.Errorf("%s: siblings swapped, got %v", h, err) } // The halves of a pair swapped: a hash shown where the secret goes. swapped = append([]byte(nil), proof...) copy(swapped[0:32], proof[32:64]) copy(swapped[32:64], proof[0:32]) if err := Verify(h, root, height, leaf, msg, swapped); err != ErrMismatch { t.Errorf("%s: halves of a pair swapped, got %v", h, err) } // The public key itself is not a signature of anything. key := testKey{h, byte(leaf)} tree := newTestTree(h, height, map[int]testKey{leaf: key}) forged := append(key.public(), tree.path(leaf)...) if err := Verify(h, root, height, leaf, msg, forged); err != ErrMismatch { t.Errorf("%s: public key shown as a signature, got %v", h, err) } } } func TestVerifyRejectsChangedRequest(t *testing.T) { const height, leaf = MinHeight, 37 msg := []byte("bind this key to my account") for _, h := range bothHashes { root, proof := testProof(h, height, leaf, msg) // Another message, however close. for _, other := range [][]byte{ nil, []byte("bind this key to my accounu"), []byte("bind this key to my account "), []byte("bind this key to my accoun"), []byte("Bind this key to my account"), } { if err := Verify(h, root, height, leaf, other, proof); err != ErrMismatch { t.Errorf("%s: message %q, got %v", h, other, err) } } // Another leaf: the path only leads to the root from where it started. for other := 0; other < Leaves(height); other++ { if other == leaf { continue } if err := Verify(h, root, height, other, msg, proof); err != ErrMismatch { t.Fatalf("%s: leaf %d, got %v", h, other, err) } } // Another root. for at := 0; at < HashSize; at++ { other := root other[at] ^= 0x10 if err := Verify(h, other, height, leaf, msg, proof); err != ErrMismatch { t.Fatalf("%s: root byte %d changed, got %v", h, at, err) } } if err := Verify(h, [HashSize]byte{}, height, leaf, msg, proof); err != ErrMismatch { t.Errorf("%s: zero root, got %v", h, err) } // The other hash function. other := SHA256 if h == SHA256 { other = Keccak256 } if err := Verify(other, root, height, leaf, msg, proof); err != ErrMismatch { t.Errorf("%s proof checked with %s, got %v", h, other, err) } } } func TestVerifyRejectsBadArguments(t *testing.T) { const height, leaf = MinHeight, 37 msg := []byte("bind this key to my account") root, proof := testProof(SHA256, height, leaf, msg) for _, tc := range []struct { name string h Hash height int leaf int proof []byte want error }{ {"valid", SHA256, height, leaf, proof, nil}, {"hash 0", 0, height, leaf, proof, ErrUnknownHash}, {"hash 3", 3, height, leaf, proof, ErrUnknownHash}, {"height 0", SHA256, 0, 0, proof, ErrHeight}, {"height 5", SHA256, MinHeight - 1, leaf % 32, proof[:ProofSize(MinHeight-1)], ErrHeight}, {"height 17", SHA256, MaxHeight + 1, leaf, make([]byte, ProofSize(MaxHeight+1)), ErrHeight}, {"height -1", SHA256, -1, leaf, proof, ErrHeight}, {"leaf -1", SHA256, height, -1, proof, ErrLeafIndex}, {"leaf 64", SHA256, height, 64, proof, ErrLeafIndex}, {"leaf 64+37", SHA256, height, 64 + leaf, proof, ErrLeafIndex}, {"no proof", SHA256, height, leaf, nil, ErrProofSize}, {"proof one byte short", SHA256, height, leaf, proof[:len(proof)-1], ErrProofSize}, {"proof one byte long", SHA256, height, leaf, append(append([]byte(nil), proof...), 0), ErrProofSize}, {"proof one sibling short", SHA256, height, leaf, proof[:len(proof)-HashSize], ErrProofSize}, {"signature alone", SHA256, height, leaf, proof[:SignatureSize], ErrProofSize}, // A proof cannot pass for one of a taller or shorter tree. {"height 7, proof of 6", SHA256, height + 1, leaf, proof, ErrProofSize}, {"height 7, padded proof", SHA256, height + 1, leaf, append(append([]byte(nil), proof...), make([]byte, HashSize)...), ErrMismatch}, } { if err := Verify(tc.h, root, tc.height, tc.leaf, msg, tc.proof); err != tc.want { t.Errorf("%s: got %v, want %v", tc.name, err, tc.want) } } } // TestSHA256TreeIsTheChainTree checks the SHA-256 tree against crypto/merkle, // the native implementation of the Merkle tree gno.land itself uses for its // blocks: same leaf and node hashing, same root, same proof layout. func TestSHA256TreeIsTheChainTree(t *testing.T) { msg := []byte("bind this key to my account") for _, tc := range []struct { height int leaves []int }{ {6, []int{0, 41, 63}}, {9, []int{1, 170, 341, 511}}, } { keys := map[int]testKey{} for _, leaf := range tc.leaves { keys[leaf] = testKey{SHA256, byte(leaf)} } tree := newTestTree(SHA256, tc.height, keys) root := tree.root() // The root, computed by crypto/merkle from what the leaves stand for. count := len(tree.items) encoded := []byte{byte(count >> 24), byte(count >> 16), byte(count >> 8), byte(count)} for _, item := range tree.items { size := len(item) encoded = append(encoded, byte(size>>24), byte(size>>16), byte(size>>8), byte(size)) encoded = append(encoded, item...) } if got := merkle.HashFromByteSlices(encoded); hex.EncodeToString(got) != hex.EncodeToString(root[:]) { t.Fatalf("height %d: crypto/merkle root %x, tree root %x", tc.height, got, root) } // The path part of a proof is a crypto/merkle proof of the public key. for _, leaf := range tc.leaves { path := tree.path(leaf) if !merkle.VerifySimpleProof(root[:], keys[leaf].public(), leaf, count, path) { t.Errorf("height %d, leaf %d: crypto/merkle rejects the path", tc.height, leaf) } proof := append(keys[leaf].sign(msg), path...) if err := Verify(SHA256, root, tc.height, leaf, msg, proof); err != nil { t.Errorf("height %d, leaf %d: %v", tc.height, leaf, err) } } } }