Introduction

In the week from February 12th to February 16th 2024, zkSecurity performed a security audit of Darkfi’s built-in contracts and circuits. The audit was performed on the public darkfi repository https://github.com/darkrenaissance/darkfi at commit ea50f9ac. The engagement lasted 1 week and was conducted by 2 consultants.

Scope

The scope of the audit includes the three built-in contracts of the Darkfi protocol:

  • Money contract. Responsible for the creation and transfer of fungible tokens (native DRK token and custom tokens). Provides the core protocol functions of transaction fees and proof-of-work rewards.
  • DAO contract. Implements an anonymous DAO, with an onchain proposal and voting system accessible to holders of a governance token.
  • Deployooor contract. Responsible for deployment and locking of additional user-defined contracts.

Each of these contracts comes in three parts, all of which are in scope of the audit:

  • Onchain contract. Rust code which is compiled to a WASM binary and run by network participants during transaction application, with read/write access to contract-owned state and read access to other contracts and various network state.
  • zk-SNARK circuits. Contract code which operates on private data and is executed on the client, along with a zk proof to be verified onchain. Circuits are written in zkas, a custom DSL, and compiled to a halo2 backend. The audit covers the circuit logic, but not the implementation of zkas and halo2 gadgets it uses.
  • Client-side wrapper code. Rust code which builds transactions, including zk proofs, and submits them to the onchain contract.

The audit explicitly covers the design and implementation of cryptographic schemes used in the contracts and client SDK, including:

  • Schnorr signatures over the Pallas curve
  • Homomorphic Pedersen commitments
  • AEAD encryption
  • Elgamal encryption
  • ECVRF
  • Various commitment and derivation schemes based on the Poseidon hash function
  • Poseidon-based sparse Merkle tree

Summary and recommendations

Darkfi’s codebase was found to be well-organized, with clarity provided by a large number of inline code comments. The protocol is elegantly designed, powered by a lightweight, unopinionated contract VM, on top of which most of the core protocol is implemented as userland contracts. Great care is taken to preserve user privacy and ownership at all levels of the software stack.

Apart from the findings described in the following section, we offer a general recommendation:

Complete protocol specification. The Darkfi book features a detailed spec of the DAO contract, with a section describing each individual contract method: its purpose, inputs, and contract statement. However, the other two contracts are not covered at the same level of detail; and important functionality like transaction fees, proof-of-work rewards, deployment and locking is essentially undocumented. We recommend extending the protocol specification to provide full details about all core contracts.