openvm_circuit/system/memory/merkle/
air.rs1use std::{borrow::Borrow, iter};
2
3use openvm_stark_backend::{
4 interaction::{InteractionBuilder, PermutationCheckBus},
5 p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir},
6 p3_field::{Field, FieldAlgebra},
7 p3_matrix::Matrix,
8 rap::{BaseAirWithPublicValues, PartitionedBaseAir},
9};
10
11use crate::system::memory::merkle::{MemoryDimensions, MemoryMerkleCols, MemoryMerklePvs};
12
13#[derive(Clone, Debug)]
14pub struct MemoryMerkleAir<const CHUNK: usize> {
15 pub memory_dimensions: MemoryDimensions,
16 pub merkle_bus: PermutationCheckBus,
17 pub compression_bus: PermutationCheckBus,
18}
19
20impl<const CHUNK: usize, F: Field> PartitionedBaseAir<F> for MemoryMerkleAir<CHUNK> {}
21impl<const CHUNK: usize, F: Field> BaseAir<F> for MemoryMerkleAir<CHUNK> {
22 fn width(&self) -> usize {
23 MemoryMerkleCols::<F, CHUNK>::width()
24 }
25}
26impl<const CHUNK: usize, F: Field> BaseAirWithPublicValues<F> for MemoryMerkleAir<CHUNK> {
27 fn num_public_values(&self) -> usize {
28 MemoryMerklePvs::<F, CHUNK>::width()
29 }
30}
31
32impl<const CHUNK: usize, AB: InteractionBuilder + AirBuilderWithPublicValues> Air<AB>
33 for MemoryMerkleAir<CHUNK>
34{
35 fn eval(&self, builder: &mut AB) {
36 let main = builder.main();
37 let (local, next) = (main.row_slice(0), main.row_slice(1));
38 let local: &MemoryMerkleCols<_, CHUNK> = (*local).borrow();
39 let next: &MemoryMerkleCols<_, CHUNK> = (*next).borrow();
40
41 builder.assert_eq(
43 local.expand_direction,
44 local.expand_direction * local.expand_direction * local.expand_direction,
45 );
46
47 builder.assert_bool(local.left_direction_different);
48 builder.assert_bool(local.right_direction_different);
49
50 builder
52 .when_ne(local.expand_direction, AB::F::NEG_ONE)
53 .assert_zero(local.left_direction_different);
54 builder
55 .when_ne(local.expand_direction, AB::F::NEG_ONE)
56 .assert_zero(local.right_direction_different);
57
58 builder
61 .when_transition()
62 .assert_bool(local.parent_height - next.parent_height);
63 builder
64 .when_transition()
65 .assert_bool(local.height_section - next.height_section);
66 builder
67 .when_transition()
68 .assert_bool(local.is_root - next.is_root);
69
70 builder.when_first_row().assert_one(local.height_section);
72 builder.when_first_row().assert_one(local.is_root);
74 builder.when_first_row().assert_one(next.is_root);
75 builder.when_last_row().assert_zero(local.height_section);
77 builder.when_last_row().assert_zero(local.is_root);
78 builder
81 .when_transition()
82 .when_ne(
83 local.parent_height,
84 AB::F::from_canonical_usize(self.memory_dimensions.address_height + 1),
85 )
86 .assert_eq(local.height_section, next.height_section);
87 builder
88 .when_transition()
89 .when_ne(
90 next.parent_height,
91 AB::F::from_canonical_usize(self.memory_dimensions.address_height),
92 )
93 .assert_eq(local.height_section, next.height_section);
94 builder
97 .when(local.is_root)
98 .when(next.is_root)
99 .assert_eq(local.expand_direction - next.expand_direction, AB::F::TWO);
100
101 builder.when(local.is_root).assert_eq(
103 local.parent_height,
104 AB::Expr::from_canonical_usize(self.memory_dimensions.overall_height()),
105 );
106
107 let &MemoryMerklePvs::<_, CHUNK> {
109 initial_root,
110 final_root,
111 } = builder.public_values().borrow();
112 for i in 0..CHUNK {
113 builder
114 .when_first_row()
115 .assert_eq(local.parent_hash[i], initial_root[i]);
116 builder
117 .when_first_row()
118 .assert_eq(next.parent_hash[i], final_root[i]);
119 }
120
121 self.eval_interactions(builder, local);
122 }
123}
124
125impl<const CHUNK: usize> MemoryMerkleAir<CHUNK> {
126 pub fn eval_interactions<AB: InteractionBuilder>(
127 &self,
128 builder: &mut AB,
129 local: &MemoryMerkleCols<AB::Var, CHUNK>,
130 ) {
131 self.merkle_bus.interact(
134 builder,
135 [
136 local.expand_direction.into(),
137 local.parent_height.into(),
138 local.parent_as_label.into(),
139 local.parent_address_label.into(),
140 ]
141 .into_iter()
142 .chain(local.parent_hash.into_iter().map(Into::into)),
143 (AB::Expr::ONE - local.is_root) * local.expand_direction,
145 );
146
147 self.merkle_bus.interact(
148 builder,
149 [
150 local.expand_direction + (local.left_direction_different * AB::F::TWO),
151 local.parent_height - AB::F::ONE,
152 local.parent_as_label * (AB::Expr::ONE + local.height_section),
153 local.parent_address_label * (AB::Expr::TWO - local.height_section),
154 ]
155 .into_iter()
156 .chain(local.left_child_hash.into_iter().map(Into::into)),
157 -local.expand_direction.into(),
158 );
159
160 self.merkle_bus.interact(
161 builder,
162 [
163 local.expand_direction + (local.right_direction_different * AB::F::TWO),
164 local.parent_height - AB::F::ONE,
165 (local.parent_as_label * (AB::Expr::ONE + local.height_section))
166 + local.height_section,
167 (local.parent_address_label * (AB::Expr::TWO - local.height_section))
168 + (AB::Expr::ONE - local.height_section),
169 ]
170 .into_iter()
171 .chain(local.right_child_hash.into_iter().map(Into::into)),
172 -local.expand_direction.into(),
173 );
174
175 let compress_fields = iter::empty()
176 .chain(local.left_child_hash)
177 .chain(local.right_child_hash)
178 .chain(local.parent_hash);
179 self.compression_bus.interact(
180 builder,
181 compress_fields,
182 local.expand_direction * local.expand_direction,
183 );
184 }
185}