package qrc // The tests of this package sign with the throwaway signer below instead of // package qrcsigner. Its secrets come from a counter, which is as good as // anything to a verifier: no proof tells how its secrets were chosen. It also // builds whole trees, level by level, where Verify only walks one path. The // two meet at the root or the test fails. // testKey is one Lamport one-time key. type testKey struct { h Hash seed byte } func (k testKey) secret(bit, side int) [HashSize]byte { return k.h.Sum([]byte{'k', k.seed, byte(bit >> 8), byte(bit), byte(side)}) } // public returns the public key: the hash of secret 0 then of secret 1, for // every digest bit in turn. func (k testKey) public() []byte { pk := make([]byte, 0, PublicKeySize) for bit := 0; bit < DigestBits; bit++ { for side := 0; side < 2; side++ { secret := k.secret(bit, side) hashed := k.h.Sum(secret[:]) pk = append(pk, hashed[:]...) } } return pk } // sign returns the signature of msg. For every bit of its digest, most // significant bit of the first byte first, it holds two values: the secret // of the side the bit selects, and the hash of the secret of the other side, // side 0 before side 1. func (k testKey) sign(msg []byte) []byte { digest := k.h.Sum(msg) sig := make([]byte, 0, SignatureSize) for bit := 0; bit < DigestBits; bit++ { selected := int(digest[bit/8]>>(7-uint(bit%8))) & 1 for side := 0; side < 2; side++ { value := k.secret(bit, side) if side != selected { value = k.h.Sum(value[:]) } sig = append(sig, value[:]...) } } return sig } // testTree is a complete Merkle tree. items holds what the leaves stand for, // levels[0] the leaves, and the last level the root alone. type testTree struct { h Hash items [][]byte levels [][][HashSize]byte } // newTestTree builds the tree of the given height in which the leaves named // in keys stand for real one-time keys. Every other leaf stands for three // bytes of filler. func newTestTree(h Hash, height int, keys map[int]testKey) *testTree { tree := &testTree{h: h, items: make([][]byte, Leaves(height))} for i := range tree.items { tree.items[i] = []byte{'f', byte(i >> 8), byte(i)} } for leaf, key := range keys { tree.items[leaf] = key.public() } level := make([][HashSize]byte, len(tree.items)) for i, item := range tree.items { level[i] = h.Sum(append([]byte{0x00}, item...)) } tree.levels = [][][HashSize]byte{level} for len(level) > 1 { next := make([][HashSize]byte, len(level)/2) for i := range next { node := append([]byte{0x01}, level[2*i][:]...) node = append(node, level[2*i+1][:]...) next[i] = h.Sum(node) } tree.levels = append(tree.levels, next) level = next } return tree } func (t *testTree) root() [HashSize]byte { return t.levels[len(t.levels)-1][0] } // path returns the siblings met on the way from leaf to the root. func (t *testTree) path(leaf int) []byte { var path []byte for level := 0; level < len(t.levels)-1; level++ { sibling := t.levels[level][(leaf>>uint(level))^1] path = append(path, sibling[:]...) } return path } // testProof signs msg with a fresh key placed at leaf of a tree of the given // height, and returns the root of that tree with the proof. func testProof(h Hash, height, leaf int, msg []byte) (root [HashSize]byte, proof []byte) { key := testKey{h, byte(leaf)} tree := newTestTree(h, height, map[int]testKey{leaf: key}) return tree.root(), append(key.sign(msg), tree.path(leaf)...) } var bothHashes = []Hash{SHA256, Keccak256}