Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into th/pull_219_circom
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hoffman committed Sep 5, 2024
2 parents fd0a9af + b1f795d commit dfd41ac
Show file tree
Hide file tree
Showing 32 changed files with 457 additions and 184 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Release notes
## April 23, 2024 circom 2.1.9

#### Extensions
- Improvement of error messages: providing more descriptive messages.
- Improvement of documentation and new sections: detailed explanation of the applied simplification techniques and precise description of constraint-related output formats.
- Allowing arbitrary number of different input names in the main component.

#### Fixed bugs
- Removing non-determinism in R1CS generation: fixing assignment order of anonymous components inputs.
- Fixing an error when computing array accesses involving complex expressions in multidimensional arrays.
- Improving known/unknown analysis: arrays of vars combining known/unknown positions.
- Fixing minor panics: main component not calling to a template, anonymous component with wrong number of arguments, log messages containing line breaks.


## Jan 17, 2024 circom 2.1.8

#### Extensions
Expand Down
2 changes: 1 addition & 1 deletion circom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "circom"
version = "2.1.8"
version = "2.1.9"
authors = ["Costa Group UCM","iden3"]
edition = "2018"

Expand Down
5 changes: 4 additions & 1 deletion circom/src/parser_user.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use super::input_user::Input;
use program_structure::constants::UsefulConstants;
use program_structure::error_definition::Report;
use program_structure::program_archive::ProgramArchive;
use crate::VERSION;


pub fn parse_project(input_info: &Input) -> Result<ProgramArchive, ()> {
let initial_file = input_info.input_file().to_string();
let result_program_archive = parser::run_parser(initial_file, VERSION, input_info.get_link_libraries().to_vec());
//We get the prime number from the input
let prime = UsefulConstants::new(&input_info.prime()).get_p().clone();
let result_program_archive = parser::run_parser(initial_file, VERSION, input_info.get_link_libraries().to_vec(), &prime);
match result_program_archive {
Result::Err((file_library, report_collection)) => {
Report::print_reports(&report_collection, &file_library);
Expand Down
4 changes: 2 additions & 2 deletions circom_algebra/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ impl<C: Default + Clone + Display + Hash + Eq> ArithmeticExpression<C> {
}

// Bit operations
pub fn complement_256(
pub fn complement_254(
elem: &ArithmeticExpression<C>,
field: &BigInt,
) -> ArithmeticExpression<C> {
use ArithmeticExpression::*;
if let Number { value } = elem {
Number { value: modular_arithmetic::complement_256(value, field) }
Number { value: modular_arithmetic::complement_254(value, field) }
} else {
NonQuadratic
}
Expand Down
15 changes: 8 additions & 7 deletions circom_algebra/src/modular_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ pub fn multi_inv(values: &Vec<BigInt>, field: &BigInt) -> Vec<BigInt>{

//Bit operations

// 256 bit complement
pub fn complement_256(elem: &BigInt, field: &BigInt) -> BigInt {
// 254 bit complement
pub fn complement_254(elem: &BigInt, field: &BigInt) -> BigInt {
let (sign, mut bit_repr) = bit_representation(elem);
while bit_repr.len() > 256 {
let new_sign = if elem == &BigInt::from(0) { Sign::Plus } else { sign};
while bit_repr.len() > 254 {
bit_repr.pop();
}
for _i in bit_repr.len()..256 {
for _i in bit_repr.len()..254 {
bit_repr.push(0);
}
for bit in &mut bit_repr {
*bit = if *bit == 0 { 1 } else { 0 };
}
let cp = BigInt::from_radix_le(sign, &bit_repr, 2).unwrap();
let cp = BigInt::from_radix_le(new_sign, &bit_repr, 2).unwrap();
modulus(&cp, field)
}

Expand Down Expand Up @@ -252,8 +253,8 @@ mod tests {
.expect("generating the big int was not possible");
let big_num = BigInt::parse_bytes("1234".as_bytes(), 10)
.expect("generating the big int was not possible");
let big_num_complement = complement_256(&big_num, &field);
let big_num_complement_complement = complement_256(&big_num_complement, &field);
let big_num_complement = complement_254(&big_num, &field);
let big_num_complement_complement = complement_254(&big_num_complement, &field);
let big_num_modulus = modulus(&big_num, &field);
assert_eq!(big_num_complement_complement, big_num_modulus);
}
Expand Down
2 changes: 1 addition & 1 deletion code_producers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "code_producers"
version = "2.1.8"
version = "2.1.9"
authors = ["Costa Group UCM","iden3"]
edition = "2018"

Expand Down
21 changes: 10 additions & 11 deletions code_producers/src/c_elements/c_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const S_IO_DEF: &str = "IODef";
const IO_DEF_FIELDS: [&str; 2] = ["offset", "lengths"];

// Global variables
pub const SIZE_INPUT_HASHMAP: usize = 256;
const G_INPUT_HASHMAP: &str = "inputHashMap"; // type HashSignalInfo[256]
//pub const SIZE_INPUT_HASHMAP: usize = 256;
const G_INPUT_HASHMAP: &str = "inputHashMap"; // type HashSignalInfo[max(256,needed)]
const G_RM_INPUT_SIGNAL_COUNTER: &str = "remainingInputSignalCounter"; // type u32
const G_INPUT_SIGNAL_SET: &str = "inputSignalSetMap"; // type bool[M]
const G_WITNESS_TO_SIGNAL: &str = "witness2signalList"; // type u64[W]
Expand Down Expand Up @@ -469,15 +469,14 @@ pub fn collect_function_headers(functions: Vec<String>) -> Vec<String> {

//--------------- generate all kinds of Data for the .dat file ---------------

pub fn generate_hash_map(signal_name_list: &Vec<(String, usize, usize)>) -> Vec<(u64, u64, u64)> {
assert!(signal_name_list.len() <= 256);
let len = 256;
let mut hash_map = vec![(0, 0, 0); len];
pub fn generate_hash_map(signal_name_list: &Vec<(String, usize, usize)>, size: usize) -> Vec<(u64, u64, u64)> {
assert!(signal_name_list.len() <= size);
let mut hash_map = vec![(0, 0, 0); size];
for i in 0..signal_name_list.len() {
let h = hasher(&signal_name_list[i].0);
let mut p = (h % 256) as usize;
let mut p = h as usize % size;
while hash_map[p].1 != 0 {
p = (p + 1) % 256;
p = (p + 1) % size;
}
hash_map[p] = (h, signal_name_list[i].1 as u64, signal_name_list[i].2 as u64);
}
Expand Down Expand Up @@ -653,9 +652,9 @@ pub fn generate_dat_file(dat_file: &mut dyn Write, producer: &CProducer) -> std:
//dfile.flush()?;

let aux = producer.get_main_input_list();
let map = generate_hash_map(&aux);
let map = generate_hash_map(&aux,producer.get_input_hash_map_entry_size());
let hashmap = generate_dat_from_hash_map(&map); //bytes u64 --> u64
//let hml = 256 as u32;
//let hml = producer.get_input_hash_map_entry_size() as u32;
//dfile.write_all(&hml.to_be_bytes())?;
dat_file.write_all(&hashmap)?;
//dat_file.flush()?;
Expand Down Expand Up @@ -946,7 +945,7 @@ pub fn generate_c_file(name: String, producer: &CProducer) -> std::io::Result<()
let full_name = name + ".cpp";
let mut cfile = File::create(full_name)?;
let mut code = vec![];
let len = 256;
let len = producer.get_input_hash_map_entry_size();
code.push("#include <stdio.h>".to_string());
code.push("#include <iostream>".to_string());
code.push("#include <assert.h>".to_string());
Expand Down
6 changes: 3 additions & 3 deletions code_producers/src/c_elements/common/calcwit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ uint Circom_CalcWit::getInputSignalHashPosition(u64 h) {
uint pos = (uint)(h % (u64)n);
if (circuit->InputHashMap[pos].hash!=h){
uint inipos = pos;
pos++;
pos = (pos+1)%n;
while (pos != inipos) {
if (circuit->InputHashMap[pos].hash==h) return pos;
if (circuit->InputHashMap[pos].hash==0) {
if (circuit->InputHashMap[pos].hash == h) return pos;
if (circuit->InputHashMap[pos].signalid == 0) {
fprintf(stderr, "Signal not found\n");
assert(false);
}
Expand Down
3 changes: 3 additions & 0 deletions code_producers/src/c_elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ impl CProducer {
pub fn get_main_input_list(&self) -> &InputList {
&self.main_input_list
}
pub fn get_input_hash_map_entry_size(&self) -> usize {
std::cmp::max(usize::pow(2,(self.main_input_list.len() as f32).log2().ceil() as u32),256)
}
pub fn get_number_of_witness(&self) -> usize {
self.signals_in_witness
}
Expand Down
5 changes: 4 additions & 1 deletion code_producers/src/wasm_elements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ impl WASMProducer {
pub fn get_main_input_list(&self) -> &InputList {
&self.main_input_list
}
pub fn get_input_hash_map_entry_size(&self) -> usize {
std::cmp::max(usize::pow(2,(self.main_input_list.len() as f32).log2().ceil() as u32),256)
}
pub fn get_number_of_witness(&self) -> usize {
self.signals_in_witness
}
Expand Down Expand Up @@ -262,7 +265,7 @@ impl WASMProducer {
(4 * self.size_32_bit) + 8 + self.get_shared_rw_memory_start()
}
pub fn get_remaining_input_signal_counter(&self) -> usize {
self.get_input_signals_hashmap_start() + 4096 // 256*(8(h)+4(pos)+4(size))
self.get_input_signals_hashmap_start() + self.get_input_hash_map_entry_size()*16 // input_hash_map_entry_size*(8(h)+4(pos)+4(size))
}
pub fn get_input_signal_set_map_start(&self) -> usize {
self.get_remaining_input_signal_counter() + 4
Expand Down
18 changes: 9 additions & 9 deletions code_producers/src/wasm_elements/wasm_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,14 @@ pub fn get_initial_size_of_memory(producer: &WASMProducer) -> usize {

//------------------- generate all kinds of Data ------------------

pub fn generate_hash_map(signal_name_list: &Vec<(String, usize, usize)>) -> Vec<(u64, usize, usize)> {
assert!(signal_name_list.len() <= 256);
let len = 256;
let mut hash_map = vec![(0, 0, 0); len];
pub fn generate_hash_map(signal_name_list: &Vec<(String, usize, usize)>, size: usize) -> Vec<(u64, usize, usize)> {
assert!(signal_name_list.len() <= size);
let mut hash_map = vec![(0, 0, 0); size];
for i in 0..signal_name_list.len() {
let h = hasher(&signal_name_list[i].0);
let mut p = (h % 256) as usize;
let mut p = h as usize % size;
while hash_map[p].1 != 0 {
p = (p + 1) % 256;
p = (p + 1) % size;
}
hash_map[p] = (h, signal_name_list[i].1, signal_name_list[i].2);
}
Expand Down Expand Up @@ -567,7 +566,7 @@ pub fn generate_data_list(producer: &WASMProducer) -> Vec<WasmInstruction> {
producer.get_shared_rw_memory_start() - 8,
"\\00\\00\\00\\00\\00\\00\\00\\80"
));
let map = generate_hash_map(&producer.get_main_input_list());
let map = generate_hash_map(&producer.get_main_input_list(),producer.get_input_hash_map_entry_size());
wdata.push(format!(
"(data (i32.const {}) \"{}\")",
producer.get_input_signals_hashmap_start(),
Expand Down Expand Up @@ -852,6 +851,7 @@ pub fn init_generator(producer: &WASMProducer) -> Vec<WasmInstruction> {
pub fn get_input_signal_map_position_generator(producer: &WASMProducer) -> Vec<WasmInstruction> {
let mut instructions = vec![];
let header = "(func $getInputSignalMapPosition (type $_t_i64ri32)".to_string();
let sizeones = producer.get_input_hash_map_entry_size()-1;
instructions.push(header);
instructions.push(" (param $hn i64)".to_string());
instructions.push("(result i32)".to_string());
Expand All @@ -860,7 +860,7 @@ pub fn get_input_signal_map_position_generator(producer: &WASMProducer) -> Vec<W
instructions.push(" (local $aux i32)".to_string());
instructions.push(get_local("$hn"));
instructions.push(wrap_i6432());
instructions.push(set_constant("255"));
instructions.push(set_constant(&sizeones.to_string()));
instructions.push(and32());
instructions.push(set_local("$ini"));
instructions.push(get_local("$ini"));
Expand Down Expand Up @@ -891,7 +891,7 @@ pub fn get_input_signal_map_position_generator(producer: &WASMProducer) -> Vec<W
instructions.push(get_local("$i"));
instructions.push(set_constant("1"));
instructions.push(add32());
instructions.push(set_constant("255"));
instructions.push(set_constant(&sizeones.to_string()));
instructions.push(and32());
instructions.push(set_local("$i"));
instructions.push(get_local("$i"));
Expand Down
2 changes: 1 addition & 1 deletion compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compiler"
version = "2.1.8"
version = "2.1.9"
authors = ["Costa Group UCM","iden3"]
edition = "2018"

Expand Down
6 changes: 4 additions & 2 deletions compiler/src/circuit_design/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ impl WriteC for Circuit {
"uint get_number_of_components() {{return {};}}\n",
producer.get_number_of_components()
));
code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", SIZE_INPUT_HASHMAP));
//code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", SIZE_INPUT_HASHMAP));
code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", producer.get_input_hash_map_entry_size()));
code.push(format!(
"uint get_size_of_witness() {{return {};}}\n",
producer.get_witness_to_signal_list().len()
Expand Down Expand Up @@ -653,7 +654,8 @@ impl WriteC for Circuit {
"uint get_number_of_components() {{return {};}}\n",
producer.get_number_of_components()
));
code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", SIZE_INPUT_HASHMAP));
//code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", SIZE_INPUT_HASHMAP));
code.push(format!("uint get_size_of_input_hashmap() {{return {};}}\n", producer.get_input_hash_map_entry_size()));
code.push(format!(
"uint get_size_of_witness() {{return {};}}\n",
producer.get_witness_to_signal_list().len()
Expand Down
Loading

0 comments on commit dfd41ac

Please sign in to comment.