### Full ZoKrates Workflow Example Source: https://zokrates.github.io/toolbox/zokrates_js.html Demonstrates the complete ZoKrates workflow: compilation, witness computation, trusted setup, proof generation, and verification using the JavaScript bindings. ```javascript initialize().then((zokratesProvider) => { const source = "def main(private field a) -> field { return a * a; }"; // compilation const artifacts = zokratesProvider.compile(source); // computation const { witness, output } = zokratesProvider.computeWitness(artifacts, ["2"]); // run setup const keypair = zokratesProvider.setup(artifacts.program); // generate proof const proof = zokratesProvider.generateProof( artifacts.program, witness, keypair.pk ); // export solidity verifier const verifier = zokratesProvider.exportSolidityVerifier(keypair.vk); // or verify off-chain const isVerified = zokratesProvider.verify(keypair.vk, proof); }); ``` -------------------------------- ### Setup and Export Verifier Source: https://zokrates.github.io/print.html Performs the setup phase to generate keys and exports the verifier smart contract. ```bash zokrates setup zokrates export-verifier ``` -------------------------------- ### View ZoKrates Subcommands Source: https://zokrates.github.io/toolbox/cli.html Run this command to see an overview of all available subcommands in the ZoKrates CLI. No setup is required beyond having ZoKrates installed. ```bash zokrates ``` -------------------------------- ### Perform Universal Trusted Setup for Marlin Scheme Source: https://zokrates.github.io/print.html Performs the universal phase of a trusted setup, specifically for the 'marlin' proving scheme. Requires specifying the size of the setup as an exponent. ```javascript zokratesProvider.universalSetup(8); ``` -------------------------------- ### Generate Trusted Setup with Universal Public Parameters (Marlin Scheme) Source: https://zokrates.github.io/toolbox/zokrates_js.html Generates a trusted setup keypair using pre-computed universal public parameters from the universal setup phase for the Marlin scheme. ```javascript const keypair = zokratesProvider.universalSetupKeypair(srs, artifacts.program); ``` -------------------------------- ### Install ZoKrates JS Source: https://zokrates.github.io/toolbox/zokrates_js.html Install the zokrates-js package using npm. ```bash npm install zokrates-js ``` -------------------------------- ### Perform Universal Setup Phase (Marlin Scheme) Source: https://zokrates.github.io/toolbox/zokrates_js.html Performs the universal phase of a trusted setup, required for the Marlin proving scheme. Takes a size exponent and optional entropy. ```javascript const srs = zokratesProvider.universalSetup(8); ``` -------------------------------- ### Compile and Setup ZoKrates Program Source: https://zokrates.github.io/print.html Initializes the proving and verification keys for a ZoKrates program. ```bash zokrates compile -i reveal_bit.zok -o reveal_bit zokrates setup -i reveal_bit ``` -------------------------------- ### ZoKrates.js Universal Setup (Marlin Scheme) Source: https://zokrates.github.io/print.html Perform the universal phase of a trusted setup, specifically for the Marlin proving scheme. ```APIDOC ## ZoKrates.js Universal Setup (Marlin Scheme) ### Description Performs the universal phase of a trusted setup. This method is only available when using the `marlin` proving scheme. ### Method `zokratesProvider.universalSetup(size: number): SetupKeypair` ### Endpoint N/A (Client-side JavaScript library) ### Parameters #### `size` - **`size`** (number) - Required - The size of the trusted setup, passed as an exponent. For example, `8` for `2**8`. ### Returns `SetupKeypair` - An object containing the proving key (`pk`) and verification key (`vk`). ### Request Example ```javascript // This requires the scheme to be set to 'marlin' during initialization or via withOptions const keypair = zokratesProvider.universalSetup(8); // For a setup size of 2**8 ``` ``` -------------------------------- ### Compile and Setup Verification Source: https://zokrates.github.io/examples/sha256example.html Commands to compile the updated circuit and generate verification keys and smart contracts. ```bash zokrates compile -i hashexample.zok ``` ```bash zokrates setup zokrates export-verifier ``` ```bash zokrates compute-witness -a 0 0 0 5 ``` -------------------------------- ### ZoKrates Example Program Source: https://zokrates.github.io/toolbox/abi.html This is an example ZoKrates program that defines structs and a main function. It is used to illustrate ABI generation and argument passing. ```zokrates struct Bar { field a; } struct Foo { u8 a; Bar b; } def main(Foo foo, bool[2] bar, field num) -> field { return 42; } ``` -------------------------------- ### ZoKrates.js Trusted Setup Source: https://zokrates.github.io/print.html Generate a trusted setup (keypair) for a compiled ZoKrates program. ```APIDOC ## ZoKrates.js Trusted Setup ### Description Generates a trusted setup (keypair) required for generating zero-knowledge proofs. This involves a cryptographic ceremony. ### Method `zokratesProvider.setup(program: string, entropy?: string): SetupKeypair` ### Endpoint N/A (Client-side JavaScript library) ### Parameters #### `program` - **`program`** (string) - Required - The compiled program from `CompilationArtifacts`. #### `entropy` - **`entropy`** (string) - Optional - User-provided randomness to influence the setup. ### Returns `SetupKeypair` - An object containing the proving key (`pk`) and verification key (`vk`). ### Request Example ```javascript // Assuming 'artifacts' is the result of zokratesProvider.compile() const keypair = zokratesProvider.setup(artifacts.program); ``` ``` -------------------------------- ### Install ZoKrates with cURL Source: https://zokrates.github.io/gettingstarted.html Installs ZoKrates using a one-line command for Linux, MacOS, and FreeBSD. ```bash curl -LSfs get.zokrat.es | sh ``` -------------------------------- ### ZoKrates CLI Workflow Source: https://zokrates.github.io/gettingstarted.html Demonstrates the typical workflow for using the ZoKrates CLI, including compilation, setup, witness computation, proof generation, and verification. ```bash # compile zokrates compile -i root.zok # perform the setup phase zokrates setup # execute the program zokrates compute-witness -a 337 113569 # generate a proof of computation zokrates generate-proof # export a solidity verifier zokrates export-verifier # or verify natively zokrates verify ``` -------------------------------- ### Compile a ZoKrates program Source: https://zokrates.github.io/toolbox/trusted_setup.html Compile the circuit file into a format ready for setup. ```bash zokrates compile -i program.zok -o circuit ``` -------------------------------- ### Generate Trusted Setup Keypair Source: https://zokrates.github.io/toolbox/zokrates_js.html Generates a trusted setup keypair (proving and verification keys) for a compiled ZoKrates program. An optional entropy can be provided for randomness. ```javascript const keypair = zokratesProvider.setup(artifacts.program); ``` -------------------------------- ### ZoKrates Example Program for Nova Source: https://zokrates.github.io/toolbox/experimental.html A simple ZoKrates program demonstrating addition, suitable for use with the Nova proof system. It takes a public sum and a private element, returning their sum. ```zokrates def main(public field sum, private field element) -> field { return sum + element; } ``` -------------------------------- ### Trusted Setup Generation Source: https://zokrates.github.io/print.html Generates a trusted setup with universal public parameters for the compiled program, specifically for the marlin scheme. ```APIDOC ## POST /setup/marlin ### Description Generates a trusted setup with universal public parameters for the compiled program. Only available for the marlin scheme. ### Parameters #### Request Body - **srs** (object) - Required - Universal public parameters from the universal setup phase - **program** (object) - Required - Compiled program ### Response #### Success Response (200) - **SetupKeypair** (object) - The generated setup keypair ``` -------------------------------- ### Generate Zokrates Setup Keys Source: https://zokrates.github.io/examples/rng_tutorial.html Generates the proving and verification keys required for creating and verifying zero-knowledge proofs. ```bash zokrates setup -i reveal_bit ``` -------------------------------- ### Start Truffle Console Source: https://zokrates.github.io/examples/rng_tutorial.html Launches the Truffle interactive JavaScript console for contract deployment and interaction. ```bash truffle console ``` -------------------------------- ### Initialize MPC ceremony Source: https://zokrates.github.io/toolbox/trusted_setup.html Start the phase 2 ceremony using the compiled circuit and phase 1 parameters. ```bash $ zokrates mpc init -i circuit -o mpc.params -r ./phase1radix2m2 ``` -------------------------------- ### Array Declaration and Initialization Syntax Source: https://zokrates.github.io/language/types.html Basic examples of declaring and initializing arrays with specific values or repeated constants. ```ZoKrates field[3] a = [1, 2, 3]; // initialize a field array with field values bool[13] b = [false; 13]; // initialize a bool array with value false ``` -------------------------------- ### Simple Nova Program Example Source: https://zokrates.github.io/print.html A basic ZoKrates program demonstrating Nova's functionality, which adds a private element to a public sum. ```zokrates __ def main(public field sum, private field element) -> field { return sum + element; } ``` -------------------------------- ### Get Help for ZoKrates Subcommand Source: https://zokrates.github.io/toolbox/cli.html Use the --help flag with any ZoKrates subcommand to get detailed usage information. This is useful for understanding specific command options and parameters. ```bash zokrates compile --help ``` -------------------------------- ### ZoKrates ABI Specification Example Source: https://zokrates.github.io/toolbox/abi.html This JSON object represents the ABI specification generated for the example ZoKrates program. It details the input and output types, names, and structures. ```json { "inputs":[ { "name":"foo", "public":true, "type":"struct", "components":{ "name":"Foo", "members":[ { "name":"a", "type":"u8" }, { "name":"b", "type":"struct", "components":{ "name":"Bar", "members":[ { "name":"a", "type":"field" } ] } } ] } }, { "name":"bar", "public":true, "type":"array", "components":{ "size":2, "type":"bool" } }, { "name":"num", "public":true, "type":"field" } ], "output": { "type":"field" } } ``` -------------------------------- ### Witness Output Example Source: https://zokrates.github.io/examples/rng_tutorial.html The resulting witness output after computing the hash of the provided inputs. ```text Computing witness... Witness: ["3592665057","2164530888","1223339564","3041196771","2006723467","2963045520","3851824201","3453903005"] ``` -------------------------------- ### ZoKrates Assembly: Quadratic Constraint Example Source: https://zokrates.github.io/language/assembly.html Demonstrates a valid quadratic constraint 'x * (x - 1) === 0;' which ZoKrates may transform. ```zokrates asm { x * (x - 1) === 0; } ``` -------------------------------- ### Constraint transformation example Source: https://zokrates.github.io/print.html ZoKrates may transform constraints to meet the required quadratic format. ```ZoKrates asm { x * (x - 1) === 0; } ``` ```ZoKrates asm { x === x * x; } ``` -------------------------------- ### Generic Functions Source: https://zokrates.github.io/print.html Examples of generic functions over u32 values, including explicit parameter passing. ```ZoKrates def foo() -> field[N] { return [42; N]; } def main() -> field[2] { field[2] res = foo(); return res; } ``` ```ZoKrates // a function to sum the N first powers of a field element def sum_powers(field a) -> field { field mut res = 0; for u32 i in 0..N { res = res + a ** i; } return res; } def main(field a) -> field { // call `sum_powers` providing the explicit generic parameter `N := 5` return sum_powers::<5>(a); } ``` -------------------------------- ### Tuple Usage in ZoKrates Source: https://zokrates.github.io/print.html Provides an example of using tuples in ZoKrates to group different data types. Shows how to declare, initialize, and modify tuple elements using dot notation. ```zokrates __ def main() -> bool { (field[2], bool) mut v = ([1, 2], true); v.0 = [42, 43]; return v.1; } ``` -------------------------------- ### Invalid non-quadratic constraint example Source: https://zokrates.github.io/print.html Constraints must be quadratic; expressions like d === a*b*c are disallowed. ```ZoKrates asm { d === a*b*c; } ``` -------------------------------- ### File System Resolver for ZoKrates Imports Source: https://zokrates.github.io/toolbox/zokrates_js.html A Node.js example of a file system resolver function that can be used with ZoKrates compilation to resolve imported modules. ```javascript import fs from "fs"; import path from "path"; const fileSystemResolver = (from, to) => { const location = path.resolve(path.dirname(path.resolve(from)), to); const source = fs.readFileSync(location).toString(); return { source, location }; }; ``` -------------------------------- ### Initialize and Prove Nova Iterations Source: https://zokrates.github.io/print.html Set up initial state and step inputs, then generate a proof for multiple iterations using ZoKrates Nova. ```bash __ echo "\"0\"" > init.json echo "[\"1\", \"7\", \"42\"]" > steps.json zokrates nova prove ``` -------------------------------- ### Importing from Parent Directories Source: https://zokrates.github.io/print.html Demonstrates how to import resources from parent directories using relative paths like `../`. ```zokrates __ from "../../../mycode" import foo; ``` -------------------------------- ### Access ZoKrates CLI help Source: https://zokrates.github.io/print.html Run the base command or use --help to view available subcommands. ```bash zokrates ``` ```bash zokrates compile --help ``` -------------------------------- ### Importing from Local Directory Source: https://zokrates.github.io/print.html Shows how to import a resource directly from a file in the same directory. ```zokrates __ from "./mycode" import foo; ``` -------------------------------- ### Inner Bit Loop Source: https://zokrates.github.io/print.html Starts the loop to iterate through the 32 bits of a word. ```ZoKrates for u32 bit in 0..32 { ``` -------------------------------- ### Comment for Bit Conversion Source: https://zokrates.github.io/print.html Placeholder comment indicating the start of bit conversion logic. ```ZoKrates // Convert the preimage to bits ``` -------------------------------- ### Initialize ZoKrates Provider with Options Source: https://zokrates.github.io/toolbox/zokrates_js.html Initializes the ZoKrates provider with custom options for backend, curve, and proving scheme. Returns a `ZoKratesProvider` configured with given options. ```javascript initialize().then((defaultProvider) => { let zokratesProvider = defaultProvider.withOptions({ backend: "ark", curve: "bls12_381", scheme: "g16", }); // ... }); ``` -------------------------------- ### Build ZoKrates from Source Source: https://zokrates.github.io/gettingstarted.html Builds ZoKrates from its GitHub repository. Ensure to set the ZOKRATES_STDLIB environment variable. ```bash git clone https://github.com/ZoKrates/ZoKrates cd ZoKrates export ZOKRATES_STDLIB=$PWD/zokrates_stdlib/stdlib cargo build -p zokrates_cli --release cd target/release ``` -------------------------------- ### Verify ZoKrates Proof Source: https://zokrates.github.io/toolbox/zokrates_js.html Verifies a generated ZK-SNARK proof using the verification key obtained from the trusted setup. ```javascript const isVerified = zokratesProvider.verify(keypair.vk, proof); ``` -------------------------------- ### Import Bit Conversion Utility Source: https://zokrates.github.io/examples/rng_tutorial.html Import the utility to convert u32 values to boolean arrays. ```zok import "utils/casts/u32_to_bits" as u32_to_bits; ``` -------------------------------- ### Run ZoKrates with Docker Source: https://zokrates.github.io/gettingstarted.html Launches an interactive ZoKrates Docker container. ```bash docker run -ti zokrates/zokrates /bin/bash ``` -------------------------------- ### Return Multiple Values Source: https://zokrates.github.io/examples/rng_tutorial.html Zokrates functions can return multiple values by separating them with commas. This example returns a hash and a specific bit. ```zokrates return (sha256(preimage[0..8], preimage[8..16]), preimageBits[bitNum]); ``` -------------------------------- ### Initialize ZoKrates JS (Browser Script) Source: https://zokrates.github.io/toolbox/zokrates_js.html Include the ZoKrates JS library via a script tag for browser usage and initialize it. ```html ``` -------------------------------- ### Generate and Verify Proofs with ZoKrates CLI Source: https://zokrates.github.io/print.html Commands to compute a witness, generate a proof, and verify it using the bellman backend. ```bash zokrates compute-witness -i circuit -a 123456789 987654321 --verbose zokrates generate-proof -i circuit -b bellman zokrates verify -b bellman ``` -------------------------------- ### Define a ZoKrates program Source: https://zokrates.github.io/toolbox/trusted_setup.html Create a basic circuit file named program.zok. ```zokrates def main(private field a, private field b) -> field { return a * b; } ``` -------------------------------- ### Create Tampered Proof Input Source: https://zokrates.github.io/examples/rng_tutorial.html Creates a modified version of the proof inputs to simulate a cheating attempt. This example flips the last bit of the last input. ```javascript cheat = [...proof.inputs] cheat[cheat.length-1] = cheat[cheat.length-1].replace(/[01]$/, cheat[cheat.length-1][65] == '1' ? '0': '1') ``` -------------------------------- ### Generate and Verify Proofs with Zokrates Source: https://zokrates.github.io/toolbox/trusted_setup.html Use the exported keys to generate proofs and verify them. This involves computing witness, generating the proof, and then verifying it. ```bash # use the keys to generate proofs and verify zokrates compute-witness -i circuit -a 123456789 987654321 --verbose zokrates generate-proof -i circuit -b bellman zokrates verify -b bellman ``` -------------------------------- ### Large Field Element Representation in ZIR Source: https://zokrates.github.io/print.html ZoKrates uses an isomorphism to display large field elements in ZIR. This example shows how 'p - 1' is represented as '-1'. ```plaintext -1 ``` -------------------------------- ### Initialize ZoKrates JS (Dynamic Import) Source: https://zokrates.github.io/toolbox/zokrates_js.html Dynamically import the initialize function from zokrates-js. ```javascript let { initialize } = await import("zokrates-js"); ``` -------------------------------- ### ZoKrates.js Initialization Source: https://zokrates.github.io/print.html Initialize the ZoKrates.js provider. This is the first step before using any other ZoKrates.js functionalities. ```APIDOC ## ZoKrates.js Initialization ### Description Initializes the ZoKrates.js provider. This function returns a promise that resolves to a `ZoKratesProvider` instance. ### Method `initialize()` ### Endpoint N/A (Client-side JavaScript library) ### Returns `Promise` - A promise that resolves to an initialized `ZoKratesProvider`. ### Request Example ```javascript import { initialize } from "zokrates-js"; initialize().then((zokratesProvider) => { // Use zokratesProvider here }); ``` ### Response Example ```javascript // The resolved value is a ZoKratesProvider instance ``` ``` -------------------------------- ### Compute Witness and Generate Proof Source: https://zokrates.github.io/examples/rng_tutorial.html Computes the witness file with program parameters and then generates the zero-knowledge proof using the witness and proving key. ```bash zokrates compute-witness --verbose -i reveal_bit -a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 510 zokrates generate-proof -i reveal_bit ``` -------------------------------- ### Constant Definitions Source: https://zokrates.github.io/print.html Shows how to define global constants and reference them within functions. ```ZoKrates const field PRIME = 31; def main() -> field { return PRIME; } ``` ```ZoKrates const field ONE = 1; const field TWO = ONE + ONE; def main() -> field { return TWO; } ``` -------------------------------- ### Compile a ZoKrates program Source: https://zokrates.github.io/print.html Define a main function and compile it into a circuit file. ```ZoKrates def main(private field a, private field b) -> field { return a * b; } ``` ```bash zokrates compile -i program.zok -o circuit ``` -------------------------------- ### Array Initialization and Manipulation Source: https://zokrates.github.io/language/types.html Shows various ways to initialize, modify, and slice arrays in ZoKrates. ```ZoKrates def main() -> field { field[3] mut a = [1, 2, 3]; // initialize a field array with field values a[2] = 4; // set a member to a value field[4] b = [42; 4]; // initialize an array of 4 values all equal to 42 field[4] c = [...a, 4]; // initialize an array copying values from `a`, followed by 4 field[2] d = a[1..3]; // initialize an array copying a slice from `a` bool[3] e = [true, true || false, true]; // initialize a boolean array u32 SIZE = 3; field[SIZE] f = [1, 2, 3]; // initialize a field array with a size that's a compile-time constant return a[0] + b[1] + c[2]; } ``` -------------------------------- ### Initialize and contribute to MPC ceremony Source: https://zokrates.github.io/print.html Commands for initializing a phase 2 ceremony and contributing to the parameters. ```bash $ zokrates mpc init -i circuit -o mpc.params -r ./phase1radix2m2 Initializing MPC... Parameters written to `mpc.params` ``` ```bash $ zokrates mpc contribute -i mpc.params -o alice.params -e "alice 1" Contributing to `mpc.params`... The BLAKE2b hash of your contribution is: 4ebf1359 416fbc42 31af6476 9173cb33 32b8c2f9 475d143a 25634a5c e461eb67 ``` -------------------------------- ### Array Initialization and Usage in ZoKrates Source: https://zokrates.github.io/print.html Shows various ways to declare and initialize static arrays in ZoKrates, including arrays of fields, booleans, and arrays with compile-time constant sizes. Demonstrates element assignment, initialization with repeated values, and array composition using the spread operator. ```zokrates __ def main() -> field { field[3] mut a = [1, 2, 3]; // initialize a field array with field values a[2] = 4; // set a member to a value field[4] b = [42; 4]; // initialize an array of 4 values all equal to 42 field[4] c = [...a, 4]; // initialize an array copying values from `a`, followed by 4 field[2] d = a[1..3]; // initialize an array copying a slice from `a` bool[3] e = [true, true || false, true]; // initialize a boolean array u32 SIZE = 3; field[SIZE] f = [1, 2, 3]; // initialize a field array with a size that's a compile-time constant return a[0] + b[1] + c[2]; } ``` -------------------------------- ### Generate Proof with ZoKrates Source: https://zokrates.github.io/examples/sha256example.html Run this command to construct the zkSNARK proof. Inputs declared as private in the program will not appear in the proof. ```bash zokrates generate-proof ``` -------------------------------- ### Verify Nova Proof Source: https://zokrates.github.io/print.html Verify the compressed Nova proof. ```bash __ zokrates nova verify ``` -------------------------------- ### Initialize ZoKrates Provider Source: https://zokrates.github.io/toolbox/zokrates_js.html Initializes the ZoKrates provider. Returns an initialized `ZoKratesProvider` as a promise. ```javascript initialize().then((zokratesProvider) => { // call api functions here }); ``` -------------------------------- ### Continue Nova Proof with More Steps Source: https://zokrates.github.io/print.html Extend an existing Nova proof by adding more computation steps with new intermediate inputs. ```bash __ zokrates nova prove --continue ``` -------------------------------- ### Compile and Run Reveal Bit Program Source: https://zokrates.github.io/examples/rng_tutorial.html Commands to compile the reveal bit circuit and compute a witness with a specific bit index. ```bash zokrates compile -i reveal_bit.zok -o reveal_bit zokrates compute-witness --verbose -i reveal_bit -a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 510 ``` -------------------------------- ### Verify Proof with Web3 Source: https://zokrates.github.io/toolbox/verification.html Use web3.js to interact with a verifier smart contract and check a proof. Ensure the verifier contract address and ABI are correctly set. ```javascript const accounts = await web3.eth.getAccounts(); const address = '0x456...'; // verifier contract address let verifier = new web3.eth.Contract(abi, address, { from: accounts[0], // default from address gasPrice: '20000000000000'; // default gas price in wei }); let result = await verifier.methods .verifyTx(proof.proof, proof.inputs) .call({ from: accounts[0] }); ``` -------------------------------- ### Initialize ZoKrates JS (ES Module) Source: https://zokrates.github.io/toolbox/zokrates_js.html Import the initialize function from the zokrates-js library when using ES modules. ```javascript import { initialize } from "zokrates-js"; ``` -------------------------------- ### Check Solidity Version Source: https://zokrates.github.io/examples/rng_tutorial.html Searches for the Solidity version pragma directive within the 'verifier.sol' file. ```bash grep solidity contracts/verifier.sol ``` -------------------------------- ### Compile and Inspect ZoKrates Program Source: https://zokrates.github.io/examples/rng_tutorial.html Commands to compile the ZoKrates source file and generate a textual representation of the circuit. ```bash zokrates compile -i get_hash.zok -o get_hash && zokrates inspect -i get_hash ``` -------------------------------- ### ZoKrates Nova Prove Command Source: https://zokrates.github.io/toolbox/experimental.html Generate a Nova proof for a compiled ZoKrates program. Initial state and step inputs are provided via JSON files. The proof is saved to proof.json. ```bash echo "\"0\"" > init.json echo "[\"1\", \"7\", \"42\"]" > steps.json zokrates nova prove ``` -------------------------------- ### ZoKrates Nova Continue Prove Command Source: https://zokrates.github.io/toolbox/experimental.html Extend an existing Nova proof by running additional steps. This is useful for iterative computations where the state can be carried over. ```bash zokrates nova prove --continue ``` -------------------------------- ### Initialize Bit Array Source: https://zokrates.github.io/print.html Initializes a mutable array of 512 boolean values to false. ```ZoKrates bool[512] mut preimageBits = [false; 512]; for u32 i in 0..16 { ``` -------------------------------- ### Verify MPC Ceremony Contributions Source: https://zokrates.github.io/toolbox/trusted_setup.html Run this command to verify contributions to the MPC ceremony. Ensure you have the final parameters and circuit files. ```bash $ zokrates mpc verify -i final.params -c circuit -r ./phase1radix2m2 ``` -------------------------------- ### ZoKratesProvider Configuration Source: https://zokrates.github.io/print.html Configure the ZoKratesProvider with specific backend, curve, and proving scheme options. ```APIDOC ## ZoKratesProvider Configuration ### Description Configures the `ZoKratesProvider` with custom options for the backend, elliptic curve, and proving scheme. This method returns a new `ZoKratesProvider` instance with the specified configuration. ### Method `zokratesProvider.withOptions(options: ZoKratesOptions): ZoKratesProvider` ### Endpoint N/A (Client-side JavaScript library) ### Parameters #### Options - **`backend`** (string) - Optional - Backend to use. Options: `ark` | `bellman`. Default: `ark`. - **`curve`** (string) - Optional - Elliptic curve to use. Options: `bn128` | `bls12_381` | `bls12_377` | `bw6_761`. Default: `bn128`. - **`scheme`** (string) - Optional - Proving scheme to use. Options: `g16` | `gm17` | `marlin`. Default: `g16`. ### Request Example ```javascript initialize().then((defaultProvider) => { let zokratesProvider = defaultProvider.withOptions({ backend: "ark", curve: "bls12_381", scheme: "g16", }); // Use the configured zokratesProvider }); ``` ### Returns `ZoKratesProvider` - A new `ZoKratesProvider` instance configured with the provided options. ``` -------------------------------- ### Define and Use Nested Structs Source: https://zokrates.github.io/language/types.html Demonstrates defining generic structs and initializing them within a main function. ```ZoKrates struct Bar { field[N] c; bool d; } struct Foo

{ Bar

a; bool b; } def main() -> Foo<2> { Foo<2>[2] mut f = [Foo { a: Bar { c: [0, 0], d: false }, b: true}, Foo { a: Bar {c: [0, 0], d: false}, b: true }]; f[0].a.c = [42, 43]; return f[0]; } ``` -------------------------------- ### Define Type Aliases Source: https://zokrates.github.io/language/types.html Shows how to create type aliases for existing types and generic types. ```ZoKrates type MyField = field; type Rectangle = bool[L][W]; type Square = Rectangle; def main() { MyField f = 42; Rectangle<2, 2> r = [[true; 2]; 2]; Square<2> s = r; return; } ``` -------------------------------- ### ZoKrates Nova Compile Command Source: https://zokrates.github.io/toolbox/experimental.html Compile a ZoKrates program for the Nova proof system using the Pallas curve. Ensure the program signature matches the required format for Nova. ```bash zokrates compile -i sum.zok --curve pallas ``` -------------------------------- ### Compile and Inspect ZoKrates Program Source: https://zokrates.github.io/print.html Compile a ZoKrates program and inspect its intermediate representation. ```bash __ zokrates compile -i get_hash.zok -o get_hash && zokrates inspect -i get_hash ``` -------------------------------- ### Initialize a Struct Variable Source: https://zokrates.github.io/language/types.html Demonstrates declaring and initializing a struct variable in a single statement. ```ZoKrates struct Point { field x; field y; } def main() -> Point { Point p = Point { x: 1, y: 0 }; return p; } ``` -------------------------------- ### Compile Solidity Contract with Truffle Source: https://zokrates.github.io/examples/rng_tutorial.html Compiles the smart contracts in the Truffle project, including the exported 'verifier.sol'. ```bash truffle compile ``` -------------------------------- ### Proof Formatting Source: https://zokrates.github.io/print.html Formats the proof into an array of field elements compatible with the generated Solidity contract. ```APIDOC ## POST /proof/format ### Description Formats the proof into an array of field elements that are compatible as input to the generated solidity contract. ### Parameters #### Request Body - **proof** (object) - Required - Generated proof ### Response #### Success Response (200) - **elements** (array) - Array of field elements ``` -------------------------------- ### Compile ZoKrates Program Source: https://zokrates.github.io/print.html Compiles the specified ZoKrates file into an arithmetic circuit. ```bash zokrates compile -i hashexample.zok ``` -------------------------------- ### Import SHA256 Library Source: https://zokrates.github.io/examples/rng_tutorial.html Import the SHA256 function from the ZoKrates standard library. ```zok import "hashes/sha256/512bit" as sha256; ``` -------------------------------- ### Compile and Compute Witness Source: https://zokrates.github.io/examples/sha256example.html Commands to compile the circuit and generate a witness for specific input values. ```bash zokrates compile -i hashexample.zok ``` ```bash zokrates compute-witness -a 0 0 0 5 --verbose ``` -------------------------------- ### Relative path imports Source: https://zokrates.github.io/language/imports.html Import resources using relative paths within the file system. ```ZoKrates from "./mycode" import foo; ``` ```ZoKrates from "../../../mycode" import foo; ``` -------------------------------- ### Compute Witness for Hash Program Source: https://zokrates.github.io/examples/rng_tutorial.html Execute the compiled circuit with specific input arguments to generate a witness. ```bash zokrates compute-witness --verbose -i get_hash -a 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ``` -------------------------------- ### Tuple Usage Source: https://zokrates.github.io/language/types.html Demonstrates defining and accessing elements within a tuple. ```ZoKrates def main() -> bool { (field[2], bool) mut v = ([1, 2], true); v.0 = [42, 43]; return v.1; } ``` -------------------------------- ### Verify Zokrates Proof Source: https://zokrates.github.io/examples/rng_tutorial.html Verifies the generated zero-knowledge proof. This command assumes 'proof.json' is in the current directory. ```bash zokrates verify ``` -------------------------------- ### Unsigned Integer Arithmetic and Inference Source: https://zokrates.github.io/language/types.html Demonstrates how decimal literals are inferred based on context and how unsigned integer arithmetic handles overflows. ```ZoKrates def main() { // `255` is inferred to `255f`, and the addition happens between field elements assert(255 + 1f == 256); // `255` is inferred to `255u8`, and the addition happens between u8 // This causes an overflow assert(255 + 1u8 == 0); return; } ``` -------------------------------- ### Define Main Function Signature Source: https://zokrates.github.io/examples/rng_tutorial.html The entry point for the ZoKrates program accepting 16 unsigned 32-bit integers. ```zok def main(u32[16] hashMe) -> u32[8] { ``` -------------------------------- ### Alice's Contribution to MPC Ceremony Source: https://zokrates.github.io/print.html Alice initiates the MPC ceremony by providing her contribution, which includes entropy. This step generates the initial `alice.params` file. ```bash __ $ zokrates mpc contribute -o alice.params -e "alice 1" ``` -------------------------------- ### ZoKrates Program Execution Arguments Source: https://zokrates.github.io/toolbox/abi.html This JSON array shows how to format arguments for executing a ZoKrates program based on its ABI specification. Note the specific formatting for field elements, unsigned integers, and structs. ```json [ { "a":"0x2a", "b":{ "a":"42" } }, [ true, false ], "42" ] ``` -------------------------------- ### ZoKrates Array Slicing Source: https://zokrates.github.io/print.html Demonstrates array slicing in ZoKrates, where a subset of an existing array is copied to create a new array. ```zokrates __ field[3] a = [1, 2, 3]; field[2] b = a[1..3]; // initialize an array copying a slice from `a` ``` -------------------------------- ### Call Generic Function with Explicit Parameters Source: https://zokrates.github.io/language/functions.html Demonstrates calling a generic function, `sum_powers`, by explicitly providing the generic parameter `N` using the `::` syntax. ```zokrates // a function to sum the N first powers of a field element def sum_powers(field a) -> field { field mut res = 0; for u32 i in 0..N { res = res + a ** i; } return res; } def main(field a) -> field { // call `sum_powers` providing the explicit generic parameter `N := 5` return sum_powers::<5>(a); } ``` -------------------------------- ### Initialize Preimage Bits Array Source: https://zokrates.github.io/examples/rng_tutorial.html Initializes a boolean array of 512 elements to false. Zokrates requires variables to be initialized upon declaration. ```zokrates bool[512] mut preimageBits = [false; 512]; ``` -------------------------------- ### Format Proof for Solidity Source: https://zokrates.github.io/toolbox/zokrates_js.html Formats a generated proof into an array of field elements compatible with the generated Solidity contract. ```APIDOC ## POST /format-proof ### Description Formats the proof into an array of field elements that are compatible as input to the generated solidity contract. ### Parameters #### Request Body - **proof** (object) - Required - Generated proof ### Response #### Success Response (200) - **result** (array) - Returns an array of field elements. ``` -------------------------------- ### Verify a Zokrates proof using a smart contract Source: https://zokrates.github.io/examples/rng_tutorial.html Use this snippet to verify a proof against a deployed verifier contract. Ensure the proof parameters match the expected structure of the contract's verifyTx function. ```javascript await contract.verifyTx(proof.proof.a, proof.proof.b, proof.proof.c, cheat) ``` -------------------------------- ### Field Element Arithmetic in ZoKrates Source: https://zokrates.github.io/language/types.html Demonstrates arithmetic with field elements, specifically showing that 0 - 1 equals p - 1 in a finite field, where p is the large prime modulus. This highlights the modular arithmetic behavior. ```zokrates def main() { field pMinusOne = 21888242871839275222246405745257275088548364400416034343698204186575808495616; assert(0 - 1 == pMinusOne); return; } ``` -------------------------------- ### Use generic parameters Source: https://zokrates.github.io/language/control_flow.html Generic parameters must be compile-time constants and can be inferred or provided explicitly. ```ZoKrates def foo() -> field[P] { return [42; P]; } def main() -> field[2] { // `P` is inferred from the declaration of `res`, while `N` is provided explicitly field[2] res = foo::<3, _>(); return res; } ``` -------------------------------- ### Multi-dimensional Array Declaration in ZoKrates Source: https://zokrates.github.io/print.html Demonstrates the syntax for declaring and accessing elements in multi-dimensional arrays (arrays of arrays) in ZoKrates. Shows how to initialize nested arrays and retrieve specific elements. ```zokrates __ def main() -> field { // Array of two elements of array of 3 elements field[2][3] a = [[1, 2, 3],[4, 5, 6]]; field[3] b = a[0]; // should be [1, 2, 3] // allowed access [0..2][0..3] return a[1][2]; } ``` -------------------------------- ### Compute Witness Source: https://zokrates.github.io/print.html Generates a witness file using the provided arguments for the program inputs. ```bash zokrates compute-witness -a 0 0 0 5 --verbose ``` ```bash zokrates compute-witness -a 0 0 0 5 ``` -------------------------------- ### Import multiple symbols Source: https://zokrates.github.io/language/imports.html Import multiple symbols from the same module by separating them with commas. ```ZoKrates from "./path/to/my/module" import MySymbol, MyOtherSymbol; ``` -------------------------------- ### Compile Zokrates Program Source: https://zokrates.github.io/examples/rng_tutorial.html Compiles the Zokrates program 'reveal_bit.zok' and outputs the compiled artifact to 'reveal_bit'. ```bash zokrates compile -i reveal_bit.zok -o reveal_bit ``` -------------------------------- ### Importing Symbols by Module and Name Source: https://zokrates.github.io/print.html Shows the preferred method for importing symbols from other ZoKrates modules using the `from ... import ...` syntax. This allows specific symbols to be brought into the current scope. ```zokrates __ from "./path/to/my/module" import MySymbol; // `MySymbol` is now in scope. ``` -------------------------------- ### Import a single symbol Source: https://zokrates.github.io/language/imports.html Use the from keyword to import a specific symbol from a module path. ```ZoKrates from "./path/to/my/module" import MySymbol; // `MySymbol` is now in scope. ```