-
Notifications
You must be signed in to change notification settings - Fork 91
Support blake hash #1657
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
Support blake hash #1657
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5c8e97d
Implement blake hash function
MKowalski8 29b6708
Add possibility to use `blake` with `rpc >= 10`
MKowalski8 dd2b55f
Change import place
MKowalski8 50cfdf9
Update comments
MKowalski8 c379352
Review changes
MKowalski8 e0a05e2
Remove unneded directive
MKowalski8 89b8e63
Save rpc version
MKowalski8 b290d59
Fix warn display test
MKowalski8 e511c8a
Nits changes
MKowalski8 f695102
Change lock file
MKowalski8 67dffec
Add changes form rpc 0.10.0-rc.0
franciszekjob c4f0b31
Resolve typecheck
MKowalski8 9dc5ce0
Fix test; Add todo
franciszekjob 831a8d4
Fix test
franciszekjob 8c8acb2
Merge branch 'rpc-0.10.0-rc.0' of https://github.com/software-mansion…
MKowalski8 7683e39
Update migration guide
MKowalski8 870ab28
Add todos
franciszekjob 0eda51f
Merge branch 'rpc-0.10.0-rc.0' of https://github.com/software-mansion…
MKowalski8 dda5b31
Merge branch 'development' into support-blake-hash
MKowalski8 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
Some comments aren't visible on the classic Files Changed page.
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
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
MKowalski8 marked this conversation as resolved.
Show resolved
Hide resolved
MKowalski8 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,81 @@ | ||
| import hashlib | ||
| from typing import List | ||
|
|
||
| from starknet_py.constants import FIELD_PRIME | ||
|
|
||
| SMALL_THRESHOLD = 2**63 | ||
| BIG_MARKER = 1 << 31 # MSB mask for the first u32 in the 8-limb case | ||
|
|
||
|
|
||
| def encode_felts_to_u32s(felts: List[int]) -> List[int]: | ||
| """ | ||
| Encode each Felt into 32-bit words: | ||
| - Small values < 2^63 get 2 words: [high_32_bits, low_32_bits] from the last 8 bytes | ||
| - Large values >= 2^63 get 8 words: the full 32-byte big-endian split, with the | ||
| MSB of the first word set as a marker (+2^255) | ||
| Returns a flat list of u32 values. | ||
| """ | ||
| unpacked_u32s = [] | ||
| for felt in felts: | ||
| # Convert felt to 32-byte big-endian representation | ||
| felt_as_be_bytes = felt.to_bytes(32, byteorder="big") | ||
|
|
||
| if felt < SMALL_THRESHOLD: | ||
| # Small: 2 limbs only, high-32 then low-32 of the last 8 bytes | ||
| hi = int.from_bytes(felt_as_be_bytes[24:28], byteorder="big") | ||
| lo = int.from_bytes(felt_as_be_bytes[28:32], byteorder="big") | ||
MKowalski8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| unpacked_u32s.append(hi) | ||
| unpacked_u32s.append(lo) | ||
| else: | ||
| # Big: 8 limbs, big-endian order | ||
| start = len(unpacked_u32s) | ||
| for i in range(0, 32, 4): | ||
| limb = int.from_bytes(felt_as_be_bytes[i : i + 4], byteorder="big") | ||
| unpacked_u32s.append(limb) | ||
| # Set the MSB of the very first limb as the Cairo hint does with "+ 2**255" | ||
| unpacked_u32s[start] |= BIG_MARKER | ||
|
|
||
| return unpacked_u32s | ||
|
|
||
|
|
||
| def pack_256_le_to_felt(hash_bytes: bytes) -> int: | ||
| """ | ||
| Packs the first 32 bytes (256 bits) of hash_bytes into a Felt (252 bits). | ||
| Interprets the bytes as a Felt (252 bits) | ||
| """ | ||
| assert len(hash_bytes) >= 32, "need at least 32 bytes to pack" | ||
| # Interpret the 32-byte buffer as a little-endian integer and convert to Felt | ||
| return int.from_bytes(hash_bytes[:32], byteorder="little") % FIELD_PRIME | ||
|
|
||
|
|
||
| def blake2s_to_felt(data: bytes) -> int: | ||
| """ | ||
| Compute Blake2s-256 over data and return as a Felt (252-bit field element). | ||
| """ | ||
| hash_bytes = hashlib.blake2s(data, digest_size=32).digest() | ||
| return pack_256_le_to_felt(hash_bytes) | ||
|
|
||
|
|
||
| def encode_felt252_data_and_calc_blake_hash(felts: List[int]) -> int: | ||
| """ | ||
| Encodes a list of Felt values into 32-bit words exactly as Cairo's | ||
| encode_felt252_to_u32s hint does, then hashes the resulting byte stream | ||
| with Blake2s-256 and returns the 256-bit digest as a 252-bit field element. | ||
| """ | ||
| # 1) Unpack each Felt into 2 or 8 u32 limbs | ||
| u32_words = encode_felts_to_u32s(felts) | ||
|
|
||
| # 2) Serialize the u32 limbs into a little-endian byte stream | ||
| byte_stream = b"".join(word.to_bytes(4, byteorder="little") for word in u32_words) | ||
|
|
||
| # 3) Compute Blake2s-256 over the bytes and pack the result into a Felt | ||
MKowalski8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return blake2s_to_felt(byte_stream) | ||
|
|
||
|
|
||
| def blake2s_hash_many(values: List[int]) -> int: | ||
| """ | ||
| Hash multiple Felt values using the Cairo-compatible Blake2s encoding. | ||
| This is the proper way to hash Felt values for Starknet. | ||
| """ | ||
| return encode_felt252_data_and_calc_blake_hash(values) | ||
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
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,54 @@ | ||
| # pylint: disable=line-too-long | ||
| # fmt: off | ||
MKowalski8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
franciszekjob marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Test the encode_felt252_data_and_calc_blake_hash function | ||
| with the same results as the Cairo v0.14 implementation. | ||
| Reference: https://github.com/starkware-libs/cairo-lang/releases/tag/v0.14.0 | ||
franciszekjob marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| import pytest | ||
|
|
||
| from starknet_py.hash.blake2s import encode_felt252_data_and_calc_blake_hash | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_felts, expected_result", | ||
| [ | ||
| # Empty array | ||
| ( | ||
| [], | ||
| 874258848688468311465623299960361657518391155660316941922502367727700287818 | ||
| ), | ||
| # Boundary: small felt at (2^63 - 1) | ||
| ( | ||
| [(1 << 63) - 1], | ||
| 94160078030592802631039216199460125121854007413180444742120780261703604445 | ||
| ), | ||
| # Boundary: at 2^63 | ||
| ( | ||
| [1 << 63], | ||
| 318549634615606806810268830802792194529205864650702991817600345489579978482 | ||
| ), | ||
| # Very large felt | ||
| ( | ||
| [0x800000000000011000000000000000000000000000000000000000000000000], | ||
| 3505594194634492896230805823524239179921427575619914728883524629460058657521 | ||
| ), | ||
| # Mixed: small and large felts | ||
| ( | ||
| [42, 1 << 63, 1337], | ||
| 1127477916086913892828040583976438888091205536601278656613505514972451246501 | ||
| ), | ||
| ], | ||
| ids=[ | ||
| "empty", | ||
| "boundary_small_felt", | ||
| "boundary_at_2_63", | ||
| "very_large_felt", | ||
| "mixed_small_large" | ||
| ] | ||
| ) | ||
| def test_encode_felt252_data_and_calc_blake_hash(input_felts, expected_result): | ||
| result = encode_felt252_data_and_calc_blake_hash(input_felts) | ||
| assert result == expected_result, ( | ||
| f"StarknetPy implementation: {result} != Cairo implementation: {expected_result}" | ||
| ) | ||
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.