1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! The `BinaryBasicInstance` module defines an instance to perform witness computations
//! for binary-related operations using the Binary Basic State Machine.
//!
//! It manages collected inputs and interacts with the `BinaryBasicSM` to compute witnesses for
//! execution plans.
use crate::{BinaryBasicCollector, BinaryBasicSM, ChunkCollect, ADD_KINDS};
use pil2_std_lib::Std;
use proofman_common::{AirInstance, ProofCtx, ProofmanResult, SetupCtx};
use proofman_fields::PrimeField64;
use std::{collections::HashMap, sync::Arc};
use zisk_common::StatsType;
use zisk_common::{
BusDevice, CheckPoint, ChunkId, Instance, InstanceCtx, InstanceType, PayloadType,
};
use zisk_pil::{
BinaryHugeTrace, BinaryHugeTraceRow, BinaryHugeTraceRowPacked, BinaryLargeTrace,
BinaryLargeTraceRow, BinaryLargeTraceRowPacked, BinaryTrace, BinaryTraceRow,
BinaryTraceRowPacked,
};
/// Air id of each `Binary` air. They no longer differ only in height: each packs a different number
/// of operations per row, so each has its own row type and the trace itself carries the rest.
const AIR_ID: usize = BinaryTrace::<()>::AIR_ID;
const LARGE_AIR_ID: usize = BinaryLargeTrace::<()>::AIR_ID;
const HUGE_AIR_ID: usize = BinaryHugeTrace::<()>::AIR_ID;
/// The `BinaryBasicInstance` struct represents an instance for binary-related witness computations.
///
/// It encapsulates the `BinaryBasicSM` and its associated context, and it processes input data
/// to compute witnesses for binary operations.
pub struct BinaryBasicInstance<F: PrimeField64> {
/// Binary Basic state machine.
binary_basic_sm: Arc<BinaryBasicSM<F>>,
/// Instance context.
ictx: InstanceCtx,
/// What this instance takes from each chunk: a `(count, skip)` per kind of operation, plus the
/// frequent operations it accounts for.
collect_info: HashMap<ChunkId, ChunkCollect<ADD_KINDS>>,
/// Standard library instance, providing common functionalities.
std: Arc<Std<F>>,
}
impl<F: PrimeField64> BinaryBasicInstance<F> {
/// Creates a new `BinaryBasicInstance`.
///
/// # Arguments
/// * `binary_basic_sm` - An `Arc`-wrapped reference to the Binary Basic State Machine.
/// * `ictx` - The `InstanceCtx` associated with this instance, containing the execution plan.
///
/// # Returns
/// A new `BinaryBasicInstance` instance initialized with the provided state machine and
/// context.
pub fn new(
binary_basic_sm: Arc<BinaryBasicSM<F>>,
mut ictx: InstanceCtx,
std: Arc<Std<F>>,
) -> Self {
assert!(
matches!(ictx.plan.air_id, AIR_ID | LARGE_AIR_ID | HUGE_AIR_ID),
"BinaryBasicInstance: Unsupported air_id: {:?}",
ictx.plan.air_id
);
let meta = ictx.plan.meta.take().expect("Expected metadata in ictx.plan.meta");
let collect_info = *meta
.downcast::<HashMap<ChunkId, ChunkCollect<ADD_KINDS>>>()
.expect("Failed to downcast ictx.plan.meta to expected type");
Self { binary_basic_sm, ictx, collect_info, std }
}
/// Which of the three `Binary` airs this instance is. They pack a different number of
/// operations per row, so this picks the row type the trace is built with.
fn air_id(&self) -> usize {
self.ictx.plan.air_id
}
pub fn build_binary_basic_collector(&self, chunk_id: ChunkId) -> BinaryBasicCollector<F> {
BinaryBasicCollector::new(self.collect_info[&chunk_id], self.std.clone())
}
}
impl<F: PrimeField64> Instance<F> for BinaryBasicInstance<F> {
/// Computes the witness for the binary execution plan.
///
/// This method leverages the `BinaryBasicSM` to generate an `AirInstance` using the collected
/// inputs.
///
/// # Arguments
/// * `_pctx` - The proof context, unused in this implementation.
/// * `_sctx` - The setup context, unused in this implementation.
/// * `collectors` - A vector of input collectors to process and collect data for witness
///
/// # Returns
/// An `Option` containing the computed `AirInstance`.
fn compute_witness(
&self,
_pctx: &ProofCtx<F>,
_sctx: &SetupCtx<F>,
collectors: Vec<(usize, Box<dyn BusDevice<PayloadType>>)>,
trace_buffer: Vec<F>,
packed: bool,
) -> ProofmanResult<Option<AirInstance<F>>> {
let inputs: Vec<_> = collectors
.into_iter()
.map(|(_, collector)| {
let _collector = collector.as_any().downcast::<BinaryBasicCollector<F>>().unwrap();
_collector.inputs
})
.collect();
let sm = &self.binary_basic_sm;
Ok(Some(match (self.air_id(), packed) {
(AIR_ID, true) => {
sm.compute_witness::<_, BinaryTraceRowPacked<F>>(&inputs, trace_buffer)?
}
(AIR_ID, false) => sm.compute_witness::<_, BinaryTraceRow<F>>(&inputs, trace_buffer)?,
(LARGE_AIR_ID, true) => {
sm.compute_witness::<_, BinaryLargeTraceRowPacked<F>>(&inputs, trace_buffer)?
}
(LARGE_AIR_ID, false) => {
sm.compute_witness::<_, BinaryLargeTraceRow<F>>(&inputs, trace_buffer)?
}
(HUGE_AIR_ID, true) => {
sm.compute_witness::<_, BinaryHugeTraceRowPacked<F>>(&inputs, trace_buffer)?
}
(HUGE_AIR_ID, false) => {
sm.compute_witness::<_, BinaryHugeTraceRow<F>>(&inputs, trace_buffer)?
}
(air_id, _) => panic!("BinaryBasicInstance: Unsupported air_id: {air_id:?}"),
}))
}
/// Retrieves the checkpoint associated with this instance.
///
/// # Returns
/// A `CheckPoint` object representing the checkpoint of the execution plan.
fn check_point(&self) -> &CheckPoint {
&self.ictx.plan.check_point
}
/// Retrieves the type of this instance.
///
/// # Returns
/// An `InstanceType` representing the type of this instance (`InstanceType::Instance`).
fn instance_type(&self) -> InstanceType {
InstanceType::Instance
}
fn stats_type(&self) -> StatsType {
StatsType::Opcodes
}
/// Builds an input collector for the instance.
///
/// # Arguments
/// * `chunk_id` - The chunk ID associated with the input collector.
///
/// # Returns
/// An `Option` containing the input collector for the instance.
fn build_inputs_collector(&self, chunk_id: ChunkId) -> Option<Box<dyn BusDevice<PayloadType>>> {
Some(Box::new(BinaryBasicCollector::new(self.collect_info[&chunk_id], self.std.clone())))
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}