AgentSkillsCN

crypto-core

密码学工程基础——代数思维、安全不变量与代码审查。

SKILL.md
--- frontmatter
name: crypto-core
description: Cryptographic engineering foundations — algebraic thinking, security invariants, and code review

What I do

  • Provide algebraic thinking foundations for cryptographic engineering
  • Document finite field and elliptic curve properties used in modern protocols
  • Define security invariants, threat models, and constant-time programming discipline
  • Supply a cryptographic code review checklist for auditing implementations

When to use me

Use this skill as the entry point for any cryptographic engineering task. Pair with a domain-specific skill (crypto-zkp, crypto-garbled-circuits, crypto-bitvm) for protocol-specific guidance.

Core Mental Models

Mental ModelWhat It MeansWhen To Apply
Everything is a polynomialComputations become polynomial evaluations; proofs become commitment checksCircuit design, proof system selection
Constraint != computationA circuit constrains relationships between values; it does not compute themCircuit writing, debugging
The simulator argumentIf a simulator produces indistinguishable output without the witness, the protocol is zero-knowledgeSecurity analysis
Soundness by reductionSecurity reduces to a hard mathematical problem (DLP, hash collision)Threat modeling
Communication is the bottleneckIn MPC, minimizing rounds and bandwidth dominates local computation costGarbled circuit and MPC optimization
Prover-verifier asymmetryProvers do heavy work so verifiers stay cheap -- this is a featureSystem design
Composability is fragileSecure protocols composed naively can become insecureProtocol design, integration

Core Principles

  1. Never roll your own crypto primitives -- Use audited libraries for field arithmetic, curve operations, and hash functions. Your job is to compose them correctly.
  2. State your threat model explicitly -- Every design decision depends on who the adversary is and what they can do. Document it.
  3. Constraint count is your performance metric -- In ZK, fewer constraints means faster proofs and lower costs. Optimize ruthlessly.
  4. Verify your verifier -- A bug in the prover wastes time; a bug in the verifier breaks security. The verifier is the critical path.
  5. Test with adversarial witnesses -- Do not just test the happy path. Generate invalid witnesses and confirm they are rejected.
  6. Fiat-Shamir requires domain separation -- When making interactive protocols non-interactive, include all public parameters in the transcript.
  7. Constant-time is non-negotiable -- Any branch or memory access that depends on secret data is a side channel.

Finite Fields & Elliptic Curves

Curve/FieldField SizePairingPost-QuantumUsed By
BN254254-bitYesNoGroth16 (Ethereum), circom
BLS12-381381-bitYesNoEthereum 2.0, Zcash Sapling
Pasta (Pallas/Vesta)255-bitNoNoHalo2, Mina
Goldilocks64-bitNoNoPlonky2, STARKs
BabyBear31-bitNoNoPlonky3, RISC Zero, SP1
Mersenne3131-bitNoNoCircle STARKs, Stwo

Small fields (Goldilocks, BabyBear) enable faster native arithmetic. Pairing-friendly curves (BN254, BLS12-381) are required for Groth16 and KZG commitments.

Security Invariants

Properties to verify in any cryptographic protocol:

  • Completeness -- an honest prover can always convince an honest verifier
  • Soundness -- a cheating prover cannot convince a verifier (except with negligible probability)
  • Zero-knowledge -- the verifier learns nothing beyond the statement's truth
  • Witness indistinguishability -- the verifier cannot tell which witness was used

Attack Catalog

AttackTargetMitigation
GrindingPoW-based commitmentsSufficient security parameter (>= 128 bits)
Proof malleabilityVerification contextsBind proofs to session and context identifiers
Trusted setup compromiseSNARKs with ceremonyUse universal setup (PLONK) or STARKs
Side channel (timing)Secret-dependent branchesConstant-time implementations only
Weak Fiat-ShamirNon-interactive proofsInclude ALL public inputs in the transcript
Subgroup attackDeserialized curve pointsValidate on-curve AND subgroup membership

Anti-Patterns

Anti-PatternWhy It's CatastrophicWhat To Do Instead
Field arithmetic with native integersOverflow, timing leaks, incorrect modular reductionUse audited libraries (arkworks, gnark, ffjavascript)
Reusing nonces across proofsEnables secret key extractionFresh cryptographic randomness per proof
Weak Fiat-Shamir transcriptProof malleability and replay attacksInclude unique session ID and all public inputs
No domain separation on hashesCross-protocol hash collisionsPrefix every hash with a unique domain tag
Skipping subgroup checks on deserializationInvalid curve attacks in wrong subgroupValidate on-curve AND correct subgroup
Hardcoded security parametersCannot adapt to evolving threatsParameterize security level (lambda = 128, 256)
Using Math.random() for cryptoPredictable; adversary reconstructs secretscrypto.getRandomValues() or /dev/urandom
Branching on secret valuesTiming side channel leaks secret bitsConstant-time conditional selection

Code Review Checklist

Field Arithmetic

  • All arithmetic operates in the correct field (check prime)
  • No integer overflow before modular reduction
  • Inversion handles zero case (error, not silent failure)

Constant-Time Operations

  • No branching on secret values
  • No secret-dependent memory access patterns
  • Timing of operations does not leak input structure

Randomness

  • Cryptographically secure RNG for all secret generation
  • No nonce reuse across proofs or protocol instances
  • Randomness is fresh per proof (no caching or reuse)

Protocol Composition

  • Fiat-Shamir transcript includes ALL public parameters
  • Domain separation tags on all hash calls
  • No challenge reuse across protocol instances
  • Binding and hiding properties preserved under composition

Serialization & Transport

  • Deserialized points validated (on-curve + subgroup check)
  • Canonical serialization (no ambiguous encoding)
  • Proof format includes version identifier

Domain-Specific Skills

After establishing foundations with this skill, load the appropriate domain-specific skill for protocol-level guidance:

DomainSkillCoverage
Zero-knowledge proofscrypto-zkpProof systems, circuit design, constraint fuzzing, formal verification
Garbled circuits and MPCcrypto-garbled-circuitsYao's protocol, OT, half-gates, multi-party extensions
BitVM3 protocolcrypto-bitvmBitcoin script, Taproot, RISC-V verification, fraud proofs