Skip to content

Commit

Permalink
Generate CALLDATA* marginal programs (#233)
Browse files Browse the repository at this point in the history
* Generate CALLDATA* marginal programs

* fix: call* marginal programs

---------

Co-authored-by: lukasz-glen <>
  • Loading branch information
pgebal authored Dec 30, 2024
1 parent f3a4e26 commit f78b2f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/program_generator/data/current_gas_cost.csv
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CALLER,2,0
CALLVALUE,2,0
CALLDATALOAD,3,0
CALLDATASIZE,2,0
CALLDATACOPY,2,0
CALLDATACOPY,6,0
CODESIZE,2,0
CODECOPY,2,0
GASPRICE,2,0
Expand Down
37 changes: 36 additions & 1 deletion src/program_generator/pg_marginal.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def _do_generate(self, opcode, max_op_count, shuffle_counts, step_op_count):
def _generate_single_program(self, operation, op_count, max_op_count):
assert op_count <= constants.MAX_INSTRUCTIONS
# for compatibility with the generate_single_marginal function
if operation['Mnemonic'] in ['CALLDATASIZE', 'CALLDATACOPY', 'CALLDATALOAD']:
return Program(_generate_calldata_program(operation, op_count, max_op_count), operation['Mnemonic'], op_count)
if operation['Mnemonic'] == 'CREATE':
return Program(_generate_create_program(operation, op_count), operation['Mnemonic'], op_count)
if operation['Mnemonic'] in ['EXTCODEHASH', 'EXTCODESIZE']:
Expand Down Expand Up @@ -395,6 +397,40 @@ def _generate_subcontext_exit_program(operation, op_count, max_op_count):

return op_deployment_code + op_address_store + no_op_deployment_code + no_op_calls + op_address_load + op_calls

def _generate_calldata_program(operation, op_count, max_op_count):
"""
Generates a program for CALLDATASIZE
"""
assert operation['Mnemonic'] in ['CALLDATASIZE', 'CALLDATACOPY', 'CALLDATALOAD']

op_deployment_code = '' # this prevents warnings
no_op_deployment_code = ''
if operation['Mnemonic'] == 'CALLDATALOAD':
no_op_subcontext_code = '5f'
op_subcontext_code = '5f35'
op_deployment_code = '6961' + op_subcontext_code + '5f526002601ef3600052600a60166000f0'
no_op_deployment_code = '6860' + no_op_subcontext_code + '5f526001601ff3600052600960176000f0'
elif operation['Mnemonic'] == 'CALLDATASIZE':
no_op_subcontext_code = '5f'
op_subcontext_code = '5f36'
op_deployment_code = '6961' + op_subcontext_code + '5f526002601ef3600052600a60166000f0'
no_op_deployment_code = '6860' + no_op_subcontext_code + '5f526001601ff3600052600960176000f0'
elif operation['Mnemonic'] == 'CALLDATACOPY':
no_op_subcontext_code = '5f5f5260205f5f'
op_subcontext_code = no_op_subcontext_code + '37'
op_deployment_code = '6f67' + op_subcontext_code + '5f5260086018f3600052601060106000f0'
no_op_deployment_code = '6e66' + no_op_subcontext_code + '5f5260076019f3600052600f60116000f0'

op_address_store = '60ff52'
op_address_load = '60ff51'

# 32 bytes calldata
no_op_calls = '60006000602060008461fffff450' * (max_op_count - op_count)
op_calls = '60006000602060008461fffff450' * op_count

return op_deployment_code + op_address_store + no_op_deployment_code + no_op_calls + op_address_load + op_calls


def _generate_sload_cold_program(operation, op_count, max_op_count):
"""
Generates a program for SLOAD where every SLOAD loads from a cold slot
Expand Down Expand Up @@ -472,7 +508,6 @@ def _generate_sstore_warm_change_program(operation, op_count, max_op_count):
return slot_warmup_code + args_pushes + opcodes



def main():
fire.Fire(ProgramGenerator, name='generate')

Expand Down

0 comments on commit f78b2f6

Please sign in to comment.