package qrc // Domain opens every message signed with a commit key, so that such a // signature can never be mistaken for anything else. const Domain = "QuantumRecoveryCommit/v1" // Action tells what a signed message asks for. type Action uint8 const ( // ActionBind registers new public key bytes for the account. ActionBind Action = 1 // ActionRotate replaces the account's commit by a new one. ActionRotate Action = 2 // ActionAuthorize approves a payload defined by another realm. ActionAuthorize Action = 3 ) // ID identifies one commit: the n-th commit of an account, in one registry, // on one chain. Messages carry it so that a signature made for one commit is // worthless for any other, and signers derive their keys from it so that no // one-time key is ever shared between two commits. type ID struct { ChainID string // chain the registry runs on Registry string // package path of the realm that stores the commit Account string // address of the account, as the chain writes it (lower-case bech32) Generation uint32 // number of times the account replaced its commit } // Bytes returns the canonical encoding of id: each string preceded by its // length as a big-endian uint16, then the generation as a big-endian uint32. func (id ID) Bytes() []byte { b := make([]byte, 0, 10+len(id.ChainID)+len(id.Registry)+len(id.Account)) b = appendString(b, id.ChainID) b = appendString(b, id.Registry) b = appendString(b, id.Account) return appendUint32(b, id.Generation) } // BindMessage returns the bytes to sign, with one-time key number leaf, in // order to register key as the new public key of the account. keyType is a // free label naming the scheme key belongs to. // // header || uint16 len(keyType) || keyType || uint32 len(key) || key // // The header common to all messages is // // Domain || 0x00 || action || id.Bytes() || uint32 leaf // // All integers are big-endian. Every variable-length field carries its // length, so two different requests never produce the same bytes. func BindMessage(id ID, leaf uint32, keyType string, key []byte) []byte { b := header(id, leaf, ActionBind, 6+len(keyType)+len(key)) b = appendString(b, keyType) return appendBytes(b, key) } // RotateMessage returns the bytes to sign, with one-time key number leaf, in // order to replace the commit id by a new one made of the two given roots // and tree height. // // header || rootSHA256 || rootKeccak256 || uint8 height func RotateMessage(id ID, leaf uint32, rootSHA256, rootKeccak256 [HashSize]byte, height int) []byte { if height < MinHeight || height > MaxHeight { panic(ErrHeight.Error()) } b := header(id, leaf, ActionRotate, 2*HashSize+1) b = append(b, rootSHA256[:]...) b = append(b, rootKeccak256[:]...) return append(b, byte(height)) } // AuthorizeMessage returns the bytes to sign, with one-time key number leaf, // in order to approve payload on behalf of the account. app is the package // path of the realm the approval is meant for; no other realm can use it. // // header || uint16 len(app) || app || uint32 len(payload) || payload func AuthorizeMessage(id ID, leaf uint32, app string, payload []byte) []byte { b := header(id, leaf, ActionAuthorize, 6+len(app)+len(payload)) b = appendString(b, app) return appendBytes(b, payload) } func header(id ID, leaf uint32, action Action, bodySize int) []byte { idBytes := id.Bytes() b := make([]byte, 0, len(Domain)+2+len(idBytes)+4+bodySize) b = append(b, []byte(Domain)...) b = append(b, 0x00, byte(action)) b = append(b, idBytes...) return appendUint32(b, leaf) } func appendString(b []byte, s string) []byte { if len(s) > 0xffff { panic("qrc: string too long") } b = append(b, byte(len(s)>>8), byte(len(s))) return append(b, []byte(s)...) } func appendBytes(b []byte, p []byte) []byte { if uint64(len(p)) > 0xffffffff { panic("qrc: payload too long") } b = appendUint32(b, uint32(len(p))) return append(b, p...) } func appendUint32(b []byte, v uint32) []byte { return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) }