2020-05-20 03:08:38 +03:00
|
|
|
use crate::{compile_program, get_error, get_output};
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
use leo_compiler::{compiler::Compiler, types::Integer, ConstrainedValue, InputValue};
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
use leo_compiler::errors::{CompilerError, FunctionError, IntegerError};
|
2020-05-20 01:45:40 +03:00
|
|
|
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
|
|
|
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
const DIRECTORY_NAME: &str = "tests/integer/u32/";
|
2020-05-20 01:45:40 +03:00
|
|
|
|
2020-05-21 03:08:32 +03:00
|
|
|
pub(crate) fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
|
2020-05-20 01:45:40 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
|
|
|
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
|
|
|
Integer::U32(UInt32::constant(0u32))
|
|
|
|
)]),
|
|
|
|
output
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
pub(crate) fn output_one(program: Compiler<Fr, EdwardsProjective>) {
|
2020-05-20 01:45:40 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
|
|
|
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
|
|
|
Integer::U32(UInt32::constant(1u32))
|
|
|
|
)]),
|
|
|
|
output
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output_two(program: Compiler<Fr, EdwardsProjective>) {
|
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
|
|
|
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
|
|
|
Integer::U32(UInt32::constant(2u32))
|
|
|
|
)]),
|
|
|
|
output
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-20 03:08:38 +03:00
|
|
|
fn fail_integer(program: Compiler<Fr, EdwardsProjective>) {
|
|
|
|
match get_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
|
|
|
IntegerError::InvalidInteger(_string),
|
|
|
|
)) => {}
|
|
|
|
error => panic!("Expected invalid boolean error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
|
|
|
|
match get_error(program) {
|
|
|
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
|
|
|
IntegerError::SynthesisError(_string),
|
|
|
|
)) => {}
|
|
|
|
error => panic!("Expected synthesis error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_input_u32_bool() {
|
|
|
|
let mut program = compile_program(DIRECTORY_NAME, "input_u32.leo").unwrap();
|
|
|
|
program.set_inputs(vec![Some(InputValue::Boolean(true))]);
|
|
|
|
fail_integer(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_input_u32_none() {
|
|
|
|
let mut program = compile_program(DIRECTORY_NAME, "input_u32.leo").unwrap();
|
|
|
|
program.set_inputs(vec![None]);
|
|
|
|
fail_synthesis(program);
|
|
|
|
}
|
|
|
|
|
2020-05-20 01:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
|
|
|
|
output_zero(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_one() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "one.leo").unwrap();
|
|
|
|
output_one(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1_plus_1() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "1+1.leo").unwrap();
|
|
|
|
output_two(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1_minus_1() {
|
|
|
|
let program = compile_program(DIRECTORY_NAME, "1-1.leo").unwrap();
|
|
|
|
output_zero(program)
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[test] // Todo: Catch subtraction overflow error in gadget
|
|
|
|
// fn test_1_minus_2() {
|
|
|
|
// let program = compile_program(DIRECTORY_NAME, "1-2.leo").unwrap();
|
|
|
|
// let error = get_error(program);
|
|
|
|
// println!("{}", error);
|
|
|
|
// }
|