-
Notifications
You must be signed in to change notification settings - Fork 58
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
Showing
11 changed files
with
1,682 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
; the format for these test cases are: | ||
; expression => expected result | expected-cost | ||
|
||
keccak256 "foobar" => 0x38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e | 542 | ||
keccak256 "f" "oobar" => 0x38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e | 702 | ||
keccak256 "f" "o" "obar" => 0x38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e | 862 | ||
keccak256 "f" "o" "o" "bar" => 0x38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e | 1022 | ||
keccak256 "f" "o" "o" "b" "a" "r" => 0x38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e | 1342 | ||
|
||
keccak256 "foo" => 0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d | 536 | ||
keccak256 "fo" "o" => 0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d | 696 | ||
keccak256 "f" "o" "o" => 0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d | 856 |
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,32 @@ | ||
use crate::allocator::{Allocator, NodePtr}; | ||
use crate::cost::check_cost; | ||
use crate::cost::Cost; | ||
use crate::op_utils::atom; | ||
use crate::op_utils::new_atom_and_cost; | ||
use crate::reduction::Response; | ||
use sha3::{Digest, Keccak256}; | ||
|
||
const KECCAK256_BASE_COST: Cost = 50; | ||
const KECCAK256_COST_PER_ARG: Cost = 160; | ||
const KECCAK256_COST_PER_BYTE: Cost = 2; | ||
|
||
pub fn op_keccak256(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Response { | ||
let mut cost = KECCAK256_BASE_COST; | ||
|
||
let mut byte_count: usize = 0; | ||
let mut hasher = Keccak256::new(); | ||
while let Some((arg, rest)) = a.next(input) { | ||
input = rest; | ||
cost += KECCAK256_COST_PER_ARG; | ||
check_cost( | ||
a, | ||
cost + byte_count as Cost * KECCAK256_COST_PER_BYTE, | ||
max_cost, | ||
)?; | ||
let blob = atom(a, arg, "keccak256")?; | ||
byte_count += blob.as_ref().len(); | ||
hasher.update(blob); | ||
} | ||
cost += byte_count as Cost * KECCAK256_COST_PER_BYTE; | ||
new_atom_and_cost(a, cost, &hasher.finalize()) | ||
} |
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,24 @@ | ||
from eth_hash.auto import keccak | ||
from random import randbytes, randint, seed, sample | ||
from more_itertools import sliced | ||
|
||
# need | ||
# python -m pip install "eth-hash[pycryptodome]" | ||
|
||
seed(1337) | ||
|
||
SIZE = 100 | ||
|
||
with open("../op-tests/test-keccak256-generated.txt", "w+") as f: | ||
f.write("; This file was generated by tools/generate-keccak-tests.py\n\n") | ||
|
||
for i in range(SIZE): | ||
|
||
blob = randbytes(randint(1, 30)) | ||
result = keccak(blob) | ||
|
||
for i in range(1, len(blob) + 1): | ||
args = sliced(blob, i) | ||
cost = 50 + len(blob) * 2 + (len(blob)+i-1)//i * 160 + len(result) * 10 | ||
args_str = " ".join([f"0x{a.hex()}" for a in args]) | ||
f.write(f"keccak256 {args_str} => 0x{result.hex()} | {cost}\n") |
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