include_bytes field tests

This commit is contained in:
collin 2020-06-08 18:27:11 -07:00
parent c0682de33d
commit b3ec78c6fd
2 changed files with 62 additions and 26 deletions

View File

@ -1,8 +1,8 @@
use crate::{ use crate::{
boolean::{output_expected_boolean, output_false, output_true}, boolean::{output_expected_boolean, output_false, output_true},
compile_program,
get_error, get_error,
get_output, get_output,
parse_program,
EdwardsConstrainedValue, EdwardsConstrainedValue,
EdwardsTestCompiler, EdwardsTestCompiler,
}; };
@ -24,8 +24,6 @@ use snarkos_models::{
}; };
use snarkos_utilities::biginteger::BigInteger256; use snarkos_utilities::biginteger::BigInteger256;
const DIRECTORY_NAME: &str = "tests/field/";
fn output_expected_constant(program: EdwardsTestCompiler, expected: Fq) { fn output_expected_constant(program: EdwardsTestCompiler, expected: Fq) {
let output = get_output(program); let output = get_output(program);
assert_eq!( assert_eq!(
@ -70,19 +68,25 @@ fn fail_synthesis(program: EdwardsTestCompiler) {
#[test] #[test]
fn test_zero() { fn test_zero() {
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap(); let bytes = include_bytes!("zero.leo");
let program = parse_program(bytes).unwrap();
output_zero(program); output_zero(program);
} }
#[test] #[test]
fn test_one() { fn test_one() {
let program = compile_program(DIRECTORY_NAME, "one.leo").unwrap(); let bytes = include_bytes!("one.leo");
let program = parse_program(bytes).unwrap();
output_one(program); output_one(program);
} }
#[test] #[test]
fn test_input_pass() { fn test_input_pass() {
let mut program = compile_program(DIRECTORY_NAME, "input.leo").unwrap(); let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![Some(InputValue::Field("1".into()))]); program.set_inputs(vec![Some(InputValue::Field("1".into()))]);
let cs = TestConstraintSystem::<Fq>::new(); let cs = TestConstraintSystem::<Fq>::new();
@ -93,14 +97,18 @@ fn test_input_pass() {
#[test] #[test]
fn test_input_fail_bool() { fn test_input_fail_bool() {
let mut program = compile_program(DIRECTORY_NAME, "input.leo").unwrap(); let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![Some(InputValue::Boolean(true))]); program.set_inputs(vec![Some(InputValue::Boolean(true))]);
fail_field(program); fail_field(program);
} }
#[test] #[test]
fn test_input_fail_none() { fn test_input_fail_none() {
let mut program = compile_program(DIRECTORY_NAME, "input.leo").unwrap(); let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![None]); program.set_inputs(vec![None]);
fail_synthesis(program); fail_synthesis(program);
} }
@ -124,7 +132,9 @@ fn test_add() {
let cs = TestConstraintSystem::<Fq>::new(); let cs = TestConstraintSystem::<Fq>::new();
let sum_allocated = FqGadget::from(cs, &sum); let sum_allocated = FqGadget::from(cs, &sum);
let mut program = compile_program(DIRECTORY_NAME, "add.leo").unwrap(); let bytes = include_bytes!("add.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -153,7 +163,9 @@ fn test_sub() {
let cs = TestConstraintSystem::<Fq>::new(); let cs = TestConstraintSystem::<Fq>::new();
let difference_allocated = FqGadget::from(cs, &difference); let difference_allocated = FqGadget::from(cs, &difference);
let mut program = compile_program(DIRECTORY_NAME, "sub.leo").unwrap(); let bytes = include_bytes!("sub.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -182,7 +194,9 @@ fn test_mul() {
let cs = TestConstraintSystem::<Fq>::new(); let cs = TestConstraintSystem::<Fq>::new();
let product_allocated = FqGadget::from(cs, &product); let product_allocated = FqGadget::from(cs, &product);
let mut program = compile_program(DIRECTORY_NAME, "mul.leo").unwrap(); let bytes = include_bytes!("mul.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -211,7 +225,9 @@ fn test_div() {
let cs = TestConstraintSystem::<Fq>::new(); let cs = TestConstraintSystem::<Fq>::new();
let quotient_allocated = FqGadget::from(cs, &quotient); let quotient_allocated = FqGadget::from(cs, &quotient);
let mut program = compile_program(DIRECTORY_NAME, "div.leo").unwrap(); let bytes = include_bytes!("div.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -227,7 +243,9 @@ fn test_eq() {
let r1: u64 = rand::random(); let r1: u64 = rand::random();
// test equal // test equal
let mut program = compile_program(DIRECTORY_NAME, "eq.leo").unwrap(); let bytes = include_bytes!("eq.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
@ -240,7 +258,8 @@ fn test_eq() {
let result = r1.eq(&r2); let result = r1.eq(&r2);
let mut program = compile_program(DIRECTORY_NAME, "eq.leo").unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -256,7 +275,9 @@ fn test_ge() {
let r1: u64 = rand::random(); let r1: u64 = rand::random();
// test equal // test equal
let mut program = compile_program(DIRECTORY_NAME, "ge.leo").unwrap(); let bytes = include_bytes!("ge.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
@ -269,7 +290,8 @@ fn test_ge() {
let result = r1.ge(&r2); let result = r1.ge(&r2);
let mut program = compile_program(DIRECTORY_NAME, "ge.leo").unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -285,7 +307,9 @@ fn test_gt() {
let r1: u64 = rand::random(); let r1: u64 = rand::random();
// test equal // test equal
let mut program = compile_program(DIRECTORY_NAME, "gt.leo").unwrap(); let bytes = include_bytes!("gt.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
@ -298,7 +322,8 @@ fn test_gt() {
let result = r1.gt(&r2); let result = r1.gt(&r2);
let mut program = compile_program(DIRECTORY_NAME, "gt.leo").unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -314,7 +339,9 @@ fn test_le() {
let r1: u64 = rand::random(); let r1: u64 = rand::random();
// test equal // test equal
let mut program = compile_program(DIRECTORY_NAME, "le.leo").unwrap(); let bytes = include_bytes!("le.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
@ -327,7 +354,8 @@ fn test_le() {
let result = r1.le(&r2); let result = r1.le(&r2);
let mut program = compile_program(DIRECTORY_NAME, "le.leo").unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -343,7 +371,9 @@ fn test_lt() {
let r1: u64 = rand::random(); let r1: u64 = rand::random();
// test equal // test equal
let mut program = compile_program(DIRECTORY_NAME, "lt.leo").unwrap(); let bytes = include_bytes!("lt.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
@ -356,7 +386,8 @@ fn test_lt() {
let result = r1.lt(&r2); let result = r1.lt(&r2);
let mut program = compile_program(DIRECTORY_NAME, "lt.leo").unwrap(); let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -371,7 +402,9 @@ fn test_assert_eq_pass() {
for _ in 0..10 { for _ in 0..10 {
let r1: u64 = rand::random(); let r1: u64 = rand::random();
let mut program = compile_program(DIRECTORY_NAME, "assert_eq.leo").unwrap(); let bytes = include_bytes!("assert_eq.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
@ -391,7 +424,9 @@ fn test_assert_eq_fail() {
continue; continue;
} }
let mut program = compile_program(DIRECTORY_NAME, "assert_eq.leo").unwrap(); let bytes = include_bytes!("assert_eq.leo");
let mut program = parse_program(bytes).unwrap();
program.set_inputs(vec![ program.set_inputs(vec![
Some(InputValue::Field(r1.to_string())), Some(InputValue::Field(r1.to_string())),
Some(InputValue::Field(r2.to_string())), Some(InputValue::Field(r2.to_string())),
@ -418,7 +453,8 @@ fn test_ternary() {
let g1 = FqGadget::from(cs.ns(|| "g1"), &f1); let g1 = FqGadget::from(cs.ns(|| "g1"), &f1);
let g2 = FqGadget::from(cs.ns(|| "g2"), &f2); let g2 = FqGadget::from(cs.ns(|| "g2"), &f2);
let mut program_1 = compile_program(DIRECTORY_NAME, "ternary.leo").unwrap(); let bytes = include_bytes!("ternary.leo");
let mut program_1 = parse_program(bytes).unwrap();
let mut program_2 = program_1.clone(); let mut program_2 = program_1.clone();
// true -> field 1 // true -> field 1

View File

@ -1,7 +1,7 @@
pub mod array; pub mod array;
pub mod boolean; pub mod boolean;
pub mod circuits; pub mod circuits;
// pub mod field; pub mod field;
// pub mod function; // pub mod function;
// pub mod group; // pub mod group;
// pub mod import; // pub mod import;