forked from openvm-org/openvm
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbridge.rs
More file actions
377 lines (345 loc) · 12.9 KB
/
bridge.rs
File metadata and controls
377 lines (345 loc) · 12.9 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use getset::CopyGetters;
use openvm_circuit_primitives::{
assert_less_than::{AssertLessThanIo, AssertLtSubAir},
is_zero::{IsZeroIo, IsZeroSubAir},
utils::not,
var_range::VariableRangeCheckerBus,
SubAir,
};
use openvm_stark_backend::{
interaction::InteractionBuilder, p3_air::AirBuilder, p3_field::FieldAlgebra,
};
use super::bus::{HintBus, MemoryBus};
use crate::system::memory::{
offline_checker::columns::{
MemoryBaseAuxCols, MemoryReadAuxCols, MemoryReadOrImmediateAuxCols, MemoryWriteAuxCols,
},
MemoryAddress,
};
/// AUX_LEN is the number of auxiliary columns (aka the number of limbs that the input numbers will
/// be decomposed into) for the `AssertLtSubAir` in the `MemoryOfflineChecker`.
/// Warning: This requires that (timestamp_max_bits + decomp - 1) / decomp = AUX_LEN
/// in MemoryOfflineChecker (or whenever AssertLtSubAir is used)
pub const AUX_LEN: usize = 2;
/// The [MemoryBridge] is used within AIR evaluation functions to constrain logical memory
/// operations (read/write). It adds all necessary constraints and interactions.
#[derive(Clone, Copy, Debug)]
pub struct MemoryBridge {
offline_checker: MemoryOfflineChecker,
}
impl MemoryBridge {
/// Create a new [MemoryBridge] with the provided offline_checker.
pub fn new(
memory_bus: MemoryBus,
timestamp_max_bits: usize,
range_bus: VariableRangeCheckerBus,
) -> Self {
Self {
offline_checker: MemoryOfflineChecker::new(memory_bus, timestamp_max_bits, range_bus),
}
}
pub fn memory_bus(&self) -> MemoryBus {
self.offline_checker.memory_bus
}
pub fn range_bus(&self) -> VariableRangeCheckerBus {
self.offline_checker.timestamp_lt_air.bus
}
/// Prepare a logical memory read operation.
#[must_use]
pub fn read<'a, T, V, const N: usize>(
&self,
address: MemoryAddress<impl Into<T>, impl Into<T>>,
data: [impl Into<T>; N],
timestamp: impl Into<T>,
aux: &'a MemoryReadAuxCols<V>,
) -> MemoryReadOperation<'a, T, V, N> {
MemoryReadOperation {
offline_checker: self.offline_checker,
address: MemoryAddress::from(address),
data: data.map(Into::into),
timestamp: timestamp.into(),
aux,
}
}
/// Prepare a logical memory read or immediate operation.
#[must_use]
pub fn read_or_immediate<'a, T, V>(
&self,
address: MemoryAddress<impl Into<T>, impl Into<T>>,
data: impl Into<T>,
timestamp: impl Into<T>,
aux: &'a MemoryReadOrImmediateAuxCols<V>,
) -> MemoryReadOrImmediateOperation<'a, T, V> {
MemoryReadOrImmediateOperation {
offline_checker: self.offline_checker,
address: MemoryAddress::from(address),
data: data.into(),
timestamp: timestamp.into(),
aux,
}
}
/// Prepare a logical memory write operation.
#[must_use]
pub fn write<'a, T, V, const N: usize>(
&self,
address: MemoryAddress<impl Into<T>, impl Into<T>>,
data: [impl Into<T>; N],
timestamp: impl Into<T>,
aux: &'a MemoryWriteAuxCols<V, N>,
) -> MemoryWriteOperation<'a, T, V, N> {
MemoryWriteOperation {
offline_checker: self.offline_checker,
address: MemoryAddress::from(address),
data: data.map(Into::into),
timestamp: timestamp.into(),
aux,
}
}
}
/// Constraints and interactions for a logical memory read of `(address, data)` at time `timestamp`.
/// This reads `(address, data, timestamp_prev)` from the memory bus and writes
/// `(address, data, timestamp)` to the memory bus.
/// Includes constraints for `timestamp_prev < timestamp`.
///
/// The generic `T` type is intended to be `AB::Expr` where `AB` is the [AirBuilder].
/// The auxiliary columns are not expected to be expressions, so the generic `V` type is intended
/// to be `AB::Var`.
pub struct MemoryReadOperation<'a, T, V, const N: usize> {
offline_checker: MemoryOfflineChecker,
address: MemoryAddress<T, T>,
data: [T; N],
timestamp: T,
aux: &'a MemoryReadAuxCols<V>,
}
/// The max degree of constraints is:
/// eval_timestamps: deg(enabled) + max(1, deg(self.timestamp))
/// eval_bulk_access: refer to private function MemoryOfflineChecker::eval_bulk_access
impl<F: FieldAlgebra, V: Copy + Into<F>, const N: usize> MemoryReadOperation<'_, F, V, N> {
/// Evaluate constraints and send/receive interactions.
pub fn eval<AB>(self, builder: &mut AB, enabled: impl Into<AB::Expr>)
where
AB: InteractionBuilder<Var = V, Expr = F>,
{
let enabled = enabled.into();
// NOTE: We do not need to constrain `address_space != 0` since this is done implicitly by
// the memory interactions argument together with initial/final memory chips.
self.offline_checker.eval_timestamps(
builder,
self.timestamp.clone(),
&self.aux.base,
enabled.clone(),
);
self.offline_checker.eval_bulk_access(
builder,
self.address,
&self.data,
&self.data,
self.timestamp.clone(),
self.aux.base.prev_timestamp,
enabled,
);
}
}
/// Constraints and interactions for a logical memory read of `(address, data)` at time `timestamp`,
/// supporting `address.address_space = 0` for immediates.
///
/// If `address.address_space` is non-zero, it behaves like `MemoryReadOperation`. Otherwise,
/// it constrains the immediate value appropriately.
///
/// The generic `T` type is intended to be `AB::Expr` where `AB` is the [AirBuilder].
/// The auxiliary columns are not expected to be expressions, so the generic `V` type is intended
/// to be `AB::Var`.
pub struct MemoryReadOrImmediateOperation<'a, T, V> {
offline_checker: MemoryOfflineChecker,
address: MemoryAddress<T, T>,
data: T,
timestamp: T,
aux: &'a MemoryReadOrImmediateAuxCols<V>,
}
/// The max degree of constraints is:
/// IsZeroSubAir.subair_eval:
/// deg(enabled) + max(deg(address.address_space) + deg(aux.is_immediate),
/// deg(address.address_space) + deg(aux.is_zero_aux))
/// is_immediate check: deg(aux.is_immediate) + max(deg(data), deg(address.pointer))
/// eval_timestamps: deg(enabled) + max(1, deg(self.timestamp))
/// eval_bulk_access: refer to private function MemoryOfflineChecker::eval_bulk_access
impl<F: FieldAlgebra, V: Copy + Into<F>> MemoryReadOrImmediateOperation<'_, F, V> {
/// Evaluate constraints and send/receive interactions.
pub fn eval<AB>(self, builder: &mut AB, enabled: impl Into<AB::Expr>)
where
AB: InteractionBuilder<Var = V, Expr = F>,
{
let enabled = enabled.into();
// `is_immediate` should be an indicator for `address_space == 0` (when `enabled`).
{
let is_zero_io = IsZeroIo::new(
self.address.address_space.clone(),
self.aux.is_immediate.into(),
enabled.clone(),
);
IsZeroSubAir.eval(builder, (is_zero_io, self.aux.is_zero_aux));
}
// When `is_immediate`, the data should be the pointer value.
builder
.when(self.aux.is_immediate)
.assert_eq(self.data.clone(), self.address.pointer.clone());
// Timestamps should be increasing (when enabled).
self.offline_checker.eval_timestamps(
builder,
self.timestamp.clone(),
&self.aux.base,
enabled.clone(),
);
self.offline_checker.eval_bulk_access(
builder,
self.address,
&[self.data.clone()],
&[self.data],
self.timestamp,
self.aux.base.prev_timestamp,
enabled * not(self.aux.is_immediate),
);
}
}
/// Constraints and interactions for a logical memory write of `(address, data)` at time
/// `timestamp`. This reads `(address, data_prev, timestamp_prev)` from the memory bus and writes
/// `(address, data, timestamp)` to the memory bus.
/// Includes constraints for `timestamp_prev < timestamp`.
///
/// **Note:** This can be used as a logical read operation by setting `data_prev = data`.
pub struct MemoryWriteOperation<'a, T, V, const N: usize> {
offline_checker: MemoryOfflineChecker,
address: MemoryAddress<T, T>,
data: [T; N],
/// The timestamp of the current read
timestamp: T,
aux: &'a MemoryWriteAuxCols<V, N>,
}
/// The max degree of constraints is:
/// eval_timestamps: deg(enabled) + max(1, deg(self.timestamp))
/// eval_bulk_access: refer to private function MemoryOfflineChecker::eval_bulk_access
impl<T: FieldAlgebra, V: Copy + Into<T>, const N: usize> MemoryWriteOperation<'_, T, V, N> {
/// Evaluate constraints and send/receive interactions. `enabled` must be boolean.
pub fn eval<AB>(self, builder: &mut AB, enabled: impl Into<AB::Expr>)
where
AB: InteractionBuilder<Var = V, Expr = T>,
{
let enabled = enabled.into();
self.offline_checker.eval_timestamps(
builder,
self.timestamp.clone(),
&self.aux.base,
enabled.clone(),
);
self.offline_checker.eval_bulk_access(
builder,
self.address,
&self.data,
&self.aux.prev_data.map(Into::into),
self.timestamp,
self.aux.base.prev_timestamp,
enabled,
);
}
}
#[derive(Clone, Copy, Debug, CopyGetters)]
struct MemoryOfflineChecker {
#[get_copy = "pub"]
memory_bus: MemoryBus,
#[get_copy = "pub"]
timestamp_lt_air: AssertLtSubAir,
}
impl MemoryOfflineChecker {
fn new(
memory_bus: MemoryBus,
timestamp_max_bits: usize,
range_bus: VariableRangeCheckerBus,
) -> Self {
Self {
memory_bus,
timestamp_lt_air: AssertLtSubAir::new(range_bus, timestamp_max_bits),
}
}
/// The max degree of constraints is:
/// deg(enabled) + max(1, deg(timestamp))
/// Note: deg(prev_timestamp) = 1 since prev_timestamp is Var
fn eval_timestamps<AB: InteractionBuilder>(
&self,
builder: &mut AB,
timestamp: AB::Expr,
base: &MemoryBaseAuxCols<AB::Var>,
enabled: AB::Expr,
) {
let lt_io = AssertLessThanIo::new(base.prev_timestamp, timestamp.clone(), enabled);
self.timestamp_lt_air
.eval(builder, (lt_io, &base.timestamp_lt_aux.lower_decomp));
}
/// At the core, eval_bulk_access is a bunch of push_sends and push_receives.
/// The max constraint degree of expressions in sends/receives is:
/// max(max_deg(data), max_deg(prev_data), max_deg(timestamp), max_deg(prev_timestamps))
/// Also, each one of them has count with degree: deg(enabled)
#[allow(clippy::too_many_arguments)]
fn eval_bulk_access<AB, const N: usize>(
&self,
builder: &mut AB,
address: MemoryAddress<AB::Expr, AB::Expr>,
data: &[AB::Expr; N],
prev_data: &[AB::Expr; N],
timestamp: AB::Expr,
prev_timestamp: AB::Var,
enabled: AB::Expr,
) where
AB: InteractionBuilder,
{
self.memory_bus
.receive(address.clone(), prev_data.to_vec(), prev_timestamp)
.eval(builder, enabled.clone());
self.memory_bus
.send(address, data.to_vec(), timestamp)
.eval(builder, enabled);
}
}
/// The [HintBridge] is used to constrain hint space lookups.
/// Consumer chips call `lookup` to verify that values they read from hint_space
/// match what was originally loaded via the hint bus lookup table.
#[derive(Clone, Copy, Debug)]
pub struct HintBridge {
hint_bus: HintBus,
}
impl HintBridge {
/// Create a new [HintBridge] with the provided hint bus.
pub fn new(hint_bus: HintBus) -> Self {
Self { hint_bus }
}
pub fn hint_bus(&self) -> HintBus {
self.hint_bus
}
/// Perform a lookup on the hint bus for a single element.
///
/// Constrains that `(hint_id, offset, value)` exists in the hint lookup table.
/// Caller must constrain that `enabled` is boolean.
pub fn lookup<AB: InteractionBuilder>(
&self,
builder: &mut AB,
hint_id: impl Into<AB::Expr>,
offset: impl Into<AB::Expr>,
value: impl Into<AB::Expr>,
enabled: impl Into<AB::Expr>,
) {
self.hint_bus.lookup(builder, hint_id, offset, value, enabled);
}
/// Add a key to the hint lookup table.
///
/// Provider chips call this to register that `(hint_id, offset, value)` is available.
pub fn provide<AB: InteractionBuilder>(
&self,
builder: &mut AB,
hint_id: impl Into<AB::Expr>,
offset: impl Into<AB::Expr>,
value: impl Into<AB::Expr>,
num_lookups: impl Into<AB::Expr>,
) {
self.hint_bus
.provide(builder, hint_id, offset, value, num_lookups);
}
}