Skip to content

Commit

Permalink
Update recursive proof example WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaki committed Apr 5, 2024
1 parent 6823523 commit 6ede541
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 48 deletions.
2 changes: 1 addition & 1 deletion circuits/simple/Nargo.toml → circuits/not_odd/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "simple"
type = "lib"
type = "bin"
authors = [""]
compiler_version = ">=0.26.0"

Expand Down
30 changes: 30 additions & 0 deletions circuits/not_odd/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[recursive]
fn main(n: Field) -> pub u8 {
// not_odd(n);
n as u8
}

// fn main(x: Field, y: Field) -> pub bool {
// not_odd(x) & not_odd(y) & not_equal(x, y);
// }

fn not_equal(x: Field, y: Field) -> bool {
x != y
}

fn not_odd(n: Field) -> bool {
(n as u8) & 1 == 0
}

#[test]
fn test_not_equal() {
assert(not_equal(1, 2));
// Uncomment to make test fail
// assert(not_equal(1, 1));
}

fn test_not_odd() {
assert(not_odd(2));
// Uncomment to make test fail
// assert(not_odd(1));
}
1 change: 0 additions & 1 deletion circuits/recurse/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ authors = [""]
compiler_version = ">=0.26.0"

[dependencies]
simple = { path = "../simple" }
1 change: 0 additions & 1 deletion circuits/recurse/export/main.json

This file was deleted.

1 change: 0 additions & 1 deletion circuits/recurse/export/main_call.json

This file was deleted.

35 changes: 19 additions & 16 deletions circuits/recurse/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use dep::std;
use dep::simple;
// use dep::simple;

#[export]
fn main_call(x: Field, y: Field) -> bool {
let mut res = simple::not_odd(x);
res &= simple::not_odd(y);
res &= simple::not_equal(x, y);
res
}

// #[recursive]
// #[export]
// fn main(x: Field, y: Field) -> pub bool {}
// fn main_call(x: Field, y: Field) -> bool {
// let mut res = simple::not_odd(x);
// res &= simple::not_odd(y);
// res &= simple::not_equal(x, y);
// res
// }

#[test]
fn test_not_equal() {
assert(main_call(2, 4));
// Uncomment to make test fail
// assert(not_equal(1, 1));
#[export]
fn main(x: Field, y: Field) -> pub bool {
// Hash public inputs together
//TODO: take proofs
x != y
}

// #[test]
// fn test_not_equal() {
// assert(main_call(2, 4));
// // Uncomment to make test fail
// // assert(not_equal(1, 1));
// }
1 change: 0 additions & 1 deletion circuits/simple/export/not_equal.json

This file was deleted.

1 change: 0 additions & 1 deletion circuits/simple/export/not_odd.json

This file was deleted.

22 changes: 0 additions & 22 deletions circuits/simple/src/lib.nr

This file was deleted.

38 changes: 35 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import { CompiledCircuit } from '@noir-lang/types';
import { not_equal, not_odd, main_call, main } from './codegen';
import { main } from './codegen';
import { setTimeout } from "timers/promises";

import { Noir } from '@noir-lang/noir_js';
import { BarretenbergBackend } from '@noir-lang/backend_barretenberg';
import { join, resolve } from 'path';
import { compile, createFileManager } from '@noir-lang/noir_wasm';

import { ProofData } from '@noir-lang/types';

async function getCircuit(name: string) {
const basePath = resolve(join('./circuits', name));
const fm = createFileManager(basePath);
const compiled = await compile(fm, basePath);
console.log(compiled.program.abi);
if (!('program' in compiled)) {
throw new Error('Compilation failed');
}
return compiled.program;
}

const input1 = { n: 2 };
const input2 = { n: 4 };

async function start() {
let res = await main_call("02", "04");
console.log(res);
let simpleCircuit: CompiledCircuit = await getCircuit('not_odd');
let simpleBackend: BarretenbergBackend = new BarretenbergBackend(simpleCircuit, { threads: 8 });
let simpleNoir: Noir = new Noir(simpleCircuit, simpleBackend);

const witness1 = (await simpleNoir.execute(input1)).witness;
let proof1: ProofData = await simpleBackend.generateProof(witness1);

const witness2 = (await simpleNoir.execute(input2)).witness;
let proof2: ProofData = await simpleBackend.generateProof(witness2);

// let res = await main(/* intermediate proofs */);
// console.log(res);

simpleBackend.destroy();
}

start();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compile": "yarn clean && tsc",
"start": "node build/main.js",
"clean:codegen": "rm -rf codegen",
"export:simple": "nargo export --program-dir=./circuits/simple",
"export:simple": "nargo export --program-dir=./circuits/not_odd",
"export:recurse": "nargo export --program-dir=./circuits/recurse",
"export:all": "yarn clean:codegen && yarn export:simple && yarn export:recurse",
"codegen": "yarn noir-codegen ./circuits/**/export/*.json",
Expand All @@ -15,6 +15,7 @@
"@noir-lang/backend_barretenberg": "^0.26.0",
"@noir-lang/noir_codegen": "^0.26.0",
"@noir-lang/noir_js": "^0.26.0",
"@noir-lang/noir_wasm": "^0.26.0",
"@types/node": "^20.12.2",
"typescript": "^5.4.3"
}
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
"@noir-lang/noirc_abi" "0.26.0"
"@noir-lang/types" "0.26.0"

"@noir-lang/noir_wasm@^0.26.0":
version "0.26.0"
resolved "https://registry.yarnpkg.com/@noir-lang/noir_wasm/-/noir_wasm-0.26.0.tgz#1c634932f53580022998e6811fee6077c47501d1"
integrity sha512-PmVyd/HE0fSxL6UU3XzCwpf9JyoR2Ql6V8odq3LFndosx5/FDWZtpxe1o7aNLdjebK2l4KmGJPN3eMh9JaiRoQ==
dependencies:
"@noir-lang/types" "0.26.0"
pako "^2.1.0"

"@noir-lang/[email protected]":
version "0.26.0"
resolved "https://registry.yarnpkg.com/@noir-lang/noirc_abi/-/noirc_abi-0.26.0.tgz#5052ab7a55228cc04d77cebf8753ea42636963ff"
Expand Down Expand Up @@ -330,6 +338,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

pako@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/pako/-/pako-2.1.0.tgz#266cc37f98c7d883545d11335c00fbd4062c9a86"
integrity sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==

path-key@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375"
Expand Down

0 comments on commit 6ede541

Please sign in to comment.