Skip to content

Commit cf4d3aa

Browse files
feat(starknet_os): integrate to_bytes and serialize_blob
1 parent f752bbb commit cf4d3aa

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ blockifier = { path = "crates/blockifier", version = "0.0.0" }
9898
blockifier_test_utils = { path = "crates/blockifier_test_utils", version = "0.0.0" }
9999
byteorder = "1.4.3"
100100
bytes = "1"
101+
c-kzg = "1.0.3"
101102
cached = "0.44.0"
102103
cairo-felt = "0.9.1"
103104
cairo-lang-casm = "2.10.0"

crates/starknet_os/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ testing = ["blockifier/testing"]
1818

1919
[dependencies]
2020
blockifier.workspace = true
21+
c-kzg.workspace = true
2122
cairo-lang-starknet-classes.workspace = true
2223
cairo-vm = { workspace = true, features = ["extensive_hints"] }
2324
indexmap.workspace = true

crates/starknet_os/src/hints/hint_implementation/kzg/utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
use std::num::ParseIntError;
22

33
use blockifier::utils::usize_from_u32;
4+
use c_kzg::BYTES_PER_FIELD_ELEMENT;
45
use num_bigint::BigInt;
56
use num_traits::One;
67

78
#[allow(dead_code)]
89
pub(crate) const BLS_PRIME: &str =
910
"52435875175126190479447740508185965837690552500527637822603658699938581184513";
11+
const FIELD_ELEMENTS_PER_BLOB: usize = 4096;
1012

1113
#[derive(Debug, thiserror::Error)]
1214
pub enum FftError {
1315
#[error("Group is missing last element.")]
1416
GroupMissingLastElement,
1517
#[error(transparent)]
1618
InvalidBinaryToUsize(ParseIntError),
19+
#[error("Blob size must be {FIELD_ELEMENTS_PER_BLOB}, got {0}.")]
20+
InvalidBlobSize(usize),
1721
#[error("Invalid coefficients length (must be a power of two): {0}.")]
1822
InvalidCoeffsLength(usize),
1923
}
2024

25+
fn to_bytes(x: &BigInt, length: usize) -> Vec<u8> {
26+
let mut bytes = x.to_bytes_be().1;
27+
let padding = length.saturating_sub(bytes.len());
28+
if padding > 0 {
29+
let mut padded_bytes = std::iter::repeat(0u8).take(padding).collect::<Vec<u8>>();
30+
padded_bytes.extend(bytes);
31+
bytes = padded_bytes;
32+
}
33+
bytes
34+
}
35+
36+
#[allow(dead_code)]
37+
fn serialize_blob(blob: &[BigInt]) -> Result<Vec<u8>, FftError> {
38+
if blob.len() != FIELD_ELEMENTS_PER_BLOB {
39+
return Err(FftError::InvalidBlobSize(blob.len()));
40+
}
41+
Ok(blob.iter().flat_map(|x| to_bytes(x, BYTES_PER_FIELD_ELEMENT)).collect())
42+
}
43+
2144
/// Performs the recursive Fast Fourier Transform (FFT) on the input coefficient vector `coeffs`
2245
/// using the provided group elements `group` and modulus `prime`.
2346
///

0 commit comments

Comments
 (0)