From 5484b947230d3ccb73b34930cb44be9a4105af3e Mon Sep 17 00:00:00 2001 From: Nikolay Date: Thu, 30 Nov 2023 10:18:01 +0800 Subject: [PATCH] field_size correction --- constraint_list/src/r1cs_porting.rs | 56 ++++++++++++++++++++++------- dag/src/r1cs_porting.rs | 30 ++++++++++------ 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/constraint_list/src/r1cs_porting.rs b/constraint_list/src/r1cs_porting.rs index 7f012dda6..ed769b69b 100644 --- a/constraint_list/src/r1cs_porting.rs +++ b/constraint_list/src/r1cs_porting.rs @@ -1,13 +1,20 @@ use super::{ConstraintList, C, EncodingIterator, SignalMap}; -use constraint_writers::r1cs_writer::{ConstraintSection, CustomGatesAppliedData, HeaderData, R1CSWriter, SignalSection}; +use constraint_writers::r1cs_writer::{ + ConstraintSection, CustomGatesAppliedData, HeaderData, R1CSWriter, SignalSection, +}; +use crate::BigInt; + +pub fn field_size(field: &BigInt) -> usize { + // 254 => 32, // bn128, grumpkin + // 255 => 32, // bls12381, pallas, vesta + // 64 => 8, // goldilocks + // 256 => 32, // secq256k1 + field.bits().next_power_of_two() / 8 +} pub fn port_r1cs(list: &ConstraintList, output: &str, custom_gates: bool) -> Result<(), ()> { use constraint_writers::log_writer::Log; - let field_size = if list.field.bits() % 64 == 0 { - list.field.bits() / 8 - } else{ - (list.field.bits() / 64 + 1) * 8 - }; + let field_size = field_size(&list.field); let mut log = Log::new(); log.no_labels = ConstraintList::no_labels(list); log.no_wires = ConstraintList::no_wires(list); @@ -51,7 +58,7 @@ pub fn port_r1cs(list: &ConstraintList, output: &str, custom_gates: bool) -> Res } let r1cs = signal_section.end_section()?; if !custom_gates { - R1CSWriter::finish_writing(r1cs)?; + R1CSWriter::finish_writing(r1cs)?; } else { let mut custom_gates_used_section = R1CSWriter::start_custom_gates_used_section(r1cs)?; let (usage_data, occurring_order) = { @@ -61,7 +68,7 @@ pub fn port_r1cs(list: &ConstraintList, output: &str, custom_gates: bool) -> Res if node.is_custom_gate { let mut name = node.name.clone(); occurring_order.push(name.clone()); - while name.pop() != Some('(') {}; + while name.pop() != Some('(') {} usage_data.push((name, node.parameters.clone())); } } @@ -70,11 +77,12 @@ pub fn port_r1cs(list: &ConstraintList, output: &str, custom_gates: bool) -> Res custom_gates_used_section.write_custom_gates_usages(usage_data)?; let r1cs = custom_gates_used_section.end_section()?; - let mut custom_gates_applied_section = R1CSWriter::start_custom_gates_applied_section(r1cs)?; + let mut custom_gates_applied_section = + R1CSWriter::start_custom_gates_applied_section(r1cs)?; let application_data = { fn find_indexes( occurring_order: Vec, - application_data: Vec<(String, Vec)> + application_data: Vec<(String, Vec)>, ) -> CustomGatesAppliedData { let mut new_application_data = vec![]; for (custom_gate_name, signals) in application_data { @@ -90,7 +98,7 @@ pub fn port_r1cs(list: &ConstraintList, output: &str, custom_gates: bool) -> Res fn iterate( iterator: EncodingIterator, map: &SignalMap, - application_data: &mut Vec<(String, Vec)> + application_data: &mut Vec<(String, Vec)>, ) { let node = &iterator.encoding.nodes[iterator.node_id]; if node.is_custom_gate { @@ -116,8 +124,32 @@ pub fn port_r1cs(list: &ConstraintList, output: &str, custom_gates: bool) -> Res }; custom_gates_applied_section.write_custom_gates_applications(application_data)?; let r1cs = custom_gates_applied_section.end_section()?; - R1CSWriter::finish_writing(r1cs)?; + R1CSWriter::finish_writing(r1cs)?; } Log::print(&log); Ok(()) } + +#[cfg(test)] +mod tests { + use program_structure::constants::UsefulConstants; + use super::*; + + fn field_size_old(field: &BigInt) -> usize { + let field_size = + if field.bits() % 64 == 0 { field.bits() / 8 } else { (field.bits() / 64 + 1) * 8 }; + field_size + } + + #[test] + fn test_field_size() { + for possible_prime in + ["bn128", "bls12381", "goldilocks", "grumpkin", "pallas", "vesta", "secq256k1"] + { + let constant = UsefulConstants::new(&possible_prime.to_string()); + let field_size = field_size(&constant.get_p()); + let field_size_old = field_size_old(&constant.get_p()); + assert_eq!(field_size, field_size_old, "{}", possible_prime); + } + } +} diff --git a/dag/src/r1cs_porting.rs b/dag/src/r1cs_porting.rs index 27eae18bd..866b5f528 100644 --- a/dag/src/r1cs_porting.rs +++ b/dag/src/r1cs_porting.rs @@ -1,14 +1,21 @@ use super::{Constraint, Tree, DAG}; use constraint_writers::log_writer::Log; -use constraint_writers::r1cs_writer::{ConstraintSection, CustomGatesAppliedData, HeaderData, R1CSWriter}; +use constraint_writers::r1cs_writer::{ + ConstraintSection, CustomGatesAppliedData, HeaderData, R1CSWriter, +}; +use crate::BigInt; + +pub fn field_size(field: &BigInt) -> usize { + // 254 => 32, // bn128, grumpkin + // 255 => 32, // bls12381, pallas, vesta + // 64 => 8, // goldilocks + // 256 => 32, // secq256k1 + field.bits().next_power_of_two() / 8 +} pub fn write(dag: &DAG, output: &str, custom_gates: bool) -> Result<(), ()> { let tree = Tree::new(dag); - let field_size = if tree.field.bits() % 64 == 0 { - tree.field.bits() / 8 - } else{ - (tree.field.bits() / 64 + 1) * 8 - }; + let field_size = field_size(&tree.field); let mut log = Log::new(); let r1cs = R1CSWriter::new(output.to_string(), field_size, custom_gates)?; @@ -43,9 +50,9 @@ pub fn write(dag: &DAG, output: &str, custom_gates: bool) -> Result<(), ()> { signal_section.write_signal_usize(signal)?; } let r1cs = signal_section.end_section()?; - + if !custom_gates { - R1CSWriter::finish_writing(r1cs)?; + R1CSWriter::finish_writing(r1cs)?; } else { let mut custom_gates_used_section = R1CSWriter::start_custom_gates_used_section(r1cs)?; let (usage_data, occurring_order) = { @@ -55,7 +62,7 @@ pub fn write(dag: &DAG, output: &str, custom_gates: bool) -> Result<(), ()> { if node.is_custom_gate() { let mut name = node.template_name.clone(); occurring_order.push(name.clone()); - while name.pop() != Some('(') {}; + while name.pop() != Some('(') {} usage_data.push((name, node.parameters().clone())); } } @@ -64,11 +71,12 @@ pub fn write(dag: &DAG, output: &str, custom_gates: bool) -> Result<(), ()> { custom_gates_used_section.write_custom_gates_usages(usage_data)?; let r1cs = custom_gates_used_section.end_section()?; - let mut custom_gates_applied_section = R1CSWriter::start_custom_gates_applied_section(r1cs)?; + let mut custom_gates_applied_section = + R1CSWriter::start_custom_gates_applied_section(r1cs)?; let application_data = { fn find_indexes( occurring_order: Vec, - application_data: Vec<(String, Vec)> + application_data: Vec<(String, Vec)>, ) -> CustomGatesAppliedData { let mut new_application_data = vec![]; for (custom_gate_name, signals) in application_data {