-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efa0842
commit cdd0d2b
Showing
20 changed files
with
862 additions
and
78 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
1 change: 1 addition & 0 deletions
1
noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base.nr
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,5 +1,6 @@ | ||
mod base_rollup_inputs; | ||
mod state_diff_hints; | ||
mod calldata_gas; | ||
|
||
use base_rollup_inputs::BaseRollupInputs; | ||
use crate::abis::base_or_merge_rollup_public_inputs::BaseOrMergeRollupPublicInputs; |
62 changes: 62 additions & 0 deletions
62
noir-projects/noir-protocol-circuits/crates/rollup-lib/src/base/calldata_gas.nr
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,62 @@ | ||
use dep::types::abis::accumulated_data::CombinedAccumulatedData; | ||
use dep::types::abis::side_effect::{SideEffect, SideEffectLinkedToNoteHash}; | ||
use dep::types::traits::is_empty; | ||
use dep::types::constants::{ | ||
MAX_NEW_NULLIFIERS_PER_TX, MAX_NEW_NOTE_HASHES_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX, | ||
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX | ||
}; | ||
|
||
global FIXED_DA_GAS: u64 = 272; | ||
global DA_GAS_PER_BYTE: u64 = 16; | ||
|
||
pub fn compute_calldata_da_gas(data: CombinedAccumulatedData) -> u64 { | ||
let mut non_zero_bytes = (data.unencrypted_log_preimages_length + data.encrypted_log_preimages_length) as u64; | ||
|
||
for i in 0..MAX_NEW_NOTE_HASHES_PER_TX { | ||
if !is_empty(data.new_note_hashes[i]) { | ||
non_zero_bytes += 32; | ||
} | ||
} | ||
|
||
for i in 0..MAX_NEW_NULLIFIERS_PER_TX { | ||
if !is_empty(data.new_nullifiers[i]) { | ||
non_zero_bytes += 32; | ||
} | ||
} | ||
|
||
for i in 0..MAX_NEW_L2_TO_L1_MSGS_PER_TX { | ||
if !is_empty(data.new_l2_to_l1_msgs[i]) { | ||
non_zero_bytes += 32; | ||
} | ||
} | ||
|
||
for i in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX { | ||
if !is_empty(data.public_data_update_requests[i]) { | ||
non_zero_bytes += 64; | ||
} | ||
} | ||
|
||
non_zero_bytes * DA_GAS_PER_BYTE + FIXED_DA_GAS | ||
} | ||
|
||
pub fn compute_calldata_compute_gas(data: CombinedAccumulatedData) -> u64 { | ||
data.compute_gas_used | ||
} | ||
|
||
mod tests { | ||
|
||
use dep::types::abis::accumulated_data::{CombinedAccumulatedDataBuilder, CombinedAccumulatedData}; | ||
|
||
use crate::base::calldata_gas::compute_calldata_da_gas; | ||
|
||
#[test] | ||
fn test_compute_calldata_gas() { | ||
let mut builder: CombinedAccumulatedDataBuilder = dep::std::unsafe::zeroed(); | ||
builder.unencrypted_log_preimages_length = 4; | ||
builder.encrypted_log_preimages_length = 4; | ||
let data = builder.finish(); | ||
// see calldata_tx_effect_factory.test.ts for the expected value | ||
assert_eq(compute_calldata_da_gas(data), 400); | ||
} | ||
} | ||
|
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,12 @@ | ||
import type { Config } from 'jest'; | ||
|
||
const config: Config = { | ||
preset: 'ts-jest/presets/default-esm', | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.[cm]?js$': '$1', | ||
}, | ||
testRegex: './src/.*\\.test\\.(js|mjs|ts)$', | ||
rootDir: './src', | ||
}; | ||
|
||
export default config; |
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
34 changes: 34 additions & 0 deletions
34
yarn-project/circuit-types/src/calldata_tx_effect_factory.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,34 @@ | ||
import { | ||
Fr, | ||
MAX_NEW_L2_TO_L1_MSGS_PER_TX, | ||
MAX_NEW_NOTE_HASHES_PER_TX, | ||
MAX_NEW_NULLIFIERS_PER_TX, | ||
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, | ||
RevertCode, | ||
} from '@aztec/circuits.js'; | ||
import { makeTuple } from '@aztec/foundation/array'; | ||
|
||
import { CalldataTxEffectFactory, DA_BYTE_GAS, FIXED_DA_GAS, TxL2Logs } from './index.js'; | ||
import { PublicDataWrite } from './public_data_write.js'; | ||
import { ITxEffectWithoutGasUsed } from './tx_effect.js'; | ||
|
||
describe('calldata_tx_effect_factory', () => { | ||
it('correctly calculates DA gas for empty TxEffect', () => { | ||
const effect: ITxEffectWithoutGasUsed = { | ||
revertCode: RevertCode.OK, | ||
noteHashes: makeTuple(MAX_NEW_NOTE_HASHES_PER_TX, Fr.zero), | ||
nullifiers: makeTuple(MAX_NEW_NULLIFIERS_PER_TX, Fr.zero), | ||
l2ToL1Msgs: makeTuple(MAX_NEW_L2_TO_L1_MSGS_PER_TX, Fr.zero), | ||
publicDataWrites: makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataWrite.empty), | ||
encryptedLogs: TxL2Logs.empty(), | ||
unencryptedLogs: TxL2Logs.empty(), | ||
}; | ||
|
||
const gasUsed = CalldataTxEffectFactory.build(effect).daGasUsed; | ||
|
||
// 4n for each log, due to encoding of the length of the logs | ||
expect(gasUsed.value).toEqual(FIXED_DA_GAS + 4n * DA_BYTE_GAS + 4n * DA_BYTE_GAS); | ||
}); | ||
|
||
// TODO(@just-mitch) more tests please | ||
}); |
78 changes: 78 additions & 0 deletions
78
yarn-project/circuit-types/src/calldata_tx_effect_factory.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,78 @@ | ||
import { Fr, GasUsed, RevertCode } from '@aztec/circuits.js'; | ||
import { arrayNonEmptyLength } from '@aztec/foundation/collection'; | ||
|
||
import { PublicDataWrite } from './public_data_write.js'; | ||
import { GasProfiler, GasType, ITxEffectWithoutGasUsed, TxEffect, TxEffectFactory } from './tx_effect.js'; | ||
|
||
export const DA_BYTE_GAS = 16n; | ||
|
||
export const FIXED_BYTES = // 17 bytes | ||
GasUsed.PACKED_SIZE_IN_BYTES + // da_gas_used | ||
GasUsed.PACKED_SIZE_IN_BYTES + // compute_gas_used | ||
RevertCode.PACKED_SIZE_IN_BYTES; // revert_code | ||
|
||
export const FIXED_DA_GAS = FIXED_BYTES * DA_BYTE_GAS; // 272n | ||
|
||
const getComputeGasUsed = (_effect: ITxEffectWithoutGasUsed) => { | ||
// Just a dummy for now | ||
return new GasUsed(1n); | ||
}; | ||
|
||
/** | ||
* Note. This does not exactly match ethereum calldata cost. | ||
* It is correlated, but simplified to ease circuit calculations: | ||
* We don't want to bitwise deconstruct the calldata to count the non-zero bytes in the circuit. | ||
* | ||
* We overcompensate by | ||
* - assuming our FIXED_BYTE "header" is always non-zero. | ||
* - assuming there is no zero byte in any non-zero field | ||
* | ||
* We undercompensate by | ||
* - not counting the bytes used to store the lengths of the various arrays | ||
* | ||
* @param effect the TxEffect to calculate the DA gas used for | ||
* @returns our interpretation of the DA gas used | ||
*/ | ||
const getDAGasUsed = (effect: ITxEffectWithoutGasUsed) => { | ||
const nonEmptyFields = | ||
arrayNonEmptyLength(effect.noteHashes, Fr.isZero) + | ||
arrayNonEmptyLength(effect.nullifiers, Fr.isZero) + | ||
arrayNonEmptyLength(effect.l2ToL1Msgs, Fr.isZero) + | ||
2 * arrayNonEmptyLength(effect.publicDataWrites, PublicDataWrite.isEmpty); | ||
|
||
const gasUsed = | ||
FIXED_DA_GAS + | ||
DA_BYTE_GAS * | ||
BigInt( | ||
Fr.SIZE_IN_BYTES * nonEmptyFields + | ||
effect.encryptedLogs.getSerializedLength() + | ||
effect.unencryptedLogs.getSerializedLength(), | ||
); | ||
|
||
return new GasUsed(gasUsed); | ||
}; | ||
|
||
const gasProfiler: GasProfiler = (effect: ITxEffectWithoutGasUsed) => { | ||
return { | ||
[GasType.DA]: getDAGasUsed(effect), | ||
[GasType.COMPUTE]: getComputeGasUsed(effect), | ||
}; | ||
}; | ||
|
||
export const CalldataTxEffectFactory: TxEffectFactory = { | ||
gasProfiler, | ||
build(effect: ITxEffectWithoutGasUsed) { | ||
const { [GasType.DA]: daGasUsed, [GasType.COMPUTE]: computeGasUsed } = this.gasProfiler(effect); | ||
return new TxEffect( | ||
daGasUsed, | ||
computeGasUsed, | ||
effect.revertCode, | ||
effect.noteHashes, | ||
effect.nullifiers, | ||
effect.l2ToL1Msgs, | ||
effect.publicDataWrites, | ||
effect.encryptedLogs, | ||
effect.unencryptedLogs, | ||
); | ||
}, | ||
}; |
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,23 +1,24 @@ | ||
export { CompleteAddress, GrumpkinPrivateKey, PartialAddress, PublicKey } from '@aztec/circuits.js'; | ||
export * from './auth_witness.js'; | ||
export * from './aztec_node/rpc/index.js'; | ||
export * from './body.js'; | ||
export * from './calldata_tx_effect_factory.js'; | ||
export * from './function_call.js'; | ||
export * from './interfaces/index.js'; | ||
export * from './keys/index.js'; | ||
export * from './notes/index.js'; | ||
export * from './messaging/index.js'; | ||
export * from './l2_block.js'; | ||
export * from './body.js'; | ||
export * from './l2_block_context.js'; | ||
export * from './l2_block_downloader/index.js'; | ||
export * from './l2_block_source.js'; | ||
export * from './tx_effect.js'; | ||
export * from './logs/index.js'; | ||
export * from './merkle_tree_id.js'; | ||
export * from './messaging/index.js'; | ||
export * from './mocks.js'; | ||
export * from './notes/index.js'; | ||
export * from './packed_arguments.js'; | ||
export * from './public_data_write.js'; | ||
export * from './simulation_error.js'; | ||
export * from './sibling_path/index.js'; | ||
export * from './simulation_error.js'; | ||
export * from './tx/index.js'; | ||
export * from './tx_effect.js'; | ||
export * from './tx_execution_request.js'; | ||
export * from './packed_arguments.js'; | ||
export * from './interfaces/index.js'; | ||
export * from './auth_witness.js'; | ||
export * from './aztec_node/rpc/index.js'; | ||
export { CompleteAddress, PublicKey, PartialAddress, GrumpkinPrivateKey } from '@aztec/circuits.js'; |
Oops, something went wrong.