-
Notifications
You must be signed in to change notification settings - Fork 234
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
fix: better error message for compute_note_hash_and_nullifier. #3097
Merged
Merged
Changes from all commits
Commits
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
113 changes: 113 additions & 0 deletions
113
yarn-project/acir-simulator/src/client/simulator.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,113 @@ | ||
import { CircuitsWasm, CompleteAddress } from '@aztec/circuits.js'; | ||
import { computeUniqueCommitment, siloCommitment } from '@aztec/circuits.js/abis'; | ||
import { pedersenHashInputs } from '@aztec/circuits.js/barretenberg'; | ||
import { ABIParameterVisibility } from '@aztec/foundation/abi'; | ||
import { AztecAddress } from '@aztec/foundation/aztec-address'; | ||
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields'; | ||
import { TokenContractArtifact } from '@aztec/noir-contracts/artifacts'; | ||
|
||
import { MockProxy, mock } from 'jest-mock-extended'; | ||
|
||
import { getFunctionArtifact } from '../test/utils.js'; | ||
import { DBOracle, FunctionArtifactWithDebugMetadata } from './db_oracle.js'; | ||
import { AcirSimulator } from './simulator.js'; | ||
|
||
describe('Simulator', () => { | ||
let oracle: MockProxy<DBOracle>; | ||
let simulator: AcirSimulator; | ||
let circuitsWasm: CircuitsWasm; | ||
let ownerCompleteAddress: CompleteAddress; | ||
let owner: AztecAddress; | ||
const ownerPk = GrumpkinScalar.fromString('2dcc5485a58316776299be08c78fa3788a1a7961ae30dc747fb1be17692a8d32'); | ||
|
||
const hashFields = (data: Fr[]) => | ||
Fr.fromBuffer( | ||
pedersenHashInputs( | ||
circuitsWasm, | ||
data.map(f => f.toBuffer()), | ||
), | ||
); | ||
|
||
beforeAll(async () => { | ||
circuitsWasm = await CircuitsWasm.get(); | ||
|
||
ownerCompleteAddress = await CompleteAddress.fromPrivateKeyAndPartialAddress(ownerPk, Fr.random()); | ||
owner = ownerCompleteAddress.address; | ||
}); | ||
|
||
beforeEach(() => { | ||
oracle = mock<DBOracle>(); | ||
oracle.getSecretKey.mockResolvedValue(ownerPk); | ||
oracle.getCompleteAddress.mockResolvedValue(ownerCompleteAddress); | ||
|
||
simulator = new AcirSimulator(oracle); | ||
}); | ||
|
||
describe('computeNoteHashAndNullifier', () => { | ||
const artifact = getFunctionArtifact(TokenContractArtifact, 'compute_note_hash_and_nullifier'); | ||
const contractAddress = AztecAddress.random(); | ||
const nonce = Fr.random(); | ||
const storageSlot = Fr.random(); | ||
|
||
const createPreimage = (amount = 123n) => [new Fr(amount), owner.toField(), Fr.random()]; | ||
|
||
it('should compute note hashes and nullifier', async () => { | ||
oracle.getFunctionArtifactByName.mockResolvedValue(artifact); | ||
|
||
const preimage = createPreimage(); | ||
const valueNoteHash = hashFields(preimage); | ||
const innerNoteHash = hashFields([storageSlot, valueNoteHash]); | ||
const siloedNoteHash = siloCommitment(circuitsWasm, contractAddress, innerNoteHash); | ||
const uniqueSiloedNoteHash = computeUniqueCommitment(circuitsWasm, nonce, siloedNoteHash); | ||
const innerNullifier = hashFields([uniqueSiloedNoteHash, ownerPk.low, ownerPk.high]); | ||
|
||
const result = await simulator.computeNoteHashAndNullifier(contractAddress, nonce, storageSlot, preimage); | ||
|
||
expect(result).toEqual({ | ||
innerNoteHash, | ||
siloedNoteHash, | ||
uniqueSiloedNoteHash, | ||
innerNullifier, | ||
}); | ||
}); | ||
|
||
it('throw if the contract does not implement "compute_note_hash_and_nullifier"', async () => { | ||
oracle.getFunctionArtifactByName.mockResolvedValue(undefined); | ||
|
||
const preimage = createPreimage(); | ||
await expect( | ||
simulator.computeNoteHashAndNullifier(contractAddress, nonce, storageSlot, preimage), | ||
).rejects.toThrowError(/Mandatory implementation of "compute_note_hash_and_nullifier" missing/); | ||
}); | ||
|
||
it('throw if a note has more fields than "compute_note_hash_and_nullifier" can process', async () => { | ||
const preimage = createPreimage(); | ||
const wrongPreimageLength = preimage.length - 1; | ||
|
||
const modifiedArtifact: FunctionArtifactWithDebugMetadata = { | ||
...artifact, | ||
parameters: [ | ||
...artifact.parameters.slice(0, -1), | ||
{ | ||
name: 'preimage', | ||
type: { | ||
kind: 'array', | ||
length: wrongPreimageLength, | ||
type: { | ||
kind: 'field', | ||
}, | ||
}, | ||
visibility: ABIParameterVisibility.SECRET, | ||
}, | ||
], | ||
}; | ||
oracle.getFunctionArtifactByName.mockResolvedValue(modifiedArtifact); | ||
|
||
await expect( | ||
simulator.computeNoteHashAndNullifier(contractAddress, nonce, storageSlot, preimage), | ||
).rejects.toThrowError( | ||
new RegExp(`"compute_note_hash_and_nullifier" can only handle a maximum of ${wrongPreimageLength} fields`), | ||
); | ||
}); | ||
}); | ||
}); |
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
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.
this error message seems different to what you used in the test file ?
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.
The test matches part of the message, which (I think) is the most important part that tells users the "compute_note_hash_and_nullifier" function current only takes n fields.
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.
ah yea!