-
Notifications
You must be signed in to change notification settings - Fork 5
Add keccak.py
simulation
#30
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
897e74d
Initial draft
InfiniteEchoes 8fb843d
Update
InfiniteEchoes 2db26a7
minor updates
InfiniteEchoes 5552215
Update current progress & conflict at `gas`
InfiniteEchoes 7e9c1d6
Update `keccak.v` & fixed all other files
InfiniteEchoes 6321a77
Fixing `ExtendMemory` not found issue on local br
InfiniteEchoes 7923cb0
Minor updates according to comments
InfiniteEchoes ed326c7
minor updates
InfiniteEchoes 5cb658d
Merge branch 'main' into draft@AddKeccak
InfiniteEchoes 0e8a07c
More updates?
InfiniteEchoes 8e43584
Update: resolved `ExtendMemory` issue
InfiniteEchoes 55f8df5
Finish `keccak`
InfiniteEchoes 6818d7c
Remove test codes & Fix format
InfiniteEchoes 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
109 changes: 109 additions & 0 deletions
109
CoqOfPython/ethereum/paris/vm/instructions/simulations/keccak.v
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
Require Import CoqOfPython.CoqOfPython. | ||
Require Import simulations.CoqOfPython. | ||
Require Import simulations.builtins. | ||
|
||
Require ethereum.simulations.base_types. | ||
Definition U255_CEIL_VALUE := base_types.U255_CEIL_VALUE. | ||
Module U256 := base_types.U256. | ||
Definition U256_CEIL_VALUE := base_types.U256_CEIL_VALUE. | ||
Module Uint := base_types.Uint. | ||
Module FixedBytes := base_types.FixedBytes. | ||
|
||
Definition bytearray := base_types.bytearray. | ||
|
||
Require ethereum.paris.vm.simulations.__init__. | ||
Module Evm := __init__.Evm. | ||
|
||
Require ethereum.paris.vm.simulations.gas. | ||
Definition GAS_KECCAK256 := gas.GAS_KECCAK256. | ||
Definition GAS_KECCAK256_WORD := gas.GAS_KECCAK256_WORD. | ||
Definition ExtendMemory := gas.ExtendMemory.t. | ||
Definition calculate_gas_extend_memory := gas.calculate_gas_extend_memory. | ||
Definition charge_gas := gas.charge_gas. | ||
|
||
Require ethereum.paris.vm.simulations.stack. | ||
Definition pop := stack.pop. | ||
Definition push := stack.push. | ||
|
||
Require ethereum.utils.simulations.numeric. | ||
Definition ceil32 := numeric.ceil32. | ||
|
||
Import simulations.CoqOfPython.Notations. | ||
|
||
Definition keccak256 (bytes : bytearray) : FixedBytes.t. Admitted. | ||
(* | ||
def memory_read_bytes( | ||
memory: bytearray, start_position: U256, size: U256 | ||
) -> bytearray: | ||
*) | ||
(* NOTE: Upon further implementations on `memory`, we can move this axiomatized function to `memory.py`. *) | ||
Definition memory_read_bytes (memory : bytearray) (start_position size : U256.t) : bytearray. | ||
Admitted. | ||
(* | ||
def keccak(evm: Evm) -> None: | ||
""" | ||
Pushes to the stack the Keccak-256 hash of a region of memory. | ||
|
||
This also expands the memory, in case the memory is insufficient to | ||
access the data's memory location. | ||
|
||
Parameters | ||
---------- | ||
evm : | ||
The current EVM frame. | ||
|
||
""" | ||
# STACK | ||
memory_start_index = pop(evm.stack) | ||
size = pop(evm.stack) | ||
|
||
# GAS | ||
words = ceil32(Uint(size)) // 32 | ||
word_gas_cost = GAS_KECCAK256_WORD * words | ||
extend_memory = calculate_gas_extend_memory( | ||
evm.memory, [(memory_start_index, size)] | ||
) | ||
charge_gas(evm, GAS_KECCAK256 + word_gas_cost + extend_memory.cost) | ||
|
||
# OPERATION | ||
evm.memory += b"\x00" * extend_memory.expand_by | ||
data = memory_read_bytes(evm.memory, memory_start_index, size) | ||
hash = keccak256(data) | ||
|
||
push(evm.stack, U256.from_be_bytes(hash)) | ||
|
||
# PROGRAM COUNTER | ||
evm.pc += 1 | ||
*) | ||
Definition bitwise_and : MS? Evm.t Exception.t unit := | ||
(* STACK *) | ||
letS? memory_start_index := StateError.lift_lens Evm.Lens.stack pop in | ||
letS? size := StateError.lift_lens Evm.Lens.stack pop in | ||
(* GAS *) | ||
(* size : U256 -> Uint *) | ||
let words := ceil32(U256.to_Uint size) in | ||
let word_gas_cost := Uint.Make (Uint.get GAS_KECCAK256_WORD * Uint.get words) in | ||
|
||
(* Get evm.memory *) | ||
letS? evm := readS? in | ||
let '(Evm.Make _ rest) := evm in | ||
let evm_memory := rest.(Evm.Rest.memory) in | ||
|
||
let extend_memory := calculate_gas_extend_memory evm_memory ((memory_start_index, size) :: []) in | ||
|
||
letS? _ := charge_gas (Uint.Make ((Uint.get GAS_KECCAK256) + (Uint.get word_gas_cost) + (Uint.get extend_memory.(gas.ExtendMemory.cost)))) in | ||
(* OPERATION *) | ||
(* Construct correrctly the b"\x00" *) | ||
let b_x00 := base_types.FixedBytes.Make [Ascii.ascii_of_N 0] in | ||
let b_x00 := hash.Bytes32.Make b_x00 in | ||
let b_x00 := __init__.Hash32.Make b_x00 in | ||
|
||
let data := memory_read_bytes evm_memory memory_start_index size in | ||
let hash := keccak256 data in | ||
|
||
letS? _ := StateError.lift_lens Evm.Lens.stack ( | ||
push (U256.of_Z (Uint.get (Uint.from_bytes hash)))) in | ||
(* PROGRAM COUNTER *) | ||
letS? _ := StateError.lift_lens Evm.Lens.pc (fun pc => | ||
(inl tt, Uint.__add__ pc (Uint.Make 1))) in | ||
returnS? tt. |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Require Import CoqOfPython.CoqOfPython. | ||
Require Import simulations.CoqOfPython. | ||
Require Import simulations.builtins. | ||
|
||
Require ethereum.simulations.base_types. | ||
Module U256 := base_types.U256. | ||
Module Uint := base_types.Uint. | ||
|
||
Import simulations.CoqOfPython.Notations. | ||
|
||
(* def ceil32(value: Uint) -> Uint: | ||
""" | ||
Converts a unsigned integer to the next closest multiple of 32. | ||
|
||
Parameters | ||
---------- | ||
value : | ||
The value whose ceil32 is to be calculated. | ||
|
||
Returns | ||
------- | ||
ceil32 : `ethereum.base_types.U256` | ||
The same value if it's a perfect multiple of 32 | ||
else it returns the smallest multiple of 32 | ||
that is greater than `value`. | ||
""" | ||
ceiling = Uint(32) | ||
remainder = value % ceiling | ||
if remainder == Uint(0): | ||
return value | ||
else: | ||
return value + ceiling - remainder *) | ||
|
||
(* TODO: Finish below *) | ||
Definition ceil32 (value : Uint.t) : Uint.t. Admitted. | ||
(* := | ||
let ceiling := Uint.Make 32 in | ||
let remainder := value % ceiling in | ||
if remainder =? Uint 0 | ||
then value | ||
else value + ceiling - remainder. *) |
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.
Uh oh!
There was an error while loading. Please reload this page.