pow.Mine finds the smallest nonce, exactly what trying 0, 1, 2, ... finds, so every signed record and golden fixture stays the same. It saves the SHA-256 state after the whole blocks before the nonce, hashes only the number and what follows per attempt, and uses every core but one from difficulty 14 up. Listing, report, revocation and message mining use it; each re-checks the result before signing. One listing at difficulty 16 takes about 19 ms instead of about 2 s. docs/PROTOCOL.md says what proof-of-work can and cannot do against fast hardware. allmende-load fills a data directory with signed invented listings for capacity tests and measures mining time (-mine-only). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
134 lines
4.1 KiB
Go
134 lines
4.1 KiB
Go
package identity
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"time"
|
|
|
|
"git.stinnesbeck.com/nils/allmende/internal/jcs"
|
|
"git.stinnesbeck.com/nils/allmende/internal/pow"
|
|
)
|
|
|
|
const (
|
|
RevocationVersion = 1
|
|
revocationContext = "allmende-revocation-v1\n"
|
|
)
|
|
|
|
// Revocation voids a device: every signature made by Device is rejected from
|
|
// now on. With Device empty it voids every device of Author whose certificate
|
|
// was issued before RevokedAt, which is what recovery on a new machine needs:
|
|
// the owner only has the phrase, not the old device's key. Nodes keep it
|
|
// until the certificates it covers would have expired anyway
|
|
// (CertExpiresAt). It carries proof-of-work so nobody can flood the network
|
|
// with revocations for throwaway identities.
|
|
type Revocation struct {
|
|
Version int `json:"v"`
|
|
Author string `json:"author"`
|
|
Device string `json:"device"`
|
|
RevokedAt int64 `json:"revoked_at"`
|
|
CertExpiresAt int64 `json:"cert_expires_at"`
|
|
PoW uint64 `json:"pow"`
|
|
Sig string `json:"sig,omitempty"`
|
|
}
|
|
|
|
func (r *Revocation) signingBytes() ([]byte, error) {
|
|
cp := *r
|
|
cp.Sig = ""
|
|
b, err := jcs.Marshal(&cp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append([]byte(revocationContext), b...), nil
|
|
}
|
|
|
|
// Revoke signs a revocation of the device named in cert, mining the
|
|
// proof-of-work to the given difficulty.
|
|
func Revoke(root *Identity, cert *Cert, at time.Time, difficulty int) (*Revocation, error) {
|
|
if root.PublicKeyHex() != cert.Author {
|
|
return nil, errors.New("identity: cert does not belong to this identity")
|
|
}
|
|
return mineRevocation(root, &Revocation{
|
|
Version: RevocationVersion,
|
|
Author: cert.Author,
|
|
Device: cert.Device,
|
|
RevokedAt: at.Unix(),
|
|
CertExpiresAt: cert.ExpiresAt,
|
|
}, difficulty)
|
|
}
|
|
|
|
// RevokeAll voids every device certified before at. Enroll the replacement
|
|
// device with the same timestamp: certificates issued at or after at stay valid.
|
|
func RevokeAll(root *Identity, at time.Time, difficulty int) (*Revocation, error) {
|
|
return mineRevocation(root, &Revocation{
|
|
Version: RevocationVersion,
|
|
Author: root.PublicKeyHex(),
|
|
RevokedAt: at.Unix(),
|
|
CertExpiresAt: at.Add(MaxCertLifetime).Unix(), // older certs all expire before this
|
|
}, difficulty)
|
|
}
|
|
|
|
func mineRevocation(root *Identity, r *Revocation, difficulty int) (*Revocation, error) {
|
|
r.Sig = ""
|
|
r.PoW = 0
|
|
b, err := r.signingBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if r.PoW, err = pow.Mine(b, difficulty, 1<<53-1); err != nil {
|
|
return nil, err
|
|
}
|
|
if b, err = r.signingBytes(); err != nil {
|
|
return nil, err
|
|
}
|
|
if pow.LeadingZeroBits(b) < difficulty { // cannot happen; a wrong miner must not sign
|
|
return nil, errors.New("identity: mined nonce does not meet the difficulty")
|
|
}
|
|
r.Sig = hex.EncodeToString(root.Sign(b))
|
|
return r, nil
|
|
}
|
|
|
|
// AllDevices reports whether r covers every earlier device of the author.
|
|
func (r *Revocation) AllDevices() bool { return r.Device == "" }
|
|
|
|
// Covers reports whether r voids the device certified by c.
|
|
func (r *Revocation) Covers(c *Cert) bool {
|
|
if r.Author != c.Author {
|
|
return false
|
|
}
|
|
if r.AllDevices() {
|
|
return c.IssuedAt < r.RevokedAt
|
|
}
|
|
return r.Device == c.Device
|
|
}
|
|
|
|
// Check verifies structure, signature and proof-of-work.
|
|
func (r *Revocation) Check(difficulty int) error {
|
|
if r.Version != RevocationVersion {
|
|
return errors.New("identity: unsupported revocation version")
|
|
}
|
|
if !isHex(r.Author, 64) || (r.Device != "" && !isHex(r.Device, 64)) {
|
|
return errors.New("identity: revocation keys must be 64 hex characters")
|
|
}
|
|
if r.PoW > 1<<53-1 {
|
|
return errors.New("identity: number out of range")
|
|
}
|
|
sig, err := hex.DecodeString(r.Sig)
|
|
if err != nil || len(sig) != 64 {
|
|
return errors.New("identity: malformed revocation signature")
|
|
}
|
|
b, err := r.signingBytes()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !Verify(r.Author, b, sig) {
|
|
return errors.New("identity: bad revocation signature")
|
|
}
|
|
if pow.LeadingZeroBits(b) < difficulty {
|
|
return errors.New("identity: insufficient proof-of-work")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Bytes returns the canonical encoding including the signature.
|
|
func (r *Revocation) Bytes() ([]byte, error) { return jcs.Marshal(r) }
|