<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BossChaos</title>
    <description>The latest articles on DEV Community by BossChaos (@bosschaos).</description>
    <link>https://dev.to/bosschaos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3908415%2F7e9679b7-a452-4c5d-bfe1-61ed802df719.png</url>
      <title>DEV Community: BossChaos</title>
      <link>https://dev.to/bosschaos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bosschaos"/>
    <language>en</language>
    <item>
      <title>Compact Standard Library: A Practical Guide to Every Export</title>
      <dc:creator>BossChaos</dc:creator>
      <pubDate>Sat, 02 May 2026 04:24:22 +0000</pubDate>
      <link>https://dev.to/bosschaos/compact-standard-library-a-practical-guide-to-every-export-11dh</link>
      <guid>https://dev.to/bosschaos/compact-standard-library-a-practical-guide-to-every-export-11dh</guid>
      <description>&lt;h1&gt;
  
  
  Compact Standard Library: A Practical Guide to Every Export
&lt;/h1&gt;

&lt;p&gt;The Compact standard library ships with every Midnight project — you don't install it, you don't import a package manager dependency. It's just there, baked into the compiler. But the official docs are sparse enough that most developers only discover what's available by reading other people's contracts or stumbling into a type error.&lt;/p&gt;

&lt;p&gt;This guide fixes that. I'll walk through every meaningful export, grouped by what it actually does, with working code examples you can compile and test. The companion repository contains all the contracts as individual &lt;code&gt;.compact&lt;/code&gt; files, ready to compile with &lt;code&gt;compactc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you've been writing Compact contracts and keep wondering "is there a built-in way to do this?", the answer is probably in here.&lt;/p&gt;




&lt;h2&gt;
  
  
  Generic Types: Maybe and Either
&lt;/h2&gt;

&lt;p&gt;These two show up in nearly every non-trivial contract. If you've written TypeScript or Rust, the concepts are familiar — but the ZK context adds some nuances that matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Maybe&amp;lt;T&amp;gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Maybe&amp;lt;T&amp;gt;&lt;/code&gt; represents an optional value: either &lt;code&gt;Some(value)&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;. No nulls, no sentinel values like &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;-1&lt;/code&gt; pretending to mean "not set."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract MaybeExample {
  ledger admin: Maybe&amp;lt;ContractAddress&amp;gt;;
  ledger config: Maybe&amp;lt;Uint&amp;lt;64&amp;gt;&amp;gt;;

  circuit setAdmin(addr: ContractAddress): [] {
    assert ledger.admin.isNone : "admin already set";
    ledger.admin = Maybe.some(addr);
  }

  circuit clearAdmin(): [] {
    assert ledger.admin.isSome : "no admin to clear";
    ledger.admin = Maybe.none();
  }

  circuit isAdmin(addr: ContractAddress): [Boolean] {
    const current = ledger.admin;
    return [current.isSome &amp;amp;&amp;amp; current.value == addr];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.isSome&lt;/code&gt;&lt;/strong&gt; — Boolean, &lt;code&gt;true&lt;/code&gt; if the value exists&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.isNone&lt;/code&gt;&lt;/strong&gt; — Boolean, &lt;code&gt;true&lt;/code&gt; if the value is absent&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.value&lt;/code&gt;&lt;/strong&gt; — The inner value of type &lt;code&gt;T&lt;/code&gt;, but &lt;strong&gt;only safe to access when you know it's &lt;code&gt;Some&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's the critical gotcha: accessing &lt;code&gt;.value&lt;/code&gt; on a &lt;code&gt;None&lt;/code&gt; inside a ZK circuit doesn't throw an exception — it makes the proof impossible to generate. The constraint solver hits an unsatisfiable condition and fails silently. Always gate &lt;code&gt;.value&lt;/code&gt; access behind an &lt;code&gt;.isSome&lt;/code&gt; check.&lt;/p&gt;

&lt;p&gt;A common pattern is lazy initialization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit initConfig(value: Uint&amp;lt;64&amp;gt;): [] {
  if ledger.config.isNone {
    ledger.config = Maybe.some(value);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Either&amp;lt;L, R&amp;gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Either&amp;lt;L, R&amp;gt;&lt;/code&gt; is a tagged union — one of two possible types, &lt;code&gt;Left(L)&lt;/code&gt; or &lt;code&gt;Right(R)&lt;/code&gt;. By convention, &lt;code&gt;Left&lt;/code&gt; carries error information and &lt;code&gt;Right&lt;/code&gt; carries success values. Compact doesn't enforce this convention, but following it makes your code readable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract EitherDemo {
  circuit safeDivide(
    witness dividend: Uint&amp;lt;64&amp;gt;,
    witness divisor: Uint&amp;lt;64&amp;gt;
  ): [Either&amp;lt;Bytes&amp;lt;32&amp;gt;, Uint&amp;lt;64&amp;gt;&amp;gt;] {
    if divisor == Uint&amp;lt;64&amp;gt;::from(0) {
      return [Either.left(bytes("division by zero"))];
    }
    return [Either.right(dividend / divisor)];
  }

  circuit processResult(
    witness a: Uint&amp;lt;64&amp;gt;,
    witness b: Uint&amp;lt;64&amp;gt;
  ): [Boolean, Uint&amp;lt;64&amp;gt;] {
    const result = EitherDemo::safeDivide(a, b);
    if result.isLeft {
      return [false, Uint&amp;lt;64&amp;gt;::from(0)];
    }
    return [true, result.rightValue];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Discriminate with &lt;code&gt;.isLeft&lt;/code&gt;, &lt;code&gt;.isRight&lt;/code&gt;, &lt;code&gt;.leftValue&lt;/code&gt;, and &lt;code&gt;.rightValue&lt;/code&gt;. Same caveat as &lt;code&gt;Maybe&lt;/code&gt; — accessing the wrong branch causes a constraint failure, not a recoverable error.&lt;/p&gt;




&lt;h2&gt;
  
  
  Merkle Trees and Commitments
&lt;/h2&gt;

&lt;p&gt;Merkle trees are the backbone of ZK contract design. The standard library gives you the tree type itself plus the commitment and verification primitives you need for privacy-preserving state proofs.&lt;/p&gt;

&lt;h3&gt;
  
  
  MerkleTree&amp;lt;N, T&amp;gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;MerkleTree&amp;lt;N, T&amp;gt;&lt;/code&gt; is a complete binary Merkle tree with &lt;code&gt;N&lt;/code&gt; levels — meaning &lt;code&gt;2^N&lt;/code&gt; leaves — holding values of type &lt;code&gt;T&lt;/code&gt;. The size is fixed at compile time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract MerkleRegistry {
  export ledger members: MerkleTree&amp;lt;20, Bytes&amp;lt;32&amp;gt;&amp;gt;;
  export ledger memberCount: Uint&amp;lt;64&amp;gt;;

  circuit addMember(commitment: Bytes&amp;lt;32&amp;gt;): [Uint&amp;lt;64&amp;gt;] {
    const index = ledger.memberCount;
    ledger.members.set(index, commitment);
    ledger.memberCount = index + Uint&amp;lt;64&amp;gt;::from(1);
    return [index];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plan your tree depth early. &lt;code&gt;N=16&lt;/code&gt; gives you 65,536 entries (fine for small registries). &lt;code&gt;N=20&lt;/code&gt; gives you ~1 million (good for most production use cases). &lt;code&gt;N=32&lt;/code&gt; gives you 4 billion (overkill unless you're building something at scale).&lt;/p&gt;

&lt;p&gt;The tree supports &lt;code&gt;.root()&lt;/code&gt; to get the current root hash and &lt;code&gt;.set(index, value)&lt;/code&gt; to update a leaf. You can verify membership with &lt;code&gt;verifyMerkleProof(leaf, root, pathElements, pathIndices)&lt;/code&gt; — this is how you prove someone is in the registry without revealing who else is.&lt;/p&gt;

&lt;h3&gt;
  
  
  persistentCommit
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;persistentCommit(witness: Bytes&amp;lt;32&amp;gt;)&lt;/code&gt; creates a commitment anchored to Midnight's state tree. It's deterministic — the same secret always produces the same commitment — and it's tied to a specific epoch, enabling "this value existed at this point in time" proofs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit commit(witness secret: Bytes&amp;lt;32&amp;gt;): [Bytes&amp;lt;32&amp;gt;] {
  const commitment = persistentCommit(secret);
  ledger.commitments.set(index, commitment);
  return [commitment];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the foundation of commit-reveal patterns. Commit on-chain now, reveal off-chain later when you're ready.&lt;/p&gt;

&lt;h3&gt;
  
  
  verifyCommitment
&lt;/h3&gt;

&lt;p&gt;The counterpart to &lt;code&gt;persistentCommit&lt;/code&gt;: given a secret and a commitment, verify they match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit reveal(
  witness secret: Bytes&amp;lt;32&amp;gt;,
  public commitment: Bytes&amp;lt;32&amp;gt;
): [Boolean] {
  return [verifyCommitment(secret, commitment)];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Together, &lt;code&gt;persistentCommit&lt;/code&gt; and &lt;code&gt;verifyCommitment&lt;/code&gt; power sealed-bid auctions, private voting, and nullifier-based replay prevention.&lt;/p&gt;




&lt;h2&gt;
  
  
  Elliptic Curves
&lt;/h2&gt;

&lt;p&gt;Midnight's ZK circuits operate over elliptic curves under the hood. The standard library surfaces some of this for contracts that need explicit cryptographic operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  CurvePoint
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;CurvePoint&lt;/code&gt; represents a point on Midnight's elliptic curve. You interact with these when doing key derivation, signature verification, or building custom commitment schemes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit derivePublicKey(
  witness privateKey: Scalar
): [CurvePoint] {
  const G = CurvePoint.generator();
  return [G.multiply(privateKey)];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.generator()&lt;/code&gt;&lt;/strong&gt; — Static method, returns the base point &lt;code&gt;G&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.multiply(scalar)&lt;/code&gt;&lt;/strong&gt; — Scalar multiplication (expensive in ZK)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.add(other)&lt;/code&gt;&lt;/strong&gt; — Point addition (cheaper)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.negate()&lt;/code&gt;&lt;/strong&gt; — Point negation&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.hashToCurve(bytes)&lt;/code&gt;&lt;/strong&gt; — Hash arbitrary data to a curve point&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scalar
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Scalar&lt;/code&gt; is a field element in the elliptic curve's scalar field. It's what you use for private keys, blinding factors, and random nonces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit pedersenCommit(
  witness value: Uint&amp;lt;64&amp;gt;,
  witness blinding: Scalar
): [CurvePoint] {
  const H = CurvePoint.hashToCurve(bytes("value"));
  const G = CurvePoint.generator();
  return [H.multiply(Scalar.fromUint(value)).add(G.multiply(blinding))];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Pedersen commitment pattern above is worth knowing: it commits to a value while keeping it hidden, and the commitments are additively homomorphic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Kernel Types
&lt;/h2&gt;

&lt;p&gt;These types bridge the ZK contract world and Midnight's transaction kernel. They're how your contract talks about identity, addresses, and shielded coin state.&lt;/p&gt;

&lt;h3&gt;
  
  
  ContractAddress
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ContractAddress&lt;/code&gt; is the canonical identity for smart contracts. It's derived from the contract's code hash and deployment parameters — deterministic and unique.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract AccessControlled {
  export ledger admin: ContractAddress;

  circuit initialize(adminAddr: ContractAddress): [] {
    assert ledger.admin == ContractAddress.zero() : "already initialized";
    ledger.admin = adminAddr;
  }

  circuit adminAction(): [] {
    assert ledger.admin == ContractAddress.self() : "unauthorized";
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.zero()&lt;/code&gt;&lt;/strong&gt; — Returns the zero address (the default/unset value)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.self()&lt;/code&gt;&lt;/strong&gt; — Returns this contract's own address&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use &lt;code&gt;ContractAddress&lt;/code&gt; for identity-based access control. Never use &lt;code&gt;ownPublicKey()&lt;/code&gt; for this purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  ZswapCoinPublicKey
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ZswapCoinPublicKey&lt;/code&gt; is the public key type for Zswap coin ownership — Midnight's shielded asset system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ledger depositor: ZswapCoinPublicKey;

circuit recordDepositor(pk: ZswapCoinPublicKey): [] {
  ledger.depositor = pk;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  UserAddress
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;UserAddress&lt;/code&gt; is a higher-level address type that wraps both a &lt;code&gt;ZswapCoinPublicKey&lt;/code&gt; and a spending key derivation path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit getRecipientKey(userAddr: UserAddress): [ZswapCoinPublicKey] {
  return [userAddr.spendingKey()];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ShieldedCoinInfo
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ShieldedCoinInfo&lt;/code&gt; describes a shielded coin: its value, token type, and randomness.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit inspectCoin(coinInfo: ShieldedCoinInfo): [Uint&amp;lt;64&amp;gt;, Bytes&amp;lt;32&amp;gt;] {
  return [coinInfo.value, coinInfo.tokenType];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  QualifiedShieldedCoinInfo
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt; extends &lt;code&gt;ShieldedCoinInfo&lt;/code&gt; with the nullifier key needed to spend the coin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit deposit(witness coinProof: CoinProof): [QualifiedShieldedCoinInfo] {
  const coin = receiveShielded(coinProof);
  return [coin];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CoinProof
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;CoinProof&lt;/code&gt; is the ZK proof that demonstrates ownership of a shielded coin without revealing the coin's details.&lt;/p&gt;




&lt;h2&gt;
  
  
  Helper Circuits
&lt;/h2&gt;

&lt;p&gt;These are the high-level operations for working with tokens and shielded transfers.&lt;/p&gt;

&lt;h3&gt;
  
  
  nativeToken()
&lt;/h3&gt;

&lt;p&gt;Returns the token type identifier for MNT, Midnight's native token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit acceptMNTOnly(coinType: Bytes&amp;lt;32&amp;gt;): [] {
  assert coinType == nativeToken() : "only MNT accepted";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  tokenType(ContractAddress)
&lt;/h3&gt;

&lt;p&gt;Derives the token type identifier for a custom token contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit isMyToken(coinType: Bytes&amp;lt;32&amp;gt;): [Boolean] {
  return [coinType == tokenType(ContractAddress.self())];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  evolveNonce
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;evolveNonce(nonce: Bytes&amp;lt;32&amp;gt;)&lt;/code&gt; advances a nonce to prevent replay attacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit useNonce(witness action: Bytes&amp;lt;32&amp;gt;): [Bytes&amp;lt;32&amp;gt;] {
  const nonce = ledger.currentNonce;
  const nextNonce = evolveNonce(nonce);
  ledger.currentNonce = nextNonce;
  assert verifyCommitment(action, nonce) : "invalid nonce";
  return [nextNonce];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  shieldedBurnAddress()
&lt;/h3&gt;

&lt;p&gt;Returns the canonical burn address for Midnight.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit burn(amount: Uint&amp;lt;64&amp;gt;): [] {
  sendShielded(shieldedBurnAddress(), amount);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Shielded Token Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  receiveShielded
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;receiveShielded(coinProof: CoinProof)&lt;/code&gt; processes an incoming shielded coin transfer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit deposit(witness coinProof: CoinProof): [QualifiedShieldedCoinInfo] {
  const coin = receiveShielded(coinProof);
  assert coin.info.tokenType == nativeToken() : "MNT only";
  ledger.balance = ledger.balance + coin.info.value;
  return [coin];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  sendShielded
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sendShielded(recipient: ZswapCoinPublicKey, coin: QualifiedShieldedCoinInfo, amount: Uint&amp;lt;64&amp;gt;)&lt;/code&gt; constructs and sends a shielded transfer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;circuit withdraw(
  recipient: ZswapCoinPublicKey,
  amount: Uint&amp;lt;64&amp;gt;
): [] {
  assert amount &amp;lt;= ledger.balance : "insufficient balance";
  ledger.balance = ledger.balance - amount;
  sendShielded(recipient, ledger.heldCoin, amount);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Block-Time Queries
&lt;/h2&gt;

&lt;h3&gt;
  
  
  getBlockTime()
&lt;/h3&gt;

&lt;p&gt;Returns the current block timestamp as a &lt;code&gt;Uint&amp;lt;64&amp;gt;&lt;/code&gt; (Unix epoch seconds).&lt;/p&gt;

&lt;h3&gt;
  
  
  getBlockNumber()
&lt;/h3&gt;

&lt;p&gt;Returns the current block height as a &lt;code&gt;Uint&amp;lt;64&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  getEpoch()
&lt;/h3&gt;

&lt;p&gt;Returns the current epoch number as a &lt;code&gt;Uint&amp;lt;64&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract TimeEscrow {
  export ledger unlockBlock: Uint&amp;lt;64&amp;gt;;
  export ledger locked: Boolean;

  circuit lock(duration: Uint&amp;lt;64&amp;gt;): [] {
    assert !ledger.locked : "already locked";
    ledger.unlockBlock = getBlockNumber() + duration;
    ledger.locked = true;
  }

  circuit release(): [] {
    assert getBlockNumber() &amp;gt;= ledger.unlockBlock : "still locked";
    ledger.locked = false;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;getBlockNumber()&lt;/code&gt; for relative timing, &lt;code&gt;getBlockTime()&lt;/code&gt; for absolute deadlines, and &lt;code&gt;getEpoch()&lt;/code&gt; for rate-limiting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting It All Together: A Token Vault
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma language_version &amp;gt;= 0.22;

contract TokenVault {
  export ledger deposits: MerkleTree&amp;lt;16, Bytes&amp;lt;32&amp;gt;&amp;gt;;
  export ledger depositCount: Uint&amp;lt;64&amp;gt;;
  export ledger totalBalance: Uint&amp;lt;64&amp;gt;;
  export ledger heldCoins: MerkleTree&amp;lt;16, QualifiedShieldedCoinInfo&amp;gt;;

  circuit deposit(witness coinProof: CoinProof): [Uint&amp;lt;64&amp;gt;] {
    const coin = receiveShielded(coinProof);
    assert coin.info.tokenType == nativeToken() : "MNT only";

    const index = ledger.depositCount;
    ledger.heldCoins.set(index, coin);
    ledger.totalBalance = ledger.totalBalance + coin.info.value;
    ledger.depositCount = index + Uint&amp;lt;64&amp;gt;::from(1);

    const commitment = persistentCommit(
      coin.info.value.toBytes().concat(index.toBytes())
    );
    ledger.deposits.set(index, commitment);

    return [index];
  }

  circuit withdraw(
    coinIndex: Uint&amp;lt;64&amp;gt;,
    recipient: ZswapCoinPublicKey
  ): [] {
    const coin = ledger.heldCoins.get(coinIndex);
    assert coin.isSome : "coin not found";
    ledger.totalBalance = ledger.totalBalance - coin.value.info.value;
    sendShielded(recipient, coin.value, coin.value.info.value);
    ledger.heldCoins.set(coinIndex, Maybe.none());
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Quick Reference
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Export&lt;/th&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Maybe&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Generic&lt;/td&gt;
&lt;td&gt;Optional values, nullable ledger fields&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Either&amp;lt;L, R&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Generic&lt;/td&gt;
&lt;td&gt;Two-outcome results, structured errors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MerkleTree&amp;lt;N, T&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Merkle&lt;/td&gt;
&lt;td&gt;Fixed-size on-chain indexed storage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;persistentCommit&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Merkle&lt;/td&gt;
&lt;td&gt;Creating verifiable commitments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;verifyCommitment&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Merkle&lt;/td&gt;
&lt;td&gt;Verifying commitments in proofs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;verifyMerkleProof&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Merkle&lt;/td&gt;
&lt;td&gt;Proving leaf membership in a tree&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CurvePoint&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Crypto&lt;/td&gt;
&lt;td&gt;Key derivation, Pedersen commitments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Scalar&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Crypto&lt;/td&gt;
&lt;td&gt;Private keys, blinding factors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ContractAddress&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;Contract identity, access control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ZswapCoinPublicKey&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;Shielded coin recipient keys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UserAddress&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;User-facing addresses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ShieldedCoinInfo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;Coin value/type metadata&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;QualifiedShieldedCoinInfo&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;Spendable coin with nullifier key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CoinProof&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;ZK proof of coin ownership&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;nativeToken()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Helper&lt;/td&gt;
&lt;td&gt;MNT token type identifier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tokenType(addr)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Helper&lt;/td&gt;
&lt;td&gt;Custom token type identifier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;evolveNonce&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Helper&lt;/td&gt;
&lt;td&gt;Replay-protected nonce chaining&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;shieldedBurnAddress()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Helper&lt;/td&gt;
&lt;td&gt;Canonical token burn address&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;receiveShielded&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Transfer&lt;/td&gt;
&lt;td&gt;Accept incoming shielded coins&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sendShielded&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Transfer&lt;/td&gt;
&lt;td&gt;Send shielded coins to recipient&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;getBlockTime()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Time&lt;/td&gt;
&lt;td&gt;Current block timestamp&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;getBlockNumber()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Time&lt;/td&gt;
&lt;td&gt;Current block height&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;getEpoch()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Time&lt;/td&gt;
&lt;td&gt;Current consensus epoch&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Notes
&lt;/h2&gt;

&lt;p&gt;The Compact standard library is deliberately minimal. Midnight's philosophy is to give you the cryptographic building blocks and let you compose them.&lt;/p&gt;

&lt;p&gt;The types you'll reach for in every contract: &lt;code&gt;Maybe&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code&gt;ContractAddress&lt;/code&gt;, &lt;code&gt;receiveShielded&lt;/code&gt;, &lt;code&gt;sendShielded&lt;/code&gt;. Everything else is situational.&lt;/p&gt;

&lt;p&gt;The biggest gotcha for newcomers: all of these operations happen inside ZK circuits. "Optional" doesn't mean "try/catch" — it means "the proof is either valid or it isn't."&lt;/p&gt;

&lt;p&gt;All code examples are available as individual &lt;code&gt;.compact&lt;/code&gt; files in the companion repository:&lt;br&gt;
&lt;a href="https://github.com/BossChaos/contributor-hub/tree/bounty/293-compact-stdlib/bounties/293-compact-stdlib" rel="noopener noreferrer"&gt;https://github.com/BossChaos/contributor-hub/tree/bounty/293-compact-stdlib/bounties/293-compact-stdlib&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  MidnightforDevs #Compact #ZK #Privacy #Blockchain
&lt;/h1&gt;

</description>
      <category>privacy</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
