signer.gno
8.01 Kb · 248 lines
1// Package qrcsigner derives the one-time keys of a Quantum Recovery Commit
2// from 32 bytes of entropy and signs with them.
3//
4// It is the reference signer: the key derivation written as running code,
5// used by the tests of the registry, and by anyone who wants to check
6// another implementation against it. Run it off-chain, with
7// `gno test` or `gno run`. Never call it from a transaction: everything a
8// transaction computes is public, and so would be the entropy.
9//
10// Nothing on chain depends on how secrets are derived. A commit made by any
11// other signer verifies just the same, as long as its keys are Lamport keys
12// arranged as package qrc describes.
13//
14// # Key derivation
15//
16// Each of the two trees first gets a seed of its own, computed with the hash
17// function of the other tree:
18//
19// seed(SHA256) = Keccak256("QuantumRecoveryCommit/v1/seed/sha256" || 0x00 || entropy)
20// seed(Keccak256) = SHA256( "QuantumRecoveryCommit/v1/seed/keccak256" || 0x00 || entropy)
21//
22// Each seed thus rests on the function of the other tree. Should one
23// function ever be broken badly enough to walk back from the published
24// hashes of its tree to that tree's seed, the entropy still sits behind the
25// other function, and with it the seed of the other tree.
26//
27// From there on a tree only uses its own function. The secret number side
28// (0 or 1) of digest bit number bit, in the one-time key number leaf, is
29//
30// info = "QuantumRecoveryCommit/v1/secret" || 0x00 || id.Bytes()
31// || uint32 leaf || uint16 bit || uint8 side
32// secret(SHA256) = HMAC-SHA256(key = seed, message = info)
33// secret(Keccak256) = Keccak256(seed || info)
34//
35// with big-endian integers. Because info holds the commit ID, two commits
36// never share a key, whatever the chain, registry, account or generation.
37// The height is not part of info: a commit ID goes with a single height,
38// since a kit derived for the same ID at another height shares its keys.
39// A generation's keys are derived with the chain ID in force when the
40// generation began; its messages carry the chain ID in force when they are
41// sent.
42package qrcsigner
43
44import (
45 "crypto/keccak256"
46 "crypto/sha256"
47
48 "gno.land/p/nym-rootlatch001/qrc/v1"
49)
50
51// EntropySize is the size, in bytes, of the entropy a kit is derived from.
52const EntropySize = 32
53
54const (
55 seedLabel = qrc.Domain + "/seed/"
56 secretLabel = qrc.Domain + "/secret"
57
58 hmacBlockSize = 64 // block size of SHA-256
59 tailSize = 7 // uint32 leaf || uint16 bit || uint8 side
60)
61
62// Tree holds the one-time keys of one of the two commits of an account.
63type Tree struct {
64 h qrc.Hash
65 height int
66
67 // Input buffers of the secret derivation. The last tailSize bytes of
68 // inner change with every secret, everything before them is fixed.
69 // SHA256: inner = (seed ^ ipad) || info, outer = (seed ^ opad) || inner digest
70 // Keccak256: inner = seed || info
71 inner []byte
72 outer []byte
73
74 // levels[0] holds the leaf hashes side by side, levels[height] the root.
75 levels [][]byte
76}
77
78// NewTree derives the tree of commit id built on hash function h. It computes
79// every public key of the tree, 512 hashes per leaf.
80func NewTree(h qrc.Hash, entropy []byte, id qrc.ID, height int) *Tree {
81 if !h.Valid() {
82 panic(qrc.ErrUnknownHash.Error())
83 }
84 if len(entropy) != EntropySize {
85 panic("qrcsigner: entropy must be 32 bytes long")
86 }
87 if height < qrc.MinHeight || height > qrc.MaxHeight {
88 panic(qrc.ErrHeight.Error())
89 }
90
91 seed := treeSeed(h, entropy)
92 info := append([]byte(secretLabel), 0x00)
93 info = append(info, id.Bytes()...)
94
95 t := &Tree{h: h, height: height}
96 if h == qrc.SHA256 {
97 t.inner = make([]byte, hmacBlockSize, hmacBlockSize+len(info)+tailSize)
98 t.outer = make([]byte, hmacBlockSize+qrc.HashSize)
99 for i := 0; i < hmacBlockSize; i++ {
100 var k byte
101 if i < len(seed) {
102 k = seed[i]
103 }
104 t.inner[i] = k ^ 0x36
105 t.outer[i] = k ^ 0x5c
106 }
107 } else {
108 t.inner = append(make([]byte, 0, len(seed)+len(info)+tailSize), seed[:]...)
109 }
110 t.inner = append(t.inner, info...)
111 t.inner = append(t.inner, make([]byte, tailSize)...)
112
113 t.build()
114 return t
115}
116
117// Hash returns the hash function the tree is built on.
118func (t *Tree) Hash() qrc.Hash { return t.h }
119
120// Height returns the height of the tree.
121func (t *Tree) Height() int { return t.height }
122
123// Root returns the commit: the root of the tree.
124func (t *Tree) Root() [qrc.HashSize]byte {
125 var root [qrc.HashSize]byte
126 copy(root[:], t.levels[t.height])
127 return root
128}
129
130// Sign signs msg with one-time key number leaf and returns the proof
131// qrc.Verify expects.
132//
133// A one-time key must sign a single message. Signing two different messages
134// with the same leaf weakens the key, and a few more signatures give it away.
135func (t *Tree) Sign(leaf int, msg []byte) []byte {
136 if leaf < 0 || leaf >= qrc.Leaves(t.height) {
137 panic(qrc.ErrLeafIndex.Error())
138 }
139 digest := t.h.Sum(msg)
140 proof := make([]byte, 0, qrc.ProofSize(t.height))
141 for i := 0; i < qrc.DigestBits; i++ {
142 bit := int(digest[i>>3]>>(7-uint(i&7))) & 1
143 for side := 0; side < 2; side++ {
144 // The secret on the side the digest bit selects, the hash of
145 // the secret on the other side.
146 value := t.secret(leaf, i, side)
147 if side != bit {
148 value = t.h.Sum(value[:])
149 }
150 proof = append(proof, value[:]...)
151 }
152 }
153 index := leaf
154 for level := 0; level < t.height; level++ {
155 sibling := index ^ 1
156 proof = append(proof, t.levels[level][sibling*qrc.HashSize:(sibling+1)*qrc.HashSize]...)
157 index >>= 1
158 }
159 return proof
160}
161
162// build computes every node of the tree.
163func (t *Tree) build() {
164 leaves := qrc.Leaves(t.height)
165 level := make([]byte, leaves*qrc.HashSize)
166 pk := make([]byte, 1+qrc.PublicKeySize)
167 pk[0] = 0x00 // leaf prefix
168 for leaf := 0; leaf < leaves; leaf++ {
169 for i := 0; i < qrc.DigestBits; i++ {
170 for side := 0; side < 2; side++ {
171 secret := t.secret(leaf, i, side)
172 hashed := t.h.Sum(secret[:])
173 at := 1 + (2*i+side)*qrc.HashSize
174 copy(pk[at:at+qrc.HashSize], hashed[:])
175 }
176 }
177 leafHash := t.h.Sum(pk)
178 copy(level[leaf*qrc.HashSize:(leaf+1)*qrc.HashSize], leafHash[:])
179 }
180 t.levels = [][]byte{level}
181
182 var node [1 + 2*qrc.HashSize]byte
183 node[0] = 0x01 // inner node prefix
184 for len(level) > qrc.HashSize {
185 next := make([]byte, len(level)/2)
186 for at := 0; at < len(next); at += qrc.HashSize {
187 copy(node[1:], level[2*at:2*at+2*qrc.HashSize])
188 parent := t.h.Sum(node[:])
189 copy(next[at:at+qrc.HashSize], parent[:])
190 }
191 t.levels = append(t.levels, next)
192 level = next
193 }
194}
195
196// secret derives one secret of one one-time key.
197func (t *Tree) secret(leaf, bit, side int) [qrc.HashSize]byte {
198 tail := t.inner[len(t.inner)-tailSize:]
199 tail[0] = byte(leaf >> 24)
200 tail[1] = byte(leaf >> 16)
201 tail[2] = byte(leaf >> 8)
202 tail[3] = byte(leaf)
203 tail[4] = byte(bit >> 8)
204 tail[5] = byte(bit)
205 tail[6] = byte(side)
206 if t.h == qrc.SHA256 {
207 digest := sha256.Sum256(t.inner)
208 copy(t.outer[hmacBlockSize:], digest[:])
209 return sha256.Sum256(t.outer)
210 }
211 return keccak256.Sum256(t.inner)
212}
213
214// treeSeed derives the seed of the tree built on h from the kit entropy,
215// using the hash function of the other tree.
216func treeSeed(h qrc.Hash, entropy []byte) [qrc.HashSize]byte {
217 in := append([]byte(seedLabel+h.String()), 0x00)
218 in = append(in, entropy...)
219 if h == qrc.SHA256 {
220 return keccak256.Sum256(in)
221 }
222 return sha256.Sum256(in)
223}
224
225// Kit holds both trees of a Quantum Recovery Commit.
226type Kit struct {
227 sha256 *Tree
228 keccak256 *Tree
229}
230
231// New derives both trees of commit id from the kit entropy.
232func New(entropy []byte, id qrc.ID, height int) *Kit {
233 return &Kit{
234 sha256: NewTree(qrc.SHA256, entropy, id, height),
235 keccak256: NewTree(qrc.Keccak256, entropy, id, height),
236 }
237}
238
239// Roots returns the two commits to register.
240func (k *Kit) Roots() (rootSHA256, rootKeccak256 [qrc.HashSize]byte) {
241 return k.sha256.Root(), k.keccak256.Root()
242}
243
244// Sign signs msg with one-time key number leaf of both trees and returns the
245// two proofs. See [Tree.Sign] about never signing twice with one leaf.
246func (k *Kit) Sign(leaf int, msg []byte) (proofSHA256, proofKeccak256 []byte) {
247 return k.sha256.Sign(leaf, msg), k.keccak256.Sign(leaf, msg)
248}