Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Sep 29, 2023
1 parent ef6ad1c commit 01aee47
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 73 deletions.
8 changes: 4 additions & 4 deletions tooling/noir_js/src/base64_decode.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 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: string): Uint8Array {
if (typeof Buffer !== 'undefined') {
if (typeof Buffer !== "undefined") {
// Node.js environment
return Buffer.from(input, 'base64');
} else if (typeof atob === 'function') {
return Buffer.from(input, "base64");
} else if (typeof atob === "function") {
// Browser environment
return Uint8Array.from(atob(input), (c) => c.charCodeAt(0));
} else {
throw new Error('No implementation found for base64 decoding.');
throw new Error("No implementation found for base64 decoding.");
}
}
10 changes: 5 additions & 5 deletions tooling/noir_js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as acvm from '@noir-lang/acvm_js';
import * as abi from '@noir-lang/noirc_abi';
import * as acvm from "@noir-lang/acvm_js";
import * as abi from "@noir-lang/noirc_abi";

export { acvm, abi };

export { generateWitness } from './witness_generation.js';
export { acirToUint8Array, witnessMapToUint8Array } from './serialize.js';
export { generateWitness } from "./witness_generation.js";
export { acirToUint8Array, witnessMapToUint8Array } from "./serialize.js";

export { Noir } from './program.js';
export { Noir } from "./program.js";
4 changes: 2 additions & 2 deletions tooling/noir_js/src/program.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Backend, CompiledCircuit } from '@noir-lang/types';
import { generateWitness } from './witness_generation.js';
import { Backend, CompiledCircuit } from "@noir-lang/types";
import { generateWitness } from "./witness_generation.js";

export class Noir {
constructor(
Expand Down
6 changes: 3 additions & 3 deletions tooling/noir_js/src/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WitnessMap, compressWitness } from '@noir-lang/acvm_js';
import { decompressSync as gunzip } from 'fflate';
import { base64Decode } from './base64_decode.js';
import { WitnessMap, compressWitness } from "@noir-lang/acvm_js";
import { decompressSync as gunzip } from "fflate";
import { base64Decode } from "./base64_decode.js";

// After solving the witness, to pass it a backend, we need to serialize it to a Uint8Array
export function witnessMapToUint8Array(solvedWitness: WitnessMap): Uint8Array {
Expand Down
25 changes: 16 additions & 9 deletions tooling/noir_js/src/witness_generation.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { abiEncode } from '@noir-lang/noirc_abi';
import { base64Decode } from './base64_decode.js';
import { executeCircuit } from '@noir-lang/acvm_js';
import { witnessMapToUint8Array } from './serialize.js';
import { CompiledCircuit } from '@noir-lang/types';
import { abiEncode } from "@noir-lang/noirc_abi";
import { base64Decode } from "./base64_decode.js";
import { executeCircuit } from "@noir-lang/acvm_js";
import { witnessMapToUint8Array } from "./serialize.js";
import { CompiledCircuit } from "@noir-lang/types";

// Generates the witnesses needed to feed into the chosen proving system
export async function generateWitness(compiledProgram: CompiledCircuit, inputs: unknown): Promise<Uint8Array> {
export async function generateWitness(
compiledProgram: CompiledCircuit,
inputs: unknown,
): Promise<Uint8Array> {
// Throws on ABI encoding error
const witnessMap = abiEncode(compiledProgram.abi, inputs, null);

// Execute the circuit to generate the rest of the witnesses and serialize
// them into a Uint8Array.
try {
const solvedWitness = await executeCircuit(base64Decode(compiledProgram.bytecode), witnessMap, () => {
throw Error('unexpected oracle during execution');
});
const solvedWitness = await executeCircuit(
base64Decode(compiledProgram.bytecode),
witnessMap,
() => {
throw Error("unexpected oracle during execution");
},
);
return witnessMapToUint8Array(solvedWitness);
} catch (err) {
throw new Error(`Circuit execution failed: ${err}`);
Expand Down
54 changes: 30 additions & 24 deletions tooling/noir_js/test/node/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect } from 'chai';
import assert_lt_json from '../noir_compiled_examples/assert_lt/target/assert_lt.json' assert { type: 'json' };
import { generateWitness } from '../../src/index.js';
import { Noir } from '../../src/program.js';
import { BarretenbergBackend as Backend } from '../backend/barretenberg.js';
import { expect } from "chai";
import assert_lt_json from "../noir_compiled_examples/assert_lt/target/assert_lt.json" assert { type: "json" };
import { generateWitness } from "../../src/index.js";
import { Noir } from "../../src/program.js";
import { BarretenbergBackend as Backend } from "../backend/barretenberg.js";

it('end-to-end proof creation and verification (outer)', async () => {
it("end-to-end proof creation and verification (outer)", async () => {
// Noir.Js part
const inputs = {
x: '2',
y: '3',
x: "2",
y: "3",
};
const serializedWitness = await generateWitness(assert_lt_json, inputs);

Expand All @@ -23,11 +23,11 @@ it('end-to-end proof creation and verification (outer)', async () => {
expect(isValid).to.be.true;
});

it('end-to-end proof creation and verification (outer) -- Program API', async () => {
it("end-to-end proof creation and verification (outer) -- Program API", async () => {
// Noir.Js part
const inputs = {
x: '2',
y: '3',
x: "2",
y: "3",
};

// Initialize backend
Expand All @@ -42,11 +42,11 @@ it('end-to-end proof creation and verification (outer) -- Program API', async ()
expect(isValid).to.be.true;
});

it('end-to-end proof creation and verification (inner)', async () => {
it("end-to-end proof creation and verification (inner)", async () => {
// Noir.Js part
const inputs = {
x: '2',
y: '3',
x: "2",
y: "3",
};
const serializedWitness = await generateWitness(assert_lt_json, inputs);

Expand All @@ -73,11 +73,11 @@ it('end-to-end proof creation and verification (inner)', async () => {
// a prover and verifier class to more accurately reflect what happens in production.
//
// If its not fixable, we can leave it in as documentation of this behavior.
it('[BUG] -- bb.js null function or function signature mismatch (different instance) ', async () => {
it("[BUG] -- bb.js null function or function signature mismatch (different instance) ", async () => {
// Noir.Js part
const inputs = {
x: '2',
y: '3',
x: "2",
y: "3",
};
const serializedWitness = await generateWitness(assert_lt_json, inputs);

Expand All @@ -90,11 +90,13 @@ it('[BUG] -- bb.js null function or function signature mismatch (different insta
const verifier = await Backend.initialize(assert_lt_json);
await verifier.verifyFinalProof(proof);
expect.fail(
'bb.js currently returns a bug when we try to verify a proof with a different Barretenberg instance that created it.',
"bb.js currently returns a bug when we try to verify a proof with a different Barretenberg instance that created it.",
);
} catch (error) {
const knownError = error as Error;
expect(knownError.message).to.contain('null function or function signature mismatch');
expect(knownError.message).to.contain(
"null function or function signature mismatch",
);
}
});

Expand All @@ -105,11 +107,11 @@ it('[BUG] -- bb.js null function or function signature mismatch (different insta
// If we only create one type of proof, then this works as expected.
//
// If we do not create an inner proof, then this will work as expected.
it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ', async () => {
it("[BUG] -- bb.js null function or function signature mismatch (outer-inner) ", async () => {
// Noir.Js part
const inputs = {
x: '2',
y: '3',
x: "2",
y: "3",
};
const serializedWitness = await generateWitness(assert_lt_json, inputs);

Expand All @@ -131,9 +133,13 @@ it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ',
// We can also try verifying an inner proof and it will fail.
// const isValidInner = await prover.verifyInnerProof(_proofInner);
// expect(isValidInner).to.be.true;
expect.fail('bb.js currently returns a bug when we try to verify an inner and outer proof with the same backend');
expect.fail(
"bb.js currently returns a bug when we try to verify an inner and outer proof with the same backend",
);
} catch (error) {
const knownError = error as Error;
expect(knownError.message).to.contain('null function or function signature mismatch');
expect(knownError.message).to.contain(
"null function or function signature mismatch",
);
}
});
68 changes: 42 additions & 26 deletions tooling/noir_js/test/node/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,89 @@
import { expect } from 'chai';
import assert_lt_json from '../noir_compiled_examples/assert_lt/target/assert_lt.json' assert { type: 'json' };
import { generateWitness } from '../../src/index.js';
import { expect } from "chai";
import assert_lt_json from "../noir_compiled_examples/assert_lt/target/assert_lt.json" assert { type: "json" };
import { generateWitness } from "../../src/index.js";

it('generates witnesses successfully', async () => {
it("generates witnesses successfully", async () => {
const inputs = {
x: '2',
y: '3',
x: "2",
y: "3",
};
expect(() => generateWitness(assert_lt_json, inputs)).to.not.throw;
});

it('string input and number input are the same', async () => {
it("string input and number input are the same", async () => {
const inputsString = {
x: '2',
y: '3',
x: "2",
y: "3",
};
const inputsNumber = {
x: 2,
y: 3,
};
const solvedWitnessString = await generateWitness(assert_lt_json, inputsString);
const solvedWitnessNumber = await generateWitness(assert_lt_json, inputsNumber);
const solvedWitnessString = await generateWitness(
assert_lt_json,
inputsString,
);
const solvedWitnessNumber = await generateWitness(
assert_lt_json,
inputsNumber,
);
expect(solvedWitnessString).to.deep.equal(solvedWitnessNumber);
});

it('string input and number input are the same', async () => {
it("string input and number input are the same", async () => {
const inputsString = {
x: '2',
y: '3',
x: "2",
y: "3",
};
const inputsNumber = {
x: 2,
y: 3,
};

const solvedWitnessString = await generateWitness(assert_lt_json, inputsString);
const solvedWitnessNumber = await generateWitness(assert_lt_json, inputsNumber);
const solvedWitnessString = await generateWitness(
assert_lt_json,
inputsString,
);
const solvedWitnessNumber = await generateWitness(
assert_lt_json,
inputsNumber,
);
expect(solvedWitnessString).to.deep.equal(solvedWitnessNumber);
});

it('0x prefixed string input for inputs will throw', async () => {
it("0x prefixed string input for inputs will throw", async () => {
const inputsHexPrefix = {
x: '0x2',
y: '0x3',
x: "0x2",
y: "0x3",
};

try {
await generateWitness(assert_lt_json, inputsHexPrefix);
expect.fail('Expected generatedWitness to throw, due to inputs being prefixed with 0x. Currently not supported');
expect.fail(
"Expected generatedWitness to throw, due to inputs being prefixed with 0x. Currently not supported",
);
} catch (error) {
// Successfully errored due to 0x not being supported. Update this test once/if we choose
// to support 0x prefixed inputs.
}
});

describe('input validation', () => {
it('x should be a uint64 not a string', async () => {
describe("input validation", () => {
it("x should be a uint64 not a string", async () => {
const inputs = {
x: 'foo',
y: '3',
x: "foo",
y: "3",
};

try {
await generateWitness(assert_lt_json, inputs);
expect.fail('Expected generatedWitness to throw, due to x not being convertible to a uint64');
expect.fail(
"Expected generatedWitness to throw, due to x not being convertible to a uint64",
);
} catch (error) {
const knownError = error as Error;
expect(knownError.message).to.equal(
'Expected witness values to be integers, provided value causes `invalid digit found in string` error',
"Expected witness values to be integers, provided value causes `invalid digit found in string` error",
);
}
});
Expand Down

0 comments on commit 01aee47

Please sign in to comment.