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
//! The `BinaryAddHiCollector` struct represents an input collector for the packed add operations
//! proven by `BinaryAddHi`.
use crate::{
add_family_kind, BinaryBasicFrops, BinaryCollectCursor, BinaryInput, ChunkCollect,
CollectAction, ADD_KINDS, KIND_BASIC,
};
use zisk_common::{BusDevice, BusId, ExtOperationData, OperationBusData, A, B, OPERATION_BUS_ID};
use pil2_std_lib::Std;
use proofman_fields::PrimeField64;
use std::sync::Arc;
/// The `BinaryAddHiCollector` struct represents an input collector for packed add operations.
pub struct BinaryAddHiCollector<F: PrimeField64> {
/// Collected inputs for witness computation.
pub inputs: Vec<BinaryInput>,
/// Decides, operation by operation, what belongs to this instance.
cursor: BinaryCollectCursor<ADD_KINDS>,
/// The table ID for the Binary Add FROPS
frops_table_id: usize,
/// Standard library instance, providing common functionalities.
std: Arc<Std<F>>,
}
impl<F: PrimeField64> BinaryAddHiCollector<F> {
/// Creates a new `BinaryAddHiCollector`.
///
/// # Arguments
/// * `collect` - What this instance takes from the chunk: a `(count, skip)` per kind of
/// operation, plus which of the chunk's frequent operations it accounts for.
/// * `std` - PIL2 standard library utilities.
///
/// # Returns
/// A new `BinaryAddHiCollector` ready to replay the chunk.
pub fn new(collect: ChunkCollect<ADD_KINDS>, std: Arc<Std<F>>) -> Self {
let frops_table_id = std
.get_virtual_table_id(BinaryBasicFrops::TABLE_ID)
.expect("Failed to get FROPS table ID");
Self { inputs: Vec::new(), cursor: BinaryCollectCursor::new(collect), frops_table_id, std }
}
/// Processes data received on the bus, collecting the inputs necessary for witness computation.
///
/// # Arguments
/// * `bus_id` - The ID of the bus sending the data.
/// * `data` - The data received from the bus.
///
/// # Returns
/// A boolean indicating whether the program should continue execution or terminate.
/// Returns `true` to continue execution, `false` to stop.
#[inline(always)]
pub fn process_data(&mut self, bus_id: &BusId, data: &[u64]) -> bool {
debug_assert!(*bus_id == OPERATION_BUS_ID);
let op_data: ExtOperationData<u64> =
data.try_into().expect("Regular Metrics: Failed to convert data");
// One classifier for the whole family, shared with the counter, so this air never collects
// an operation the plan counted somewhere else. A basic kind is not this air's: only the
// `Binary` airs prove those, and they are the ones that account for their frops too.
let op = OperationBusData::get_op(&op_data);
let kind = add_family_kind(op, data[A], data[B]);
if kind == KIND_BASIC {
return true;
}
let frops_row = BinaryBasicFrops::get_row(op, data[A], data[B]);
match self.cursor.next(kind, frops_row != BinaryBasicFrops::NO_FROPS) {
CollectAction::Stop => false,
CollectAction::Pass => true,
CollectAction::CountFrop => {
self.std.inc_virtual_row_one(self.frops_table_id, frops_row);
true
}
CollectAction::Collect => {
self.inputs.push(BinaryInput::from(&op_data));
!self.cursor.is_done()
}
}
}
}
impl<F: PrimeField64> BusDevice<u64> for BinaryAddHiCollector<F> {
/// Provides a dynamic reference for downcasting purposes.
fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
self
}
}