Midnight ZK Standard Library
The Midnight ZK Standard Library (midnight-zk-stdlib) provides a
high-level abstraction for building zero-knowledge circuits using the
Midnight Circuits library and the Midnight Proofs
proving system.
WARNING: This library has not been audited. Use it at your own risk.
Overview
ZkStdLib encapsulates the functionality required by Midnight and serves as an abstraction layer, allowing developers to focus on circuit logic rather than the configuration and chip creation. Developers only need to implement the Relation trait, avoiding the boilerplate of Halo2's Circuit trait.
The architecture of ZkStdLib is configurable via the following structure:
The configuration can be defined via the Relation trait with the used_chips function. The default architecture activates only JubJub, Poseidon and sha256, and uses a single column for the pow2range chip. The maximum number of columns accepted for the pow2range chip is currently 4.
Example: Proving Knowledge of a SHA-256 Preimage
Here is a complete example showing how to build a circuit for proving knowledge of a SHA-256 preimage.
Given a public input x, we will prove that we know w ∈ {0,1}^192 such that x = SHA-256(w).
use ;
use ;
use ;
use ;
use ChaCha8Rng;
use Digest;
type F = Fq;
;
// An upper bound on the log2 of the number of rows in the circuit.
// The closer to the real value, the better, but you do not have to worry too much.
const K: u32 = 14;
let mut srs = filecoin_srs;
let relation = ShaPreImageCircuit;
// The actual k needed by this circuit is 13. We can downsize it automatically.
downsize_srs_for_relation;
let vk = setup_vk;
let pk = setup_pk;
// Sample a random preimage as the witness.
let mut rng = from_entropy;
let witness: = from_fn;
let instance = digest.into;
let proof =
.expect;
assert!
You can find more examples in the examples directory.
Versioning
We use Semantic Versioning. To capture the changes that do not affect the API, do not add any new functionality, but are breaking changes, we increment the MAJOR version. This happens when the circuit is modified for performance or bug fixes; the modification of the verification keys break backwards compatibility.
- MAJOR: Incremented when you make incompatible API or VK changes
- MINOR: Incremented when you add functionality in a backward-compatible manner
- PATCH: Incremented when you make backward-compatible bug fixes