#![forbid(unsafe_code)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]
pub mod circuit;
pub mod native_types;
pub use acir_field;
pub use acir_field::FieldElement;
pub use brillig;
pub use circuit::black_box_functions::BlackBoxFunc;
#[cfg(test)]
mod reflection {
use std::{
fs::File,
io::Write,
path::{Path, PathBuf},
};
use brillig::{
BinaryFieldOp, BinaryIntOp, BlackBoxOp, Opcode as BrilligOpcode, RegisterOrMemory,
};
use serde_reflection::{Tracer, TracerConfig};
use crate::{
circuit::{
brillig::{BrilligInputs, BrilligOutputs},
directives::Directive,
opcodes::BlackBoxFuncCall,
Circuit, Opcode, OpcodeLocation,
},
native_types::{Witness, WitnessMap},
};
#[test]
fn serde_acir_cpp_codegen() {
let path = PathBuf::from("./codegen/acir.cpp");
let old_hash = if path.is_file() {
let old_source = std::fs::read(&path).unwrap();
Some(fxhash::hash64(&old_source))
} else {
None
};
let mut tracer = Tracer::new(TracerConfig::default());
tracer.trace_simple_type::<Circuit>().unwrap();
tracer.trace_simple_type::<Opcode>().unwrap();
tracer.trace_simple_type::<OpcodeLocation>().unwrap();
tracer.trace_simple_type::<BinaryFieldOp>().unwrap();
tracer.trace_simple_type::<BlackBoxFuncCall>().unwrap();
tracer.trace_simple_type::<BrilligInputs>().unwrap();
tracer.trace_simple_type::<BrilligOutputs>().unwrap();
tracer.trace_simple_type::<BrilligOpcode>().unwrap();
tracer.trace_simple_type::<BinaryIntOp>().unwrap();
tracer.trace_simple_type::<BlackBoxOp>().unwrap();
tracer.trace_simple_type::<Directive>().unwrap();
tracer.trace_simple_type::<RegisterOrMemory>().unwrap();
let registry = tracer.registry().unwrap();
let mut source = Vec::new();
let config = serde_generate::CodeGeneratorConfig::new("Circuit".to_string())
.with_encodings(vec![serde_generate::Encoding::Bincode]);
let generator = serde_generate::cpp::CodeGenerator::new(&config);
generator.output(&mut source, ®istry).unwrap();
if let Some(old_hash) = old_hash {
let new_hash = fxhash::hash64(&source);
assert_eq!(new_hash, old_hash, "Serialization format has changed");
}
write_to_file(&source, &path);
}
#[test]
fn serde_witness_map_cpp_codegen() {
let path = PathBuf::from("./codegen/witness.cpp");
let old_hash = if path.is_file() {
let old_source = std::fs::read(&path).unwrap();
Some(fxhash::hash64(&old_source))
} else {
None
};
let mut tracer = Tracer::new(TracerConfig::default());
tracer.trace_simple_type::<Witness>().unwrap();
tracer.trace_simple_type::<WitnessMap>().unwrap();
let registry = tracer.registry().unwrap();
let mut source = Vec::new();
let config = serde_generate::CodeGeneratorConfig::new("WitnessMap".to_string())
.with_encodings(vec![serde_generate::Encoding::Bincode]);
let generator = serde_generate::cpp::CodeGenerator::new(&config);
generator.output(&mut source, ®istry).unwrap();
if let Some(old_hash) = old_hash {
let new_hash = fxhash::hash64(&source);
assert_eq!(new_hash, old_hash, "Serialization format has changed");
}
write_to_file(&source, &path);
}
fn write_to_file(bytes: &[u8], path: &Path) -> String {
let display = path.display();
let parent_dir = path.parent().unwrap();
if !parent_dir.is_dir() {
std::fs::create_dir_all(parent_dir).unwrap();
}
let mut file = match File::create(path) {
Err(why) => panic!("couldn't create {display}: {why}"),
Ok(file) => file,
};
match file.write_all(bytes) {
Err(why) => panic!("couldn't write to {display}: {why}"),
Ok(_) => display.to_string(),
}
}
}