-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: send and receive unflattened public inputs to backend #3543
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
207f611
feat: send and receive unflattened public inputs to backend
TomAFrench f07651c
Update tooling/noir_js_backend_barretenberg/src/index.ts
TomAFrench 3bbe275
chore: flatten inputs before feeding them into smart contract
TomAFrench 70c0bb7
chore: add helper functions for flattening/unflattening public inputs
TomAFrench 53e0c33
chore: reexport types through `@noir-lang/types`
TomAFrench 49f7cca
chore: fix integration tests
TomAFrench a1543b5
Merge branch 'master' into tf/unflatten-backend-inputs
TomAFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Abi, WitnessMap } from '@noir-lang/types'; | ||
|
||
export function flattenPublicInputs(publicInputs: WitnessMap): string[] { | ||
const publicInputIndices = [...publicInputs.keys()].sort(); | ||
const flattenedPublicInputs = publicInputIndices.map((index) => publicInputs.get(index) as string); | ||
return flattenedPublicInputs; | ||
} | ||
|
||
export function flattenPublicInputsAsArray(publicInputs: WitnessMap): Uint8Array { | ||
const flatPublicInputs = flattenPublicInputs(publicInputs); | ||
const flattenedPublicInputs = flatPublicInputs.map(hexToUint8Array); | ||
return flattenUint8Arrays(flattenedPublicInputs); | ||
} | ||
|
||
export function deflattenPublicInputs(flattenedPublicInputs: Uint8Array, abi: Abi): WitnessMap { | ||
const publicInputSize = 32; | ||
const chunkedFlattenedPublicInputs: Uint8Array[] = []; | ||
|
||
for (let i = 0; i < flattenedPublicInputs.length; i += publicInputSize) { | ||
const publicInput = flattenedPublicInputs.slice(i, i + publicInputSize); | ||
chunkedFlattenedPublicInputs.push(publicInput); | ||
} | ||
|
||
const return_value_witnesses = abi.return_witnesses; | ||
const public_parameters = abi.parameters.filter((param) => param.visibility === 'public'); | ||
const public_parameter_witnesses: number[] = public_parameters.flatMap((param) => | ||
abi.param_witnesses[param.name].flatMap((witness_range) => | ||
Array.from({ length: witness_range.end - witness_range.start }, (_, i) => witness_range.start + i), | ||
), | ||
); | ||
|
||
// We now have an array of witness indices which have been deduplicated and sorted in ascending order. | ||
// The elements of this array should correspond to the elements of `flattenedPublicInputs` so that we can build up a `WitnessMap`. | ||
const public_input_witnesses = [...new Set(public_parameter_witnesses.concat(return_value_witnesses))].sort(); | ||
|
||
const publicInputs: WitnessMap = new Map(); | ||
public_input_witnesses.forEach((witness_index, index) => { | ||
const witness_value = uint8ArrayToHex(chunkedFlattenedPublicInputs[index]); | ||
publicInputs.set(witness_index, witness_value); | ||
}); | ||
|
||
return publicInputs; | ||
} | ||
|
||
function flattenUint8Arrays(arrays: Uint8Array[]): Uint8Array { | ||
const totalLength = arrays.reduce((acc, val) => acc + val.length, 0); | ||
const result = new Uint8Array(totalLength); | ||
|
||
let offset = 0; | ||
for (const arr of arrays) { | ||
result.set(arr, offset); | ||
offset += arr.length; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function uint8ArrayToHex(buffer: Uint8Array): string { | ||
const hex: string[] = []; | ||
|
||
buffer.forEach(function (i) { | ||
let h = i.toString(16); | ||
if (h.length % 2) { | ||
h = '0' + h; | ||
} | ||
hex.push(h); | ||
}); | ||
|
||
return '0x' + hex.join(''); | ||
} | ||
|
||
function hexToUint8Array(hex: string): Uint8Array { | ||
const sanitised_hex = BigInt(hex).toString(16).padStart(64, '0'); | ||
|
||
const len = sanitised_hex.length / 2; | ||
const u8 = new Uint8Array(len); | ||
|
||
let i = 0; | ||
let j = 0; | ||
while (i < len) { | ||
u8[i] = parseInt(sanitised_hex.slice(j, j + 2), 16); | ||
i += 1; | ||
j += 2; | ||
} | ||
|
||
return u8; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @noir-lang/developerrelations might have to update https://github.com/noir-lang/noir-examples/.