-
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
+320
−40
Merged
Support blake hash #1657
Changes from 18 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,105 @@ | ||
| """ | ||
| This module's Blake2s felt encoding and hashing logic is based on StarkWare's | ||
| sequencer implementation: | ||
| https://github.com/starkware-libs/sequencer/blob/b29c0e8c61f7b2340209e256cf87dfe9f2c811aa/crates/blake2s/src/lib.rs | ||
| """ | ||
|
|
||
| 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 following Cairo's encoding scheme. | ||
|
|
||
| Small values (< 2^63) are encoded as 2 words: [high_32_bits, low_32_bits] from the last 8 bytes. | ||
| Large values (>= 2^63) are encoded as 8 words: the full 32-byte big-endian split, | ||
| with the MSB of the first word set as a marker (+2^255). | ||
|
|
||
| :param felts: List of Felt values to encode | ||
| :return: 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 | ||
| high = int.from_bytes(felt_as_be_bytes[24:28], byteorder="big") | ||
| low = int.from_bytes(felt_as_be_bytes[28:32], byteorder="big") | ||
| unpacked_u32s.append(high) | ||
| unpacked_u32s.append(low) | ||
| 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) | ||
|
|
||
| :param hash_bytes: Hash bytes (at least 32 bytes required) | ||
| :return: Felt value (252-bit field element) | ||
| """ | ||
| 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 hash over data and return as a Felt. | ||
|
|
||
| :param data: Input data to hash | ||
| :return: Blake2s-256 hash as a 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 Felt values using Cairo's encoding scheme and computes Blake2s hash. | ||
|
|
||
| This function matches Cairo's encode_felt252_to_u32s hint behavior. It encodes | ||
| each Felt into 32-bit words, serializes them as little-endian bytes, then | ||
| computes Blake2s-256 hash over the byte stream. | ||
|
|
||
| :param felts: List of Felt values to encode and hash | ||
| :return: Blake2s-256 hash as a 252-bit field element | ||
| """ | ||
| # Unpack each Felt into 2 or 8 u32 limbs | ||
| u32_words = encode_felts_to_u32s(felts) | ||
|
|
||
| # 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) | ||
|
|
||
| # Compute Blake2s-256 over the bytes and pack the result into a Felt | ||
| return blake2s_to_felt(byte_stream) | ||
|
|
||
|
|
||
| def blake2s_hash_many(values: List[int]) -> int: | ||
| """ | ||
| Hash multiple Felt values using Cairo-compatible Blake2s encoding. | ||
|
|
||
| This is the recommended way to hash Felt values for Starknet when using | ||
| Blake2s as the hash method. | ||
|
|
||
| :param values: List of Felt values to hash | ||
| :return: Blake2s-256 hash as a 252-bit field element | ||
| """ | ||
| 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
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.