From c0e0ccfc30a95fbfd616672deca4774ee69e6761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Mon, 9 Oct 2023 15:18:41 +0100 Subject: [PATCH 1/4] chore: splitting --- tooling/noir_js/src/program.ts | 32 ------------------- tooling/noir_js/tsconfig.json | 9 ++++-- tooling/noir_js/typedoc.json | 5 +++ .../noir_js_backend_barretenberg/src/index.ts | 30 +++++++++++------ .../tsconfig.json | 7 +++- .../noir_js_backend_barretenberg/typedoc.json | 5 +++ tooling/noir_js_types/.gitignore | 3 +- tooling/noir_js_types/tsconfig.json | 3 +- typedoc.json | 14 ++++++++ 9 files changed, 62 insertions(+), 46 deletions(-) delete mode 100644 tooling/noir_js/src/program.ts create mode 100644 tooling/noir_js/typedoc.json create mode 100644 tooling/noir_js_backend_barretenberg/typedoc.json create mode 100644 typedoc.json diff --git a/tooling/noir_js/src/program.ts b/tooling/noir_js/src/program.ts deleted file mode 100644 index 1fd32862010..00000000000 --- a/tooling/noir_js/src/program.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types'; -import { generateWitness } from './witness_generation.js'; -import initAbi from '@noir-lang/noirc_abi'; -import initACVM from '@noir-lang/acvm_js'; - -export class Noir { - constructor( - private circuit: CompiledCircuit, - private backend: Backend, - ) {} - - async init(): Promise { - // If these are available, then we are in the - // web environment. For the node environment, this - // is a no-op. - if (typeof initAbi === 'function') { - await Promise.all([initAbi(), initACVM()]); - } - } - - // Initial inputs to your program - async generateFinalProof(inputs: any): Promise { - await this.init(); - const serializedWitness = await generateWitness(this.circuit, inputs); - return this.backend.generateFinalProof(serializedWitness); - } - - async verifyFinalProof(proofData: ProofData): Promise { - return this.backend.verifyFinalProof(proofData); - } -} diff --git a/tooling/noir_js/tsconfig.json b/tooling/noir_js/tsconfig.json index 1e0fdea09c7..c99e0a05764 100644 --- a/tooling/noir_js/tsconfig.json +++ b/tooling/noir_js/tsconfig.json @@ -12,5 +12,10 @@ "noImplicitAny": false, }, "include": ["src/**/*.ts"], - "exclude": ["node_modules"] -} \ No newline at end of file + "exclude": ["node_modules"], + "references": [ + { + "path": "../noir_js_types" + } + ] +} diff --git a/tooling/noir_js/typedoc.json b/tooling/noir_js/typedoc.json new file mode 100644 index 00000000000..9fef4e3a360 --- /dev/null +++ b/tooling/noir_js/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": ["src/index.ts"], + "excludeInternal": true, + "disableSources": true, +} diff --git a/tooling/noir_js_backend_barretenberg/src/index.ts b/tooling/noir_js_backend_barretenberg/src/index.ts index b3d3af79b9e..5515714815a 100644 --- a/tooling/noir_js_backend_barretenberg/src/index.ts +++ b/tooling/noir_js_backend_barretenberg/src/index.ts @@ -7,13 +7,13 @@ import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types'; const numBytesInProofWithoutPublicInputs: number = 2144; export class BarretenbergBackend implements Backend { - // 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. + /** @internal */ private api: any; + /** @internal */ private acirComposer: any; + /** @internal */ private acirUncompressedBytecode: Uint8Array; + /** @internal */ private numberOfThreads = 1; constructor(acirCircuit: CompiledCircuit, numberOfThreads = 1) { @@ -22,7 +22,15 @@ export class BarretenbergBackend implements Backend { this.acirUncompressedBytecode = acirToUint8Array(acirBytecodeBase64); } - private async instantiate(): Promise { + /** + * + * This async method is called by the Noir class. + * It allocates resources, decompresses the bytecode, and inits the SRS. + * + * @param numberOfThreads - The number of threads to be used by the backend. Defaults to 1. + * + */ + async instantiate(): Promise { if (!this.api) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore @@ -44,6 +52,7 @@ export class BarretenbergBackend implements Backend { // // The settings for this proof are the same as the settings for a "normal" proof // ie one that is not in the recursive setting. + /** @internal */ async generateFinalProof(decompressedWitness: Uint8Array): Promise { const makeEasyToVerifyInCircuit = false; return this.generateProof(decompressedWitness, makeEasyToVerifyInCircuit); @@ -60,13 +69,13 @@ export class BarretenbergBackend implements Backend { // 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. + /** @internal */ async generateIntermediateProof(witness: Uint8Array): Promise { const makeEasyToVerifyInCircuit = true; return this.generateProof(witness, makeEasyToVerifyInCircuit); } - + /** @internal */ async generateProof(decompressedWitness: Uint8Array, makeEasyToVerifyInCircuit: boolean): Promise { - await this.instantiate(); const proofWithPublicInputs = await this.api.acirCreateProof( this.acirComposer, this.acirUncompressedBytecode, @@ -100,6 +109,7 @@ export class BarretenbergBackend implements Backend { // method. // // The number of public inputs denotes how many public inputs are in the inner proof. + /** @internal */ async generateIntermediateProofArtifacts( proofData: ProofData, numOfPublicInputs = 0, @@ -108,7 +118,6 @@ export class BarretenbergBackend implements Backend { vkAsFields: string[]; vkHash: string; }> { - await this.instantiate(); const proof = reconstructProofWithPublicInputs(proofData); const proofAsFields = await this.api.acirSerializeProofIntoFields(this.acirComposer, proof, numOfPublicInputs); @@ -126,6 +135,7 @@ export class BarretenbergBackend implements Backend { }; } + /** @internal */ async verifyFinalProof(proofData: ProofData): Promise { const proof = reconstructProofWithPublicInputs(proofData); const makeEasyToVerifyInCircuit = false; @@ -133,18 +143,20 @@ export class BarretenbergBackend implements Backend { return verified; } + /** @internal */ async verifyIntermediateProof(proofData: ProofData): Promise { const proof = reconstructProofWithPublicInputs(proofData); const makeEasyToVerifyInCircuit = true; return this.verifyProof(proof, makeEasyToVerifyInCircuit); } + /** @internal */ async verifyProof(proof: Uint8Array, makeEasyToVerifyInCircuit: boolean): Promise { - await this.instantiate(); await this.api.acirInitVerificationKey(this.acirComposer); return await this.api.acirVerifyProof(this.acirComposer, proof, makeEasyToVerifyInCircuit); } + /** @internal */ async destroy(): Promise { if (!this.api) { return; diff --git a/tooling/noir_js_backend_barretenberg/tsconfig.json b/tooling/noir_js_backend_barretenberg/tsconfig.json index 393fa38f583..1e28c044bba 100644 --- a/tooling/noir_js_backend_barretenberg/tsconfig.json +++ b/tooling/noir_js_backend_barretenberg/tsconfig.json @@ -12,5 +12,10 @@ "noImplicitAny": false, }, "include": ["src/**/*.ts"], - "exclude": ["node_modules"] + "exclude": ["node_modules"], + "references": [ + { + "path": "../noir_js_types" + } + ] } diff --git a/tooling/noir_js_backend_barretenberg/typedoc.json b/tooling/noir_js_backend_barretenberg/typedoc.json new file mode 100644 index 00000000000..9fef4e3a360 --- /dev/null +++ b/tooling/noir_js_backend_barretenberg/typedoc.json @@ -0,0 +1,5 @@ +{ + "entryPoints": ["src/index.ts"], + "excludeInternal": true, + "disableSources": true, +} diff --git a/tooling/noir_js_types/.gitignore b/tooling/noir_js_types/.gitignore index 7951405f85a..73c8594a9ff 100644 --- a/tooling/noir_js_types/.gitignore +++ b/tooling/noir_js_types/.gitignore @@ -1 +1,2 @@ -lib \ No newline at end of file +lib +*.tsbuildinfo diff --git a/tooling/noir_js_types/tsconfig.json b/tooling/noir_js_types/tsconfig.json index 1fe2a46abaa..6540f78884c 100644 --- a/tooling/noir_js_types/tsconfig.json +++ b/tooling/noir_js_types/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "composite": true, "declaration": true, "module": "ESNext", "moduleResolution": "node", @@ -9,4 +10,4 @@ }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] -} \ No newline at end of file +} diff --git a/typedoc.json b/typedoc.json new file mode 100644 index 00000000000..0956b1d3435 --- /dev/null +++ b/typedoc.json @@ -0,0 +1,14 @@ +{ + // Comments are supported, like tsconfig.json + "$schema": "https://typedoc.org/schema.json", + "entryPoints": [ + "tooling/noir_js", + "tooling/noir_js_backend_barretenberg" + ], + "entryPointStrategy": "packages", + "out": "docs", + "plugin": [ "typedoc-plugin-merge-modules"], + "mergeModulesRenameDefaults": true, // NEW option of TypeDoc added by this plugin + "mergeModulesMergeMode": "project", // NEW option of TypeDoc added by this plugin + "readme": "none" +} From c3b341ed4b191deea84c76c22a5a6bcdf2b4d7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Mon, 9 Oct 2023 15:21:12 +0100 Subject: [PATCH 2/4] chore: splitting --- typedoc.json | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 typedoc.json diff --git a/typedoc.json b/typedoc.json deleted file mode 100644 index 0956b1d3435..00000000000 --- a/typedoc.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - // Comments are supported, like tsconfig.json - "$schema": "https://typedoc.org/schema.json", - "entryPoints": [ - "tooling/noir_js", - "tooling/noir_js_backend_barretenberg" - ], - "entryPointStrategy": "packages", - "out": "docs", - "plugin": [ "typedoc-plugin-merge-modules"], - "mergeModulesRenameDefaults": true, // NEW option of TypeDoc added by this plugin - "mergeModulesMergeMode": "project", // NEW option of TypeDoc added by this plugin - "readme": "none" -} From 9fe87cb043ec4d91d8486ffbac34809312e71d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Mon, 9 Oct 2023 15:23:10 +0100 Subject: [PATCH 3/4] chore: splitting --- tooling/noir_js/src/program.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tooling/noir_js/src/program.ts diff --git a/tooling/noir_js/src/program.ts b/tooling/noir_js/src/program.ts new file mode 100644 index 00000000000..3394d2a0d2a --- /dev/null +++ b/tooling/noir_js/src/program.ts @@ -0,0 +1,31 @@ +import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types'; +import { generateWitness } from './witness_generation.js'; +import initAbi from '@noir-lang/noirc_abi'; +import initACVM from '@noir-lang/acvm_js'; + +export class Noir { + constructor( + private circuit: CompiledCircuit, + private backend: Backend, + ) {} + + async init(): Promise { + // If these are available, then we are in the + // web environment. For the node environment, this + // is a no-op. + if (typeof initAbi === 'function') { + await Promise.all([initAbi(), initACVM()]); + } + } + + // Initial inputs to your program + async generateFinalProof(inputs: any): Promise { + await this.init(); + const serializedWitness = await generateWitness(this.circuit, inputs); + return this.backend.generateFinalProof(serializedWitness); + } + + async verifyFinalProof(proofData: ProofData): Promise { + return this.backend.verifyFinalProof(proofData); + } +} From 225238ab80c8527784c8d89c1688681e42710745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Mon, 9 Oct 2023 15:24:31 +0100 Subject: [PATCH 4/4] chore: splitting --- tooling/noir_js/src/program.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tooling/noir_js/src/program.ts b/tooling/noir_js/src/program.ts index 3394d2a0d2a..1fd32862010 100644 --- a/tooling/noir_js/src/program.ts +++ b/tooling/noir_js/src/program.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types'; import { generateWitness } from './witness_generation.js'; import initAbi from '@noir-lang/noirc_abi';