Skip to content

Commit

Permalink
field_size correction
Browse files Browse the repository at this point in the history
  • Loading branch information
NikZak committed Nov 30, 2023
1 parent c01343c commit 5484b94
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
56 changes: 44 additions & 12 deletions constraint_list/src/r1cs_porting.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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) = {
Expand All @@ -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()));
}
}
Expand All @@ -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<String>,
application_data: Vec<(String, Vec<usize>)>
application_data: Vec<(String, Vec<usize>)>,
) -> CustomGatesAppliedData {
let mut new_application_data = vec![];
for (custom_gate_name, signals) in application_data {
Expand All @@ -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<usize>)>
application_data: &mut Vec<(String, Vec<usize>)>,
) {
let node = &iterator.encoding.nodes[iterator.node_id];
if node.is_custom_gate {
Expand All @@ -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);
}
}
}
30 changes: 19 additions & 11 deletions dag/src/r1cs_porting.rs
Original file line number Diff line number Diff line change
@@ -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)?;

Expand Down Expand Up @@ -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) = {
Expand All @@ -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()));
}
}
Expand All @@ -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<String>,
application_data: Vec<(String, Vec<usize>)>
application_data: Vec<(String, Vec<usize>)>,
) -> CustomGatesAppliedData {
let mut new_application_data = vec![];
for (custom_gate_name, signals) in application_data {
Expand Down

0 comments on commit 5484b94

Please sign in to comment.