#![allow(missing_docs)]
use std::fmt::Debug;
use hashbrown::HashMap;
use p3_matrix::{dense::RowMajorMatrixView, stack::VerticalPair};
use serde::{Deserialize, Serialize};
use super::{Challenge, Com, OpeningProof, StarkGenericConfig, Val};
pub type QuotientOpenedValues<T> = Vec<T>;
pub struct ShardMainData<SC: StarkGenericConfig, M, P> {
pub traces: Vec<M>,
pub main_commit: Com<SC>,
pub main_data: P,
pub chip_ordering: HashMap<String, usize>,
pub public_values: Vec<SC::Val>,
}
impl<SC: StarkGenericConfig, M, P> ShardMainData<SC, M, P> {
pub const fn new(
traces: Vec<M>,
main_commit: Com<SC>,
main_data: P,
chip_ordering: HashMap<String, usize>,
public_values: Vec<Val<SC>>,
) -> Self {
Self { traces, main_commit, main_data, chip_ordering, public_values }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShardCommitment<C> {
pub main_commit: C,
pub permutation_commit: C,
pub quotient_commit: C,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AirOpenedValues<T> {
pub local: Vec<T>,
pub next: Vec<T>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChipOpenedValues<T: Serialize> {
pub preprocessed: AirOpenedValues<T>,
pub main: AirOpenedValues<T>,
pub permutation: AirOpenedValues<T>,
pub quotient: Vec<Vec<T>>,
pub cumulative_sum: T,
pub log_degree: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShardOpenedValues<T: Serialize> {
pub chips: Vec<ChipOpenedValues<T>>,
}
pub const PROOF_MAX_NUM_PVS: usize = 370;
#[derive(Serialize, Deserialize, Clone)]
#[serde(bound = "")]
pub struct ShardProof<SC: StarkGenericConfig> {
pub commitment: ShardCommitment<Com<SC>>,
pub opened_values: ShardOpenedValues<Challenge<SC>>,
pub opening_proof: OpeningProof<SC>,
pub chip_ordering: HashMap<String, usize>,
pub public_values: Vec<Val<SC>>,
}
impl<SC: StarkGenericConfig> Debug for ShardProof<SC> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ShardProof").finish()
}
}
impl<T: Send + Sync + Clone> AirOpenedValues<T> {
#[must_use]
pub fn view(&self) -> VerticalPair<RowMajorMatrixView<'_, T>, RowMajorMatrixView<'_, T>> {
let a = RowMajorMatrixView::new_row(&self.local);
let b = RowMajorMatrixView::new_row(&self.next);
VerticalPair::new(a, b)
}
}
impl<SC: StarkGenericConfig> ShardProof<SC> {
pub fn cumulative_sum(&self) -> Challenge<SC> {
self.opened_values.chips.iter().map(|c| c.cumulative_sum).sum()
}
pub fn log_degree_cpu(&self) -> usize {
let idx = self.chip_ordering.get("CPU").expect("CPU chip not found");
self.opened_values.chips[*idx].log_degree
}
pub fn contains_cpu(&self) -> bool {
self.chip_ordering.contains_key("CPU")
}
pub fn contains_memory_init(&self) -> bool {
self.chip_ordering.contains_key("MemoryInit")
}
pub fn contains_memory_finalize(&self) -> bool {
self.chip_ordering.contains_key("MemoryFinalize")
}
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(bound = "")]
pub struct MachineProof<SC: StarkGenericConfig> {
pub shard_proofs: Vec<ShardProof<SC>>,
}
impl<SC: StarkGenericConfig> Debug for MachineProof<SC> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Proof").field("shard_proofs", &self.shard_proofs.len()).finish()
}
}
pub struct PublicValuesDigest(pub [u8; 32]);
impl From<[u32; 8]> for PublicValuesDigest {
fn from(arr: [u32; 8]) -> Self {
let mut bytes = [0u8; 32];
for (i, word) in arr.iter().enumerate() {
bytes[i * 4..(i + 1) * 4].copy_from_slice(&word.to_le_bytes());
}
PublicValuesDigest(bytes)
}
}
pub struct DeferredDigest(pub [u8; 32]);
impl From<[u32; 8]> for DeferredDigest {
fn from(arr: [u32; 8]) -> Self {
let mut bytes = [0u8; 32];
for (i, word) in arr.iter().enumerate() {
bytes[i * 4..(i + 1) * 4].copy_from_slice(&word.to_le_bytes());
}
DeferredDigest(bytes)
}
}