Skip to content

Commit 80082d8

Browse files
feat(ci): upgrade cairo-lang to 0.14.0a1 (#6353)
1 parent 35b001a commit 80082d8

File tree

14 files changed

+107
-14
lines changed

14 files changed

+107
-14
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ base64 = "0.13.0"
150150
bincode = "1.3.3"
151151
bisection = "0.1.0"
152152
bitvec = "1.0.1"
153+
blake2 = "0.10.6"
153154
blockifier = { path = "crates/blockifier", version = "0.0.0" }
154155
blockifier_reexecution.path = "crates/blockifier_reexecution"
155156
blockifier_test_utils = { path = "crates/blockifier_test_utils", version = "0.0.0" }
@@ -175,6 +176,7 @@ criterion = "0.5.1"
175176
deadqueue = "0.2.4"
176177
defaultmap = "0.5.0"
177178
derive_more = "0.99.17"
179+
digest = "0.10.7"
178180
enum-as-inner = "0.6.1"
179181
enum-assoc = "1.1.0"
180182
enum-iterator = "1.4.1"

crates/blockifier/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ark-ec.workspace = true
3636
ark-ff.workspace = true
3737
ark-secp256k1.workspace = true
3838
ark-secp256r1.workspace = true
39+
blake2.workspace = true
3940
blockifier_test_utils = { workspace = true, optional = true }
4041
cached.workspace = true
4142
cairo-lang-casm = { workspace = true, features = ["parity-scale-codec"] }
@@ -44,6 +45,7 @@ cairo-lang-starknet-classes.workspace = true
4445
cairo-native = { workspace = true, optional = true }
4546
cairo-vm.workspace = true
4647
derive_more.workspace = true
48+
digest.workspace = true
4749
indexmap.workspace = true
4850
itertools.workspace = true
4951
keccak.workspace = true

crates/blockifier/src/utils.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::collections::HashMap;
22

3+
use blake2::Blake2s256;
34
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
5+
use digest::Digest;
6+
use starknet_types_core::felt::Felt;
47

58
use crate::blockifier_versioned_constants::{BaseGasCosts, BuiltinGasCosts};
69
use crate::transaction::errors::NumericConversionError;
@@ -86,3 +89,76 @@ pub fn get_gas_cost_from_vm_resources(
8689
+ n_memory_holes * base_costs.memory_hole_gas_cost
8790
+ total_builtin_gas_cost
8891
}
92+
93+
// Unpack each `Felt` into 32-bit words exactly as Cairo’s hint expects:
94+
/// - **Small** values `< 2^63` get **2** words: `[ high_32_bits, low_32_bits ]` from the last 8
95+
/// bytes of the 256-bit BE representation.
96+
/// - **Large** values `>= 2^63` get **8** words: the full 32-byte big-endian split, **with** the
97+
/// MSB of the first word set as a marker (`+2^255`).
98+
///
99+
/// # Returns
100+
/// A flat `Vec<u32>` containing all the unpacked words, in the same order.
101+
fn unpack_felt_into_u32(felts: Vec<Felt>) -> Vec<u32> {
102+
// 2**63.
103+
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000");
104+
// MSB mask for the first u32 in the 8-limb case.
105+
const BIG_MARKER: u32 = 1 << 31;
106+
107+
let mut unpacked_u32s = Vec::new();
108+
for felt in felts {
109+
let felt_as_be_bytes = felt.to_bytes_be();
110+
if felt < SMALL_THRESHOLD {
111+
// small: 2 limbs only, high‐32 then low‐32 of the last 8 bytes
112+
let hi = u32::from_be_bytes(felt_as_be_bytes[24..28].try_into().unwrap());
113+
let lo = u32::from_be_bytes(felt_as_be_bytes[28..32].try_into().unwrap());
114+
unpacked_u32s.push(hi);
115+
unpacked_u32s.push(lo);
116+
} else {
117+
// big: 8 limbs, big‐endian order
118+
let start = unpacked_u32s.len();
119+
for chunk in felt_as_be_bytes.chunks_exact(4) {
120+
unpacked_u32s.push(u32::from_be_bytes(chunk.try_into().unwrap()));
121+
}
122+
// set the MSB of the very first limb as the Cairo hint does with "+ 2**255"
123+
unpacked_u32s[start] |= BIG_MARKER;
124+
}
125+
}
126+
unpacked_u32s
127+
}
128+
129+
/// Packs the first 7 little-endian 32-bit words (28 bytes) of `bytes`
130+
/// into a single 224-bit Felt.
131+
fn pack_224_le_to_felt(bytes: &[u8]) -> Felt {
132+
assert!(bytes.len() >= 28, "need at least 28 bytes to pack 7 words");
133+
134+
// 1) copy your 28-byte LE-hash into the low 28 bytes of a 32-byte buffer
135+
let mut buf = [0u8; 32];
136+
buf[..28].copy_from_slice(&bytes[..28]);
137+
138+
// 2) interpret the whole 32-byte buffer as a little-endian Felt
139+
Felt::from_bytes_le(&buf)
140+
}
141+
142+
pub fn blake2s_to_felt(data: &[u8]) -> Felt {
143+
let mut hasher = Blake2s256::new();
144+
hasher.update(data);
145+
let hash32 = hasher.finalize();
146+
pack_224_le_to_felt(hash32.as_slice())
147+
}
148+
149+
/// Encodes a slice of `Felt` values into 32-bit words exactly as Cairo’s
150+
/// `encode_felt252_to_u32s` hint does, then hashes the resulting byte stream
151+
/// with Blake2s-256 and returns the 224-bit truncated digest as a `Felt`.
152+
pub fn encode_felt252_data_and_calc_224_bit_blake_hash(data: &[Felt]) -> Felt {
153+
// 1) Unpack each Felt into 2 or 8 u32 limbs
154+
let u32_words = unpack_felt_into_u32(data.to_vec());
155+
156+
// 2) Serialize the u32 limbs into a little-endian byte stream
157+
let mut byte_stream = Vec::with_capacity(u32_words.len() * 4);
158+
for word in u32_words {
159+
byte_stream.extend_from_slice(&word.to_le_bytes());
160+
}
161+
162+
// 3) Compute Blake2s-256 over the bytes and pack the first 224 bits into a Felt
163+
blake2s_to_felt(&byte_stream)
164+
}

crates/blockifier_test_utils/resources/feature_contracts/cairo0/compiled/account_faulty_compiled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"pedersen",
132132
"range_check"
133133
],
134-
"compiler_version": "0.13.5",
134+
"compiler_version": "0.14.0a1",
135135
"data": [
136136
"0x40780017fff7fff",
137137
"0x1",

crates/blockifier_test_utils/resources/feature_contracts/cairo0/compiled/account_with_dummy_validate_compiled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"pedersen",
154154
"range_check"
155155
],
156-
"compiler_version": "0.13.5",
156+
"compiler_version": "0.14.0a1",
157157
"data": [
158158
"0x480680017fff8000",
159159
"0x43616c6c436f6e7472616374",

crates/blockifier_test_utils/resources/feature_contracts/cairo0/compiled/account_with_long_validate_compiled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"pedersen",
139139
"range_check"
140140
],
141-
"compiler_version": "0.13.5",
141+
"compiler_version": "0.14.0a1",
142142
"data": [
143143
"0x480680017fff8000",
144144
"0x43616c6c436f6e7472616374",

crates/blockifier_test_utils/resources/feature_contracts/cairo0/compiled/empty_contract_compiled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"builtins": [
1111
"range_check"
1212
],
13-
"compiler_version": "0.13.5",
13+
"compiler_version": "0.14.0a1",
1414
"data": [],
1515
"debug_info": null,
1616
"hints": {},

crates/blockifier_test_utils/resources/feature_contracts/cairo0/compiled/security_tests_contract_compiled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
"ecdsa",
411411
"ec_op"
412412
],
413-
"compiler_version": "0.13.5",
413+
"compiler_version": "0.14.0a1",
414414
"data": [
415415
"0x40780017fff7fff",
416416
"0x1",

crates/blockifier_test_utils/resources/feature_contracts/cairo0/compiled/test_contract_compiled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@
907907
"bitwise",
908908
"ec_op"
909909
],
910-
"compiler_version": "0.13.5",
910+
"compiler_version": "0.14.0a1",
911911
"data": [
912912
"0x40780017fff7fff",
913913
"0x1",

0 commit comments

Comments
 (0)