/midnight-skills:midnight-securityfrom midnight-skills
Privacy audit checklist, data leak patterns, and defensive Compact contract patterns for Midnight Network. Covers what data is publicly visible on-chain, how to prevent accidental disclosure, and how to audit contracts for privacy issues.
Ships in midnight-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Versions: every pin in this skill comes from
references/versions.json, the repo's single source of truth (checked in CI). Resolve the current set at use time withreferences/volatile.md. Never install@latest.
Midnight's privacy model is opt-in privacy on top of a public chain. The ZK proof proves correctness of execution — it does not hide the existence of a transaction, its structure, or anything in export ledger. Security on Midnight means being precise about what leaks and designing contracts so that leaks are intentional.
Shipping? Run the pass/fail gate in midnight-security-checklist/ first; this skill explains each item.
Primary references:
docs.midnight.network/concepts/how-midnight-works/keeping-data-privatedocs.midnight.network/concepts/how-midnight-works/semanticsdocs.midnight.network/concepts/how-midnight-works/smart-contractsdocs.midnight.network/compact (language reference + standard library) and the toolchain release notes. There is still no dedicated security guide, which this skill fills inEverything in export ledger is public. Everything in witness is private. The proof is public. The witness data is not.
More precisely, for every ledger operation:
| Operation | What is revealed |
|---|---|
ledger.insert(v) | The value v |
ledger.lookup(k) | The key k and the returned value |
set.insert(v) | The value v |
set.member(f(x)) | The value f(x) — not x directly |
map.insert(k, v) | The key k and value v |
counter.increment(n) | The increment amount n and new value |
merkleTree.insert(v) | Does NOT reveal v — only that the tree grew |
merkleTree.checkRoot(path) | That someone proved membership — not which entry |
| Circuit arguments | All of them — they are part of the public transcript |
witness return values | Nothing on-chain — the ZK proof only proves they existed |
The public transcript is what the chain records: ledger reads, writes, and the structure of those operations. Witness values exist only in the proof's private inputs — they are never posted.
Run this against every contract before shipping.
export ledger field: who can read it, and is that acceptable?export ledger field per-user data that should be local (witness)?Map<K, V>: is the key K itself sensitive? (Map keys are public on insert/lookup.)Set<T> used when anonymity is required? (Set.member(v) reveals v — use MerkleTree instead.)persistentHash over a small value space (e.g., boolean, vote, role)? → brute-forcible → use persistentCommit with a fresh nonce instead.transientHash/transientCommit used for ledger storage? → does not survive contract upgrades → use persistent*.persistentHash call: does it include a unique domain separator string?assert before being used in logic?submitVote reveals that someone voted)merkleTree.insert calls within a circuit reveal anything about the private inputs?These things are always visible to any chain observer, regardless of privacy measures:
entryPoint field in the indexerexport circuitexport ledger field that changesPractical implication: If your circuit takes vote: Boolean as an argument, the vote is public even though you never write it to the ledger directly. Move it to a witness.
// ❌ vote is a circuit argument → public transcript → everyone sees it
export circuit submitVote(vote: Boolean): [] {
votes.insert(disclose(makeCommitment(vote)));
}
// ✅ vote comes from witness → stays in proof's private inputs
witness userVote(): Boolean;
export circuit submitVote(): [] {
const vote = userVote(); // private
votes.insert(disclose(makeCommitment(vote))); // only commitment is public
}
Set when anonymity is required// ❌ Set.member(v) reveals v — which user's commitment is being checked
export ledger members: Set<Bytes<32>>;
export circuit prove(): [] {
const pk = publicKey(secretKey());
assert(members.member(pk), "not a member"); // reveals pk
}
// ✅ MerkleTree proves membership without revealing which entry
export ledger members: MerkleTree<16, Bytes<32>>;
witness findMemberPath(pk: Bytes<32>): MerkleTreePath<16, Bytes<32>>;
export circuit prove(): [] {
const pk = publicKey(secretKey());
const path = findMemberPath(pk);
assert(
members.checkRoot(merkleTreePathRoot<16, Bytes<32>>(path)),
"not a member"
);
// Only proves: "someone in the tree called this." Does not reveal who.
}
// ❌ persistentHash over Boolean → 2 possible outputs → trivially brute-forced
export ledger commitment: Bytes<32>;
export circuit commit(vote: Boolean): [] {
commitment = disclose(persistentHash<Boolean>(vote));
}
// ✅ persistentCommit with fresh nonce → infeasible to brute-force
witness voteNonce(): Bytes<32>;
export circuit commit(vote: Boolean): [] { // vote still a circuit arg here — see pattern 1
const nonce = voteNonce();
commitment = disclose(persistentCommit<Boolean>(vote, nonce));
}
// Combine with pattern 1 to also move vote to witness
// ❌ same secret key as nonce → if same value committed twice, identical on-chain
export circuit recordBalance(amount: Uint<64>): [] {
const sk = secretKey();
const c = persistentCommit<Uint<64>>(amount, sk); // reusing sk as nonce
commitments.insert(disclose(c));
}
// ✅ derive a unique nonce per commitment using round counter or fresh randomness
export ledger round: Counter;
export circuit recordBalance(amount: Uint<64>): [] {
const sk = secretKey();
const nonce = persistentHash<Vector<2, Bytes<32>>>(
[sk, pad(32, "balance-nonce"), round as Field as Bytes<32>]
);
const c = persistentCommit<Uint<64>>(amount, nonce);
commitments.insert(disclose(c));
round.increment(1);
}
// ❌ same sk → same hash for auth and for commitment → linkable across contracts
pure circuit publicKey(sk: Bytes<32>): Bytes<32> {
return persistentHash<Bytes<32>>(sk);
}
pure circuit commitmentKey(sk: Bytes<32>): Bytes<32> {
return persistentHash<Bytes<32>>(sk); // identical output
}
// ✅ unique domain separator per purpose
pure circuit authPublicKey(sk: Bytes<32>): Bytes<32> {
return persistentHash<Vector<2, Bytes<32>>>([pad(32, "myapp:auth:v1"), sk]);
}
pure circuit commitmentKey(sk: Bytes<32>): Bytes<32> {
return persistentHash<Vector<2, Bytes<32>>>([pad(32, "myapp:commit:v1"), sk]);
}
// ❌ trusting that the DApp-provided witness returns the right balance
export circuit transfer(to: Bytes<32>, amount: Uint<64>): [] {
const balance = userBalance(); // could return anything
balances.insert(to, disclose(balance - amount));
}
// ✅ assert all witness outputs before using them
export circuit transfer(to: Bytes<32>, amount: Uint<64>): [] {
const balance = userBalance();
assert(balance >= amount, "insufficient balance");
assert(balance <= MAX_SUPPLY, "invalid balance");
balances.insert(to, disclose((balance - amount) as Uint<64>));
}
Proves knowledge of a secret key without revealing it. The sequence counter prevents replay across rounds.
pragma language_version >= 0.28.0;
import CompactStandardLibrary;
export ledger admin: Bytes<32>;
export ledger round: Counter;
witness secretKey(): Bytes<32>;
constructor() {
admin = disclose(publicKey(secretKey(), roundBytes(0)));
}
export circuit adminAction(): [] {
const sk = secretKey();
assert(admin == disclose(publicKey(sk, roundBytes(round.read()))), "not authorized");
// ... do the action
round.increment(1); // invalidates old proofs against this round
admin = disclose(publicKey(sk, roundBytes(round.read())));
}
pure circuit roundBytes(r: Uint<64>): Bytes<32> {
return (r as Field) as Bytes<32>;
}
pure circuit publicKey(sk: Bytes<32>, roundBytes: Bytes<32>): Bytes<32> {
return persistentHash<Vector<3, Bytes<32>>>([
pad(32, "myapp:admin:v1"), // domain separator
roundBytes, // round prevents replay
sk,
]);
}
Proves a token exists in the Merkle tree and has not been spent, without revealing which token.
pragma language_version >= 0.28.0;
import CompactStandardLibrary;
export ledger commitments: HistoricMerkleTree<16, Bytes<32>>;
export ledger nullifiers: Set<Bytes<32>>;
export ledger counter: Counter;
witness secretKey(): Bytes<32>;
witness findCommitmentPath(c: Bytes<32>): MerkleTreePath<16, Bytes<32>>;
export circuit addToken(commitment: Bytes<32>): [] {
commitments.insert(disclose(commitment));
}
export circuit spend(): [] {
const sk = secretKey();
const c = commitment(sk);
const path = findCommitmentPath(c);
// 1. Prove token is in the tree (without revealing which)
assert(
commitments.checkRoot(disclose(merkleTreePathRoot<16, Bytes<32>>(path))), // the root is public; the path stays private
"commitment not in tree"
);
// 2. Prove token has not been spent
const nul = nullifier(sk);
assert(!nullifiers.member(disclose(nul)), "already spent");
// 3. Record the spend — reveals nullifier, not the commitment
nullifiers.insert(disclose(nul));
counter.increment(1);
}
// CRITICAL: commitment and nullifier MUST use different domain separators
// If they shared a domain, the same sk would produce the same hash for both
pure circuit commitment(sk: Bytes<32>): Bytes<32> {
return persistentHash<Vector<2, Bytes<32>>>([pad(32, "myapp:commit:v1"), sk]);
}
pure circuit nullifier(sk: Bytes<32>): Bytes<32> {
return persistentHash<Vector<2, Bytes<32>>>([pad(32, "myapp:nullifier:v1"), sk]);
}
Use HistoricMerkleTree instead of MerkleTree when: the tree has frequent insertions that would otherwise invalidate outstanding proofs. Use plain MerkleTree when proofs are always generated against the current root and items are never removed.
Authorize a set of users without revealing which one is acting.
pragma language_version >= 0.28.0;
import CompactStandardLibrary;
export ledger allowlist: MerkleTree<16, Bytes<32>>;
witness secretKey(): Bytes<32>;
witness findAllowlistPath(pk: Bytes<32>): MerkleTreePath<16, Bytes<32>>;
export circuit addToAllowlist(commitment: Bytes<32>): [] {
// Admin adds a commitment — reveals commitment (hash of user's key)
// The commitment itself doesn't reveal who the user is unless brute-forced
allowlist.insert(disclose(commitment));
}
export circuit allowlistedAction(): [] {
const sk = secretKey();
const pk = allowlistKey(sk);
const path = findAllowlistPath(pk);
assert(
allowlist.checkRoot(disclose(merkleTreePathRoot<16, Bytes<32>>(path))), // the root is public; the path stays private
"not on allowlist"
);
// Action proceeds. Observer knows: "someone on the allowlist did this."
// Observer does NOT know: which allowlisted user.
}
pure circuit allowlistKey(sk: Bytes<32>): Bytes<32> {
return persistentHash<Vector<2, Bytes<32>>>([pad(32, "myapp:allowlist:v1"), sk]);
}
Ledger 9 added features that create new things to review. All of these behaviors were checked with the 0.36.1 compiler.
| Feature | What leaks / what to check |
|---|---|
emit(...) events | Everything emitted is public, indexed (contractEvents) and permanent. Treat an event like a ledger write: only emit disclose()d values. Emitting from the constructor is a compile error. |
Cross-contract calls (contract X { circuit ...; }) | The callee reference and every argument must be disclosed. The compiler rejects undisclosed ones (contract call argument 1 might disclose the witness value). The call graph is visible on-chain, so a call reveals which contract you touched and with what. Cycles fail at run time. Per the language reference, circuits that satisfy a contract type may not call witnesses. |
| Callee trust | A contract-typed argument is supplied by the caller. Verify it is the contract you expect (compare against a stored address) before trusting its return values. |
kernel.caller() | Returns Maybe<PublicAddress>. Useful for contract-to-contract authorization, but it is the immediate caller. Check is_some and don't assume an end user. |
secp256k1EcdsaVerify(msgHash, sig, pk) | Verifies a hash, not a message: hash the message yourself, with domain separation, inside the circuit. Always assert on the result. Since 0.35 it rejects the identity point as pk. The compiler changelog (0.33.x) says it accepts both low-s and high-s signatures, so treat signatures as malleable: never use one (or its hash) as a nullifier/replay key. Bind a nonce or round instead. |
keccak256 / sha512 | Fine for Ethereum interop. Prefer persistentHash / persistentCommit for Midnight-native commitments, and never hash small value spaces without a nonce. |
| Recompiling for Ledger 9 | Moving from compiler 0.31 to 0.36.x changes verifier keys. Redeploy, or install new keys via the maintenance authority, and re-audit cross-contract callers and callees together. |
Midnight transactions execute in two phases with different failure behavior:
well-formedness check → guaranteed phase → fallible phase
| Phase | Failure behavior | Fee behavior |
|---|---|---|
| Well-formedness | Transaction rejected entirely, not included | No fee |
| Guaranteed | Transaction rejected entirely, not included | No fee |
| Fallible | Guaranteed phase effects persist; fallible phase rolled back | Fees forfeited |
Security implications:
Partial success is real. If your contract does work in the guaranteed phase (e.g., burns a token) and then fails in the fallible phase (e.g., a balance check fails), the burn is permanent but the balance isn't updated. Design state transitions so they either succeed atomically or the guaranteed phase does nothing consequential.
Fees are forfeited on fallible failure. An attacker who can force fallible failures can drain DUST from users' wallets. Ensure assert conditions in fallible circuits cannot be triggered by external state that an attacker can manipulate between proof generation and submission.
Front-running via public transcript. Because the public transcript (including which circuit was called and all circuit arguments) is visible in the mempool before finalization, an observer can potentially act on that information before the transaction lands. If ordering matters (e.g., a swap), design circuits that include slippage bounds or use commitment reveals to defer sensitive data.
ZK proofs guarantee:
ZK proofs do NOT guarantee:
assert statements)Practical consequence: Every invariant your contract cares about must be enforced by an assert in the circuit. The ZK proof only proves what the circuit checks.
| Depth | Max leaves | Circuit impact | Use for |
|---|---|---|---|
| 8 | 256 | Minimal | Tiny allowlists, tests |
| 10 | 1,024 | Low | Small user sets |
| 16 | 65,536 | Moderate | Medium DApps |
| 20 | 1,048,576 | Higher | Large user sets |
| 32 | ~4 billion | Significant | Maximum scale |
Use the minimum depth needed. Each additional level increases circuit size and proof time. Overflow (inserting beyond capacity) is a runtime error — size your tree for your expected maximum.
HistoricMerkleTree stores all past roots — it has higher storage cost than MerkleTree. Only use it when you need to prove membership against a past root (e.g., when tree is frequently updated between proof generation and submission).
| Risk | Bad pattern | Safe pattern |
|---|---|---|
| Sensitive value on-chain | Circuit argument for private data | Move to witness |
| Membership reveals identity | Set<T> membership check | MerkleTree<n, T> path proof |
| Brute-forcible hash | persistentHash over small domain | persistentCommit with fresh nonce |
| Linked commitments | Same nonce across multiple commits | Round counter or fresh nonce per commit |
| Key reuse across purposes | Same sk → same hash everywhere | Unique domain separator per purpose |
| Commitment = nullifier collision | Same domain for both | Different pad(32, ...) strings |
| Replay attack | Auth without sequence counter | Include round/counter in public key derivation |
| Double-spend | No nullifier tracking | Set<Bytes<32>> nullifier registry |
| Untrusted witness | Use witness output directly | assert all witness outputs before use |
| Upgrade breaks commitments | transientHash in ledger | persistentHash / persistentCommit |
| Partial success trap | Consequential work in guaranteed phase | Keep guaranteed phase minimal |
| Front-running | Sensitive args in circuit parameters | Use witness + commitment reveal pattern |
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub kali-decoder/midnight-skills --plugin midnight-skills43 skills in this plugin
/1am-walletIntegrate the 1AM wallet for dust-free contract deployment and transaction flow on Midnight Network. Use this skill whenever a user is building a Midnight Network dApp, connecting to the 1AM browser extension, deploying or calling a Compact contract, handling ZK proving, setting up providers, or asking about dust-free transaction flow. Also use it for indexer patching, private state providers, payload encryption, or any question involving window.midnight['1am']./android-example-votingBuild a voting/poll dApp on Midnight Network using the Kuira Android SDK — Compact smart contract with create/cast/close circuits, passkey-derived identity, embedded wallet, Compose UI, reactive ledger reads via observeLedger(), and on-device ZK proving. Use when building a poll or voting dApp targeting Android./Other plugins' skills for the same job