Files
zeekay ac01b821e8 pq: per-primitive flags, unified Op enum, package-level Active
Aligns the standalone luxfi/pq library to the same per-primitive model
the geth core/vm gate now uses. One way only: the same eight Forbid*
flags, the same sixteen Op constants, the same family errors.

Profile now has 8 fields (one per cryptographic primitive family)
instead of the prior 7 categorical flags. Op renamed from Op uint16
with mixed-case names to Op uint8 with namespace-qualified names
(OpEcrecover, OpBn256Pairing, etc.). Canonical encoding bumped to
version 2; new pinned hashes:

  strict:     9efdbf424085b0557866c22b0a4e0c48e2ed90c8c9e3f699d17a3e0783cb2128
  permissive: 256f4daa37543549e06bd101aab7f21a9b9dff46d322a9392e6b035cf4f0f638

API surface (the smallest possible):

  type Profile struct { 8 Forbid* fields }
  func Strict() / Permissive() *Profile
  func (p *Profile) Forbids(op Op) bool
  func (p *Profile) Encode() [16]byte
  func (p *Profile) Hash() [32]byte
  func DecodeProfile([16]byte) (*Profile, bool)

  type Op uint8 — 16 stable identifiers
  var Err*Forbidden — 8 family errors

  func SetActive(*Profile)        — package-level state, atomic
  func Active() *Profile
  func Refuse(Op) error           — reads Active
  func RefuseUnder(*Profile, Op)  — context-free

  const EnvVar = "QUASAR_STRICT_PQ"
  func FromEnv() *Profile

Dropped concepts: context binding (WithProfile/FromContext), MustRefuse,
Violation type, categorical Forbid* flags, Op uint16 with mixed cases.

20 unit tests; vet clean.
2026-05-11 16:29:11 -07:00

40 lines
1.7 KiB
Go

// Package pq defines the strict post-quantum profile that constrains
// which cryptographic operations a Quasar node accepts.
//
// The package contains no cryptographic primitives. It expresses a
// policy:
//
// - [Profile] is the value. Eight Forbid* flags, one per cryptographic
// primitive family. nil and the zero value both mean classical
// semantics; [Strict] sets every flag true.
//
// - [Op] is the operation a precompile reports. Sixteen stable
// identifiers cover the EVM precompile families plus polynomial
// commitments.
//
// - [Refuse] is the gate. Every constrained operation calls it once
// at the top of its entry point and otherwise proceeds in its own
// lane. The profile does not know how the operations compute; the
// operations do not know how the profile is selected.
//
// One way only: one variable holds the active profile, one function
// installs it, one function reads it, one function gates each call.
//
// pq.SetActive(pq.Strict()) // chain bootstrap
// if err := pq.Refuse(pq.OpEcrecover); err != nil // every precompile
//
// Profile values are immutable. Two helpers construct the canonical
// presets:
//
// - [Strict] forbids every classical primitive family
// - [Permissive] admits every operation
//
// [Profile.Hash] is the SHA3-256 of the canonical encoding. Consensus
// emits this hash into block headers so any observer can verify which
// profile a block was produced under.
//
// Environment override: when QUASAR_STRICT_PQ is set to a truthy
// value, [FromEnv] returns [Strict]; otherwise it returns [Permissive].
// Mainnet binaries set this in their entrypoint.
package pq