2020-07-30 10:15:14 +03:00
|
|
|
pub mod address;
|
2020-07-30 10:56:17 +03:00
|
|
|
pub mod array;
|
2020-07-30 06:52:37 +03:00
|
|
|
pub mod boolean;
|
2020-07-30 21:11:54 +03:00
|
|
|
pub mod circuits;
|
2020-07-30 09:32:21 +03:00
|
|
|
pub mod field;
|
2020-07-30 22:10:33 +03:00
|
|
|
pub mod function;
|
2020-07-30 22:54:34 +03:00
|
|
|
pub mod group;
|
2020-07-30 23:01:04 +03:00
|
|
|
pub mod import;
|
2020-07-31 00:38:31 +03:00
|
|
|
pub mod inputs;
|
2020-07-31 01:41:03 +03:00
|
|
|
pub mod integers;
|
2020-07-31 02:27:31 +03:00
|
|
|
pub mod macros;
|
2020-07-31 02:37:01 +03:00
|
|
|
pub mod mutability;
|
2020-07-31 03:11:58 +03:00
|
|
|
pub mod statements;
|
2020-07-31 03:19:10 +03:00
|
|
|
pub mod syntax;
|
2020-05-19 22:01:19 +03:00
|
|
|
|
2020-06-02 04:35:43 +03:00
|
|
|
use leo_compiler::{
|
2020-06-03 02:06:25 +03:00
|
|
|
compiler::Compiler,
|
2020-07-30 09:32:21 +03:00
|
|
|
errors::CompilerError,
|
2020-07-08 05:53:37 +03:00
|
|
|
group::targets::edwards_bls12::EdwardsGroupType,
|
2020-06-02 04:35:43 +03:00
|
|
|
ConstrainedValue,
|
2020-07-30 06:52:37 +03:00
|
|
|
OutputBytes,
|
2020-06-02 04:35:43 +03:00
|
|
|
};
|
2020-07-30 04:32:35 +03:00
|
|
|
use leo_types::{InputValue, MainInputs};
|
2020-05-19 22:01:19 +03:00
|
|
|
|
2020-06-02 03:52:33 +03:00
|
|
|
use snarkos_curves::edwards_bls12::Fq;
|
2020-07-30 09:32:21 +03:00
|
|
|
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
|
2020-07-30 06:52:37 +03:00
|
|
|
|
2020-07-30 09:32:21 +03:00
|
|
|
use std::path::PathBuf;
|
2020-07-30 06:52:37 +03:00
|
|
|
|
|
|
|
pub const TEST_OUTPUTS_DIRECTORY: &str = "/outputs/";
|
|
|
|
pub const TEST_OUTPUTS_FILE_NAME: &str = "/outputs/test.out";
|
2020-07-30 04:32:35 +03:00
|
|
|
const EMPTY_FILE: &str = "";
|
|
|
|
|
2020-06-20 09:02:58 +03:00
|
|
|
pub type EdwardsTestCompiler = Compiler<Fq, EdwardsGroupType>;
|
2020-06-02 03:52:33 +03:00
|
|
|
pub type EdwardsConstrainedValue = ConstrainedValue<Fq, EdwardsGroupType>;
|
2020-05-30 03:34:31 +03:00
|
|
|
|
2020-07-30 04:32:35 +03:00
|
|
|
pub fn parse_program_with_inputs(
|
|
|
|
program_bytes: &[u8],
|
|
|
|
input_bytes: &[u8],
|
|
|
|
) -> Result<EdwardsTestCompiler, CompilerError> {
|
|
|
|
let mut compiler = new_compiler();
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-07-30 04:32:35 +03:00
|
|
|
let program_string = String::from_utf8_lossy(program_bytes);
|
|
|
|
let inputs_string = String::from_utf8_lossy(input_bytes);
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-07-30 04:32:35 +03:00
|
|
|
compiler.parse_inputs(&inputs_string, EMPTY_FILE)?;
|
|
|
|
compiler.parse_program(&program_string)?;
|
|
|
|
|
|
|
|
Ok(compiler)
|
2020-06-09 04:39:10 +03:00
|
|
|
}
|
2020-05-19 22:01:19 +03:00
|
|
|
|
2020-06-21 01:24:46 +03:00
|
|
|
fn new_compiler() -> EdwardsTestCompiler {
|
|
|
|
let program_name = "test".to_string();
|
2020-06-23 08:32:57 +03:00
|
|
|
let path = PathBuf::from("/test/src/main.leo");
|
2020-07-30 09:32:21 +03:00
|
|
|
let outputs_dir = PathBuf::from(TEST_OUTPUTS_DIRECTORY);
|
2020-06-21 01:24:46 +03:00
|
|
|
|
2020-07-30 09:32:21 +03:00
|
|
|
EdwardsTestCompiler::new(program_name, path, outputs_dir)
|
2020-06-21 01:24:46 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 03:28:09 +03:00
|
|
|
pub(crate) fn parse_program(bytes: &[u8]) -> Result<EdwardsTestCompiler, CompilerError> {
|
2020-06-21 01:24:46 +03:00
|
|
|
let mut compiler = new_compiler();
|
2020-06-09 03:28:09 +03:00
|
|
|
let program_string = String::from_utf8_lossy(bytes);
|
2020-05-19 22:01:19 +03:00
|
|
|
|
2020-06-09 03:28:09 +03:00
|
|
|
compiler.parse_program(&program_string)?;
|
2020-05-19 22:01:19 +03:00
|
|
|
|
2020-06-09 03:28:09 +03:00
|
|
|
Ok(compiler)
|
2020-05-19 22:01:19 +03:00
|
|
|
}
|
2020-06-11 21:43:05 +03:00
|
|
|
|
2020-07-31 03:19:10 +03:00
|
|
|
pub(crate) fn parse_inputs(bytes: &[u8]) -> Result<EdwardsTestCompiler, CompilerError> {
|
|
|
|
let mut compiler = new_compiler();
|
|
|
|
let inputs_string = String::from_utf8_lossy(bytes);
|
|
|
|
|
|
|
|
compiler.parse_inputs(&inputs_string, "")?;
|
|
|
|
|
|
|
|
Ok(compiler)
|
|
|
|
}
|
2020-07-30 04:32:35 +03:00
|
|
|
|
2020-07-30 06:52:37 +03:00
|
|
|
pub(crate) fn get_outputs(program: EdwardsTestCompiler) -> OutputBytes {
|
|
|
|
// synthesize the circuit on the test constraint system
|
2020-07-30 04:32:35 +03:00
|
|
|
let mut cs = TestConstraintSystem::<Fq>::new();
|
2020-07-30 06:52:37 +03:00
|
|
|
let output = program.generate_constraints_helper(&mut cs).unwrap();
|
|
|
|
|
|
|
|
// assert the constraint system is satisfied
|
2020-07-30 04:32:35 +03:00
|
|
|
assert!(cs.is_satisfied());
|
2020-07-30 06:52:37 +03:00
|
|
|
|
|
|
|
output
|
2020-07-30 04:32:35 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 06:52:37 +03:00
|
|
|
pub(crate) fn assert_satisfied(program: EdwardsTestCompiler) {
|
|
|
|
let empty_output_bytes = include_bytes!("compiler_outputs/empty.out");
|
|
|
|
let res = get_outputs(program);
|
|
|
|
|
|
|
|
// assert that the output is empty
|
|
|
|
assert_eq!(empty_output_bytes, res.bytes().as_slice());
|
|
|
|
}
|
|
|
|
|
2020-07-30 21:11:54 +03:00
|
|
|
pub(crate) fn expect_compiler_error(program: EdwardsTestCompiler) -> CompilerError {
|
2020-07-30 04:32:35 +03:00
|
|
|
let mut cs = TestConstraintSystem::<Fq>::new();
|
2020-07-30 06:52:37 +03:00
|
|
|
program.generate_constraints_helper(&mut cs).unwrap_err()
|
2020-07-30 04:32:35 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:11:54 +03:00
|
|
|
pub(crate) fn expect_synthesis_error(program: EdwardsTestCompiler) {
|
2020-07-30 04:32:35 +03:00
|
|
|
let mut cs = TestConstraintSystem::<Fq>::new();
|
2020-07-30 06:52:37 +03:00
|
|
|
let _output = program.generate_constraints_helper(&mut cs).unwrap();
|
|
|
|
|
|
|
|
assert!(!cs.is_satisfied());
|
2020-07-30 04:32:35 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:32:21 +03:00
|
|
|
// pub(crate) fn fail_enforce(program: EdwardsTestCompiler) {
|
|
|
|
// match get_compiler_error(program) {
|
|
|
|
// CompilerError::FunctionError(FunctionError::StatementError(StatementError::Error(_))) => {}
|
|
|
|
// error => panic!("Expected evaluate error, got {}", error),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
pub(crate) fn generate_main_inputs(inputs: Vec<(&str, Option<InputValue>)>) -> MainInputs {
|
|
|
|
let mut main_inputs = MainInputs::new();
|
|
|
|
|
|
|
|
for (name, value) in inputs {
|
|
|
|
main_inputs.insert(name.to_string(), value);
|
2020-07-30 04:32:35 +03:00
|
|
|
}
|
2020-07-30 09:32:21 +03:00
|
|
|
|
|
|
|
main_inputs
|
2020-07-30 04:32:35 +03:00
|
|
|
}
|