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 all 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
2 changes: 1 addition & 1 deletion compiler/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
},
"dependencies": {
"@aztec/bb.js": "^0.7.3",
"@noir-lang/backend_barretenberg": "workspace:*",
"@noir-lang/noir_js": "workspace:*",
"@noir-lang/noir_wasm": "workspace:*",
"@noir-lang/source-resolver": "workspace:*",
Expand Down
93 changes: 93 additions & 0 deletions compiler/integration-tests/test/integration/browser/1_mul.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { expect } from "@esm-bundle/chai";
import { TEST_LOG_LEVEL } from "../../environment.js";
import { Logger } from "tslog";
import { initializeResolver } from "@noir-lang/source-resolver";
import newCompiler, {
compile,
init_log_level as compilerLogLevel,
} from "@noir-lang/noir_wasm";
import { Noir } from "@noir-lang/noir_js";
import { BarretenbergBackend } from "@noir-lang/backend_barretenberg";
import * as TOML from "smol-toml";
const logger = new Logger({ name: "test", minLevel: TEST_LOG_LEVEL });

await newCompiler();

compilerLogLevel("INFO");

const base_relative_path = "../../../../..";
const one_mul = "tooling/nargo_cli/tests/execution_success/1_mul";
const circuit_recursion = "compiler/integration-tests/test/circuits/recursion";

async function getFile(url: URL): Promise<string> {
const response = await fetch(url);

if (!response.ok) throw new Error("Network response was not OK");

return await response.text();
}

async function getCircuit(noirSource: string) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
initializeResolver((id: string) => {
logger.debug("source-resolver: resolving:", id);
return noirSource;
});

return compile({});
}

describe("It compiles noir program code, receiving circuit bytes and abi object.", () => {
let circuit_main_source;
let circuit_main_toml;
let circuit_recursion_source;

before(async () => {
const circuit_main_source_url = new URL(
`${base_relative_path}/${one_mul}/src/main.nr`,
import.meta.url,
);
const circuit_main_toml_url = new URL(
`${base_relative_path}/${one_mul}/Prover.toml`,
import.meta.url,
);

circuit_main_source = await getFile(circuit_main_source_url);
circuit_main_toml = await getFile(circuit_main_toml_url);

const circuit_recursion_source_url = new URL(
`${base_relative_path}/${circuit_recursion}/src/main.nr`,
import.meta.url,
);

circuit_recursion_source = await getFile(circuit_recursion_source_url);
});

it("Should generate valid inner proof for correct input, then verify proof within a proof", async () => {
const compiled_main_circuit = await getCircuit(circuit_main_source);

const main_inputs = TOML.parse(circuit_main_toml);

const backend = new BarretenbergBackend({
bytecode: compiled_main_circuit.circuit,
abi: compiled_main_circuit.abi,
});

const program = new Noir(
{
bytecode: compiled_main_circuit.circuit,
abi: compiled_main_circuit.abi,
},
backend,
);

const main_proof = await program.generateFinalProof(main_inputs);

const main_verification = await program.verifyFinalProof(main_proof);

logger.debug("main_verification", main_verification);

expect(main_verification).to.be.true;
}).timeout(60 * 20e3);
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("It compiles noir program code, receiving circuit bytes and abi object.

const circuit_recursion_source_url = new URL(
`${base_relative_path}/${circuit_recursion}/src/main.nr`,
import.meta.url,
import.meta.url
);

circuit_recursion_source = await getFile(circuit_recursion_source_url);
Expand Down
4 changes: 1 addition & 3 deletions compiler/source-resolver/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export declare const read_file: (source_id: string) => string;
export declare function initializeResolver(
resolver: (source_id: string) => string,
): void;
export declare function initializeResolver(resolver: (source_id: string) => string): void;
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "@noir-lang/root",
"version": "0.11.1",
"version": "0.14.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 @@ -34,10 +36,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"
}
109 changes: 109 additions & 0 deletions tooling/backend_barretenberg/lib/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"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;
numberOfThreads = 1;
constructor(acirCircuit, numberOfThreads = 1) {
const acirBytecodeBase64 = acirCircuit.bytecode;
this.numberOfThreads = numberOfThreads;
this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64);
}
async instantiate() {
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(this.numberOfThreads);
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;
}
}
// 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 generateFinalProof(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 generateIntermediateProof(witness) {
const optimizeForVerifyInCircuit = true;
return this.generateFinalProof(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 generateIntermediateProofArtifacts(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 verifyIntermediateProof(proof) {
const optimizeForVerifyInCircuit = true;
return this.verifyFinalProof(proof, optimizeForVerifyInCircuit);
}
async verifyFinalProof(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;
21 changes: 21 additions & 0 deletions tooling/backend_barretenberg/lib/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
private numberOfThreads;
constructor(acirCircuit: CompiledCircuit, numberOfThreads?: number);
instantiate(): Promise<void>;
generateFinalProof(decompressedWitness: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<Uint8Array>;
generateIntermediateProof(witness: Uint8Array): Promise<Uint8Array>;
generateIntermediateProofArtifacts(proof: Uint8Array, numOfPublicInputs?: number): Promise<{
proofAsFields: string[];
vkAsFields: string[];
vkHash: string;
}>;
verifyIntermediateProof(proof: Uint8Array): Promise<boolean>;
verifyFinalProof(proof: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<boolean>;
destroy(): Promise<void>;
}
21 changes: 21 additions & 0 deletions tooling/backend_barretenberg/lib/esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
private numberOfThreads;
constructor(acirCircuit: CompiledCircuit, numberOfThreads?: number);
instantiate(): Promise<void>;
generateFinalProof(decompressedWitness: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<Uint8Array>;
generateIntermediateProof(witness: Uint8Array): Promise<Uint8Array>;
generateIntermediateProofArtifacts(proof: Uint8Array, numOfPublicInputs?: number): Promise<{
proofAsFields: string[];
vkAsFields: string[];
vkHash: string;
}>;
verifyIntermediateProof(proof: Uint8Array): Promise<boolean>;
verifyFinalProof(proof: Uint8Array, optimizeForVerifyInCircuit?: boolean): Promise<boolean>;
destroy(): Promise<void>;
}
Loading
Loading