# Basic standard PLONK JSON tool

**URL:** <https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60>\
**Category:** Implementation\
**Tags:** dsl\
**Created:** [16 June 2020 14:13 UTC](https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60 "2020-06-16T14:13:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arielgabizon](https://dub1.discourse-cdn.com/flex017/user_avatar/www.plonk.cafe/arielgabizon/32/10_2.png) [@arielgabizon](https://www.plonk.cafe/u/arielgabizon)\
**Post date:** [16 June 2020 14:13 UTC](https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60/1 "2020-06-16T14:13:15Z")

</div>

_Update: Leila Wang from our team made a much more convenient [tool](http://irdemo.aztecprotocol.com/) where you can experiment with what’s written below directly in a browser_

Hey,  
I made this very basic tool for people wanting to experiment with barretenberg easily.  
It only supports what we call “standard PLONK”,  
where you have a single type of add-multiply gate as I will explain (see also [here](https://www.plonk.cafe/t/r1cs-like-representations-of-plonk-programs/46)).

To use it you should build barretenberg as explained in the README; but use [this branch](https://github.com/AztecProtocol/barretenberg/tree/standardplonkjson)).

Now in the `build` directory, you will create a file called `constraintsystem.json`

Here is an example of what to put in it:  
`{"varnum":4,"pubvarnum":0,"constraintnum":1,"constraints":[{"a":1,"b":2,"c":3,"ql":1,"qr":1,"qm":0,"qo":-1,"qc":0}]}`

`varnum` - is the total variable numbers  
`pubvarnum` - is the number of public variables (though this will be ignored for now)  
`constraintnum` - is the total number of constraints.  
`constraints` - is an array describing the constraints, where each constraint has the following form:  
`a,b,c` are integers describing the indices of the witness variables to be used in the constraint.  
Suppose the variables used in the constraint are `x,y,z`.  
The constraint itself has the form  
q\_l\cdot x + q\_r\cdot y + q\_o\cdot z + q\_m\cdot x\cdot y + q\_c =0.  
For the constants `ql,qr,qo,qm,qc` of the constraint.  
Thus in the above example the constraint is x+y=z.

Now in the `build` directory, also create a file `witness.json` of the following form, for example:  
`{"witness":[0,1,2,3]}`

For technical reasons, the vector must always start with a zero, followed by the actual witness values.

Note that this sequence of values indeed satisfies the constraint.

Now to generate a proof and verify it, run from the same directory:  
`./src/aztec/plonk/composer/composer_tests --gtest_filter=standard_composer. prove_and_verify_from_file`

You can try modifying the witness so that the last value is not the sum of the former two, and see if it rejects.

You can make your own examples, but here is one more for the pair of constraints x+y=z, y\cdot z +1=w:

`{"varnum":4,"pubvarnum":0,"constraintnum":2,"constraints":[{"a":1,"b":2,"c":3,"ql":1,"qr":1,"qm":0,"qo":-1,"qc":0},{"a":2,"b":3,"c":4,"ql":0,"qr":0,"qm":1,"qo":-1,"qc":1}]}`

`{"witness":[0,1,2,3,7]}`

## Separate proving and verifying

Suppose you want to write the proof into a file, instead of proving and verifying in one test.  
With the same files `witness.json` and `constraintsystem.json` in the directory, use:

`./src/aztec/plonk/composer/composer_tests --gtest_filter=standard_composer. write_proof_and_keys_to_file`

This will write the proof, proving key and verifying key to the files `proof.data,pk.data,vk.data` respectively.

To verify, you must add a file `PI.json` with public inputs, e.g.  
`{"pubvarnum":1,"PI":[1]}`

And now run

`./src/aztec/plonk/composer/composer_tests --gtest_filter=standard_composer. write_proof_and_keys_to_file`

You are encouraged to try and tinker with the [test code](https://github.com/AztecProtocol/barretenberg/blob/standardplonkjson/barretenberg/src/aztec/plonk/composer/standard_composer.test.cpp).

---

<div class="post-metadata">

**Author:** ![jon-chuang](https://dub1.discourse-cdn.com/flex017/user_avatar/www.plonk.cafe/jon-chuang/32/35_2.png) [@jon-chuang](https://www.plonk.cafe/u/jon-chuang)\
**Post date:** [17 June 2020 08:38 UTC](https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60/2 "2020-06-17T08:38:12Z")

</div>

Hi,

Is the purpose of the json to serialise the circuit representation? I am just thinking that it is rather unwieldy and difficult to read.

I was thinking that standard software abstractions in terms of standard circuit components is much easier to read and reason about.

Hence, my take on this is that it is more productive to define a json representation for gates and gate configs, rather than circuits.

I have been designing such an interface in Rust that can be converted into a json representation.

Here is a preliminary look via an example.

```auto
fn arith_gate() -> Gate {
    gateCfgs = GateConfigRegistry::new()
        .insert("mul".to_string(), [
            ("m", Field::one()),
            ("l", Field::zero()),
            ("r", Field::zero()),
            ("o", -Field::one())
        ])
        .insert("add".to_string(), [
            ("m", Field::zero()),
            ("l", Field::one()),
            ("r", Field::one()),
            ("o", -Field::one()),
        ]);

    monomials = vec![
        GateMonomial{sel: vec!["m"], wit: vec![0, 1]},
        GateMonomial{sel: vec!["l"], wit: vec![0]},
        GateMonomial{sel: vec!["r"], wit: vec![1]},
        GateMonomial{sel: vec!["o"], wit: vec![2]},
    ];

    Gate {
        deg: 3,
        width: 3,
        params: params,
        selectorWires: from_list(["m", "l", "r", "o"]),
        data: monomials,
        cfgRegistry: gateCfgs,
    }
}

fn circuit() {
    let c = PlonkCircuit::new_with_expected_size(1 << 20);
    c.register_gate("arith", std_gates::arith_gate());

    c.add_constraint(ConstraintData {
            gateName: "arith",
            gateConfig: Some("mul"),
            inputs: c.get_inputs(["x", "y", "z"]),
            dynamicConfig: None,
        }
    );
}

```

---

<div class="post-metadata">

**Author:** ![arielgabizon](https://dub1.discourse-cdn.com/flex017/user_avatar/www.plonk.cafe/arielgabizon/32/10_2.png) [@arielgabizon](https://www.plonk.cafe/u/arielgabizon)\
**Post date:** [17 June 2020 12:25 UTC](https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60/3 "2020-06-17T12:25:45Z")

</div>

Hey…first of all, I’m curious, is your interface part of another plonk implementation?  
The json format is not the easiest way to understand the circuit I guess.  
It’s first and main goal is to allow people to use barretenberg without needing to delve into the C++.  
Moreover, this format of constraints with the single add-multiply gate is what the standard plonk prover can “eat”  
So I hope people will write compilers into this format, e.g. from circom, or from your interface

---

<div class="post-metadata">

**Author:** ![jon-chuang](https://dub1.discourse-cdn.com/flex017/user_avatar/www.plonk.cafe/jon-chuang/32/35_2.png) [@jon-chuang](https://www.plonk.cafe/u/jon-chuang)\
**Post date:** [17 June 2020 13:11 UTC](https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60/4 "2020-06-17T13:11:13Z")

</div>

Hi, yes, I am writing a (turbo)plonk implementation for the zexe library. My hope is to add plookup support as well.

By the way, is my understanding correct - that to combine plookup with plonk, one should just use a permutation polynomial to get values lying in the polys a, b, c to agree with some values lying in f, and then perform the plookup argument only for f? Meaning to say, one should separate the plookup wires from non-plookup wires, right? Using multi-table, one only needs one f.

Hmm, I see, I think it’s not a bad idea. But a bit of abstraction can go a long way to make it more pleasant to experiment with.

---

<div class="post-metadata">

**Author:** ![arielgabizon](https://dub1.discourse-cdn.com/flex017/user_avatar/www.plonk.cafe/arielgabizon/32/10_2.png) [@arielgabizon](https://www.plonk.cafe/u/arielgabizon)\
**Post date:** [17 June 2020 13:30 UTC](https://www.plonk.cafe/t/basic-standard-plonk-json-tool/60/5 "2020-06-17T13:30:32Z")

</div>

Hey,  
Full details of our plookup integration are still wip, but your idea is good; in some cases we use something close to what you are saying.

Like I said, I definitely hope people will write high level tools that will compile to this format.
