Skip to content

Commit

Permalink
Fixe issue overcounting when expressions are of degree 0
Browse files Browse the repository at this point in the history
  • Loading branch information
hecmas committed Dec 3, 2024
1 parent 7a7094f commit 5f1a0ef
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 103 deletions.
15 changes: 7 additions & 8 deletions pil2-components/lib/std/pil/std_prod.pil
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function init_proof_containers_prod(int name, int opid) {
}
}

private function init_containers_prod(int name, int opid) {
private function init_air_containers_prod(int name, int opid) {
container air.std.gprod {
int gprod_assumes_count = 0;
expr gprod_assumes_sel[100];
Expand Down Expand Up @@ -104,17 +104,16 @@ private function update_piop_prod(int name, int proves, int opid, expr sel, expr
init_proof_containers_prod(name, opid);

if (direct_type == PIOP_DIRECT_TYPE_AIR || direct_type == PIOP_DIRECT_TYPE_DEFAULT) {
init_containers_prod(name, opid);
}
init_air_containers_prod(name, opid);

if (direct_type == PIOP_DIRECT_TYPE_DEFAULT) {
// Create debug hints for the witness computation
const int ncols = length(cols);
string name_cols[ncols];
for (int i = 0; i < ncols; i++) {
string name_cols[cols_count];
expr sum_expr = 0;
for (int i = 0; i < cols_count; i++) {
name_cols[i] = string(cols[i]);
sum_expr += cols[i];
}
@gprod_member_data{name_piop: get_piop_name(name), names: name_cols, opid: opid, proves: proves, selector: sel, references: cols};
@gprod_member_data{name_piop: get_piop_name(name), name_expr: name_expr, opid: opid, proves: proves, selector: sel, expressions: cols, deg_expr: degree(sum_expr), deg_sel: degree(sel)};
}

initial_checks_prod(proves, opid, cols, direct_type);
Expand Down
17 changes: 8 additions & 9 deletions pil2-components/lib/std/pil/std_sum.pil
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function init_proof_containers_sum(int name, int opid[]) {
}
}

private function init_containers_sum(int name, int opid[]) {
private function init_air_containers_sum(int name, int opid[]) {
container air.std.gsum {
int gsum_nargs = 0;
expr gsum_s[100];
Expand Down Expand Up @@ -112,18 +112,17 @@ private function update_piop_sum(int name, int proves, int opid[], expr sumid, e
init_proof_containers_sum(name, opid);

if (direct_type == PIOP_DIRECT_TYPE_AIR || direct_type == PIOP_DIRECT_TYPE_DEFAULT) {
init_containers_sum(name, opid);
}
init_air_containers_sum(name, opid);

if (direct_type == PIOP_DIRECT_TYPE_DEFAULT) {
// Create debug hints for the witness computation
const int ncols = length(cols);
string name_cols[ncols];
for (int i = 0; i < ncols; i++) {
name_cols[i] = string(cols[i]);
string name_expr[cols_count];
expr sum_expr = 0;
for (int i = 0; i < cols_count; i++) {
name_expr[i] = string(cols[i]);
sum_expr += cols[i];
}
// proves = 2 marks that the user is responsible for the use of proves and assumes
@gsum_member_data{name_piop: get_piop_name(name), names: name_cols, sumid: sumid, proves: proves == 2 ? sel : proves, selector: sel, references: cols};
@gsum_member_data{name_piop: get_piop_name(name), name_expr: name_expr, opid: sumid, proves: proves == 2 ? sel : proves, selector: sel, expressions: cols, deg_expr: degree(sum_expr), deg_sel: degree(sel)};
}

initial_checks_sum(proves, opid, cols, direct_type);
Expand Down
2 changes: 1 addition & 1 deletion pil2-components/lib/std/rs/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub fn print_debug_info<F: PrimeField>(name: &str, max_values_to_print: usize, d
instance_id,
rows.len(),
rows_display,
if truncated { ", ..." } else { "" },
if truncated { ",..." } else { "" },
);
}

Expand Down
100 changes: 76 additions & 24 deletions pil2-components/lib/std/rs/src/std_prod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use p3_field::PrimeField;
use proofman::{WitnessComponent, WitnessManager};
use proofman_common::{AirInstance, ExecutionCtx, ModeName, ProofCtx, SetupCtx, StdMode};
use proofman_hints::{
acc_mul_hint_fields, get_hint_field, get_hint_field_a, get_hint_ids_by_name, HintFieldOptions, HintFieldOutput,
HintFieldValue,
acc_mul_hint_fields, get_hint_field, get_hint_field_a, get_hint_field_constant, get_hint_field_constant_a,
get_hint_ids_by_name, HintFieldOptions, HintFieldOutput, HintFieldValue, HintFieldValuesVec,
};

use crate::{print_debug_info, update_debug_data, DebugData, Decider};
Expand Down Expand Up @@ -68,9 +68,29 @@ impl<F: PrimeField> StdProd<F> {
num_rows: usize,
debug_hints_data: Vec<u64>,
) {
let debug_data = self.debug_data.as_ref().expect("Debug data missing");
let airgroup_id = air_instance.airgroup_id;
let air_id = air_instance.air_id;
let instance_id = air_instance.air_instance_id.unwrap_or_default();

for hint in debug_hints_data.iter() {
let _name =
get_hint_field::<F>(sctx, pctx, air_instance, *hint as usize, "name_piop", HintFieldOptions::default());
let _name_piop = get_hint_field_constant::<F>(
sctx,
airgroup_id,
air_id,
*hint as usize,
"name_piop",
HintFieldOptions::default(),
);

let _name_expr = get_hint_field_constant_a::<F>(
sctx,
airgroup_id,
air_id,
*hint as usize,
"name_expr",
HintFieldOptions::default(),
);

let opid =
get_hint_field::<F>(sctx, pctx, air_instance, *hint as usize, "opid", HintFieldOptions::default());
Expand Down Expand Up @@ -107,22 +127,58 @@ impl<F: PrimeField> StdProd<F> {
pctx,
air_instance,
*hint as usize,
"references",
"expressions",
HintFieldOptions::default(),
);

// let _names = get_hint_field::<F>(
// sctx,
// &pctx.public_inputs,
// &pctx.challenges,
// air_instance,
// *hint as usize,
// "names",
// HintFieldOptions::default(),
// );

(0..num_rows).for_each(|j| {
let sel = if let HintFieldOutput::Field(selector) = selector.get(j) {
let HintFieldValue::Field(deg_expr) = get_hint_field_constant::<F>(
sctx,
airgroup_id,
air_id,
*hint as usize,
"deg_expr",
HintFieldOptions::default(),
) else {
log::error!("deg_expr hint must be a field element");
panic!();
};

let HintFieldValue::Field(deg_mul) = get_hint_field_constant::<F>(
sctx,
airgroup_id,
air_id,
*hint as usize,
"deg_sel",
HintFieldOptions::default(),
) else {
log::error!("deg_mul hint must be a field element");
panic!();
};

// If both the expresion and the mul are of degree zero, then simply update the bus once
if deg_expr.is_zero() && deg_mul.is_zero() {
update_bus(airgroup_id, air_id, instance_id, opid, proves, &selector, &expressions, 0, debug_data);
} else {
// Otherwise, update the bus for each row
for j in 0..num_rows {
update_bus(airgroup_id, air_id, instance_id, opid, proves, &selector, &expressions, j, debug_data);
}
}

(0..num_rows).for_each(|j| {});

fn update_bus<F: PrimeField>(
airgroup_id: usize,
air_id: usize,
instance_id: usize,
opid: F,
proves: bool,
selector: &HintFieldValue<F>,
expressions: &HintFieldValuesVec<F>,
row: usize,
debug_data: &DebugData<F>,
) {
let sel = if let HintFieldOutput::Field(selector) = selector.get(row) {
if !selector.is_zero() && !selector.is_one() {
log::error!("Selector must be either 0 or 1");
panic!();
Expand All @@ -134,23 +190,19 @@ impl<F: PrimeField> StdProd<F> {
};

if sel {
let debug_data = self.debug_data.as_ref().expect("Debug data missing");
let airgroup_id = air_instance.airgroup_id;
let air_id = air_instance.air_id;
let instance_id = air_instance.air_instance_id.unwrap_or_default();
update_debug_data(
debug_data,
opid,
expressions.get(j),
expressions.get(row),
airgroup_id,
air_id,
instance_id,
j,
row,
proves,
F::one(),
);
}
});
}
}
}
}
Expand Down
Loading

0 comments on commit 5f1a0ef

Please sign in to comment.