helpers_test.gno
3.47 Kb · 113 lines
1package qrc
2
3// The tests of this package sign with the throwaway signer below instead of
4// package qrcsigner. Its secrets come from a counter, which is as good as
5// anything to a verifier: no proof tells how its secrets were chosen. It also
6// builds whole trees, level by level, where Verify only walks one path. The
7// two meet at the root or the test fails.
8
9// testKey is one Lamport one-time key.
10type testKey struct {
11 h Hash
12 seed byte
13}
14
15func (k testKey) secret(bit, side int) [HashSize]byte {
16 return k.h.Sum([]byte{'k', k.seed, byte(bit >> 8), byte(bit), byte(side)})
17}
18
19// public returns the public key: the hash of secret 0 then of secret 1, for
20// every digest bit in turn.
21func (k testKey) public() []byte {
22 pk := make([]byte, 0, PublicKeySize)
23 for bit := 0; bit < DigestBits; bit++ {
24 for side := 0; side < 2; side++ {
25 secret := k.secret(bit, side)
26 hashed := k.h.Sum(secret[:])
27 pk = append(pk, hashed[:]...)
28 }
29 }
30 return pk
31}
32
33// sign returns the signature of msg. For every bit of its digest, most
34// significant bit of the first byte first, it holds two values: the secret
35// of the side the bit selects, and the hash of the secret of the other side,
36// side 0 before side 1.
37func (k testKey) sign(msg []byte) []byte {
38 digest := k.h.Sum(msg)
39 sig := make([]byte, 0, SignatureSize)
40 for bit := 0; bit < DigestBits; bit++ {
41 selected := int(digest[bit/8]>>(7-uint(bit%8))) & 1
42 for side := 0; side < 2; side++ {
43 value := k.secret(bit, side)
44 if side != selected {
45 value = k.h.Sum(value[:])
46 }
47 sig = append(sig, value[:]...)
48 }
49 }
50 return sig
51}
52
53// testTree is a complete Merkle tree. items holds what the leaves stand for,
54// levels[0] the leaves, and the last level the root alone.
55type testTree struct {
56 h Hash
57 items [][]byte
58 levels [][][HashSize]byte
59}
60
61// newTestTree builds the tree of the given height in which the leaves named
62// in keys stand for real one-time keys. Every other leaf stands for three
63// bytes of filler.
64func newTestTree(h Hash, height int, keys map[int]testKey) *testTree {
65 tree := &testTree{h: h, items: make([][]byte, Leaves(height))}
66 for i := range tree.items {
67 tree.items[i] = []byte{'f', byte(i >> 8), byte(i)}
68 }
69 for leaf, key := range keys {
70 tree.items[leaf] = key.public()
71 }
72
73 level := make([][HashSize]byte, len(tree.items))
74 for i, item := range tree.items {
75 level[i] = h.Sum(append([]byte{0x00}, item...))
76 }
77 tree.levels = [][][HashSize]byte{level}
78 for len(level) > 1 {
79 next := make([][HashSize]byte, len(level)/2)
80 for i := range next {
81 node := append([]byte{0x01}, level[2*i][:]...)
82 node = append(node, level[2*i+1][:]...)
83 next[i] = h.Sum(node)
84 }
85 tree.levels = append(tree.levels, next)
86 level = next
87 }
88 return tree
89}
90
91func (t *testTree) root() [HashSize]byte {
92 return t.levels[len(t.levels)-1][0]
93}
94
95// path returns the siblings met on the way from leaf to the root.
96func (t *testTree) path(leaf int) []byte {
97 var path []byte
98 for level := 0; level < len(t.levels)-1; level++ {
99 sibling := t.levels[level][(leaf>>uint(level))^1]
100 path = append(path, sibling[:]...)
101 }
102 return path
103}
104
105// testProof signs msg with a fresh key placed at leaf of a tree of the given
106// height, and returns the root of that tree with the proof.
107func testProof(h Hash, height, leaf int, msg []byte) (root [HashSize]byte, proof []byte) {
108 key := testKey{h, byte(leaf)}
109 tree := newTestTree(h, height, map[int]testKey{leaf: key})
110 return tree.root(), append(key.sign(msg), tree.path(leaf)...)
111}
112
113var bothHashes = []Hash{SHA256, Keccak256}