Skip to content

Commit dabf5d5

Browse files
authored
fix(new-execution): add register contribtions on reset_segment (#1843)
for metering, we assume all registers are going to be read during execution and don't add their contributions in the hot loop. this pr fixes the `reset_segment` function to also add the register contributions
1 parent c4e2cb6 commit dabf5d5

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

crates/vm/src/arch/execution_mode/metered/ctx.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct MeteredCtx<const PAGE_BITS: usize = 6> {
1515

1616
pub memory_ctx: MemoryCtx<PAGE_BITS>,
1717
pub segmentation_ctx: SegmentationCtx,
18+
pub continuations_enabled: bool,
1819
}
1920

2021
impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
@@ -29,7 +30,7 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
2930
widths: Vec<usize>,
3031
interactions: Vec<usize>,
3132
) -> Self {
32-
let (mut trace_heights, is_trace_height_constant): (Vec<u32>, Vec<bool>) =
33+
let (trace_heights, is_trace_height_constant): (Vec<u32>, Vec<bool>) =
3334
constant_trace_heights
3435
.iter()
3536
.map(|&constant_height| {
@@ -41,7 +42,7 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
4142
})
4243
.unzip();
4344

44-
let mut memory_ctx = MemoryCtx::new(
45+
let memory_ctx = MemoryCtx::new(
4546
has_public_values_chip,
4647
continuations_enabled,
4748
as_byte_alignment_bits,
@@ -57,22 +58,29 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
5758

5859
let segmentation_ctx = SegmentationCtx::new(air_names, widths, interactions);
5960

61+
let mut ctx = Self {
62+
trace_heights,
63+
is_trace_height_constant,
64+
memory_ctx,
65+
segmentation_ctx,
66+
continuations_enabled,
67+
};
68+
6069
// Add merkle height contributions for all registers
61-
if continuations_enabled {
62-
memory_ctx.update_boundary_merkle_heights(
63-
&mut trace_heights,
70+
ctx.add_register_merkle_heights();
71+
72+
ctx
73+
}
74+
75+
fn add_register_merkle_heights(&mut self) {
76+
if self.continuations_enabled {
77+
self.memory_ctx.update_boundary_merkle_heights(
78+
&mut self.trace_heights,
6479
RV32_REGISTER_AS,
6580
0,
6681
(RV32_NUM_REGISTERS * RV32_REGISTER_NUM_LIMBS) as u32,
6782
);
6883
}
69-
70-
Self {
71-
trace_heights,
72-
is_trace_height_constant,
73-
memory_ctx,
74-
segmentation_ctx,
75-
}
7684
}
7785

7886
pub fn with_max_trace_height(mut self, max_trace_height: u32) -> Self {
@@ -111,6 +119,9 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
111119
self.trace_heights[i] = 0;
112120
}
113121
}
122+
123+
// Add merkle height contributions for all registers
124+
self.add_register_merkle_heights();
114125
}
115126

116127
pub fn check_and_segment(&mut self, instret: u64) {
@@ -124,6 +135,21 @@ impl<const PAGE_BITS: usize> MeteredCtx<PAGE_BITS> {
124135
self.reset_segment();
125136
}
126137
}
138+
139+
#[allow(dead_code)]
140+
pub fn print_heights(&self) {
141+
println!("{:>10} {:<30}", "Height", "Air Name");
142+
println!("{}", "-".repeat(42));
143+
for (i, height) in self.trace_heights.iter().enumerate() {
144+
let air_name = self
145+
.segmentation_ctx
146+
.air_names
147+
.get(i)
148+
.map(|s| s.as_str())
149+
.unwrap_or("Unknown");
150+
println!("{:>10} {:<30}", height, air_name);
151+
}
152+
}
127153
}
128154

129155
impl<const PAGE_BITS: usize> E1E2ExecutionCtx for MeteredCtx<PAGE_BITS> {

crates/vm/src/arch/execution_mode/metered/segment_ctx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Default for SegmentationLimits {
3737
pub struct SegmentationCtx {
3838
pub segments: Vec<Segment>,
3939
instret_last_segment_check: u64,
40-
air_names: Vec<String>,
40+
pub(crate) air_names: Vec<String>,
4141
widths: Vec<usize>,
4242
interactions: Vec<usize>,
4343
segment_check_insns: u64,

0 commit comments

Comments
 (0)