Skip to content

Commit e7ab684

Browse files
refactor(starknet_os): polynomial_coefficients_to_kzg_commitment directly outputs felts
Signed-off-by: Dori Medini <dori@starkware.co>
1 parent 38b5210 commit e7ab684

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) fn store_da_segment<S: StateReader>(
4141
}
4242
})?;
4343

44-
let kzg_commitments_bigints: Vec<(BigInt, BigInt)> = da_segment
44+
let kzg_commitments: Vec<(Felt, Felt)> = da_segment
4545
.chunks(blob_length)
4646
.enumerate()
4747
.map(|(chunk_id, chunk)| {
@@ -51,10 +51,6 @@ pub(crate) fn store_da_segment<S: StateReader>(
5151
})
5252
.collect::<Result<_, _>>()?;
5353
log::debug!("Done computing KZG commitments.");
54-
let kzg_commitments: Vec<(Felt, Felt)> = kzg_commitments_bigints
55-
.into_iter()
56-
.map(|bigint_pair| (bigint_pair.0.into(), bigint_pair.1.into()))
57-
.collect();
5854

5955
hint_processor.set_da_segment(da_segment)?;
6056

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use c_kzg::KzgCommitment;
44
use num_bigint::BigInt;
55
use num_traits::{Num, One, Zero};
66
use rstest::rstest;
7+
use starknet_types_core::felt::Felt;
78

89
use crate::hints::hint_implementation::kzg::utils::{fft, split_commitment, BLS_PRIME};
910

@@ -80,8 +81,8 @@ fn test_fft(#[values(true, false)] bit_reversed: bool) {
8081
16,
8182
).unwrap(),
8283
(
83-
BigInt::from_str_radix("590b91a225b1bf7fa5877ec546d090e0059f019c74675362", 16).unwrap(),
84-
BigInt::from_str_radix("b7a71dc9d8e15ea474a69c0531e720cf5474b189ac9afc81", 16).unwrap(),
84+
Felt::from_hex_unchecked("590b91a225b1bf7fa5877ec546d090e0059f019c74675362"),
85+
Felt::from_hex_unchecked("b7a71dc9d8e15ea474a69c0531e720cf5474b189ac9afc81"),
8586
)
8687
)]
8788
#[case(
@@ -90,13 +91,13 @@ fn test_fft(#[values(true, false)] bit_reversed: bool) {
9091
16,
9192
).unwrap(),
9293
(
93-
BigInt::from_str_radix("51732ecbaa75842afd3d8860d40b3e8eeea433bce18b5c8", 16).unwrap(),
94-
BigInt::from_str_radix("a797c1973c99961e357246ee81bde0acbdd27e801d186ccb", 16).unwrap(),
94+
Felt::from_hex_unchecked("51732ecbaa75842afd3d8860d40b3e8eeea433bce18b5c8"),
95+
Felt::from_hex_unchecked("a797c1973c99961e357246ee81bde0acbdd27e801d186ccb"),
9596
)
9697
)]
9798
fn test_split_commitment_function(
9899
#[case] commitment: BigInt,
99-
#[case] expected_output: (BigInt, BigInt),
100+
#[case] expected_output: (Felt, Felt),
100101
) {
101102
let commitment = KzgCommitment::from_bytes(&commitment.to_bytes_be().1).unwrap();
102103
assert_eq!(split_commitment(&commitment).unwrap(), expected_output);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use std::sync::LazyLock;
44

55
use blockifier::utils::usize_from_u32;
66
use c_kzg::{Blob, KzgCommitment, KzgSettings, BYTES_PER_FIELD_ELEMENT};
7-
use num_bigint::{BigInt, ParseBigIntError, Sign};
7+
use num_bigint::{BigInt, ParseBigIntError};
88
use num_traits::{Num, One, Zero};
99
use starknet_infra_utils::compile_time_cargo_manifest_dir;
10+
use starknet_types_core::felt::Felt;
1011

1112
const BLOB_SUBGROUP_GENERATOR: &str =
1213
"39033254847818212395286706435128746857159659164139250548781411570340225835782";
@@ -155,14 +156,14 @@ pub(crate) fn fft(
155156
Ok(values)
156157
}
157158

158-
pub(crate) fn split_commitment(commitment: &KzgCommitment) -> Result<(BigInt, BigInt), FftError> {
159+
pub(crate) fn split_commitment(commitment: &KzgCommitment) -> Result<(Felt, Felt), FftError> {
159160
let commitment_bytes: [u8; COMMITMENT_BYTES_LENGTH] = *commitment.to_bytes().as_ref();
160161

161162
// Split the number.
162163
let low = &commitment_bytes[COMMITMENT_BYTES_MIDPOINT..];
163164
let high = &commitment_bytes[..COMMITMENT_BYTES_MIDPOINT];
164165

165-
Ok((BigInt::from_bytes_be(Sign::Plus, low), BigInt::from_bytes_be(Sign::Plus, high)))
166+
Ok((Felt::from_bytes_be_slice(low), Felt::from_bytes_be_slice(high)))
166167
}
167168

168169
fn polynomial_coefficients_to_blob(coefficients: Vec<BigInt>) -> Result<Vec<u8>, FftError> {
@@ -186,7 +187,7 @@ fn polynomial_coefficients_to_blob(coefficients: Vec<BigInt>) -> Result<Vec<u8>,
186187

187188
pub(crate) fn polynomial_coefficients_to_kzg_commitment(
188189
coefficients: Vec<BigInt>,
189-
) -> Result<(BigInt, BigInt), FftError> {
190+
) -> Result<(Felt, Felt), FftError> {
190191
let blob = polynomial_coefficients_to_blob(coefficients)?;
191192
let commitment_bytes = blob_to_kzg_commitment(&Blob::from_bytes(&blob)?)?;
192193
split_commitment(&commitment_bytes)

0 commit comments

Comments
 (0)