Files
annulus/refuse_test.go
zeekay d2b5812200 annulus: strict post-quantum profile
Defines the policy that constrains which cryptographic operations a
Quasar node accepts. The package contains no cryptographic primitives.

Two values, one gate:

  Profile             immutable 8-byte canonical encoding; SHA3-256
                      hash is published in block headers.
  Refuse(ctx, Op)     reports whether op is admissible under the
                      profile bound to ctx.

Policy and enforcement are decomplected. Each constrained operation
(ECDSA precompile, pairing precompile, classical SNARK verifier)
calls Refuse once at 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.

Presets:

  Strict       forbids every classical category — mainnet.
  Permissive   admits every operation — testnet, default for
               unbound contexts.

Categories (Forbid* flags map 1:1):

  CatECDSAContractAuth   ECDSARecover, ECDSAVerify, Schnorr
  CatClassicalSig        Ed25519, X25519
  CatPairingPrecompile   BN254Pairing, BLS12381Pair, BLSSignature
  CatKZGPrecompile       KZGEval
  CatClassicalSNARK      Groth16, Plonk, Marlin
  CatNonSHA3Hash         SHA256, RIPEMD160, Blake2b, Blake2s, Keccak256

Strict profile hash (pinned in profile_test.go):
  9d70a692103d09414ffb2577ca53852c99b2d13b987c49f20852899f9a0be348

Permissive profile hash:
  cf7b7f49edcad4722838277dd310dcf6a61d0552ce0f2116ad39c3dcd23bd916

A change to either hash is a consensus-breaking change and requires a
canonical-encoding version bump.

Environment override: QUASAR_STRICT_PQ=1 selects Strict via
ProfileFromEnv. Mainnet binaries set it in their entrypoint.

31 unit tests; vet-clean.
2026-05-11 15:42:36 -07:00

104 lines
2.4 KiB
Go

package annulus
import (
"context"
"errors"
"testing"
)
func TestRefuseUnderStrict(t *testing.T) {
p := Strict()
forbidden := []Op{
ECDSARecover, ECDSAVerify, Ed25519, X25519, Schnorr,
BN254Pairing, BLS12381Pair, BLSSignature,
KZGEval, Groth16, Plonk, Marlin,
SHA256, RIPEMD160, Blake2b, Blake2s, Keccak256,
}
for _, op := range forbidden {
err := RefuseUnder(p, op)
if err == nil {
t.Errorf("strict profile must refuse %s", op)
continue
}
var v *Violation
if !errors.As(err, &v) {
t.Errorf("refuse of %s did not wrap *Violation: %v", op, err)
}
}
}
func TestRefuseUnderPermissive(t *testing.T) {
p := Permissive()
for op := range opName {
if op == OpUnknown {
continue
}
if err := RefuseUnder(p, op); err != nil {
t.Errorf("permissive profile must allow %s; got %v", op, err)
}
}
}
func TestRefuseUnknownOpFails(t *testing.T) {
err := RefuseUnder(Permissive(), Op(0xffff))
if !errors.Is(err, ErrUnknownOp) {
t.Errorf("expected ErrUnknownOp, got %v", err)
}
}
func TestRefuseUsesContextProfile(t *testing.T) {
ctx := WithProfile(context.Background(), Strict())
if err := Refuse(ctx, KZGEval); err == nil {
t.Error("strict context should refuse KZGEval")
}
}
func TestRefuseDefaultsToPermissive(t *testing.T) {
if err := Refuse(context.Background(), KZGEval); err != nil {
t.Errorf("bare context defaults to permissive; got %v", err)
}
}
func TestRefuseNilContextPanics(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("nil context should panic")
}
}()
_ = Refuse(nil, KZGEval) //nolint:staticcheck // intentional
}
func TestViolationCarriesProfileHash(t *testing.T) {
p := Strict()
err := RefuseUnder(p, KZGEval)
var v *Violation
if !errors.As(err, &v) {
t.Fatalf("expected *Violation, got %v", err)
}
if v.Op != KZGEval {
t.Errorf("violation op got %s want KZGEval", v.Op)
}
if v.Profile != p.Hash() {
t.Errorf("violation profile hash mismatch")
}
}
func TestMustRefusePanicsOnStrict(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("MustRefuse should panic on violation")
}
}()
ctx := WithProfile(context.Background(), Strict())
MustRefuse(ctx, KZGEval)
}
func TestMustRefuseSilentOnPermissive(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("MustRefuse should not panic under permissive; recovered %v", r)
}
}()
MustRefuse(context.Background(), KZGEval)
}