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

feat: introduce program in noir_js #2886

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ compiler/wasm/nodejs
compiler/wasm/web
tooling/noirc_abi_wasm/nodejs
tooling/noirc_abi_wasm/web
tooling/noir_js/lib
!tooling/noir_js/lib
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "0.11.1",
"private": true,
"workspaces": [
"acvm-repo/acvm_js",
"compiler/wasm",
"compiler/source-resolver",
"tooling/noirc_abi_wasm",
"compiler/integration-tests",
"tooling/noirc_abi_wasm",
"tooling/noir_js_types",
"tooling/noir_js",
"acvm-repo/acvm_js",
"tooling/backend_barretenberg",
"release-tests"
],
"scripts": {
Expand All @@ -33,10 +35,8 @@
"mocha": "^10.2.0",
"prettier": "3.0.3",
"ts-node": "^10.9.1",
"tslog": "^4.9.2",
"typescript": "^5.0.4"
},
"packageManager": "yarn@3.6.3",
"dependencies": {
"tslog": "^4.9.2"
}
}
"packageManager": "yarn@3.6.3"
}
2 changes: 2 additions & 0 deletions tooling/backend_barretenberg/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test/backend/barretenberg.ts
3 changes: 3 additions & 0 deletions tooling/backend_barretenberg/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ["../../.eslintrc.js"],
};
3 changes: 3 additions & 0 deletions tooling/backend_barretenberg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
crs

!test/noir_compiled_examples/*/target
6 changes: 6 additions & 0 deletions tooling/backend_barretenberg/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parser": "typescript",
"printWidth": 120,
"singleQuote": true,
"trailingComma": "all"
}
117 changes: 117 additions & 0 deletions tooling/backend_barretenberg/lib/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Object.defineProperty(exports, "__esModule", { value: true });
exports.BarretenbergBackend = exports.acirToUint8Array = exports.base64Decode = void 0;
// import { Barretenberg, Crs, RawBuffer } from '@aztec/bb.js/dest/browser/types/index.js';
const fflate_1 = require("fflate");
// Since this is a simple function, we can use feature detection to
// see if we are in the nodeJs environment or the browser environment.
function base64Decode(input) {
return Uint8Array.from(atob(input), (c) => c.charCodeAt(0));
}
exports.base64Decode = base64Decode;
// Converts an bytecode to a Uint8Array
function acirToUint8Array(base64EncodedBytecode) {
const compressedByteCode = base64Decode(base64EncodedBytecode);
return (0, fflate_1.decompressSync)(compressedByteCode);
}
exports.acirToUint8Array = acirToUint8Array;
class BarretenbergBackend {
// These type assertions are used so that we don't
// have to initialize `api` and `acirComposer` in the constructor.
// These are initialized asynchronously in the `init` function,
// constructors cannot be asynchronous which is why we do this.
api;
acirComposer;
acirUncompressedBytecode;
constructor(acirCircuit) {
const acirBytecodeBase64 = acirCircuit.bytecode;
this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64);
}
async instantiate() {
const numThreads = 4;
if (!this.api) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const { Barretenberg, RawBuffer, Crs } = await import('@aztec/bb.js');
const api = await Barretenberg.new(numThreads);
const [_exact, _total, subgroupSize] = await api.acirGetCircuitSizes(this.acirUncompressedBytecode);
const crs = await Crs.new(subgroupSize + 1);
await api.commonInitSlabAllocator(subgroupSize);
await api.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
this.acirComposer = await api.acirNewAcirComposer(subgroupSize);
this.api = api;
}
}
// private async initBarretenberg(numThreads: number, acirUncompressedBytecode: Uint8Array) {
// const api = await Barretenberg.new(numThreads);
// const [_exact, _total, subgroupSize] = await api.acirGetCircuitSizes(acirUncompressedBytecode);
// const crs = await Crs.new(subgroupSize + 1);
// await api.commonInitSlabAllocator(subgroupSize);
// await api.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
// const acirComposer = await api.acirNewAcirComposer(subgroupSize);
// return { api: api, composer: acirComposer };
// }
// Generate an outer proof. This is the proof for the circuit which will verify
// inner proofs and or can be seen as the proof created for regular circuits.
//
// The settings for this proof are the same as the settings for a "normal" proof
// ie one that is not in the recursive setting.
async generateProof(decompressedWitness, optimizeForVerifyInCircuit = false) {
await this.instantiate();
const proof = await this.api.acirCreateProof(this.acirComposer, this.acirUncompressedBytecode, decompressedWitness, optimizeForVerifyInCircuit);
return proof;
}
// Generates an inner proof. This is the proof that will be verified
// in another circuit.
//
// This is sometimes referred to as a recursive proof.
// We avoid this terminology as the only property of this proof
// that matters, is the fact that it is easy to verify in another
// circuit. We _could_ choose to verify this proof in the CLI.
//
// We set `makeEasyToVerifyInCircuit` to true, which will tell the backend to
// generate the proof using components that will make the proof
// easier to verify in a circuit.
async generateChildProof(witness) {
const optimizeForVerifyInCircuit = true;
return this.generateProof(witness, optimizeForVerifyInCircuit);
}
// Generates artifacts that will be passed to a circuit that will verify this proof.
//
// Instead of passing the proof and verification key as a byte array, we pass them
// as fields which makes it cheaper to verify in a circuit.
//
// The proof that is passed here will have been created using the `generateInnerProof`
// method.
//
// The number of public inputs denotes how many public inputs are in the inner proof.
async generateChildProofArtifacts(proof, numOfPublicInputs = 0) {
await this.instantiate();
const proofAsFields = await this.api.acirSerializeProofIntoFields(this.acirComposer, proof, numOfPublicInputs);
// TODO: perhaps we should put this in the init function. Need to benchmark
// TODO how long it takes.
await this.api.acirInitVerificationKey(this.acirComposer);
// Note: If you don't init verification key, `acirSerializeVerificationKeyIntoFields`` will just hang on serialization
const vk = await this.api.acirSerializeVerificationKeyIntoFields(this.acirComposer);
return {
proofAsFields: proofAsFields.map((p) => p.toString()),
vkAsFields: vk[0].map((vk) => vk.toString()),
vkHash: vk[1].toString(),
};
}
async verifyChildProof(proof) {
const optimizeForVerifyInCircuit = true;
return this.verifyProof(proof, optimizeForVerifyInCircuit);
}
async verifyProof(proof, optimizeForVerifyInCircuit = false) {
await this.instantiate();
await this.api.acirInitVerificationKey(this.acirComposer);
return await this.api.acirVerifyProof(this.acirComposer, proof, optimizeForVerifyInCircuit);
}
async destroy() {
await this.api.destroy();
}
}
exports.BarretenbergBackend = BarretenbergBackend;
20 changes: 20 additions & 0 deletions tooling/backend_barretenberg/lib/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Backend, CompiledCircuit } from '@noir-lang/types';
export declare function base64Decode(input: string): Uint8Array;
export declare function acirToUint8Array(base64EncodedBytecode: any): Uint8Array;
export declare class BarretenbergBackend implements Backend {
private api;
private acirComposer;
private acirUncompressedBytecode;
constructor(acirCircuit: CompiledCircuit);
instantiate(): Promise<void>;
generateProof(decompressedWitness: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<Uint8Array>;
generateChildProof(witness: Uint8Array): Promise<Uint8Array>;
generateChildProofArtifacts(proof: Uint8Array, numOfPublicInputs?: number): Promise<{
proofAsFields: string[];
vkAsFields: string[];
vkHash: string;
}>;
verifyChildProof(proof: Uint8Array): Promise<boolean>;
verifyProof(proof: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<boolean>;
destroy(): Promise<void>;
}
20 changes: 20 additions & 0 deletions tooling/backend_barretenberg/lib/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Backend, CompiledCircuit } from '@noir-lang/types';
export declare function base64Decode(input: string): Uint8Array;
export declare function acirToUint8Array(base64EncodedBytecode: any): Uint8Array;
export declare class BarretenbergBackend implements Backend {
private api;
private acirComposer;
private acirUncompressedBytecode;
constructor(acirCircuit: CompiledCircuit);
instantiate(): Promise<void>;
generateProof(decompressedWitness: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<Uint8Array>;
generateChildProof(witness: Uint8Array): Promise<Uint8Array>;
generateChildProofArtifacts(proof: Uint8Array, numOfPublicInputs?: number): Promise<{
proofAsFields: string[];
vkAsFields: string[];
vkHash: string;
}>;
verifyChildProof(proof: Uint8Array): Promise<boolean>;
verifyProof(proof: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<boolean>;
destroy(): Promise<void>;
}
111 changes: 111 additions & 0 deletions tooling/backend_barretenberg/lib/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// import { Barretenberg, Crs, RawBuffer } from '@aztec/bb.js/dest/browser/types/index.js';
import { decompressSync as gunzip } from 'fflate';
// Since this is a simple function, we can use feature detection to
// see if we are in the nodeJs environment or the browser environment.
export function base64Decode(input) {
return Uint8Array.from(atob(input), (c) => c.charCodeAt(0));
}
// Converts an bytecode to a Uint8Array
export function acirToUint8Array(base64EncodedBytecode) {
const compressedByteCode = base64Decode(base64EncodedBytecode);
return gunzip(compressedByteCode);
}
export class BarretenbergBackend {
// These type assertions are used so that we don't
// have to initialize `api` and `acirComposer` in the constructor.
// These are initialized asynchronously in the `init` function,
// constructors cannot be asynchronous which is why we do this.
api;
acirComposer;
acirUncompressedBytecode;
constructor(acirCircuit) {
const acirBytecodeBase64 = acirCircuit.bytecode;
this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64);
}
async instantiate() {
const numThreads = 4;
if (!this.api) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const { Barretenberg, RawBuffer, Crs } = await import('@aztec/bb.js');
const api = await Barretenberg.new(numThreads);
const [_exact, _total, subgroupSize] = await api.acirGetCircuitSizes(this.acirUncompressedBytecode);
const crs = await Crs.new(subgroupSize + 1);
await api.commonInitSlabAllocator(subgroupSize);
await api.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
this.acirComposer = await api.acirNewAcirComposer(subgroupSize);
this.api = api;
}
}
// private async initBarretenberg(numThreads: number, acirUncompressedBytecode: Uint8Array) {
// const api = await Barretenberg.new(numThreads);
// const [_exact, _total, subgroupSize] = await api.acirGetCircuitSizes(acirUncompressedBytecode);
// const crs = await Crs.new(subgroupSize + 1);
// await api.commonInitSlabAllocator(subgroupSize);
// await api.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
// const acirComposer = await api.acirNewAcirComposer(subgroupSize);
// return { api: api, composer: acirComposer };
// }
// Generate an outer proof. This is the proof for the circuit which will verify
// inner proofs and or can be seen as the proof created for regular circuits.
//
// The settings for this proof are the same as the settings for a "normal" proof
// ie one that is not in the recursive setting.
async generateProof(decompressedWitness, optimizeForVerifyInCircuit = false) {
await this.instantiate();
const proof = await this.api.acirCreateProof(this.acirComposer, this.acirUncompressedBytecode, decompressedWitness, optimizeForVerifyInCircuit);
return proof;
}
// Generates an inner proof. This is the proof that will be verified
// in another circuit.
//
// This is sometimes referred to as a recursive proof.
// We avoid this terminology as the only property of this proof
// that matters, is the fact that it is easy to verify in another
// circuit. We _could_ choose to verify this proof in the CLI.
//
// We set `makeEasyToVerifyInCircuit` to true, which will tell the backend to
// generate the proof using components that will make the proof
// easier to verify in a circuit.
async generateChildProof(witness) {
const optimizeForVerifyInCircuit = true;
return this.generateProof(witness, optimizeForVerifyInCircuit);
}
// Generates artifacts that will be passed to a circuit that will verify this proof.
//
// Instead of passing the proof and verification key as a byte array, we pass them
// as fields which makes it cheaper to verify in a circuit.
//
// The proof that is passed here will have been created using the `generateInnerProof`
// method.
//
// The number of public inputs denotes how many public inputs are in the inner proof.
async generateChildProofArtifacts(proof, numOfPublicInputs = 0) {
await this.instantiate();
const proofAsFields = await this.api.acirSerializeProofIntoFields(this.acirComposer, proof, numOfPublicInputs);
// TODO: perhaps we should put this in the init function. Need to benchmark
// TODO how long it takes.
await this.api.acirInitVerificationKey(this.acirComposer);
// Note: If you don't init verification key, `acirSerializeVerificationKeyIntoFields`` will just hang on serialization
const vk = await this.api.acirSerializeVerificationKeyIntoFields(this.acirComposer);
return {
proofAsFields: proofAsFields.map((p) => p.toString()),
vkAsFields: vk[0].map((vk) => vk.toString()),
vkHash: vk[1].toString(),
};
}
async verifyChildProof(proof) {
const optimizeForVerifyInCircuit = true;
return this.verifyProof(proof, optimizeForVerifyInCircuit);
}
async verifyProof(proof, optimizeForVerifyInCircuit = false) {
await this.instantiate();
await this.api.acirInitVerificationKey(this.acirComposer);
return await this.api.acirVerifyProof(this.acirComposer, proof, optimizeForVerifyInCircuit);
}
async destroy() {
await this.api.destroy();
}
}
39 changes: 39 additions & 0 deletions tooling/backend_barretenberg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@noir-lang/backend_barretenberg",
"collaborators": [
"The Noir Team <team@noir-lang.org>"
],
"version": "0.7.10",
"packageManager": "yarn@3.5.1",
"license": "(MIT OR Apache-2.0)",
"type": "module",
"source": "src/index.ts",
"main": "lib/cjs/index.cjs",
"module": "lib/esm/index.js",
"exports": {
"require": "./lib/cjs/index.cjs",
"default": "./lib/esm/index.js",
"types": "./lib/esm/index.d.ts"
},
"types": "lib/esm/index.d.ts",
"scripts": {
"dev": "tsc --watch",
"build": "yarn clean && tsc && tsc -p ./tsconfig.cjs.json && mv ./lib/cjs/index.js ./lib/cjs/index.cjs",
"clean": "rm -r ./lib",
"prettier": "prettier 'src/**/*.ts'",
"prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
},
"dependencies": {
"@aztec/bb.js": "0.7.10",
"@noir-lang/types": "workspace:*"
},
"devDependencies": {
"@types/node": "^20.6.2",
"@types/prettier": "^3",
"eslint": "^8.40.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "3.0.3",
"typescript": "5.1.5"
}
}
Loading