-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Flatten public inputs according to their index in numerial rathe…
…r than ascii order (#3605)
- Loading branch information
1 parent
cc041fe
commit a1f6343
Showing
6 changed files
with
142 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"require": "ts-node/register", | ||
"loader": "ts-node/esm", | ||
"extensions": [ | ||
"ts", | ||
"cjs" | ||
], | ||
"spec": [ | ||
"test/**/*.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
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
93 changes: 93 additions & 0 deletions
93
tooling/noir_js_backend_barretenberg/test/public_input_deflattening.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,93 @@ | ||
import { Abi } from '@noir-lang/types'; | ||
import { expect } from 'chai'; | ||
import { flattenPublicInputsAsArray, deflattenPublicInputs, flattenPublicInputs } from '../src/public_inputs.js'; | ||
|
||
const abi: Abi = { | ||
parameters: [ | ||
{ | ||
name: 'array_with_returned_element', | ||
type: { | ||
kind: 'array', | ||
type: { | ||
kind: 'field', | ||
}, | ||
length: 10, | ||
}, | ||
visibility: 'private', | ||
}, | ||
{ | ||
name: 'pub_field', | ||
type: { | ||
kind: 'field', | ||
}, | ||
visibility: 'public', | ||
}, | ||
], | ||
param_witnesses: { | ||
array_with_returned_element: [ | ||
{ | ||
start: 1, | ||
end: 11, | ||
}, | ||
], | ||
pub_field: [ | ||
{ | ||
start: 11, | ||
end: 12, | ||
}, | ||
], | ||
}, | ||
return_type: { | ||
kind: 'tuple', | ||
fields: [ | ||
{ | ||
kind: 'field', | ||
}, | ||
{ | ||
kind: 'field', | ||
}, | ||
{ | ||
kind: 'field', | ||
}, | ||
], | ||
}, | ||
return_witnesses: [2, 13, 13], | ||
}; | ||
|
||
it('flattens a witness map in order of its witness indices', async () => { | ||
// Note that these are not in ascending order. This means that if we read from `witness_map` in insertion order | ||
// then the witness values will be sorted incorrectly. | ||
const public_input_indices = [2, 13, 11]; | ||
|
||
const witness_map = new Map( | ||
public_input_indices.map((witness_index) => [ | ||
witness_index, | ||
'0x' + BigInt(witness_index).toString(16).padStart(64, '0'), | ||
]), | ||
); | ||
|
||
const flattened_public_inputs = flattenPublicInputs(witness_map); | ||
expect(flattened_public_inputs).to.be.deep.eq([ | ||
'0x0000000000000000000000000000000000000000000000000000000000000002', | ||
'0x000000000000000000000000000000000000000000000000000000000000000b', | ||
'0x000000000000000000000000000000000000000000000000000000000000000d', | ||
]); | ||
}); | ||
|
||
it('recovers the original witness map when deflattening a public input array', async () => { | ||
// Note that these are not in ascending order. This means that if we read from `witness_map` in insertion order | ||
// then the witness values will be sorted incorrectly. | ||
const public_input_indices = [2, 13, 11]; | ||
|
||
const witness_map = new Map( | ||
public_input_indices.map((witness_index) => [ | ||
witness_index, | ||
'0x' + BigInt(witness_index).toString(16).padStart(64, '0'), | ||
]), | ||
); | ||
|
||
const flattened_public_inputs = flattenPublicInputsAsArray(witness_map); | ||
const deflattened_public_inputs = deflattenPublicInputs(flattened_public_inputs, abi); | ||
|
||
expect(deflattened_public_inputs).to.be.deep.eq(witness_map); | ||
}); |
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