From fe12c112e1d27ad0631543cc26cfaa0b580faf14 Mon Sep 17 00:00:00 2001 From: Virgil <25692529+virgil-serbanuta@users.noreply.github.com> Date: Thu, 7 Nov 2024 12:35:14 +0200 Subject: [PATCH] Keccak for storage hashes (#181) * Keccak for storage hashes * Update tests --- tests/ulm-contracts/bytes_hooks.rs | 2 +- tests/ulm-contracts/single_value_mapper.rs | 4 +- tests/ulm-with-contract/erc_20_token.1.run | 44 +++++++++++----------- tests/ulm-with-contract/storage.256.run | 4 +- tests/ulm-with-contract/storage.key.run | 4 +- tests/ulm-with-contract/storage.simple.run | 4 +- ulm-semantics/main/execution.md | 1 + ulm-semantics/main/hooks/bytes.md | 13 +------ 8 files changed, 34 insertions(+), 42 deletions(-) diff --git a/tests/ulm-contracts/bytes_hooks.rs b/tests/ulm-contracts/bytes_hooks.rs index c14b5ba8..74c8a43b 100644 --- a/tests/ulm-contracts/bytes_hooks.rs +++ b/tests/ulm-contracts/bytes_hooks.rs @@ -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; } diff --git a/tests/ulm-contracts/single_value_mapper.rs b/tests/ulm-contracts/single_value_mapper.rs index 26be625d..7d777611 100644 --- a/tests/ulm-contracts/single_value_mapper.rs +++ b/tests/ulm-contracts/single_value_mapper.rs @@ -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: () } } diff --git a/tests/ulm-with-contract/erc_20_token.1.run b/tests/ulm-with-contract/erc_20_token.1.run index accf2552..b1c8ec05 100644 --- a/tests/ulm-with-contract/erc_20_token.1.run +++ b/tests/ulm-with-contract/erc_20_token.1.run @@ -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; diff --git a/tests/ulm-with-contract/storage.256.run b/tests/ulm-with-contract/storage.256.run index 43ceec91..045db759 100644 --- a/tests/ulm-with-contract/storage.256.run +++ b/tests/ulm-with-contract/storage.256.run @@ -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; diff --git a/tests/ulm-with-contract/storage.key.run b/tests/ulm-with-contract/storage.key.run index 8bf4ff23..8053ec84 100644 --- a/tests/ulm-with-contract/storage.key.run +++ b/tests/ulm-with-contract/storage.key.run @@ -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; diff --git a/tests/ulm-with-contract/storage.simple.run b/tests/ulm-with-contract/storage.simple.run index 0dedb8d6..6b89cd21 100644 --- a/tests/ulm-with-contract/storage.simple.run +++ b/tests/ulm-with-contract/storage.simple.run @@ -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; diff --git a/ulm-semantics/main/execution.md b/ulm-semantics/main/execution.md index e4b59525..a5034b46 100644 --- a/ulm-semantics/main/execution.md +++ b/ulm-semantics/main/execution.md @@ -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 diff --git a/ulm-semantics/main/hooks/bytes.md b/ulm-semantics/main/hooks/bytes.md index a1d0c429..fab7b2e2 100644 --- a/ulm-semantics/main/hooks/bytes.md +++ b/ulm-semantics/main/hooks/bytes.md @@ -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 @@ -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 I requires 0 <=Int I andBool I #ulmBytesHash - ( (I &Int ((1 <>Int 64) - ) + => ptrValue(null, u256(Int2MInt(Bytes2Int(Keccak256raw(B), BE, Unsigned)))) endmodule ```