package qrc import "errors" const ( // DigestBits is the number of digest bits a Lamport key signs, and so // the number of secret pairs in a key. DigestBits = HashSize * 8 // PublicKeySize is the size of a Lamport public key: two hashes per // digest bit. PublicKeySize = DigestBits * 2 * HashSize // SignatureSize is the size of a Lamport signature: the public key with // one hash of each pair replaced by the secret it is the hash of. SignatureSize = PublicKeySize // MinHeight is the smallest tree a commit may use. It holds 64 one-time // keys, the minimum the LAWS ask for. MinHeight = 6 // MaxHeight is the largest tree a commit may use, 65536 one-time keys. MaxHeight = 16 ) // Domain separation of tree nodes, as in RFC 6962. A Lamport secret is hashed // without prefix: it is exactly HashSize bytes long, a length neither a leaf // nor an inner node input can have. const ( leafPrefix = 0x00 innerPrefix = 0x01 ) var ( ErrUnknownHash = errors.New("qrc: unknown hash function") ErrHeight = errors.New("qrc: tree height out of range") ErrLeafIndex = errors.New("qrc: leaf index out of range") ErrProofSize = errors.New("qrc: proof has the wrong size") ErrMismatch = errors.New("qrc: proof does not match the commit") ) // ProofSize returns the exact size, in bytes, of a proof for a tree of the // given height: one signature and one sibling hash per tree level. func ProofSize(height int) int { return SignatureSize + height*HashSize } // Leaves returns the number of one-time keys in a tree of the given height. func Leaves(height int) int { return 1 << uint(height) } // Verify checks a proof against a commit. // // It returns nil when proof shows that msg was signed by the Lamport key // stored at leaf of the Merkle tree of the given height whose root is root, // everything hashed with h. The proof layout is described in the package // documentation; its size must be exactly [ProofSize](height). // // Verify is a pure function of its arguments. It does not know whether the // leaf has signed before: see the package documentation on one-time keys. func Verify(h Hash, root [HashSize]byte, height int, leaf int, msg []byte, proof []byte) error { if !h.Valid() { return ErrUnknownHash } if height < MinHeight || height > MaxHeight { return ErrHeight } if leaf < 0 || leaf >= Leaves(height) { return ErrLeafIndex } if len(proof) != ProofSize(height) { return ErrProofSize } sum := h.function() node := recoverLeaf(sum, sum(msg), proof) if climb(sum, node, leaf, height, proof) != root { return ErrMismatch } return nil } // recoverLeaf returns the leaf hash of the one public key under which the // first SignatureSize bytes of proof are a Lamport signature of digest. // // A signature is the public key with, in every pair, the hash selected by the // digest bit replaced by its secret. Hashing that secret in place therefore // turns the signature back into the public key: one copy of the signature, // then one hash and one 32-byte copy per digest bit. The loop is written for // the gas meter, where every built-in call has a fixed price well above that // of a SHA-256 of 32 bytes. func recoverLeaf(sum func([]byte) [HashSize]byte, digest [HashSize]byte, proof []byte) [HashSize]byte { buf := make([]byte, 1+PublicKeySize) buf[0] = leafPrefix copy(buf[1:], proof[:SignatureSize]) at := 1 // start of the pair of the current digest bit for _, d := range digest { for mask := byte(0x80); mask != 0; mask >>= 1 { slot := at if d&mask != 0 { slot += HashSize } secret := buf[slot : slot+HashSize] opened := sum(secret) copy(secret, opened[:]) at += 2 * HashSize } } return sum(buf) } // climb folds node, the leaf number index of a tree of the given height, with // the sibling hashes that follow the signature in proof, bottom first, and // returns the root they lead to. func climb(sum func([]byte) [HashSize]byte, node [HashSize]byte, index, height int, proof []byte) [HashSize]byte { var buf [1 + 2*HashSize]byte buf[0] = innerPrefix left, right := buf[1:1+HashSize], buf[1+HashSize:] at := SignatureSize for level := 0; level < height; level++ { sibling := proof[at : at+HashSize] if index&1 == 0 { copy(left, node[:]) copy(right, sibling) } else { copy(left, sibling) copy(right, node[:]) } node = sum(buf[:]) index >>= 1 at += HashSize } return node }