Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

verify.gno

4.32 Kb · 136 lines
  1package qrc
  2
  3import "errors"
  4
  5const (
  6	// DigestBits is the number of digest bits a Lamport key signs, and so
  7	// the number of secret pairs in a key.
  8	DigestBits = HashSize * 8
  9
 10	// PublicKeySize is the size of a Lamport public key: two hashes per
 11	// digest bit.
 12	PublicKeySize = DigestBits * 2 * HashSize
 13
 14	// SignatureSize is the size of a Lamport signature: the public key with
 15	// one hash of each pair replaced by the secret it is the hash of.
 16	SignatureSize = PublicKeySize
 17
 18	// MinHeight is the smallest tree a commit may use. It holds 64 one-time
 19	// keys, the minimum the LAWS ask for.
 20	MinHeight = 6
 21
 22	// MaxHeight is the largest tree a commit may use, 65536 one-time keys.
 23	MaxHeight = 16
 24)
 25
 26// Domain separation of tree nodes, as in RFC 6962. A Lamport secret is hashed
 27// without prefix: it is exactly HashSize bytes long, a length neither a leaf
 28// nor an inner node input can have.
 29const (
 30	leafPrefix  = 0x00
 31	innerPrefix = 0x01
 32)
 33
 34var (
 35	ErrUnknownHash = errors.New("qrc: unknown hash function")
 36	ErrHeight      = errors.New("qrc: tree height out of range")
 37	ErrLeafIndex   = errors.New("qrc: leaf index out of range")
 38	ErrProofSize   = errors.New("qrc: proof has the wrong size")
 39	ErrMismatch    = errors.New("qrc: proof does not match the commit")
 40)
 41
 42// ProofSize returns the exact size, in bytes, of a proof for a tree of the
 43// given height: one signature and one sibling hash per tree level.
 44func ProofSize(height int) int {
 45	return SignatureSize + height*HashSize
 46}
 47
 48// Leaves returns the number of one-time keys in a tree of the given height.
 49func Leaves(height int) int {
 50	return 1 << uint(height)
 51}
 52
 53// Verify checks a proof against a commit.
 54//
 55// It returns nil when proof shows that msg was signed by the Lamport key
 56// stored at leaf of the Merkle tree of the given height whose root is root,
 57// everything hashed with h. The proof layout is described in the package
 58// documentation; its size must be exactly [ProofSize](height).
 59//
 60// Verify is a pure function of its arguments. It does not know whether the
 61// leaf has signed before: see the package documentation on one-time keys.
 62func Verify(h Hash, root [HashSize]byte, height int, leaf int, msg []byte, proof []byte) error {
 63	if !h.Valid() {
 64		return ErrUnknownHash
 65	}
 66	if height < MinHeight || height > MaxHeight {
 67		return ErrHeight
 68	}
 69	if leaf < 0 || leaf >= Leaves(height) {
 70		return ErrLeafIndex
 71	}
 72	if len(proof) != ProofSize(height) {
 73		return ErrProofSize
 74	}
 75	sum := h.function()
 76	node := recoverLeaf(sum, sum(msg), proof)
 77	if climb(sum, node, leaf, height, proof) != root {
 78		return ErrMismatch
 79	}
 80	return nil
 81}
 82
 83// recoverLeaf returns the leaf hash of the one public key under which the
 84// first SignatureSize bytes of proof are a Lamport signature of digest.
 85//
 86// A signature is the public key with, in every pair, the hash selected by the
 87// digest bit replaced by its secret. Hashing that secret in place therefore
 88// turns the signature back into the public key: one copy of the signature,
 89// then one hash and one 32-byte copy per digest bit. The loop is written for
 90// the gas meter, where every built-in call has a fixed price well above that
 91// of a SHA-256 of 32 bytes.
 92func recoverLeaf(sum func([]byte) [HashSize]byte, digest [HashSize]byte, proof []byte) [HashSize]byte {
 93	buf := make([]byte, 1+PublicKeySize)
 94	buf[0] = leafPrefix
 95	copy(buf[1:], proof[:SignatureSize])
 96
 97	at := 1 // start of the pair of the current digest bit
 98	for _, d := range digest {
 99		for mask := byte(0x80); mask != 0; mask >>= 1 {
100			slot := at
101			if d&mask != 0 {
102				slot += HashSize
103			}
104			secret := buf[slot : slot+HashSize]
105			opened := sum(secret)
106			copy(secret, opened[:])
107			at += 2 * HashSize
108		}
109	}
110	return sum(buf)
111}
112
113// climb folds node, the leaf number index of a tree of the given height, with
114// the sibling hashes that follow the signature in proof, bottom first, and
115// returns the root they lead to.
116func climb(sum func([]byte) [HashSize]byte, node [HashSize]byte, index, height int, proof []byte) [HashSize]byte {
117	var buf [1 + 2*HashSize]byte
118	buf[0] = innerPrefix
119	left, right := buf[1:1+HashSize], buf[1+HashSize:]
120
121	at := SignatureSize
122	for level := 0; level < height; level++ {
123		sibling := proof[at : at+HashSize]
124		if index&1 == 0 {
125			copy(left, node[:])
126			copy(right, sibling)
127		} else {
128			copy(left, sibling)
129			copy(right, node[:])
130		}
131		node = sum(buf[:])
132		index >>= 1
133		at += HashSize
134	}
135	return node
136}