Skip to content

PoW+PoS hybrid consensus, post-quantum crypto (ML-DSA-87 + ML-KEM-1024), and a real CLI wallet#6

Open
devnull37 wants to merge 10 commits into
mainfrom
pow-pos-pqc-complete
Open

PoW+PoS hybrid consensus, post-quantum crypto (ML-DSA-87 + ML-KEM-1024), and a real CLI wallet#6
devnull37 wants to merge 10 commits into
mainfrom
pow-pos-pqc-complete

Conversation

@devnull37
Copy link
Copy Markdown
Owner

Summary

Turns the chain into a working hybrid Proof-of-Work + Proof-of-Stake network with real post-quantum cryptography, hardens the consensus pallet, wires real weights, and adds a genuine CLI wallet + PQ tooling.

Block authoring is real PoW via sc-consensus-pow (double-Blake2-256 over pre_hash || nonce, U256 difficulty, on-chain retargeting); Aura/GRANDPA were removed from the node and runtime. pallet-ghost-consensus is the PoS economic layer (staking, stake-weighted validator selection, 40/60 miner/staker reward split, evidence-gated slash-and-burn, validation-timeout recovery).

What's included

  • PoW + PoS consensus — node builds, boots, and mines blocks Refactor and Fix Ghost Chain Consensus #1→#20.
  • Post-quantum signatures — on-chain ML-DSA-87 (Dilithium-5, FIPS 204) verification in the no_std Wasm runtime; validators register keys, and validate_block enforces an ML-DSA signature from the selected validator when a key is registered.
  • Post-quantum encryption — node-side ML-KEM-1024 (FIPS 203) key encapsulation + ChaCha20-Poly1305 AEAD.
  • Hardening — replay guard on verify_pq_signature; immediate finality when the staker set is empty (was timeout-only); the mine demo now uses the node's real PoW work function (single source of truth).
  • Weights — real operation-grounded SubstrateWeight (DbWeight reads/writes + a conservative ~300µs ML-DSA-verify charge) wired into the runtime, replacing the () placeholder.
  • CLI wallet + tooling (ghost <cmd>, over JSON-RPC):
    • balance, validators — live state queries (validators also shows ML-DSA key status).
    • stake, unstake, transfer — build, sr25519-sign, and submit real extrinsics.
    • pq-keygen, register-key — ML-DSA-87 keygen + on-chain registration.
    • pq-kem-keygen, pq-encrypt, pq-decrypt — ML-KEM-1024 file encryption (offline).

Verification

  • 37 pallet tests + 20 node tests pass.
  • Live --dev run: mined Refactor and Fix Ghost Chain Consensus #1→#20; stake (2→5 UNIT), unstake (5→4 UNIT), and transfer (Bob +5 UNIT) executed with correct nonce sequencing; register-key then showed ML-DSA-87 key registered (2592 bytes) in validators; ML-KEM pq-encryptpq-decrypt round-tripped exactly and a wrong-key decrypt was correctly rejected.

Honest scope / caveats

  • Node-to-node transport is classical libp2p Noise/X25519 — stable2407 has no PQ-Noise variant. PQ lives at the signature + application-encryption layers, not the wire transport.
  • No external security audit has been performed; finality is probabilistic longest-chain PoW, not BFT.
  • No supply cap (inflationary subsidy — a deliberate monetary-policy decision to make before mainnet).
  • Weights are conservative and operation-grounded; empirical frame-benchmarking numbers are still recommended before mainnet.
  • Windows build note: [profile.dev] debug = false is set to avoid the MSVC LNK1318 PDB-size limit on the node binary.

Build & test

cargo build --bin ghost-node
cargo test -p pallet-ghost-consensus   # 37 tests
cargo test -p ghost-node               # 20 tests
cargo run --bin ghost-node -- --dev    # mines real PoW blocks

🤖 Generated with Claude Code

Faris Allafi and others added 10 commits September 5, 2025 19:04
Introduce detailed agent specifications for blockchain integration testing and Rust unit test writing in the .claude/agents directory. Add CLAUDE.md with project and development guidance, and update Cargo.toml files and node/pallets for new consensus and testing features, including miner module and consensus pallet enhancements.
…eights

Block authoring is real Proof-of-Work via sc-consensus-pow (double-Blake2-256
over pre_hash||nonce, U256 difficulty, on-chain retargeting); Aura/GRANDPA
removed from node and runtime. pallet-ghost-consensus is the PoS economic layer
(staking, stake-weighted selection, 40/60 miner/staker reward split,
evidence-gated slash-and-burn, validation-timeout recovery).

Post-quantum: on-chain ML-DSA-87 (Dilithium-5, FIPS 204) signature verification
in the no_std Wasm runtime; node-side ML-KEM-1024 (FIPS 203) + ChaCha20-Poly1305
payload encryption module.

Hardening this round:
- Replay guard on verify_pq_signature (rejects duplicate attestations)
- No-staker block finality is now immediate (was validation-timeout-only)
- mine CLI demo uses the node's real PoW work function (single source of truth);
  CLI --difficulty now matches the chain's conventional convention
- Real operation-grounded weights (weights.rs SubstrateWeight<Runtime>: DbWeight
  reads/writes + ~300us ML-DSA verify charge) wired into the runtime, replacing
  the () placeholder
- [profile.dev] debug=false to avoid MSVC LNK1318 (PDB size limit) on the bin

Verified: 37 pallet tests + 20 node tests pass; node boots and mines blocks
#1->#20 via sc-consensus-pow with all of the above compiled in.

Honest scope: node-to-node transport remains classical libp2p Noise/X25519
(stable2407 has no PQ-Noise); no external audit; probabilistic PoW finality.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The `ghost` subcommands now connect to a running node's JSON-RPC endpoint instead of
printing guidance:
- balance / validators: decode System.Account and GhostConsensus.ValidatorStakes from
  live state (state_getStorage / state_getKeysPaged).
- stake / unstake / transfer: build, sr25519-sign, and submit real extrinsics using the
  runtime's own RuntimeCall / UncheckedExtrinsic / TxExtension types
  (SignedPayload::from_raw with RPC-fetched genesis hash + the embedded spec/tx version,
  immortal era, metadata-hash extension disabled), via author_submitExtrinsic.

New node deps: jsonrpsee http-client, tokio, pallet-balances, pallet-ghost-consensus.
Each command takes --rpc-url (default http://127.0.0.1:9944) and --account (default //Alice).

Verified against a live --dev node: balance reflects the genesis endowment; validators
lists the genesis stakers; stake (2->5 UNIT), unstake (5->4 UNIT) and transfer (Bob +5
UNIT) all execute on-chain with correct nonce sequencing (0,1,2).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- pq-keygen: generate an ML-DSA-87 (FIPS 204) keypair to <out>.pub (2592 bytes) and
  <out>.sec (4896 bytes). Keygen runs on a 16 MiB-stack thread (the matrix expansion
  overflows the main thread's stack otherwise).
- register-key: read an ML-DSA-87 public key file and submit
  ghostConsensus.register_ml_dsa_key, activating on-chain PQ attestation checks for the
  signer.
- `validators` now also reports each validator's ML-DSA key status.

New node dep: fips204 (native keygen only; the no_std Wasm runtime builds fips204
separately, without default-rng). Generated key files are gitignored.

Verified against a live --dev node: pq-keygen produced a 2592/4896-byte keypair;
register-key from Alice was accepted and executed; `validators` then reported
"ML-DSA-87 key registered (2592 bytes)" for Alice and "no PQ key" for Bob.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Expose the existing node-side ML-KEM-1024 (FIPS 203) + ChaCha20-Poly1305 module as CLI
commands (file-based, no running node needed):
- pq-kem-keygen: write a keypair to <out>.ek (1568 bytes) and <out>.dk (3168 bytes).
- pq-encrypt: ML-KEM-encapsulate to a recipient .ek and seal the file with ChaCha20-Poly1305.
- pq-decrypt: decapsulate with a .dk and open the AEAD bundle.

Heavy crypto runs on a 16 MiB-stack thread. Generated .ek/.dk files are gitignored.

Verified: keygen produced 1568/3168-byte keys; a 39-byte plaintext encrypted to a 1639-byte
bundle and decrypted back to the exact original; decryption with a different keypair's key
was correctly rejected.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant