Skip to content

Commit

Permalink
Merge pull request #86 from noir-lang/jc/update-to-31
Browse files Browse the repository at this point in the history
Jc/update to 31
  • Loading branch information
critesjosh authored Aug 1, 2024
2 parents c4e799c + 408614b commit 3533cdc
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 154 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/with_foundry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@ jobs:
- name: Set up foundry
uses: ./.github/actions/setup-foundry

- name: Install bb
run: |
curl -L https://raw.githubusercontent.com/AztecProtocol/aztec-packages/master/barretenberg/cpp/installation/install | bash
echo "PATH=$PATH:/home/runner/.bb" >> $GITHUB_ENV
shell: bash

- name: Use bbup
run: |
bbup -v 0.41.0
bb --version # or any other bbup command you want to run
shell: bash

- name: Generate verifier contract
run: |
nargo codegen-verifier
nargo compile && bb write_vk -b ./target/with_foundry.json && bb contract
working-directory: with-foundry/circuits

- name: Generate proof
run: |
nargo prove
nargo execute witness
bb prove -b ./target/with_foundry.json -w ./target/witness.gz -o ./target/with_foundry.proof
working-directory: with-foundry/circuits

- name: Test with Foundry
Expand Down
125 changes: 0 additions & 125 deletions .github/workflows/with_foundry_nightly.yaml

This file was deleted.

32 changes: 25 additions & 7 deletions with-foundry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,27 @@ Install [noirup](https://noir-lang.org/docs/getting_started/installation/#instal
noirup
```

3. Install foundryup and follow the instructions on screen. You should then have all the foundry tools like `forge`, `cast`, `anvil` and `chisel`.
3. Install foundryup and follow the instructions on screen. You should then have all the foundry
tools like `forge`, `cast`, `anvil` and `chisel`.

```bash
curl -L https://foundry.paradigm.xyz | bash
```

4. Install the correct version of the
[Barretenberg](https://github.com/AztecProtocol/aztec-packages/tree/master/barretenberg/cpp/src/barretenberg/bb#version-compatibility-with-noir)
proving backend for Noir (bb).

```bash
curl -L https://raw.githubusercontent.com/AztecProtocol/aztec-packages/master/barretenberg/cpp/installation/install | bash
```

then

```bash
bbup -v 0.41.0 # compatible with nargo 0.31.0
```

## Generate verifier contract and proof

### Contract
Expand All @@ -38,21 +53,24 @@ The deployment assumes a verifier contract has been generated by nargo. In order

```bash
cd circuits
nargo codegen-verifier
nargo compile
bb write_vk -b ./target/with_foundry.json
bb contract
```

A file named `plonk_vk.sol` should appear in the `circuits/contracts/with_foundry` folder.
A file named `contract.sol` should appear in the `circuits/target` folder.

#### Proof

You also need a proof, as this template currently doesn't employ `ffi` to call `nargo prove` by
itself. For this, ensure your prover parameters are correct in `Prover.toml` and run:
You also need a proof, as this template currently doesn't employ `ffi` to call `nargo execute` (to
generate the witness) and `bb prove` by itself. For this, ensure your prover parameters are correct
in `Prover.toml` and run:

```bash
nargo prove
bb prove -b ./target/with_foundry.json -w ./target/witness.gz -o ./target/with_foundry.proof
```

A file named `with_foundry.proof` should appear in the `./circuits/proofs` folder.
A file named `with_foundry.proof` should appear in the `./circuits/target` folder.

### Test with Foundry

Expand Down
2 changes: 1 addition & 1 deletion with-foundry/circuits/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
name="with_foundry"
type="bin"
authors = ["critesjosh"]
compiler_version = ">=0.27.0"
compiler_version = ">=0.31.0"

[dependencies]
2 changes: 1 addition & 1 deletion with-foundry/contract/Starter.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity ^0.8.17;

import "../circuits/contract/with_foundry/plonk_vk.sol";
import "../circuits/target/contract.sol";

contract Starter {
UltraVerifier public verifier;
Expand Down
2 changes: 1 addition & 1 deletion with-foundry/script/Starter.s.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.17;

import "forge-std/Script.sol";
import "../circuits/contract/with_foundry/plonk_vk.sol";
import "../circuits/target/contract.sol";
import "../contract/Starter.sol";

contract StarterScript is Script {
Expand Down
2 changes: 1 addition & 1 deletion with-foundry/script/Verify.s.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.17;

import "forge-std/Script.sol";
import "../circuits/contract/with_foundry/plonk_vk.sol";
import "../circuits/target/contract.sol";
import "../contract/Starter.sol";

contract VerifyScript is Script {
Expand Down
5 changes: 2 additions & 3 deletions with-foundry/script/prove.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash
if [ "$#" -ne 1 ]
then
if [ "$#" -ne 1 ]; then
echo "Usage: ./prove.sh [TESTNAME_STRING]"
exit 1
fi
cd /tmp/$1 && nargo prove && echo "Proof Generated"
cd /tmp/$1 && nargo execute witness && bb prove -b ./target/with_foundry.json -w ./target/witness.gz -o ./target/with_foundry.proof && echo "Proof Generated"
41 changes: 28 additions & 13 deletions with-foundry/test/Starter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ pragma solidity ^0.8.17;

import "forge-std/Test.sol";
import "../contract/Starter.sol";
import "../circuits/contract/with_foundry/plonk_vk.sol";
import "../circuits/target/contract.sol";
import "forge-std/console.sol";

contract StarterTest is Test {
Starter public starter;
Expand All @@ -21,17 +22,17 @@ contract StarterTest is Test {
wrong[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000004);
}

function testVerifyProof() public {
string memory proof = vm.readLine("./circuits/proofs/with_foundry.proof");
bytes memory proofBytes = vm.parseBytes(proof);
starter.verifyEqual(proofBytes, correct);
function testVerifyProof() public view {
bytes memory proof_w_inputs = vm.readFileBinary("./circuits/target/with_foundry.proof");
bytes memory last = sliceAfter64Bytes(proof_w_inputs);
starter.verifyEqual(last, correct);
}

function test_wrongProof() public {
vm.expectRevert();
string memory proof = vm.readLine("./circuits/proofs/with_foundry.proof");
bytes memory proofBytes = vm.parseBytes(proof);
starter.verifyEqual(proofBytes, wrong);
bytes memory proof_w_inputs = vm.readFileBinary("./circuits/target/with_foundry.proof");
bytes memory proof = sliceAfter64Bytes(proof_w_inputs);
starter.verifyEqual(proof, wrong);
}

function test_dynamicProof() public {
Expand All @@ -47,7 +48,8 @@ contract StarterTest is Test {
dynamicCorrect[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000005);
dynamicCorrect[1] = dynamicCorrect[0];
bytes memory proofBytes = generateDynamicProof("test1", _fieldNames, _fieldValues);
starter.verifyEqual(proofBytes, dynamicCorrect);
bytes memory proof = sliceAfter64Bytes(proofBytes);
starter.verifyEqual(proof, dynamicCorrect);
}

function test_dynamicProofSecondTest() public {
Expand All @@ -63,7 +65,8 @@ contract StarterTest is Test {
dynamicCorrect[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000008);
dynamicCorrect[1] = dynamicCorrect[0];
bytes memory proofBytes = generateDynamicProof("test2", _fieldNames, _fieldValues);
starter.verifyEqual(proofBytes, dynamicCorrect);
bytes memory proof = sliceAfter64Bytes(proofBytes);
starter.verifyEqual(proof, dynamicCorrect);
}

function test_dynamicProofThirdTest() public {
Expand All @@ -79,7 +82,8 @@ contract StarterTest is Test {
dynamicCorrect[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000007);
dynamicCorrect[1] = dynamicCorrect[0];
bytes memory proofBytes = generateDynamicProof("test3", _fieldNames, _fieldValues);
starter.verifyEqual(proofBytes, dynamicCorrect);
bytes memory proof = sliceAfter64Bytes(proofBytes);
starter.verifyEqual(proof, dynamicCorrect);
}

/// @dev This function generates dynamic proofs using 2 scripts in the /script directory
Expand Down Expand Up @@ -112,7 +116,18 @@ contract StarterTest is Test {
ffi_command[1] = _testName;
bytes memory commandResponse = vm.ffi(ffi_command);
console.log(string(commandResponse));
string memory _newProof = vm.readLine(string.concat("/tmp/", _testName, "/proofs/with_foundry.proof"));
return vm.parseBytes(_newProof);
bytes memory _newProof = vm.readFileBinary(string.concat("/tmp/", _testName, "/target/with_foundry.proof"));
return _newProof;
}

// Utility function, because the proof file includes the public inputs at the beginning
function sliceAfter64Bytes(bytes memory data) internal pure returns (bytes memory) {
uint256 length = data.length - 64;
bytes memory result = new bytes(data.length - 64);
for (uint i = 0; i < length; i++) {
result[i] = data[i + 64];
}
return result;
}

}

0 comments on commit 3533cdc

Please sign in to comment.