-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: TomAFrench <tom@tomfren.ch>
- Loading branch information
1 parent
eeb5381
commit e79f1ed
Showing
28 changed files
with
909 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export declare const read_file: (source_id: string) => string; | ||
export declare function initializeResolver( | ||
resolver: (source_id: string) => string | ||
resolver: (source_id: string) => string, | ||
): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
test/backend/barretenberg.ts |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!test/noir_compiled_examples/*/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"require": "ts-node/register", | ||
"loader": "ts-node/esm", | ||
"extensions": ["ts"], | ||
"spec": [ | ||
"test/node/**/*.test.ts*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"parser": "typescript", | ||
"printWidth": 120, | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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') { | ||
// Node.js environment | ||
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
import * as acvm from "@noir-lang/acvm_js"; | ||
import * as noirc from "@noir-lang/noirc_abi"; | ||
export { acvm, noirc }; | ||
import * as acvm from '@noir-lang/acvm_js'; | ||
import * as abi from '@noir-lang/noirc_abi'; | ||
|
||
export { acvm, abi }; | ||
|
||
import { generateWitness } from './witness_generation.js'; | ||
import { acirToUint8Array, witnessMapToUint8Array } from './serialize.js'; | ||
export { acirToUint8Array, witnessMapToUint8Array, generateWitness }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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 { | ||
// TODO: We just want to serialize, but this will zip up the witness | ||
// TODO so its not ideal | ||
const compressedWitness = compressWitness(solvedWitness); | ||
return gunzip(compressedWitness); | ||
} | ||
|
||
// Converts an bytecode to a Uint8Array | ||
export function acirToUint8Array(base64EncodedBytecode): Uint8Array { | ||
const compressedByteCode = base64Decode(base64EncodedBytecode); | ||
return gunzip(compressedByteCode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { abiEncode } from '@noir-lang/noirc_abi'; | ||
import { validateInputs } from './input_validation.js'; | ||
import { base64Decode } from './base64_decode.js'; | ||
import { WitnessMap, executeCircuit } from '@noir-lang/acvm_js'; | ||
|
||
// Generates the witnesses needed to feed into the chosen proving system | ||
export async function generateWitness(compiledProgram, inputs): Promise<WitnessMap> { | ||
// Validate inputs | ||
const { isValid, error } = validateInputs(inputs, compiledProgram.abi); | ||
if (!isValid) { | ||
throw new Error(error?.toString()); | ||
} | ||
const witnessMap = abiEncode(compiledProgram.abi, inputs, null); | ||
|
||
// Execute the circuit to generate the rest of the witnesses | ||
try { | ||
const solvedWitness = await executeCircuit(base64Decode(compiledProgram.bytecode), witnessMap, () => { | ||
throw Error('unexpected oracle during execution'); | ||
}); | ||
return solvedWitness; | ||
} catch (err) { | ||
throw new Error(`Circuit execution failed: ${err}`); | ||
} | ||
} |
Oops, something went wrong.