namnc / circom-2-arithc

Circom interpreter to arithmetic circuit description
MIT License
33 stars 7 forks source link

feat: circuit evaluation #51

Closed brech1 closed 3 weeks ago

brech1 commented 1 month ago

Description

This PR introduces:

Evaluation Example

Sample evaluation of the following circuit:

pragma circom 2.0.0;

// Two element sum
template sum () {
    signal input a;
    signal input b;
    signal output out;

    out <== a + b;
}

component main = sum();
  1. Compile the circuit.
{
  "node_count": 5,
  "signals": {
    "1": { "name": "0.b", "value": null },
    "3": { "name": "0.random_3700531429", "value": null },
    "2": { "name": "0.out", "value": null },
    "0": { "name": "0.a", "value": null }
  },
  "nodes": {
    "1": { "is_const": false, "is_out": false, "signals": [0] },
    "2": { "is_const": false, "is_out": false, "signals": [1] },
    "5": { "is_const": false, "is_out": true, "signals": [3, 2] }
  },
  "gates": [{ "op": "AAdd", "lh_in": 1, "rh_in": 2, "out": 5 }]
}
  1. Generate the circuit report. This is necessary to know the circuit input and output signals and their names.
{
  "inputs": [
    { "id": 1, "names": ["0.a"], "value": null },
    { "id": 2, "names": ["0.b"], "value": null }
  ],
  "outputs": [{ "id": 5, "names": ["0.out"], "value": null }]
}
  1. Evaluate with a set of inputs. Below is the whole process.
use circom_2_arithc::{circom::input::Input, program::build_circuit};

fn evaluate_circuit() -> Result<(), ProgramError> {
    // Set the input file path
    let input = Input::new("./sum.circom".into(), "./".into()).unwrap();

    // Build the arithmetic circuit
    let circuit = build_circuit(&input)?;

    // Generate the circuit report
    let report = circuit.generate_circuit_report()?;

    // Build the MPZ circuit
    let mpz_circuit = circuit.build_mpz_circuit(&report)?;

    // Load circuit inputs
    let circuit_input: Vec<u64> = vec![1, 2];

    // Evaluate
    let result: Vec<u32> = mpz_circuit.evaluate(&circuit_input).unwrap(); // [3]

    Ok(())
}

Related Issues