diff --git a/tooling/noir_js/src/input_validation.ts b/tooling/noir_js/src/input_validation.ts deleted file mode 100644 index 9a19528a0cf..00000000000 --- a/tooling/noir_js/src/input_validation.ts +++ /dev/null @@ -1,55 +0,0 @@ -// Check if all of the input values are correct according to the ABI -export function validateInputs(inputs, abi) { - for (const param of abi.parameters) { - const inputValue = inputs[param.name]; - if (inputValue === undefined) { - // This is checked by noirc_abi, so we could likely remove this check - return { isValid: false, error: `Input for ${param.name} is missing` }; - } - if (!checkType(inputValue, param.type)) { - return { - isValid: false, - error: `Input for ${param.name} is the wrong type, expected ${type_to_string(param.type)}, got "${inputValue}"`, - }; - } - } - return { isValid: true, error: null }; -} - -// Checks that value is of type "type" -// Where type is taken from the abi -function checkType(value, type) { - switch (type.kind) { - case 'integer': - if (type.sign === 'unsigned') { - return isUnsignedInteger(value, type.width); - } - // Other integer sign checks can be added here - break; - // Other type.kind checks can be added here - } - return false; -} - -function type_to_string(type): string { - switch (type.kind) { - case 'integer': - if (type.sign === 'unsigned') { - return `uint${type.width}`; - } - break; - case 'array': - return `${type_to_string(type.element)}[${type.length}]`; - } - return 'unknown type'; -} - -// Returns true if `value` is an unsigned integer that is less than 2^{width} -function isUnsignedInteger(value: bigint, width: bigint) { - try { - const bigIntValue = BigInt(value); - return bigIntValue >= 0 && bigIntValue <= BigInt(2) ** BigInt(width) - 1n; - } catch (e) { - return false; // Not a valid integer - } -} diff --git a/tooling/noir_js/src/witness_generation.ts b/tooling/noir_js/src/witness_generation.ts index c117adcf087..b8f594a5fc5 100644 --- a/tooling/noir_js/src/witness_generation.ts +++ b/tooling/noir_js/src/witness_generation.ts @@ -1,16 +1,11 @@ import { abiEncode } from '@noir-lang/noirc_abi'; -import { validateInputs } from './input_validation.js'; import { base64Decode } from './base64_decode.js'; import { executeCircuit } from '@noir-lang/acvm_js'; import { witnessMapToUint8Array } from './serialize.js'; // Generates the witnesses needed to feed into the chosen proving system export async function generateWitness(compiledProgram, inputs): Promise { - // Validate inputs - const { isValid, error } = validateInputs(inputs, compiledProgram.abi); - if (!isValid) { - throw new Error(error?.toString()); - } + // Throws on ABI encoding error const witnessMap = abiEncode(compiledProgram.abi, inputs, null); // Execute the circuit to generate the rest of the witnesses and serialize diff --git a/tooling/noir_js/test/node/cjs.test.cjs b/tooling/noir_js/test/node/cjs.test.cjs index 0eddb3cc4af..b7b30d7dcdb 100644 --- a/tooling/noir_js/test/node/cjs.test.cjs +++ b/tooling/noir_js/test/node/cjs.test.cjs @@ -70,7 +70,11 @@ describe('input validation', () => { chai.expect.fail('Expected generatedWitness to throw, due to x not being convertible to a uint64'); } catch (error) { const knownError = error; - chai.expect(knownError.message).to.equal('Input for x is the wrong type, expected uint64, got "foo"'); + chai + .expect(knownError.message) + .to.equal( + 'Expected witness values to be integers, provided value causes `invalid digit found in string` error', + ); } }); }); diff --git a/tooling/noir_js/test/node/smoke.test.ts b/tooling/noir_js/test/node/smoke.test.ts index 179246745fe..4b0291c0f41 100644 --- a/tooling/noir_js/test/node/smoke.test.ts +++ b/tooling/noir_js/test/node/smoke.test.ts @@ -7,7 +7,7 @@ it('generates witnesses successfully', async () => { x: '2', y: '3', }; - const _solvedWitness = await generateWitness(assert_lt_json, inputs); + expect(() => generateWitness(assert_lt_json, inputs)).to.not.throw; }); it('string input and number input are the same', async () => { @@ -66,7 +66,9 @@ describe('input validation', () => { 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('Input for x is the wrong type, expected uint64, got "foo"'); + expect(knownError.message).to.equal( + 'Expected witness values to be integers, provided value causes `invalid digit found in string` error', + ); } }); });