-
Notifications
You must be signed in to change notification settings - Fork 90
Backport blake hash support #1672
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
franciszekjob
merged 35 commits into
release-hotfix-v0.28.1
from
backport-blake-hash-support
Nov 20, 2025
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
5d72c05
Bump pyright
cptartur f636cdd
RPC 0.9.0-rc.1 changes
cptartur 55277fe
Add missing methods to `Client` ABC
cptartur 901a3e3
Fix `get_messages_status` argument
cptartur 0923db5
Fix `subscription_id` type in schemas
cptartur 3ec22b7
Add migration guide
cptartur 5298ac7
Bump devnet to 0.5.0-rc.0
cptartur 115e4be
Update `MessageStatus` following the spec
cptartur 9635837
Improve docs of `wait_for_tx`, check for error code explicitly
cptartur dd2249f
Fix tests
cptartur 29c51d7
Fix docs
cptartur 4117ce6
Lint and format
cptartur bfa0c25
Fix `test_get_compiled_casm`
cptartur 6d33811
Fix `test_get_storage_proof`
cptartur 6296505
Update starknet_py/net/client_models.py
cptartur 1be4180
Update starknet_py/tests/e2e/client/client_test.py
cptartur 442b99c
Update starknet_py/net/client.py
cptartur ec246fe
Update docs/migration_guide.rst
cptartur 08f145f
Update docs/migration_guide.rst
cptartur b9b496d
Fix comments
cptartur c3d9abe
Mention supported rpc version in migration_guide.rst
cptartur c7a70f9
Replace `_to_hex_number` with `_to_rpc_felt`
cptartur 6c3fc7c
Add docstrings
cptartur cd610fc
Update devnet to 0.5.0-rc.1
cptartur 1f2d21e
Add support for tip
cptartur cd8a70b
Add tests
cptartur 15d06ba
Update migration_guide.rst
cptartur 014f19e
Merge tag 'v0.28.0' of https://github.com/software-mansion/starknet.p…
franciszekjob af44b05
Merge tag 'v0.28.0' of https://github.com/software-mansion/starknet.p…
franciszekjob 0059ce3
Remove changes
franciszekjob 68aafd3
Support blake hash
franciszekjob 5922b9a
Update migration guide
franciszekjob b1eb73f
Add missing files
franciszekjob 772ea1d
Little fixes
franciszekjob 765949a
update migration guide
franciszekjob 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
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
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,97 @@ | ||
| """ | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@franciszekjob Let's include these changes in changelog for completness sake