Skip to content

Commit

Permalink
Keccak for storage hashes (#181)
Browse files Browse the repository at this point in the history
* Keccak for storage hashes

* Update tests
  • Loading branch information
virgil-serbanuta authored Nov 7, 2024
1 parent 82e60d6 commit fe12c11
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion tests/ulm-contracts/bytes_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ extern "C" {
fn decode_u8(bytes_id: u64) -> (u64, u8);

fn decode_signature(bytes_id: u64) -> (u64, u64);
fn hash(bytes_id: u64) -> u64;
fn hash(bytes_id: u64) -> u256;
}
4 changes: 2 additions & 2 deletions tests/ulm-contracts/single_value_mapper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]

struct SingleValueMapper { key: u64, value_type: () }
struct SingleValueMapper { key: u256, value_type: () }

fn new(key:u64) -> :: single_value_mapper :: SingleValueMapper {
fn new(key:u256) -> :: single_value_mapper :: SingleValueMapper {
:: single_value_mapper :: SingleValueMapper { key: key, value_type: () }
}
44 changes: 22 additions & 22 deletions tests/ulm-with-contract/erc_20_token.1.run
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
list_mock GetAccountStorageHook ( 7809087261546347641 ) ulmIntResult(0, u256);
list_mock SetAccountStorageHook ( 7809087261546347641 , 10000 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(0, u256);
list_mock SetAccountStorageHook ( 7089066454178295295 , 10000 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(10000, u256);
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(10000, u256);
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(10000, u256);
list_mock SetAccountStorageHook ( 7089066454178295295 , 9900 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454179379067 ) ulmIntResult(0, u256);
list_mock SetAccountStorageHook ( 7089066454179379067 , 100 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(9900, u256);
list_mock GetAccountStorageHook ( 7089066454179379067 ) ulmIntResult(100, u256);
list_mock SetAccountStorageHook ( 7089066454178299391 , 200 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454178299391 ) ulmIntResult(200, u256);
list_mock SetAccountStorageHook ( 7089066454178299391 , 0 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(9900, u256);
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(9900, u256);
list_mock SetAccountStorageHook ( 7089066454178295295 , 9700 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454179379067 ) ulmIntResult(100, u256);
list_mock SetAccountStorageHook ( 7089066454179379067 , 300 ) ulmNoResult();
list_mock GetAccountStorageHook ( 7089066454178295295 ) ulmIntResult(9700, u256);
list_mock GetAccountStorageHook ( 7089066454179379067 ) ulmIntResult(300, u256);
list_mock GetAccountStorageHook ( 11059124170230400219182520145867018696635045254189466511817698468614930762963 ) ulmIntResult(0, u256);
list_mock SetAccountStorageHook ( 11059124170230400219182520145867018696635045254189466511817698468614930762963 , 10000 ) ulmNoResult();
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(0, u256);
list_mock SetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 , 10000 ) ulmNoResult();
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(10000, u256);
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(10000, u256);
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(10000, u256);
list_mock SetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 , 9900 ) ulmNoResult();
list_mock GetAccountStorageHook ( 17171626450567201640701347902808840427582371480602455275836469707331258301780 ) ulmIntResult(0, u256);
list_mock SetAccountStorageHook ( 17171626450567201640701347902808840427582371480602455275836469707331258301780 , 100 ) ulmNoResult();
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(9900, u256);
list_mock GetAccountStorageHook ( 17171626450567201640701347902808840427582371480602455275836469707331258301780 ) ulmIntResult(100, u256);
list_mock SetAccountStorageHook ( 97321503972240892886219376522881926110074550168465622121824657360422868161783 , 200 ) ulmNoResult();
list_mock GetAccountStorageHook ( 97321503972240892886219376522881926110074550168465622121824657360422868161783 ) ulmIntResult(200, u256);
list_mock SetAccountStorageHook ( 97321503972240892886219376522881926110074550168465622121824657360422868161783 , 0 ) ulmNoResult();
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(9900, u256);
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(9900, u256);
list_mock SetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 , 9700 ) ulmNoResult();
list_mock GetAccountStorageHook ( 17171626450567201640701347902808840427582371480602455275836469707331258301780 ) ulmIntResult(100, u256);
list_mock SetAccountStorageHook ( 17171626450567201640701347902808840427582371480602455275836469707331258301780 , 300 ) ulmNoResult();
list_mock GetAccountStorageHook ( 115494845046723600673943990597816811669707739681772931244236289759170204726560 ) ulmIntResult(9700, u256);
list_mock GetAccountStorageHook ( 17171626450567201640701347902808840427582371480602455275836469707331258301780 ) ulmIntResult(300, u256);

push "uint256";
hold_list_values_from_test_stack;
Expand Down
4 changes: 2 additions & 2 deletions tests/ulm-with-contract/storage.256.run
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mock SetAccountStorageHook ( 8738216329480387967 , 1000000000000000000000000000000000000000000000000000000000000 ) ulmNoResult();
mock GetAccountStorageHook ( 8738216329480387967 ) ulmIntResult(1000000000000000000000000000000000000000000000000000000000000, u256);
mock SetAccountStorageHook ( 47586510880955265726688953653028435069486243252926274554489699639913455891751 , 1000000000000000000000000000000000000000000000000000000000000 ) ulmNoResult();
mock GetAccountStorageHook ( 47586510880955265726688953653028435069486243252926274554489699639913455891751 ) ulmIntResult(1000000000000000000000000000000000000000000000000000000000000, u256);

push "setMyData256";
hold_string_from_test_stack;
Expand Down
4 changes: 2 additions & 2 deletions tests/ulm-with-contract/storage.key.run
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mock SetAccountStorageHook ( 8738216329482039167 , 123 ) ulmNoResult();
mock GetAccountStorageHook ( 8738216329482039167 ) ulmIntResult(123, u64);
mock SetAccountStorageHook ( 10244917065751466042072482011607924022453580028001931068470547844249081769915 , 123 ) ulmNoResult();
mock GetAccountStorageHook ( 10244917065751466042072482011607924022453580028001931068470547844249081769915 ) ulmIntResult(123, u256);

push "setMyDataKey";
hold_string_from_test_stack;
Expand Down
4 changes: 2 additions & 2 deletions tests/ulm-with-contract/storage.simple.run
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mock SetAccountStorageHook ( 1809217465971809 , 123 ) ulmNoResult();
mock GetAccountStorageHook ( 1809217465971809 ) ulmIntResult(123, u64);
mock SetAccountStorageHook ( 73866358942311723187445361469509963283744060252826659833950887638461707973283 , 123 ) ulmNoResult();
mock GetAccountStorageHook ( 73866358942311723187445361469509963283744060252826659833950887638461707973283 ) ulmIntResult(123, u256);

push "setMyData";
hold_string_from_test_stack;
Expand Down
1 change: 1 addition & 0 deletions ulm-semantics/main/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ requires "execution/dispatch.md"
requires "execution/storage.md"
requires "execution/syntax.md"
requires "hooks.md"
requires "plugin/krypto.md"
requires "representation.md"
module ULM-EXECUTION
Expand Down
13 changes: 2 additions & 11 deletions ulm-semantics/main/hooks/bytes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module ULM-SEMANTICS-HOOKS-BYTES
imports private BYTES
imports private COMMON-K-CELL
imports private K-EQUAL-SYNTAX
imports private KRYPTO
imports private RUST-HELPERS
imports private RUST-REPRESENTATION
imports private ULM-SEMANTICS-HOOKS-BYTES-CONFIGURATION
Expand Down Expand Up @@ -345,18 +346,8 @@ module ULM-SEMANTICS-HOOKS-BYTES
rule ulmBytesHash(ptrValue(_, u64(BytesId)))
=> ulmBytesHash(ulmBytesId(BytesId))
// TODO: use a better hash function here (lower collision probability, use 256 bytes of hash).
rule ulmBytesHash(ulmBytesValue(B:Bytes))
=> ptrValue(null, u64(Int2MInt(#ulmBytesHash(Bytes2Int(B, BE, Unsigned)))))
syntax Int ::= #ulmBytesHash(Int) [function, total]
rule #ulmBytesHash(I:Int) => #ulmBytesHash(0 -Int I) requires I <Int 0
rule #ulmBytesHash(I:Int) => I requires 0 <=Int I andBool I <Int (1 <<Int 64)
rule #ulmBytesHash(I:Int)
=> #ulmBytesHash
( (I &Int ((1 <<Int 64) -Int 1))
|Int #ulmBytesHash(I >>Int 64)
)
=> ptrValue(null, u256(Int2MInt(Bytes2Int(Keccak256raw(B), BE, Unsigned))))
endmodule
```

0 comments on commit fe12c11

Please sign in to comment.