@aztec/noir-noir_js 0.0.0-test.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.
@@ -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,38 @@
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.abi = exports.acvm = exports.Noir = exports.and = exports.xor = exports.blake2s256 = exports.ecdsa_secp256k1_verify = exports.ecdsa_secp256r1_verify = 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 acvm_js_1 = require("@noir-lang/acvm_js");
32
+ Object.defineProperty(exports, "ecdsa_secp256r1_verify", { enumerable: true, get: function () { return acvm_js_1.ecdsa_secp256r1_verify; } });
33
+ Object.defineProperty(exports, "ecdsa_secp256k1_verify", { enumerable: true, get: function () { return acvm_js_1.ecdsa_secp256k1_verify; } });
34
+ Object.defineProperty(exports, "blake2s256", { enumerable: true, get: function () { return acvm_js_1.blake2s256; } });
35
+ Object.defineProperty(exports, "xor", { enumerable: true, get: function () { return acvm_js_1.xor; } });
36
+ Object.defineProperty(exports, "and", { enumerable: true, get: function () { return acvm_js_1.and; } });
37
+ var program_js_1 = require("./program.cjs");
38
+ Object.defineProperty(exports, "Noir", { enumerable: true, get: function () { return program_js_1.Noir; } });
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import * as acvm from '@noir-lang/acvm_js';
2
+ import * as abi from '@noir-lang/noirc_abi';
3
+ import { CompiledCircuit } from '@noir-lang/types';
4
+ export { ecdsa_secp256r1_verify, ecdsa_secp256k1_verify, blake2s256, xor, and } from '@noir-lang/acvm_js';
5
+ export { InputMap } from '@noir-lang/noirc_abi';
6
+ export { WitnessMap, ForeignCallHandler, ForeignCallInput, ForeignCallOutput } from '@noir-lang/acvm_js';
7
+ export { Noir } from './program.js';
8
+ export { ErrorWithPayload } from './witness_generation.js';
9
+ /** @ignore */
10
+ export { acvm, abi };
11
+ export { CompiledCircuit };
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 { ecdsa_secp256r1_verify, ecdsa_secp256k1_verify, blake2s256, xor, and } from '@noir-lang/acvm_js';
4
+ export { Noir } from "./program.mjs";
5
+ /** @ignore */
6
+ export { acvm, abi };
@@ -0,0 +1,61 @@
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 = void 0;
27
+ const witness_generation_js_1 = require("./witness_generation.cjs");
28
+ const noirc_abi_1 = __importStar(require("@noir-lang/noirc_abi"));
29
+ const acvm_js_1 = __importStar(require("@noir-lang/acvm_js"));
30
+ class Noir {
31
+ circuit;
32
+ constructor(circuit) {
33
+ this.circuit = circuit;
34
+ }
35
+ /** @ignore */
36
+ async init() {
37
+ // If these are available, then we are in the
38
+ // web environment. For the node environment, this
39
+ // is a no-op.
40
+ if (typeof noirc_abi_1.default === 'function') {
41
+ await Promise.all([(0, noirc_abi_1.default)(), (0, acvm_js_1.default)()]);
42
+ }
43
+ }
44
+ /**
45
+ * @description
46
+ * Allows to execute a circuit to get its witness and return value.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * async execute(inputs)
51
+ * ```
52
+ */
53
+ async execute(inputs, foreignCallHandler) {
54
+ await this.init();
55
+ const witness_stack = await (0, witness_generation_js_1.generateWitness)(this.circuit, inputs, foreignCallHandler);
56
+ const main_witness = witness_stack[0].witness;
57
+ const { return_value: returnValue } = (0, noirc_abi_1.abiDecode)(this.circuit.abi, main_witness);
58
+ return { witness: (0, acvm_js_1.compressWitnessStack)(witness_stack), returnValue };
59
+ }
60
+ }
61
+ exports.Noir = Noir;
@@ -0,0 +1,22 @@
1
+ import { CompiledCircuit } from '@noir-lang/types';
2
+ import { InputMap, InputValue } from '@noir-lang/noirc_abi';
3
+ import { ForeignCallHandler } from '@noir-lang/acvm_js';
4
+ export declare class Noir {
5
+ private circuit;
6
+ constructor(circuit: CompiledCircuit);
7
+ /** @ignore */
8
+ init(): Promise<void>;
9
+ /**
10
+ * @description
11
+ * Allows to execute a circuit to get its witness and return value.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * async execute(inputs)
16
+ * ```
17
+ */
18
+ execute(inputs: InputMap, foreignCallHandler?: ForeignCallHandler): Promise<{
19
+ witness: Uint8Array;
20
+ returnValue: InputValue;
21
+ }>;
22
+ }
@@ -0,0 +1,34 @@
1
+ import { generateWitness } from "./witness_generation.mjs";
2
+ import initAbi, { abiDecode } from '@noir-lang/noirc_abi';
3
+ import initACVM, { compressWitnessStack } from '@noir-lang/acvm_js';
4
+ export class Noir {
5
+ circuit;
6
+ constructor(circuit) {
7
+ this.circuit = circuit;
8
+ }
9
+ /** @ignore */
10
+ async init() {
11
+ // If these are available, then we are in the
12
+ // web environment. For the node environment, this
13
+ // is a no-op.
14
+ if (typeof initAbi === 'function') {
15
+ await Promise.all([initAbi(), initACVM()]);
16
+ }
17
+ }
18
+ /**
19
+ * @description
20
+ * Allows to execute a circuit to get its witness and return value.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * async execute(inputs)
25
+ * ```
26
+ */
27
+ async execute(inputs, foreignCallHandler) {
28
+ await this.init();
29
+ const witness_stack = await generateWitness(this.circuit, inputs, foreignCallHandler);
30
+ const main_witness = witness_stack[0].witness;
31
+ const { return_value: returnValue } = abiDecode(this.circuit.abi, main_witness);
32
+ return { witness: compressWitnessStack(witness_stack), returnValue };
33
+ }
34
+ }
@@ -0,0 +1,57 @@
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 defaultForeignCallHandler = async (name, args) => {
8
+ if (name == 'print') {
9
+ // By default we do not print anything for `print` foreign calls due to a need for formatting,
10
+ // however we provide an empty response in order to not halt execution.
11
+ //
12
+ // If a user needs to print values then they should provide a custom foreign call handler.
13
+ return [];
14
+ }
15
+ throw Error(`Unexpected oracle during execution: ${name}(${args.join(', ')})`);
16
+ };
17
+ function parseErrorPayload(abi, originalError) {
18
+ const payload = originalError.rawAssertionPayload;
19
+ if (!payload)
20
+ return originalError;
21
+ const enrichedError = originalError;
22
+ try {
23
+ // Decode the payload
24
+ const decodedPayload = (0, noirc_abi_1.abiDecodeError)(abi, payload);
25
+ if (typeof decodedPayload === 'string') {
26
+ // If it's a string, just add it to the error message
27
+ enrichedError.message = `Circuit execution failed: ${decodedPayload}`;
28
+ }
29
+ else {
30
+ // If not, attach the payload to the original error
31
+ enrichedError.decodedAssertionPayload = decodedPayload;
32
+ }
33
+ }
34
+ catch (_errorDecoding) {
35
+ // Ignore errors decoding the payload
36
+ }
37
+ return enrichedError;
38
+ }
39
+ // Generates the witnesses needed to feed into the chosen proving system
40
+ async function generateWitness(compiledProgram, inputs, foreignCallHandler = defaultForeignCallHandler) {
41
+ // Throws on ABI encoding error
42
+ const witnessMap = (0, noirc_abi_1.abiEncode)(compiledProgram.abi, inputs);
43
+ // Execute the circuit to generate the rest of the witnesses and serialize
44
+ // them into a Uint8Array.
45
+ try {
46
+ const solvedWitness = await (0, acvm_js_1.executeProgram)((0, base64_decode_js_1.base64Decode)(compiledProgram.bytecode), witnessMap, foreignCallHandler);
47
+ return solvedWitness;
48
+ }
49
+ catch (err) {
50
+ // Typescript types catched errors as unknown or any, so we need to narrow its type to check if it has raw assertion payload.
51
+ if (typeof err === 'object' && err !== null && 'rawAssertionPayload' in err) {
52
+ throw parseErrorPayload(compiledProgram.abi, err);
53
+ }
54
+ throw new Error(`Circuit execution failed: ${err}`);
55
+ }
56
+ }
57
+ exports.generateWitness = generateWitness;
@@ -0,0 +1,7 @@
1
+ import { InputMap } from '@noir-lang/noirc_abi';
2
+ import { WitnessStack, ForeignCallHandler, ExecutionError } from '@noir-lang/acvm_js';
3
+ import { CompiledCircuit } from '@noir-lang/types';
4
+ export type ErrorWithPayload = ExecutionError & {
5
+ decodedAssertionPayload?: any;
6
+ };
7
+ export declare function generateWitness(compiledProgram: CompiledCircuit, inputs: InputMap, foreignCallHandler?: ForeignCallHandler): Promise<WitnessStack>;
@@ -0,0 +1,53 @@
1
+ import { abiDecodeError, abiEncode } from '@noir-lang/noirc_abi';
2
+ import { base64Decode } from "./base64_decode.mjs";
3
+ import { executeProgram } from '@noir-lang/acvm_js';
4
+ const defaultForeignCallHandler = async (name, args) => {
5
+ if (name == 'print') {
6
+ // By default we do not print anything for `print` foreign calls due to a need for formatting,
7
+ // however we provide an empty response in order to not halt execution.
8
+ //
9
+ // If a user needs to print values then they should provide a custom foreign call handler.
10
+ return [];
11
+ }
12
+ throw Error(`Unexpected oracle during execution: ${name}(${args.join(', ')})`);
13
+ };
14
+ function parseErrorPayload(abi, originalError) {
15
+ const payload = originalError.rawAssertionPayload;
16
+ if (!payload)
17
+ return originalError;
18
+ const enrichedError = originalError;
19
+ try {
20
+ // Decode the payload
21
+ const decodedPayload = abiDecodeError(abi, payload);
22
+ if (typeof decodedPayload === 'string') {
23
+ // If it's a string, just add it to the error message
24
+ enrichedError.message = `Circuit execution failed: ${decodedPayload}`;
25
+ }
26
+ else {
27
+ // If not, attach the payload to the original error
28
+ enrichedError.decodedAssertionPayload = decodedPayload;
29
+ }
30
+ }
31
+ catch (_errorDecoding) {
32
+ // Ignore errors decoding the payload
33
+ }
34
+ return enrichedError;
35
+ }
36
+ // Generates the witnesses needed to feed into the chosen proving system
37
+ export async function generateWitness(compiledProgram, inputs, foreignCallHandler = defaultForeignCallHandler) {
38
+ // Throws on ABI encoding error
39
+ const witnessMap = abiEncode(compiledProgram.abi, inputs);
40
+ // Execute the circuit to generate the rest of the witnesses and serialize
41
+ // them into a Uint8Array.
42
+ try {
43
+ const solvedWitness = await executeProgram(base64Decode(compiledProgram.bytecode), witnessMap, foreignCallHandler);
44
+ return solvedWitness;
45
+ }
46
+ catch (err) {
47
+ // Typescript types catched errors as unknown or any, so we need to narrow its type to check if it has raw assertion payload.
48
+ if (typeof err === 'object' && err !== null && 'rawAssertionPayload' in err) {
49
+ throw parseErrorPayload(compiledProgram.abi, err);
50
+ }
51
+ throw new Error(`Circuit execution failed: ${err}`);
52
+ }
53
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@aztec/noir-noir_js",
3
+ "contributors": [
4
+ "The Noir Team <team@noir-lang.org>"
5
+ ],
6
+ "version": "0.0.0-test.0",
7
+ "packageManager": "yarn@3.5.1",
8
+ "license": "(MIT OR Apache-2.0)",
9
+ "type": "module",
10
+ "homepage": "https://noir-lang.org/",
11
+ "repository": {
12
+ "url": "https://github.com/noir-lang/noir.git",
13
+ "directory": "tooling/noir_js",
14
+ "type": "git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/noir-lang/noir/issues"
18
+ },
19
+ "dependencies": {
20
+ "@aztec/noir-acvm_js": "0.0.0-test.0",
21
+ "@aztec/noir-noirc_abi": "0.0.0-test.0",
22
+ "@aztec/noir-types": "0.0.0-test.0"
23
+ },
24
+ "files": [
25
+ "lib",
26
+ "package.json"
27
+ ],
28
+ "source": "src/index.ts",
29
+ "main": "lib/index.cjs",
30
+ "module": "lib/index.mjs",
31
+ "exports": {
32
+ "require": "./lib/index.cjs",
33
+ "types": "./lib/index.d.ts",
34
+ "default": "./lib/index.mjs"
35
+ },
36
+ "types": "lib/index.d.ts",
37
+ "scripts": {
38
+ "dev": "tsc-multi --watch",
39
+ "build": "tsc-multi",
40
+ "test": "yarn test:compile_program && yarn test:node:esm && yarn test:node:cjs",
41
+ "test:compile_program": "./scripts/compile_test_programs.sh",
42
+ "test:node:esm": "mocha --timeout 25000 --exit --config ./.mocharc.json",
43
+ "test:node:cjs": "mocha --timeout 25000 --exit --config ./.mocharc.cjs.json",
44
+ "prettier": "prettier 'src/**/*.ts'",
45
+ "prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
46
+ "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0",
47
+ "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)$1\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json",
48
+ "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish",
49
+ "clean": "rm -rf ./lib"
50
+ },
51
+ "devDependencies": {
52
+ "@types/chai": "^4",
53
+ "@types/mocha": "^10.0.1",
54
+ "@types/node": "^20.6.2",
55
+ "@types/prettier": "^3",
56
+ "chai": "^4.4.1",
57
+ "eslint": "^8.57.0",
58
+ "eslint-plugin-prettier": "^5.1.3",
59
+ "mocha": "^10.2.0",
60
+ "prettier": "3.2.5",
61
+ "ts-node": "^10.9.1",
62
+ "tsc-multi": "^1.1.0",
63
+ "tsx": "^4.6.2",
64
+ "typescript": "^5.4.2"
65
+ }
66
+ }