// Package qrcsigner derives the one-time keys of a Quantum Recovery Commit // from 32 bytes of entropy and signs with them. // // It is the reference signer: the key derivation written as running code, // used by the tests of the registry, and by anyone who wants to check // another implementation against it. Run it off-chain, with // `gno test` or `gno run`. Never call it from a transaction: everything a // transaction computes is public, and so would be the entropy. // // Nothing on chain depends on how secrets are derived. A commit made by any // other signer verifies just the same, as long as its keys are Lamport keys // arranged as package qrc describes. // // # Key derivation // // Each of the two trees first gets a seed of its own, computed with the hash // function of the other tree: // // seed(SHA256) = Keccak256("QuantumRecoveryCommit/v1/seed/sha256" || 0x00 || entropy) // seed(Keccak256) = SHA256( "QuantumRecoveryCommit/v1/seed/keccak256" || 0x00 || entropy) // // Each seed thus rests on the function of the other tree. Should one // function ever be broken badly enough to walk back from the published // hashes of its tree to that tree's seed, the entropy still sits behind the // other function, and with it the seed of the other tree. // // From there on a tree only uses its own function. The secret number side // (0 or 1) of digest bit number bit, in the one-time key number leaf, is // // info = "QuantumRecoveryCommit/v1/secret" || 0x00 || id.Bytes() // || uint32 leaf || uint16 bit || uint8 side // secret(SHA256) = HMAC-SHA256(key = seed, message = info) // secret(Keccak256) = Keccak256(seed || info) // // with big-endian integers. Because info holds the commit ID, two commits // never share a key, whatever the chain, registry, account or generation. // The height is not part of info: a commit ID goes with a single height, // since a kit derived for the same ID at another height shares its keys. // A generation's keys are derived with the chain ID in force when the // generation began; its messages carry the chain ID in force when they are // sent. package qrcsigner import ( "crypto/keccak256" "crypto/sha256" "gno.land/p/nym-rootlatch001/qrc/v1" ) // EntropySize is the size, in bytes, of the entropy a kit is derived from. const EntropySize = 32 const ( seedLabel = qrc.Domain + "/seed/" secretLabel = qrc.Domain + "/secret" hmacBlockSize = 64 // block size of SHA-256 tailSize = 7 // uint32 leaf || uint16 bit || uint8 side ) // Tree holds the one-time keys of one of the two commits of an account. type Tree struct { h qrc.Hash height int // Input buffers of the secret derivation. The last tailSize bytes of // inner change with every secret, everything before them is fixed. // SHA256: inner = (seed ^ ipad) || info, outer = (seed ^ opad) || inner digest // Keccak256: inner = seed || info inner []byte outer []byte // levels[0] holds the leaf hashes side by side, levels[height] the root. levels [][]byte } // NewTree derives the tree of commit id built on hash function h. It computes // every public key of the tree, 512 hashes per leaf. func NewTree(h qrc.Hash, entropy []byte, id qrc.ID, height int) *Tree { if !h.Valid() { panic(qrc.ErrUnknownHash.Error()) } if len(entropy) != EntropySize { panic("qrcsigner: entropy must be 32 bytes long") } if height < qrc.MinHeight || height > qrc.MaxHeight { panic(qrc.ErrHeight.Error()) } seed := treeSeed(h, entropy) info := append([]byte(secretLabel), 0x00) info = append(info, id.Bytes()...) t := &Tree{h: h, height: height} if h == qrc.SHA256 { t.inner = make([]byte, hmacBlockSize, hmacBlockSize+len(info)+tailSize) t.outer = make([]byte, hmacBlockSize+qrc.HashSize) for i := 0; i < hmacBlockSize; i++ { var k byte if i < len(seed) { k = seed[i] } t.inner[i] = k ^ 0x36 t.outer[i] = k ^ 0x5c } } else { t.inner = append(make([]byte, 0, len(seed)+len(info)+tailSize), seed[:]...) } t.inner = append(t.inner, info...) t.inner = append(t.inner, make([]byte, tailSize)...) t.build() return t } // Hash returns the hash function the tree is built on. func (t *Tree) Hash() qrc.Hash { return t.h } // Height returns the height of the tree. func (t *Tree) Height() int { return t.height } // Root returns the commit: the root of the tree. func (t *Tree) Root() [qrc.HashSize]byte { var root [qrc.HashSize]byte copy(root[:], t.levels[t.height]) return root } // Sign signs msg with one-time key number leaf and returns the proof // qrc.Verify expects. // // A one-time key must sign a single message. Signing two different messages // with the same leaf weakens the key, and a few more signatures give it away. func (t *Tree) Sign(leaf int, msg []byte) []byte { if leaf < 0 || leaf >= qrc.Leaves(t.height) { panic(qrc.ErrLeafIndex.Error()) } digest := t.h.Sum(msg) proof := make([]byte, 0, qrc.ProofSize(t.height)) for i := 0; i < qrc.DigestBits; i++ { bit := int(digest[i>>3]>>(7-uint(i&7))) & 1 for side := 0; side < 2; side++ { // The secret on the side the digest bit selects, the hash of // the secret on the other side. value := t.secret(leaf, i, side) if side != bit { value = t.h.Sum(value[:]) } proof = append(proof, value[:]...) } } index := leaf for level := 0; level < t.height; level++ { sibling := index ^ 1 proof = append(proof, t.levels[level][sibling*qrc.HashSize:(sibling+1)*qrc.HashSize]...) index >>= 1 } return proof } // build computes every node of the tree. func (t *Tree) build() { leaves := qrc.Leaves(t.height) level := make([]byte, leaves*qrc.HashSize) pk := make([]byte, 1+qrc.PublicKeySize) pk[0] = 0x00 // leaf prefix for leaf := 0; leaf < leaves; leaf++ { for i := 0; i < qrc.DigestBits; i++ { for side := 0; side < 2; side++ { secret := t.secret(leaf, i, side) hashed := t.h.Sum(secret[:]) at := 1 + (2*i+side)*qrc.HashSize copy(pk[at:at+qrc.HashSize], hashed[:]) } } leafHash := t.h.Sum(pk) copy(level[leaf*qrc.HashSize:(leaf+1)*qrc.HashSize], leafHash[:]) } t.levels = [][]byte{level} var node [1 + 2*qrc.HashSize]byte node[0] = 0x01 // inner node prefix for len(level) > qrc.HashSize { next := make([]byte, len(level)/2) for at := 0; at < len(next); at += qrc.HashSize { copy(node[1:], level[2*at:2*at+2*qrc.HashSize]) parent := t.h.Sum(node[:]) copy(next[at:at+qrc.HashSize], parent[:]) } t.levels = append(t.levels, next) level = next } } // secret derives one secret of one one-time key. func (t *Tree) secret(leaf, bit, side int) [qrc.HashSize]byte { tail := t.inner[len(t.inner)-tailSize:] tail[0] = byte(leaf >> 24) tail[1] = byte(leaf >> 16) tail[2] = byte(leaf >> 8) tail[3] = byte(leaf) tail[4] = byte(bit >> 8) tail[5] = byte(bit) tail[6] = byte(side) if t.h == qrc.SHA256 { digest := sha256.Sum256(t.inner) copy(t.outer[hmacBlockSize:], digest[:]) return sha256.Sum256(t.outer) } return keccak256.Sum256(t.inner) } // treeSeed derives the seed of the tree built on h from the kit entropy, // using the hash function of the other tree. func treeSeed(h qrc.Hash, entropy []byte) [qrc.HashSize]byte { in := append([]byte(seedLabel+h.String()), 0x00) in = append(in, entropy...) if h == qrc.SHA256 { return keccak256.Sum256(in) } return sha256.Sum256(in) } // Kit holds both trees of a Quantum Recovery Commit. type Kit struct { sha256 *Tree keccak256 *Tree } // New derives both trees of commit id from the kit entropy. func New(entropy []byte, id qrc.ID, height int) *Kit { return &Kit{ sha256: NewTree(qrc.SHA256, entropy, id, height), keccak256: NewTree(qrc.Keccak256, entropy, id, height), } } // Roots returns the two commits to register. func (k *Kit) Roots() (rootSHA256, rootKeccak256 [qrc.HashSize]byte) { return k.sha256.Root(), k.keccak256.Root() } // Sign signs msg with one-time key number leaf of both trees and returns the // two proofs. See [Tree.Sign] about never signing twice with one leaf. func (k *Kit) Sign(leaf int, msg []byte) (proofSHA256, proofKeccak256 []byte) { return k.sha256.Sign(leaf, msg), k.keccak256.Sign(leaf, msg) }