leo/compiler/tests/group/mod.rs

183 lines
5.4 KiB
Rust
Raw Normal View History

2020-06-02 04:35:43 +03:00
use crate::{
boolean::{output_false, output_true},
2020-06-08 09:30:39 +03:00
fail_enforce,
get_output,
2020-06-09 04:39:10 +03:00
parse_program,
2020-06-08 09:30:39 +03:00
EdwardsConstrainedValue,
EdwardsTestCompiler,
2020-06-02 04:35:43 +03:00
};
2020-07-08 05:53:37 +03:00
use leo_compiler::{group::targets::edwards_bls12::EdwardsGroupType, ConstrainedValue};
2020-06-08 07:26:49 +03:00
use leo_types::InputValue;
2020-05-31 03:05:07 +03:00
2020-07-07 12:08:48 +03:00
use snarkos_curves::edwards_bls12::{EdwardsAffine, EdwardsParameters, Fq};
2020-06-02 00:20:14 +03:00
use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget;
2020-06-02 04:35:43 +03:00
use snarkos_models::{
2020-07-07 12:08:48 +03:00
curves::{TEModelParameters, Zero},
2020-06-03 01:33:09 +03:00
gadgets::{r1cs::TestConstraintSystem, utilities::alloc::AllocGadget},
2020-06-02 04:35:43 +03:00
};
2020-05-31 03:05:07 +03:00
use std::str::FromStr;
const TEST_POINT_1: &str = "(7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)";
const TEST_POINT_2: &str = "(1005842117974384149622370061042978581211342111653966059496918451529532134799, 79389132189982034519597104273449021362784864778548730890166152019533697186)";
2020-06-02 00:20:14 +03:00
fn output_expected_constant(program: EdwardsTestCompiler, expected: EdwardsAffine) {
2020-05-31 01:37:11 +03:00
let output = get_output(program);
assert_eq!(
2020-06-08 09:30:39 +03:00
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Group(EdwardsGroupType::Constant(expected))])
.to_string(),
2020-05-31 01:37:11 +03:00
output.to_string()
2020-05-31 03:05:07 +03:00
)
}
2020-06-02 00:20:14 +03:00
fn output_expected_allocated(program: EdwardsTestCompiler, expected: EdwardsBlsGadget) {
let output = get_output(program);
2020-06-03 01:33:09 +03:00
match output {
EdwardsConstrainedValue::Return(vec) => match vec.as_slice() {
[ConstrainedValue::Group(EdwardsGroupType::Allocated(gadget))] => {
assert_eq!(*gadget, expected as EdwardsBlsGadget)
}
_ => panic!("program output unknown return value"),
},
_ => panic!("program output unknown return value"),
}
2020-06-02 00:20:14 +03:00
}
2020-05-31 03:05:07 +03:00
fn output_zero(program: EdwardsTestCompiler) {
2020-06-02 00:20:14 +03:00
output_expected_constant(program, EdwardsAffine::zero())
2020-05-31 01:37:11 +03:00
}
2020-07-07 12:08:48 +03:00
fn output_one(program: EdwardsTestCompiler) {
let (x, y) = EdwardsParameters::AFFINE_GENERATOR_COEFFS;
let one = EdwardsAffine::new(x, y);
output_expected_constant(program, one)
}
2020-05-31 01:37:11 +03:00
#[test]
fn test_zero() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("zero.leo");
let program = parse_program(bytes).unwrap();
2020-05-31 01:37:11 +03:00
output_zero(program);
}
2020-07-07 12:08:48 +03:00
#[test]
fn test_one() {
let bytes = include_bytes!("one.leo");
let program = parse_program(bytes).unwrap();
output_one(program)
}
2020-05-31 03:05:07 +03:00
#[test]
fn test_point() {
let point = EdwardsAffine::from_str(TEST_POINT_1).unwrap();
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("point.leo");
let program = parse_program(bytes).unwrap();
2020-06-02 00:20:14 +03:00
output_expected_constant(program, point);
2020-05-31 03:05:07 +03:00
}
2020-06-03 02:06:25 +03:00
#[test]
fn test_input() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("input.leo");
let mut program = parse_program(bytes).unwrap();
program.set_main_inputs(vec![Some(InputValue::Group(TEST_POINT_1.into()))]);
2020-06-03 02:06:25 +03:00
let mut cs = TestConstraintSystem::<Fq>::new();
let constant_point = EdwardsAffine::from_str(TEST_POINT_1).unwrap();
let allocated_point =
2020-06-08 09:30:39 +03:00
<EdwardsBlsGadget as AllocGadget<EdwardsAffine, Fq>>::alloc(&mut cs, || Ok(constant_point)).unwrap();
2020-06-03 02:06:25 +03:00
output_expected_allocated(program, allocated_point);
}
2020-05-31 01:37:11 +03:00
#[test]
fn test_add() {
2020-05-31 03:05:07 +03:00
use std::ops::Add;
let point_1 = EdwardsAffine::from_str(TEST_POINT_1).unwrap();
let point_2 = EdwardsAffine::from_str(TEST_POINT_2).unwrap();
let sum = point_1.add(&point_2);
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("add.leo");
let program = parse_program(bytes).unwrap();
2020-06-02 00:20:14 +03:00
output_expected_constant(program, sum);
2020-05-31 01:37:11 +03:00
}
2020-05-31 03:19:26 +03:00
#[test]
fn test_sub() {
use std::ops::Sub;
let point_1 = EdwardsAffine::from_str(TEST_POINT_1).unwrap();
let point_2 = EdwardsAffine::from_str(TEST_POINT_2).unwrap();
let sum = point_1.sub(&point_2);
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("sub.leo");
let program = parse_program(bytes).unwrap();
2020-06-02 00:20:14 +03:00
output_expected_constant(program, sum);
2020-05-31 03:19:26 +03:00
}
#[test]
fn test_eq_true() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("eq_true.leo");
let program = parse_program(bytes).unwrap();
output_true(program)
}
#[test]
fn test_eq_false() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("eq_false.leo");
let program = parse_program(bytes).unwrap();
output_false(program)
}
#[test]
2020-06-03 02:06:25 +03:00
fn test_assert_eq_pass() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("assert_eq_true.leo");
let program = parse_program(bytes).unwrap();
let _res = get_output(program);
}
#[test]
2020-06-03 02:06:25 +03:00
fn test_assert_eq_fail() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("assert_eq_false.leo");
let program = parse_program(bytes).unwrap();
fail_enforce(program);
}
2020-06-02 00:20:14 +03:00
2020-06-02 03:23:13 +03:00
#[test]
fn test_ternary() {
2020-06-09 04:39:10 +03:00
let bytes = include_bytes!("ternary.leo");
let mut program_1 = parse_program(bytes).unwrap();
2020-06-02 03:23:13 +03:00
let mut program_2 = program_1.clone();
// true -> point_1
program_1.set_main_inputs(vec![Some(InputValue::Boolean(true))]);
2020-06-02 03:23:13 +03:00
2020-06-03 01:33:09 +03:00
let mut cs = TestConstraintSystem::<Fq>::new();
2020-06-02 03:23:13 +03:00
let point_1 = EdwardsAffine::from_str(TEST_POINT_1).unwrap();
2020-06-03 01:33:09 +03:00
let expected_point_1 =
2020-06-08 09:30:39 +03:00
<EdwardsBlsGadget as AllocGadget<EdwardsAffine, Fq>>::alloc(&mut cs, || Ok(point_1)).unwrap();
2020-06-03 01:33:09 +03:00
output_expected_allocated(program_1, expected_point_1);
2020-06-02 03:23:13 +03:00
// false -> point_2
program_2.set_main_inputs(vec![Some(InputValue::Boolean(false))]);
2020-06-02 03:23:13 +03:00
2020-06-03 01:33:09 +03:00
let mut cs = TestConstraintSystem::<Fq>::new();
2020-06-02 03:23:13 +03:00
let point_2 = EdwardsAffine::from_str(TEST_POINT_2).unwrap();
2020-06-03 01:33:09 +03:00
let expected_point_2 =
2020-06-08 09:30:39 +03:00
<EdwardsBlsGadget as AllocGadget<EdwardsAffine, Fq>>::alloc(&mut cs, || Ok(point_2)).unwrap();
2020-06-03 01:33:09 +03:00
output_expected_allocated(program_2, expected_point_2);
2020-06-02 00:20:14 +03:00
}