Skip to content

Cardinal-Cryptography/DarkerForestDemo

Repository files navigation

DarkerForestDemo

DarkerForestDemo is an Aztec implementation of a twist on the popular ZK game Dark Forest. It introduces partial privacy, a concept missing from the original, and is loosely inspired by Ogame.

This demo showcases a mechanic where Public State (a planet's radar technology) interacts with Private State (an attacker's spy technology) to calculate a result without revealing the private inputs.

The Core Mechanic: Dynamic Fog of War

In standard blockchain games, everything is usually public. In Dark Forest, some things are hidden (hashes), others are entirely public. This project explores the middle ground.

When you mobilize a fleet to attack, the network doesn't see the real number of ships. Instead, it sees a "Visible Amount" calculated inside a private circuit:

if (My_Spy_Tech > Your_Radar_Tech) {
    // I am stealthy. You only see a fraction of my fleet.
    Visible_Amount = Actual_Amount * 0.8^(Spy - Radar)
} else {
    // You see everything.
    Visible_Amount = Actual_Amount
}

Note: This is a PoC. In a real game, an attacker should be able to make more of the fleet visible, otherwise it would be easy to guess its spy_tech level after a single attack and calculate actual fleet size in all subsequent attacks.

How it works under the hood

  • Spy Tech is a private encrypted note; only the owner sees it.
  • Radar Tech is a public integer on-chain.
  • The Calculation happens locally on the user's device (PXE).
  • The Verification happens on-chain. If a user generates a proof using a Radar level lower than the actual one (to make their fleet invisible), the public kernel rejects the transaction.

Solved Problem: The "Vanishing Ships" Leak

A common issue in private systems is that observers can guess the created private value by watching decrease in the public value (that's why private AMM DEX is not trivial to build). We solved this by splitting planets into two zones:

  • Inner Orbit (Public Defense): These ships are actively counted and defend the planet. The count is public.
  • Outer Orbit (Private Staging): These ships are "prepared for travel." They are not visible to observers and do not count toward defense.

Because you mobilize attacks from the Outer Orbit (where the supply is already hidden), observers cannot tell the size of your attacking fleet by looking at the origin planet.

Note: Because fleet size is private, attacks are not automatic. The fleet arrives "prepared," and the attacker must manually trigger the combat resolution.

Project Structure

  • contracts/: The Noir code (DarkerForestDemo.nr). Handles logic, privacy, and state transitions.
  • tests/: TypeScript tests using the Aztec sandbox to simulate the loop:
    • Mobilize: Move ships (Private).
    • Travel: Time passes (Public enforcement).
    • Arrival: Ships land.
    • Attack: Spend fleet UTXOs to conquer planets.

Note on "Change" Management

The attack function implements a UTXO Change Pattern similar to Bitcoin cash logic:

If you have a note worth 100 ships but only want to attack with 30:

  1. The contract spends the 100-ship note (nullifying it).
  2. It sends 30 ships to battle.
  3. It creates a new private note of 70 ships and returns it to you.

Getting Started

Requires the Aztec Sandbox (v3.0.1)

  1. Install Dependencies:

    yarn install
  2. Start Sandbox:

    aztec start --local-network
  3. Compile & Generate Artifacts:

    yarn build
  4. Run Tests:

    yarn test

Disclaimer: This is educational code intended to showcase architectural patterns. It has not been audited and is not intended for production use.

About

PoC Aztec implementation of ZK game

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors