Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm committed Feb 26, 2023
1 parent ecf4046 commit 01eed9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,33 @@ Basic Noir anonymous proof of membership
## Requirements

- Noir is based upon [Rust](https://www.rust-lang.org/tools/install), and we will need to Noir's package manager `nargo` in order to compile our circuits. Further installation instructions for can be found [here](https://noir-lang.github.io/book/getting_started/install.html).
- If there are troubles installing `nargo` due to the C++ backend, replace the `aztec_backend` dependency in the `nargo` crate's `Cargo.toml` with this line:
```
aztec_backend = { optional = true, git = "https://github.com/noir-lang/aztec_backend", rev = "d91c69f2137777cec37f692f98d075ae10e7a584", default-features = false, features = [
"wasm-base",
] }
```
- The typescript tests and contracts live within a hardhat project, where we use [yarn](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable) as the package manager.

## Development

There are two projects under this repository. One is a more basic private transfer application that is suited for applications such as private airdrops or private lotteries. The second is a full private transfer application that interacts with a merkle tree in Solidity. This walkthrough will just follow the full private transfer application. Feel free to replace the respective commands to test the full basic private transfer.

Start by installing all the packages specified in the `package.json`

```shell
yarn install
```

After installing nargo it should be placed in our path and can be called from inside the `circuits` folder. We will then compile our circuit. This will generate an intermediate representation that is called the ACIR. More infomration on this can be found [here](https://noir-lang.github.io/book/acir.html). `p` in `nargo compile p` is simply the name of the ACIR and witness files generated by Noir when compiling the circuit. These will be used by the tests.
After installing nargo it should be placed in our path and can be called from inside the `circuits` folder. We will then compile our circuit. This will generate an intermediate representation that is called the ACIR. More infomration on this can be found [here](https://noir-lang.github.io/book/acir.html). `p` in `nargo compile p` is simply the name of the ACIR and witness files generated by Noir when compiling the circuit. These will be used by the tests.

```shell
cd circuits/
cd circuits/mimc_tree
nargo compile p
```

Note that compilation requires specifying inputs in `Prover.toml` that satisfy the specified circuit constraints. This toml file has been filled based on calculations done previously inside of the test files. This example aims to show different strategies for proving and verifying with Noir.
You will also notice inputs in `Prover.toml` that satisfy the specified circuit constraints. This toml file has been filled based on calculations done previously inside of the test files. You can run `nargo prove p` followed by `nargo verify p` to prove and verify a circuit natively. More info on the `nargo` commands can be found in the Noir documentation linked above.

This example aims to show different strategies for proving and verifying zk proofs with Noir. The rest of this README will describe how to prove and verify Noir circuits in typescript.

We use these three packages to interact with the ACIR. `@noir-lang/noir_wasm` to serialize the ACIR from file.
We use these three packages to interact with the ACIR. `@noir-lang/noir_wasm` lets us serialize the ACIR from file.
```
let acirByteArray = path_to_uint8array(path.resolve(__dirname, '../circuits/build/p.acir'));
let acir = acir_from_bytes(acirByteArray);
let acir = acir_read_bytes(acirByteArray);
```

It is also possible to instead compile the program in Typescript. This can be seen inside the test file. Using this strategy would allow a developer to avoid use of the `nargo compile` command.
Expand All @@ -48,16 +46,12 @@ These values in the `abi` are all calculated inside the test file for each test,
```
let abi = {
recipient: recipient,
priv_key: `0x` + sender_priv_key.toString('hex'),
priv_key: `0x` + transfers[0].sender_priv_key.toString('hex'),
note_root: `0x` + note_root,
index: 0,
note_hash_path: [
`0x` + note_hash_path[0],
`0x` + note_hash_path[1],
`0x` + note_hash_path[2],
],
note_hash_path: generateHashPathInput(note_hash_path),
secret: `0x` + transfers[0].secret.toString('hex'),
return: [`0x` + transfers[0].nullifier.toString('hex'), recipient]
nullifierHash: `0x` + transfers[0].nullifier.toString('hex'),
};
```

Expand All @@ -84,11 +78,15 @@ In the `scripts` folder you will find a script for compiling a program and gener
npx ts-node ./scripts/generate_sol_verifier.ts
```

This Solidity verifier can also be generated by calling `nargo contract`. This will produce a Solidity verifier inside of the Noir project which you can then move into the correct `contracts` folder.

It is important to note that if you change your circuit and want to verify proofs in Solidity you must also regenerate your Solidity verifier. The verifier is based upon the circuit and if the circuit changes any previous verifier will be out of date.

### Running tests

The tests show both the method of compiling the circuit using `nargo` and in Typescript. The tests also show how to complete proof verification using Typescript as well as with the Solidity verifier. Thus, to have all tests pass, it is necessary you follow all the commands listed or change the tests to your preferred method of compilation and/or proof verification.

This command will compile the Solidity verifier within the `contracts` folder and run all tests inside `./test/PrivateTransfer.test.ts`.
This command will compile the Solidity verifier within the `contracts` folder and run all tests inside `./test/PrivateTransfer-full.test.ts`.
```
npx hardhat test
npx hardhat test ./test/PrivateTransfer-full.test.ts
```
1 change: 0 additions & 1 deletion test/PrivateTransfer-basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe("Noir circuit verifies succesfully using Typescript", () => {
it("Simple shield works for merkle tree insert, compiled using nargo", async () => {
let merkleProof = tree.proof(0);
let note_hash_path = merkleProof.pathElements
// console.log(transfers[0].nullifier.toString('hex'));

let abi = {
recipient: recipient,
Expand Down

0 comments on commit 01eed9d

Please sign in to comment.