Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(noir-js): Add program API #2856

Merged
merged 41 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c9f1ea9
move backend file to src
kevaundray Sep 26, 2023
61273f6
add simple program.ts
kevaundray Sep 26, 2023
5e3633f
add an example test of using program API
kevaundray Sep 26, 2023
7f27be4
allow ts-ignore and take in compiledProgram as input
kevaundray Sep 26, 2023
8247d6c
allow any for types
kevaundray Sep 26, 2023
3c776c1
backed now takes the whole json file
kevaundray Sep 26, 2023
004b2bc
make init and the constructor private so that users are forced to use…
kevaundray Sep 26, 2023
2567498
refactor backend initialization
kevaundray Sep 26, 2023
6ac1a46
any lint
kevaundray Sep 26, 2023
67cf623
Update tooling/noir_js/src/backend/barretenberg.ts
kevaundray Sep 26, 2023
2915418
Update tooling/noir_js/src/program.ts
kevaundray Sep 26, 2023
3fd561c
type initialize
kevaundray Sep 26, 2023
1a0a052
linter
kevaundray Sep 26, 2023
4a04b44
remove browser specific import
kevaundray Sep 26, 2023
9c69369
add tsconfig files and fixup script
kevaundray Sep 26, 2023
88cc5f2
modify build to use plain tsc to build the package
kevaundray Sep 26, 2023
3cd3d06
REVERT: USING THIS TO MAKE IT BUILD WITH NODENEXT
kevaundray Sep 26, 2023
53601c5
update yarn.lock
kevaundray Sep 26, 2023
3ced25a
Merge remote-tracking branch 'origin/master' into kw/noir-js-more
kevaundray Sep 27, 2023
478df32
update yarn.lock
kevaundray Sep 27, 2023
b4b0876
use esnext for import json syntax
kevaundray Sep 27, 2023
866e197
remove extra field from tsconfig base
kevaundray Sep 27, 2023
bd06a2a
add require to mocha cjs file
kevaundray Sep 27, 2023
e5f13f8
make mjs file slimmer
kevaundray Sep 27, 2023
64da835
set tsconfig for tests and add back type module
kevaundray Sep 27, 2023
7fa87eb
Merge remote-tracking branch 'origin/master' into kw/noir-js-more
kevaundray Sep 27, 2023
ef9035e
try
kevaundray Sep 27, 2023
c7e5f8f
Merge branch 'master' into kw/noir-js-more
TomAFrench Sep 28, 2023
686c3c4
revert cjs configs
kevaundray Sep 29, 2023
5fdc967
remove cjs fixup script
kevaundray Sep 29, 2023
3bc0fca
add back ts-multi and move backend folder back to test
kevaundray Sep 29, 2023
fb690b2
revert version
kevaundray Sep 29, 2023
4b73b9e
revert import url
kevaundray Sep 29, 2023
a8eaa42
revert file rename
kevaundray Sep 29, 2023
bcb4b4d
revert file changes
kevaundray Sep 29, 2023
d92f377
revert
kevaundray Sep 29, 2023
eb56535
update yarn.lock
kevaundray Sep 29, 2023
32c3b1e
Merge remote-tracking branch 'origin/master' into kw/noir-js-more
kevaundray Sep 29, 2023
decdd38
move backend interface to src/backend
kevaundray Sep 29, 2023
36111f5
rename class to Noir from Program
kevaundray Sep 29, 2023
2695928
make default the last export
kevaundray Sep 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tooling/noir_js/fixup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Put these package.json files in the cjs and
# mjs directory respectively, so that
# tools can recognise that the .js files are either
# commonjs or ESM files.
cat >lib/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

cat >lib/mjs/package.json <<!EOF
{
"type": "module"
}
!EOF
21 changes: 11 additions & 10 deletions tooling/noir_js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"version": "0.13.0",
"packageManager": "yarn@3.5.1",
"license": "(MIT OR Apache-2.0)",
"type": "module",
"dependencies": {
"@noir-lang/acvm_js": "workspace:*",
"@noir-lang/noirc_abi": "workspace:*",
Expand All @@ -17,17 +16,20 @@
"package.json"
],
"source": "src/index.ts",
"main": "lib/index.cjs",
"module": "lib/index.js",
"main": "lib/cjs/index.js",
"module": "lib/mjs/index.js",
"exports": {
"require": "./lib/index.cjs",
"default": "./lib/index.js",
"types": "./lib/index.d.ts"
"require": "./lib/cjs/index.js",
"default": "./lib/mjs/index.js",
"types": "./lib/types/index.d.ts"
},
"types": "lib/index.d.ts",
"types": "lib/types/index.d.ts",
"scripts": {
"dev": "tsc-multi --watch",
"build": "tsc-multi",
"dev": "tsc --watch",
"build": "yarn run build:tsc:cjs && yarn run build:tsc:mjs && yarn run build:types && ./fixup.sh",
"build:tsc:cjs": "tsc -p tsconfig.cjs.json",
"build:tsc:mjs": "tsc -p tsconfig.mjs.json",
"build:types": "tsc -p tsconfig.types.json",
"test": "yarn test:node:esm && yarn test:node:cjs",
"test:node:esm": "mocha --timeout 25000 --exit --config ./.mocharc.json",
"test:node:cjs": "mocha --timeout 25000 --exit --config ./.mocharc.cjs.json",
Expand All @@ -47,7 +49,6 @@
"mocha": "^10.2.0",
"prettier": "3.0.3",
"ts-node": "^10.9.1",
"tsc-multi": "^1.1.0",
"typescript": "^5.2.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { Barretenberg, Crs, RawBuffer } from '@aztec/bb.js';
// TODO: This should be re-exported from @aztec/bb-js
import { Ptr } from '@aztec/bb.js/dest/browser/types';
import { acirToUint8Array } from '../../src/index.js';
import { Backend } from './backend_interface.js';

Expand All @@ -11,14 +11,21 @@ export class BarretenbergBackend implements Backend {
// These are initialized asynchronously in the `init` function,
// constructors cannot be asynchronous which is why we do this.
api = {} as Barretenberg;
acirComposer = {} as Ptr;
acirComposer = {} as any;
acirUncompressedBytecode: Uint8Array;

constructor(acirBytecodeBase64: string) {
private constructor(acirCircuit: { bytecode: string }) {
const acirBytecodeBase64 = acirCircuit.bytecode;
this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64);
}

async init(): Promise<void> {
static async initialize(acirCircuit: { bytecode: string }): Promise<BarretenbergBackend> {
const backend = new BarretenbergBackend(acirCircuit);
await backend.init();
return backend;
}

private async init(): Promise<void> {
const numThreads = 4;

const { api, composer } = await this.initBarretenberg(numThreads, this.acirUncompressedBytecode);
Expand Down
20 changes: 20 additions & 0 deletions tooling/noir_js/src/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Backend } from './backend/backend_interface.js';
import { generateWitness } from './witness_generation.js';

export class Program {
constructor(
private circuit: { bytecode: string; abi: any },
private backend: Backend,
) {}

// Initial inputs to your program
async generateFinalProof(inputs: any): Promise<Uint8Array> {
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
const serializedWitness = await generateWitness(this.circuit, inputs);
return this.backend.generateFinalProof(serializedWitness);
}

async verifyFinalProof(proof: Uint8Array): Promise<boolean> {
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
return this.backend.verifyFinalProof(proof);
}
}
37 changes: 26 additions & 11 deletions tooling/noir_js/test/node/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import assert_lt_json from '../noir_compiled_examples/assert_lt/target/assert_lt.json' assert { type: 'json' };
import { generateWitness } from '../../src/index.js';
import { BarretenbergBackend as Backend } from '../backend/barretenberg.js';
import { Program } from '../../src/program.js';
import { BarretenbergBackend as Backend } from '../../src/backend/barretenberg.js';

it('end-to-end proof creation and verification (outer)', async () => {
// Noir.Js part
Expand All @@ -14,15 +15,33 @@ it('end-to-end proof creation and verification (outer)', async () => {
// bb.js part
//
// Proof creation
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const prover = await Backend.initialize(assert_lt_json);
const proof = await prover.generateFinalProof(serializedWitness);

// Proof verification
const isValid = await prover.verifyFinalProof(proof);
expect(isValid).to.be.true;
});

it('end-to-end proof creation and verification (outer) -- Program API', async () => {
// Noir.Js part
const inputs = {
x: '2',
y: '3',
};

// Initialize backend
const backend = await Backend.initialize(assert_lt_json);
// Initialize program
const program = new Program(assert_lt_json, backend);
// Generate proof
const proof = await program.generateFinalProof(inputs);

// Proof verification
const isValid = await program.verifyFinalProof(proof);
expect(isValid).to.be.true;
});

it('end-to-end proof creation and verification (inner)', async () => {
// Noir.Js part
const inputs = {
Expand All @@ -34,8 +53,7 @@ it('end-to-end proof creation and verification (inner)', async () => {
// bb.js part
//
// Proof creation
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const prover = await Backend.initialize(assert_lt_json);
const proof = await prover.generateIntermediateProof(serializedWitness);

// Proof verification
Expand Down Expand Up @@ -64,14 +82,12 @@ it('[BUG] -- bb.js null function or function signature mismatch (different insta
const serializedWitness = await generateWitness(assert_lt_json, inputs);

// bb.js part
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const prover = await Backend.initialize(assert_lt_json);

const proof = await prover.generateFinalProof(serializedWitness);

try {
const verifier = new Backend(assert_lt_json.bytecode);
await verifier.init();
const verifier = await Backend.initialize(assert_lt_json);
await verifier.verifyFinalProof(proof);
expect.fail(
'bb.js currently returns a bug when we try to verify a proof with a different Barretenberg instance that created it.',
Expand Down Expand Up @@ -101,8 +117,7 @@ it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ',
//
// Proof creation
//
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const prover = await Backend.initialize(assert_lt_json);
// Create a proof using both proving systems, the majority of the time
// one would only use outer proofs.
const proofOuter = await prover.generateFinalProof(serializedWitness);
Expand Down
7 changes: 0 additions & 7 deletions tooling/noir_js/tsc-multi.json

This file was deleted.

26 changes: 26 additions & 0 deletions tooling/noir_js/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"allowJs": true,
"emitDeclarationOnly": false,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"esModuleInterop": true,
"inlineSourceMap": false,
"lib": ["esnext"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
"rootDir": "src",
"skipLibCheck": true,
"strict": true,
"traceResolution": false,
"types": ["node"]
},
"compileOnSave": false,
"exclude": ["node_modules", "lib"],
"include": ["src/**/*.ts"]
}
9 changes: 9 additions & 0 deletions tooling/noir_js/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
"outDir": "./lib/cjs",
"target": "es2020"
}
}
16 changes: 0 additions & 16 deletions tooling/noir_js/tsconfig.json

This file was deleted.

8 changes: 8 additions & 0 deletions tooling/noir_js/tsconfig.mjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"outDir": "./lib/mjs"
}
}
10 changes: 10 additions & 0 deletions tooling/noir_js/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"declarationDir": "./lib/types",
"outDir": "./lib/types",
"target": "es2020"
}
}
Loading