-
Notifications
You must be signed in to change notification settings - Fork 613
feat: hashing output of serialize() in noir + more tests
#4365
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
Merged
benesjan
merged 14 commits into
master
from
02-01-feat_hashing_output_of_serialize_in_noir
Feb 2, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3cf77de
feat: hashing output of serialize() in noir
benesjan 56cd2d1
cleanup of PrivateCircuitPublicInputs.hash
benesjan 034920f
using traits
benesjan 2801443
CallContext traits + smoke tests
benesjan 48d7696
header traits + smoke
benesjan 061c069
traits in global vars + test
benesjan 43af0fd
StateReference traits + test
benesjan 6b21053
testing state reference
benesjan e859eaa
testing partial
benesjan d2507c4
parsing issue workaround
benesjan b72f663
testing global variables
benesjan f66f00e
fix
benesjan 9801462
cleanup
benesjan 2fb99f3
updated test data
benesjan 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,24 +7,14 @@ import { boolToBuffer, numToUInt8, numToUInt16BE, numToUInt32BE } from '@aztec/f | |
| import { Buffer } from 'buffer'; | ||
| import chunk from 'lodash.chunk'; | ||
|
|
||
| import { | ||
| FUNCTION_SELECTOR_NUM_BYTES, | ||
| FUNCTION_TREE_HEIGHT, | ||
| GeneratorIndex, | ||
| PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH, | ||
| PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH, | ||
| } from '../constants.gen.js'; | ||
| import { FUNCTION_SELECTOR_NUM_BYTES, FUNCTION_TREE_HEIGHT, GeneratorIndex } from '../constants.gen.js'; | ||
| import { MerkleTreeCalculator } from '../merkle/merkle_tree_calculator.js'; | ||
| import { | ||
| CallContext, | ||
| ContractDeploymentData, | ||
| ContractStorageRead, | ||
| ContractStorageUpdateRequest, | ||
| FunctionData, | ||
| FunctionLeafPreimage, | ||
| NewContractData, | ||
| PrivateCallStackItem, | ||
| PrivateCircuitPublicInputs, | ||
| PublicCallStackItem, | ||
| PublicCircuitPublicInputs, | ||
| SideEffect, | ||
|
|
@@ -356,60 +346,6 @@ function computeContractDeploymentDataHash(data: ContractDeploymentData): Fr { | |
| ); | ||
| } | ||
|
|
||
| function computeCallContextHash(input: CallContext) { | ||
| return pedersenHash( | ||
| [ | ||
| input.msgSender.toBuffer(), | ||
| input.storageContractAddress.toBuffer(), | ||
| input.portalContractAddress.toBuffer(), | ||
| input.functionSelector.toBuffer(), | ||
| boolToBuffer(input.isDelegateCall, 32), | ||
| boolToBuffer(input.isStaticCall, 32), | ||
| boolToBuffer(input.isContractDeployment, 32), | ||
| numToUInt32BE(input.startSideEffectCounter, 32), | ||
| ], | ||
| GeneratorIndex.CALL_CONTEXT, | ||
| ); | ||
| } | ||
|
|
||
| function computePrivateInputsHash(input: PrivateCircuitPublicInputs) { | ||
| const toHash = [ | ||
| computeCallContextHash(input.callContext), | ||
| input.argsHash.toBuffer(), | ||
| ...input.returnValues.map(fr => fr.toBuffer()), | ||
| ...input.readRequests | ||
| .map(rr => rr.toFields()) | ||
| .flat() | ||
| .map(fr => fr.toBuffer()), | ||
| ...input.newCommitments | ||
| .map(n => n.toFields()) | ||
| .flat() | ||
| .map(fr => fr.toBuffer()), | ||
| ...input.newNullifiers | ||
| .map(n => n.toFields()) | ||
| .flat() | ||
| .map(fr => fr.toBuffer()), | ||
| ...input.privateCallStackHashes.map(fr => fr.toBuffer()), | ||
| ...input.publicCallStackHashes.map(fr => fr.toBuffer()), | ||
| ...input.newL2ToL1Msgs.map(fr => fr.toBuffer()), | ||
| input.endSideEffectCounter.toBuffer(), | ||
| ...input.encryptedLogsHash.map(fr => fr.toBuffer()), | ||
| ...input.unencryptedLogsHash.map(fr => fr.toBuffer()), | ||
| input.encryptedLogPreimagesLength.toBuffer(), | ||
| input.unencryptedLogPreimagesLength.toBuffer(), | ||
| ...(input.historicalHeader.toFields().map(fr => fr.toBuffer()) as Buffer[]), | ||
| computeContractDeploymentDataHash(input.contractDeploymentData).toBuffer(), | ||
| input.chainId.toBuffer(), | ||
| input.version.toBuffer(), | ||
| ]; | ||
| if (toHash.length != PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH) { | ||
| throw new Error( | ||
| `Incorrect number of input fields when hashing PrivateCircuitPublicInputs ${toHash.length}, ${PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH}`, | ||
| ); | ||
| } | ||
| return pedersenHash(toHash, GeneratorIndex.PRIVATE_CIRCUIT_PUBLIC_INPUTS); | ||
| } | ||
|
|
||
| /** | ||
| * Computes a call stack item hash. | ||
| * @param callStackItem - The call stack item. | ||
|
|
@@ -421,24 +357,13 @@ export function computePrivateCallStackItemHash(callStackItem: PrivateCallStackI | |
| [ | ||
| callStackItem.contractAddress.toBuffer(), | ||
| computeFunctionDataHash(callStackItem.functionData).toBuffer(), | ||
| computePrivateInputsHash(callStackItem.publicInputs), | ||
| callStackItem.publicInputs.hash().toBuffer(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😎 |
||
| ], | ||
| GeneratorIndex.CALL_STACK_ITEM, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| function computeContractStorageUpdateRequestHash(input: ContractStorageUpdateRequest) { | ||
| return pedersenHash( | ||
| [input.storageSlot.toBuffer(), input.oldValue.toBuffer(), input.newValue.toBuffer()], | ||
| GeneratorIndex.PUBLIC_DATA_UPDATE_REQUEST, | ||
| ); | ||
| } | ||
|
|
||
| function computeContractStorageReadsHash(input: ContractStorageRead) { | ||
| return pedersenHash([input.storageSlot.toBuffer(), input.currentValue.toBuffer()], GeneratorIndex.PUBLIC_DATA_READ); | ||
| } | ||
|
|
||
| export function computeCommitmentsHash(input: SideEffect) { | ||
| return pedersenHash([input.value.toBuffer(), input.counter.toBuffer()], GeneratorIndex.SIDE_EFFECT); | ||
| } | ||
|
|
@@ -450,28 +375,6 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) { | |
| ); | ||
| } | ||
|
|
||
| export function computePublicInputsHash(input: PublicCircuitPublicInputs) { | ||
| const toHash = [ | ||
| computeCallContextHash(input.callContext), | ||
| input.argsHash.toBuffer(), | ||
| ...input.returnValues.map(fr => fr.toBuffer()), | ||
| ...input.contractStorageUpdateRequests.map(computeContractStorageUpdateRequestHash), | ||
| ...input.contractStorageReads.map(computeContractStorageReadsHash), | ||
| ...input.publicCallStackHashes.map(fr => fr.toBuffer()), | ||
| ...input.newCommitments.map(computeCommitmentsHash), | ||
| ...input.newNullifiers.map(computeNullifierHash), | ||
| ...input.newL2ToL1Msgs.map(fr => fr.toBuffer()), | ||
| ...input.unencryptedLogsHash.map(fr => fr.toBuffer()), | ||
| input.unencryptedLogPreimagesLength.toBuffer(), | ||
| ...input.historicalHeader.toFields().map(fr => fr.toBuffer()), | ||
| input.proverAddress.toBuffer(), | ||
| ]; | ||
| if (toHash.length != PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH) { | ||
| throw new Error('Incorrect number of input fields when hashing PublicCircuitPublicInputs'); | ||
| } | ||
| return pedersenHash(toHash, GeneratorIndex.PUBLIC_CIRCUIT_PUBLIC_INPUTS); | ||
| } | ||
|
|
||
| /** | ||
| * Computes a call stack item hash. | ||
| * @param callStackItem - The call stack item. | ||
|
|
@@ -492,11 +395,7 @@ export function computePublicCallStackItemHash({ | |
|
|
||
| return Fr.fromBuffer( | ||
| pedersenHash( | ||
| [ | ||
| contractAddress.toBuffer(), | ||
| computeFunctionDataHash(functionData).toBuffer(), | ||
| computePublicInputsHash(publicInputs), | ||
| ], | ||
| [contractAddress.toBuffer(), computeFunctionDataHash(functionData).toBuffer(), publicInputs.hash().toBuffer()], | ||
| GeneratorIndex.CALL_STACK_ITEM, | ||
| ), | ||
| ); | ||
|
|
||
This file contains hidden or 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.
How come we are not doing this? 👀
The current implementation is not computing the single public inputs hash, so I know it is kinda unused currently, but it should be passed in when we are actually verifying the proofs in contract.
Edit: Oh, just moved inwards.