@noir-lang/noir_js 0.12.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/base64_decode.cjs +19 -0
- package/lib/base64_decode.d.ts +1 -0
- package/lib/base64_decode.mjs +15 -0
- package/lib/index.cjs +37 -0
- package/lib/index.d.ts +6 -3
- package/lib/index.mjs +6 -0
- package/lib/program.cjs +35 -0
- package/lib/program.d.ts +9 -0
- package/lib/program.mjs +28 -0
- package/lib/serialize.cjs +20 -0
- package/lib/serialize.d.ts +3 -0
- package/lib/serialize.mjs +15 -0
- package/lib/witness_generation.cjs +24 -0
- package/lib/witness_generation.d.ts +2 -0
- package/lib/witness_generation.mjs +20 -0
- package/package.json +37 -6
- package/lib/index.js +0 -3
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64Decode = void 0;
|
|
4
|
+
// Since this is a simple function, we can use feature detection to
|
|
5
|
+
// see if we are in the nodeJs environment or the browser environment.
|
|
6
|
+
function base64Decode(input) {
|
|
7
|
+
if (typeof Buffer !== 'undefined') {
|
|
8
|
+
// Node.js environment
|
|
9
|
+
return Buffer.from(input, 'base64');
|
|
10
|
+
}
|
|
11
|
+
else if (typeof atob === 'function') {
|
|
12
|
+
// Browser environment
|
|
13
|
+
return Uint8Array.from(atob(input), (c) => c.charCodeAt(0));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
throw new Error('No implementation found for base64 decoding.');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.base64Decode = base64Decode;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function base64Decode(input: string): Uint8Array;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Since this is a simple function, we can use feature detection to
|
|
2
|
+
// see if we are in the nodeJs environment or the browser environment.
|
|
3
|
+
export function base64Decode(input) {
|
|
4
|
+
if (typeof Buffer !== 'undefined') {
|
|
5
|
+
// Node.js environment
|
|
6
|
+
return Buffer.from(input, 'base64');
|
|
7
|
+
}
|
|
8
|
+
else if (typeof atob === 'function') {
|
|
9
|
+
// Browser environment
|
|
10
|
+
return Uint8Array.from(atob(input), (c) => c.charCodeAt(0));
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
throw new Error('No implementation found for base64 decoding.');
|
|
14
|
+
}
|
|
15
|
+
}
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.Noir = exports.witnessMapToUint8Array = exports.acirToUint8Array = exports.generateWitness = exports.abi = exports.acvm = void 0;
|
|
27
|
+
const acvm = __importStar(require("@noir-lang/acvm_js"));
|
|
28
|
+
exports.acvm = acvm;
|
|
29
|
+
const abi = __importStar(require("@noir-lang/noirc_abi"));
|
|
30
|
+
exports.abi = abi;
|
|
31
|
+
var witness_generation_js_1 = require("./witness_generation.cjs");
|
|
32
|
+
Object.defineProperty(exports, "generateWitness", { enumerable: true, get: function () { return witness_generation_js_1.generateWitness; } });
|
|
33
|
+
var serialize_js_1 = require("./serialize.cjs");
|
|
34
|
+
Object.defineProperty(exports, "acirToUint8Array", { enumerable: true, get: function () { return serialize_js_1.acirToUint8Array; } });
|
|
35
|
+
Object.defineProperty(exports, "witnessMapToUint8Array", { enumerable: true, get: function () { return serialize_js_1.witnessMapToUint8Array; } });
|
|
36
|
+
var program_js_1 = require("./program.cjs");
|
|
37
|
+
Object.defineProperty(exports, "Noir", { enumerable: true, get: function () { return program_js_1.Noir; } });
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import * as acvm from
|
|
2
|
-
import * as
|
|
3
|
-
export { acvm,
|
|
1
|
+
import * as acvm from '@noir-lang/acvm_js';
|
|
2
|
+
import * as abi from '@noir-lang/noirc_abi';
|
|
3
|
+
export { acvm, abi };
|
|
4
|
+
export { generateWitness } from './witness_generation.js';
|
|
5
|
+
export { acirToUint8Array, witnessMapToUint8Array } from './serialize.js';
|
|
6
|
+
export { Noir } from './program.js';
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as acvm from '@noir-lang/acvm_js';
|
|
2
|
+
import * as abi from '@noir-lang/noirc_abi';
|
|
3
|
+
export { acvm, abi };
|
|
4
|
+
export { generateWitness } from "./witness_generation.mjs";
|
|
5
|
+
export { acirToUint8Array, witnessMapToUint8Array } from "./serialize.mjs";
|
|
6
|
+
export { Noir } from "./program.mjs";
|
package/lib/program.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Noir = void 0;
|
|
7
|
+
const witness_generation_js_1 = require("./witness_generation.cjs");
|
|
8
|
+
const noirc_abi_1 = __importDefault(require("@noir-lang/noirc_abi"));
|
|
9
|
+
const acvm_js_1 = __importDefault(require("@noir-lang/acvm_js"));
|
|
10
|
+
class Noir {
|
|
11
|
+
circuit;
|
|
12
|
+
backend;
|
|
13
|
+
constructor(circuit, backend) {
|
|
14
|
+
this.circuit = circuit;
|
|
15
|
+
this.backend = backend;
|
|
16
|
+
}
|
|
17
|
+
async init() {
|
|
18
|
+
// If these are available, then we are in the
|
|
19
|
+
// web environment. For the node environment, this
|
|
20
|
+
// is a no-op.
|
|
21
|
+
if (typeof noirc_abi_1.default === 'function') {
|
|
22
|
+
await Promise.all([(0, noirc_abi_1.default)(), (0, acvm_js_1.default)()]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Initial inputs to your program
|
|
26
|
+
async generateFinalProof(inputs) {
|
|
27
|
+
await this.init();
|
|
28
|
+
const serializedWitness = await (0, witness_generation_js_1.generateWitness)(this.circuit, inputs);
|
|
29
|
+
return this.backend.generateFinalProof(serializedWitness);
|
|
30
|
+
}
|
|
31
|
+
async verifyFinalProof(proof) {
|
|
32
|
+
return this.backend.verifyFinalProof(proof);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.Noir = Noir;
|
package/lib/program.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Backend, CompiledCircuit } from '@noir-lang/types';
|
|
2
|
+
export declare class Noir {
|
|
3
|
+
private circuit;
|
|
4
|
+
private backend;
|
|
5
|
+
constructor(circuit: CompiledCircuit, backend: Backend);
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
generateFinalProof(inputs: any): Promise<Uint8Array>;
|
|
8
|
+
verifyFinalProof(proof: Uint8Array): Promise<boolean>;
|
|
9
|
+
}
|
package/lib/program.mjs
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { generateWitness } from "./witness_generation.mjs";
|
|
2
|
+
import initAbi from '@noir-lang/noirc_abi';
|
|
3
|
+
import initACVM from '@noir-lang/acvm_js';
|
|
4
|
+
export class Noir {
|
|
5
|
+
circuit;
|
|
6
|
+
backend;
|
|
7
|
+
constructor(circuit, backend) {
|
|
8
|
+
this.circuit = circuit;
|
|
9
|
+
this.backend = backend;
|
|
10
|
+
}
|
|
11
|
+
async init() {
|
|
12
|
+
// If these are available, then we are in the
|
|
13
|
+
// web environment. For the node environment, this
|
|
14
|
+
// is a no-op.
|
|
15
|
+
if (typeof initAbi === 'function') {
|
|
16
|
+
await Promise.all([initAbi(), initACVM()]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
// Initial inputs to your program
|
|
20
|
+
async generateFinalProof(inputs) {
|
|
21
|
+
await this.init();
|
|
22
|
+
const serializedWitness = await generateWitness(this.circuit, inputs);
|
|
23
|
+
return this.backend.generateFinalProof(serializedWitness);
|
|
24
|
+
}
|
|
25
|
+
async verifyFinalProof(proof) {
|
|
26
|
+
return this.backend.verifyFinalProof(proof);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.acirToUint8Array = exports.witnessMapToUint8Array = void 0;
|
|
4
|
+
const acvm_js_1 = require("@noir-lang/acvm_js");
|
|
5
|
+
const fflate_1 = require("fflate");
|
|
6
|
+
const base64_decode_js_1 = require("./base64_decode.cjs");
|
|
7
|
+
// After solving the witness, to pass it a backend, we need to serialize it to a Uint8Array
|
|
8
|
+
function witnessMapToUint8Array(solvedWitness) {
|
|
9
|
+
// TODO: We just want to serialize, but this will zip up the witness
|
|
10
|
+
// TODO so its not ideal
|
|
11
|
+
const compressedWitness = (0, acvm_js_1.compressWitness)(solvedWitness);
|
|
12
|
+
return (0, fflate_1.decompressSync)(compressedWitness);
|
|
13
|
+
}
|
|
14
|
+
exports.witnessMapToUint8Array = witnessMapToUint8Array;
|
|
15
|
+
// Converts an bytecode to a Uint8Array
|
|
16
|
+
function acirToUint8Array(base64EncodedBytecode) {
|
|
17
|
+
const compressedByteCode = (0, base64_decode_js_1.base64Decode)(base64EncodedBytecode);
|
|
18
|
+
return (0, fflate_1.decompressSync)(compressedByteCode);
|
|
19
|
+
}
|
|
20
|
+
exports.acirToUint8Array = acirToUint8Array;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { compressWitness } from '@noir-lang/acvm_js';
|
|
2
|
+
import { decompressSync as gunzip } from 'fflate';
|
|
3
|
+
import { base64Decode } from "./base64_decode.mjs";
|
|
4
|
+
// After solving the witness, to pass it a backend, we need to serialize it to a Uint8Array
|
|
5
|
+
export function witnessMapToUint8Array(solvedWitness) {
|
|
6
|
+
// TODO: We just want to serialize, but this will zip up the witness
|
|
7
|
+
// TODO so its not ideal
|
|
8
|
+
const compressedWitness = compressWitness(solvedWitness);
|
|
9
|
+
return gunzip(compressedWitness);
|
|
10
|
+
}
|
|
11
|
+
// Converts an bytecode to a Uint8Array
|
|
12
|
+
export function acirToUint8Array(base64EncodedBytecode) {
|
|
13
|
+
const compressedByteCode = base64Decode(base64EncodedBytecode);
|
|
14
|
+
return gunzip(compressedByteCode);
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateWitness = void 0;
|
|
4
|
+
const noirc_abi_1 = require("@noir-lang/noirc_abi");
|
|
5
|
+
const base64_decode_js_1 = require("./base64_decode.cjs");
|
|
6
|
+
const acvm_js_1 = require("@noir-lang/acvm_js");
|
|
7
|
+
const serialize_js_1 = require("./serialize.cjs");
|
|
8
|
+
// Generates the witnesses needed to feed into the chosen proving system
|
|
9
|
+
async function generateWitness(compiledProgram, inputs) {
|
|
10
|
+
// Throws on ABI encoding error
|
|
11
|
+
const witnessMap = (0, noirc_abi_1.abiEncode)(compiledProgram.abi, inputs, null);
|
|
12
|
+
// Execute the circuit to generate the rest of the witnesses and serialize
|
|
13
|
+
// them into a Uint8Array.
|
|
14
|
+
try {
|
|
15
|
+
const solvedWitness = await (0, acvm_js_1.executeCircuit)((0, base64_decode_js_1.base64Decode)(compiledProgram.bytecode), witnessMap, () => {
|
|
16
|
+
throw Error('unexpected oracle during execution');
|
|
17
|
+
});
|
|
18
|
+
return (0, serialize_js_1.witnessMapToUint8Array)(solvedWitness);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
throw new Error(`Circuit execution failed: ${err}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.generateWitness = generateWitness;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { abiEncode } from '@noir-lang/noirc_abi';
|
|
2
|
+
import { base64Decode } from "./base64_decode.mjs";
|
|
3
|
+
import { executeCircuit } from '@noir-lang/acvm_js';
|
|
4
|
+
import { witnessMapToUint8Array } from "./serialize.mjs";
|
|
5
|
+
// Generates the witnesses needed to feed into the chosen proving system
|
|
6
|
+
export async function generateWitness(compiledProgram, inputs) {
|
|
7
|
+
// Throws on ABI encoding error
|
|
8
|
+
const witnessMap = abiEncode(compiledProgram.abi, inputs, null);
|
|
9
|
+
// Execute the circuit to generate the rest of the witnesses and serialize
|
|
10
|
+
// them into a Uint8Array.
|
|
11
|
+
try {
|
|
12
|
+
const solvedWitness = await executeCircuit(base64Decode(compiledProgram.bytecode), witnessMap, () => {
|
|
13
|
+
throw Error('unexpected oracle during execution');
|
|
14
|
+
});
|
|
15
|
+
return witnessMapToUint8Array(solvedWitness);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
throw new Error(`Circuit execution failed: ${err}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -3,23 +3,54 @@
|
|
|
3
3
|
"collaborators": [
|
|
4
4
|
"The Noir Team <team@noir-lang.org>"
|
|
5
5
|
],
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.16.0",
|
|
7
7
|
"packageManager": "yarn@3.5.1",
|
|
8
8
|
"license": "(MIT OR Apache-2.0)",
|
|
9
|
+
"type": "module",
|
|
9
10
|
"dependencies": {
|
|
10
|
-
"@noir-lang/acvm_js": "0.
|
|
11
|
-
"@noir-lang/noirc_abi": "
|
|
11
|
+
"@noir-lang/acvm_js": "0.28.0",
|
|
12
|
+
"@noir-lang/noirc_abi": "0.16.0",
|
|
13
|
+
"@noir-lang/types": "0.14.1",
|
|
14
|
+
"fflate": "^0.8.0"
|
|
12
15
|
},
|
|
13
16
|
"files": [
|
|
14
17
|
"lib",
|
|
15
18
|
"package.json"
|
|
16
19
|
],
|
|
17
|
-
"
|
|
20
|
+
"source": "src/index.ts",
|
|
21
|
+
"main": "lib/index.cjs",
|
|
22
|
+
"module": "lib/index.mjs",
|
|
23
|
+
"exports": {
|
|
24
|
+
"require": "./lib/index.cjs",
|
|
25
|
+
"types": "./lib/index.d.ts",
|
|
26
|
+
"default": "./lib/index.mjs"
|
|
27
|
+
},
|
|
18
28
|
"types": "lib/index.d.ts",
|
|
19
29
|
"scripts": {
|
|
20
|
-
"
|
|
30
|
+
"dev": "tsc-multi --watch",
|
|
31
|
+
"build": "tsc-multi",
|
|
32
|
+
"test": "yarn test:node:esm && yarn test:node:cjs",
|
|
33
|
+
"test:node:esm": "mocha --timeout 25000 --exit --config ./.mocharc.json",
|
|
34
|
+
"test:node:cjs": "mocha --timeout 25000 --exit --config ./.mocharc.cjs.json",
|
|
35
|
+
"prettier": "prettier 'src/**/*.ts'",
|
|
36
|
+
"prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
|
|
37
|
+
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0",
|
|
38
|
+
"publish": "yarn npm publish",
|
|
39
|
+
"clean": "rm -rf ./lib"
|
|
21
40
|
},
|
|
22
41
|
"devDependencies": {
|
|
42
|
+
"@aztec/bb.js": "0.7.2",
|
|
43
|
+
"@types/chai": "^4",
|
|
44
|
+
"@types/mocha": "^10.0.1",
|
|
45
|
+
"@types/node": "^20.6.2",
|
|
46
|
+
"@types/prettier": "^3",
|
|
47
|
+
"chai": "^4.3.8",
|
|
48
|
+
"eslint": "^8.50.0",
|
|
49
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
50
|
+
"mocha": "^10.2.0",
|
|
51
|
+
"prettier": "3.0.3",
|
|
52
|
+
"ts-node": "^10.9.1",
|
|
53
|
+
"tsc-multi": "^1.1.0",
|
|
23
54
|
"typescript": "^5.2.2"
|
|
24
55
|
}
|
|
25
|
-
}
|
|
56
|
+
}
|
package/lib/index.js
DELETED