Skip to content

Commit 90714c9

Browse files
starknet_os: structured KZG commitment info (starkware-libs#8770)
1 parent 63804d2 commit 90714c9

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

crates/starknet_os/src/io/os_output.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,47 @@ pub enum OsStateDiff {
145145
PartialCommitment(PartialCommitmentOsStateDiff),
146146
}
147147

148+
pub(crate) type KzgCommitment = (Felt, Felt);
149+
pub(crate) type PointEvaluation = (Felt, Felt);
150+
151+
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize, serde::Serialize))]
152+
#[derive(Debug, PartialEq)]
153+
pub(crate) struct OsKzgCommitmentInfo {
154+
z: Felt,
155+
n_blobs: usize,
156+
commitments: Vec<KzgCommitment>,
157+
evals: Vec<PointEvaluation>,
158+
}
159+
160+
impl TryFromOutputIter for OsKzgCommitmentInfo {
161+
fn try_from_output_iter<It: Iterator<Item = Felt>>(
162+
output_iter: &mut It,
163+
) -> Result<Self, OsOutputError> {
164+
let kzg_z = wrap_missing(output_iter.next(), "kzg_z")?;
165+
let n_blobs: usize = wrap_missing_as(output_iter.next(), "n_blobs")?;
166+
167+
let mut commitments = Vec::with_capacity(n_blobs);
168+
for i in 0..n_blobs {
169+
// Each commitment is two felts.
170+
let low = wrap_missing(output_iter.next(), &format!("kzg_commitment_low_{i}"))?;
171+
let high = wrap_missing(output_iter.next(), &format!("kzg_commitment_high_{i}"))?;
172+
commitments.push((low, high));
173+
}
174+
175+
let mut evals = Vec::with_capacity(n_blobs);
176+
for i in 0..n_blobs {
177+
// Each evaluation is two felts.
178+
let low = wrap_missing(output_iter.next(), &format!("point_evaluation_low_{i}"))?;
179+
let high = wrap_missing(output_iter.next(), &format!("point_evaluation_high_{i}"))?;
180+
evals.push((low, high));
181+
}
182+
Ok(Self { z: kzg_z, n_blobs, commitments, evals })
183+
}
184+
}
185+
148186
struct OutputIterParsedData {
149187
common_os_output: CommonOsOutput,
150-
kzg_commitment_info: Option<Vec<Felt>>,
188+
kzg_commitment_info: Option<OsKzgCommitmentInfo>,
151189
full_output: bool,
152190
}
153191

@@ -168,11 +206,7 @@ impl TryFromOutputIter for OutputIterParsedData {
168206
let full_output = wrap_missing_as_bool(output_iter.next(), "full_output")?;
169207

170208
let kzg_commitment_info = if use_kzg_da {
171-
// Read KZG data into a vec.
172-
let kzg_z = wrap_missing(output_iter.next(), "kzg_z")?;
173-
let n_blobs: usize = wrap_missing_as(output_iter.next(), "n_blobs")?;
174-
let commitments = output_iter.take(2 * 2 * n_blobs);
175-
Some([kzg_z, n_blobs.into()].into_iter().chain(commitments).collect::<Vec<_>>())
209+
Some(OsKzgCommitmentInfo::try_from_output_iter(output_iter)?)
176210
} else {
177211
None
178212
};

crates/starknet_os/src/io/os_output_types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::io::os_output::{
1010
try_into_custom_error,
1111
wrap_missing,
1212
wrap_missing_as,
13+
OsKzgCommitmentInfo,
1314
OsOutputError,
1415
};
1516

@@ -336,9 +337,9 @@ impl TryFromOutputIter for PartialOsStateDiff {
336337
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize, serde::Serialize))]
337338
#[derive(Debug, PartialEq)]
338339
// A commitment to the state diff (with KZG commitment applied) in the full output format.
339-
pub struct FullCommitmentOsStateDiff(pub(crate) Vec<Felt>);
340+
pub struct FullCommitmentOsStateDiff(pub(crate) OsKzgCommitmentInfo);
340341

341342
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize, serde::Serialize))]
342343
#[derive(Debug, PartialEq)]
343344
// A commitment to the state diff (with KZG commitment applied) in the partial output format.
344-
pub struct PartialCommitmentOsStateDiff(pub(crate) Vec<Felt>);
345+
pub struct PartialCommitmentOsStateDiff(pub(crate) OsKzgCommitmentInfo);

0 commit comments

Comments
 (0)