Skip to content

Commit

Permalink
Merge pull request #1514 from 0xPolygonMiden/plafer-1389-fix-chiplets…
Browse files Browse the repository at this point in the history
…-vtable

Fix chiplets vtable construction
  • Loading branch information
plafer authored Sep 26, 2024
2 parents 7ee5170 + be14f5b commit 6f60f74
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Fixed an issue with formatting of blocks in Miden Assembly syntax
- Fixed the construction of the block hash table (#1506)
- Fixed a bug in the block stack table (#1511)
- Fixed the construction of the chiplets virtual table (#1514)

#### Fixes

Expand Down
62 changes: 48 additions & 14 deletions processor/src/chiplets/aux_trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ impl AuxTraceBuilder {
let bus_col_builder = BusColumnBuilder::default();
let t_chip = v_table_col_builder.build_aux_column(main_trace, rand_elements);
let b_chip = bus_col_builder.build_aux_column(main_trace, rand_elements);

debug_assert_eq!(*t_chip.last().unwrap(), E::ONE);
vec![t_chip, b_chip]
}
}
Expand Down Expand Up @@ -86,14 +88,10 @@ where
E: FieldElement<BaseField = Felt>,
{
let f_mu: bool = main_trace.f_mu(row);
let f_mua: bool = if row == 0 { false } else { main_trace.f_mua(row - 1) };
let f_mua: bool = main_trace.f_mua(row);

if f_mu || f_mua {
let index = if f_mua {
main_trace.chiplet_node_index(row - 1)
} else {
main_trace.chiplet_node_index(row)
};
if f_mu {
let index = main_trace.chiplet_node_index(row);
let lsb = index.as_int() & 1;
if lsb == 0 {
let sibling = &main_trace.chiplet_hasher_state(row)[DIGEST_RANGE.end..];
Expand All @@ -112,6 +110,26 @@ where
+ alphas[10].mul_base(sibling[2])
+ alphas[11].mul_base(sibling[3])
}
} else if f_mua {
let index = main_trace.chiplet_node_index(row);
let lsb = index.as_int() & 1;
if lsb == 0 {
let sibling = &main_trace.chiplet_hasher_state(row + 1)[DIGEST_RANGE.end..];
alphas[0]
+ alphas[3].mul_base(index)
+ alphas[12].mul_base(sibling[0])
+ alphas[13].mul_base(sibling[1])
+ alphas[14].mul_base(sibling[2])
+ alphas[15].mul_base(sibling[3])
} else {
let sibling = &main_trace.chiplet_hasher_state(row + 1)[DIGEST_RANGE];
alphas[0]
+ alphas[3].mul_base(index)
+ alphas[8].mul_base(sibling[0])
+ alphas[9].mul_base(sibling[1])
+ alphas[10].mul_base(sibling[2])
+ alphas[11].mul_base(sibling[3])
}
} else {
E::ONE
}
Expand All @@ -127,14 +145,10 @@ where
E: FieldElement<BaseField = Felt>,
{
let f_mv: bool = main_trace.f_mv(row);
let f_mva: bool = if row == 0 { false } else { main_trace.f_mva(row - 1) };
let f_mva: bool = main_trace.f_mva(row);

if f_mv || f_mva {
let index = if f_mva {
main_trace.chiplet_node_index(row - 1)
} else {
main_trace.chiplet_node_index(row)
};
if f_mv {
let index = main_trace.chiplet_node_index(row);
let lsb = index.as_int() & 1;
if lsb == 0 {
let sibling = &main_trace.chiplet_hasher_state(row)[DIGEST_RANGE.end..];
Expand All @@ -153,6 +167,26 @@ where
+ alphas[10].mul_base(sibling[2])
+ alphas[11].mul_base(sibling[3])
}
} else if f_mva {
let index = main_trace.chiplet_node_index(row);
let lsb = index.as_int() & 1;
if lsb == 0 {
let sibling = &main_trace.chiplet_hasher_state(row + 1)[DIGEST_RANGE.end..];
alphas[0]
+ alphas[3].mul_base(index)
+ alphas[12].mul_base(sibling[0])
+ alphas[13].mul_base(sibling[1])
+ alphas[14].mul_base(sibling[2])
+ alphas[15].mul_base(sibling[3])
} else {
let sibling = &main_trace.chiplet_hasher_state(row + 1)[DIGEST_RANGE];
alphas[0]
+ alphas[3].mul_base(index)
+ alphas[8].mul_base(sibling[0])
+ alphas[9].mul_base(sibling[1])
+ alphas[10].mul_base(sibling[2])
+ alphas[11].mul_base(sibling[3])
}
} else {
E::ONE
}
Expand Down
32 changes: 16 additions & 16 deletions processor/src/trace/tests/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,23 @@ fn hasher_p1_mr_update() {
expected_value *= row_values[0];
assert_eq!(expected_value, p1[9]);

// and then again for the next 7 steps the value remains the same
for i in 10..17 {
// and then again for the next 6 steps the value remains the same
for i in 10..16 {
assert_eq!(expected_value, p1[i]);
}

// on step 16, the next sibling is added to the table in the following row (step 17)
// on step 15, the next sibling is added to the table in the following row (step 16)
expected_value *= row_values[1];
assert_eq!(expected_value, p1[17]);
assert_eq!(expected_value, p1[16]);

// and then again for the next 7 steps the value remains the same
for i in 18..25 {
// and then again for the next 6 steps the value remains the same
for i in 18..24 {
assert_eq!(expected_value, p1[i]);
}

// on step 24, the last sibling is added to the table in the following row (step 25)
// on step 23, the last sibling is added to the table in the following row (step 24)
expected_value *= row_values[2];
assert_eq!(expected_value, p1[25]);
assert_eq!(expected_value, p1[24]);

// and then again for the next 7 steps the value remains the same
for i in 25..33 {
Expand All @@ -126,23 +126,23 @@ fn hasher_p1_mr_update() {
expected_value *= row_values[0].inv();
assert_eq!(expected_value, p1[33]);

// then, for the next 7 steps the value remains the same
for i in 33..41 {
// then, for the next 6 steps the value remains the same
for i in 33..40 {
assert_eq!(expected_value, p1[i]);
}

// on step 40, the next sibling is removed from the table in the following row (step 41)
// on step 39, the next sibling is removed from the table in the following row (step 40)
expected_value *= row_values[1].inv();
assert_eq!(expected_value, p1[41]);
assert_eq!(expected_value, p1[40]);

// and then again for the next 7 steps the value remains the same
for i in 41..49 {
// and then again for the next 6 steps the value remains the same
for i in 41..48 {
assert_eq!(expected_value, p1[i]);
}

// on step 48, the last sibling is removed from the table in the following row (step 49)
// on step 47, the last sibling is removed from the table in the following row (step 48)
expected_value *= row_values[2].inv();
assert_eq!(expected_value, p1[49]);
assert_eq!(expected_value, p1[48]);

// at this point the table should be empty again, and it should stay empty until the end
assert_eq!(expected_value, ONE);
Expand Down

0 comments on commit 6f60f74

Please sign in to comment.