2020-05-20 22:45:40 +03:00
|
|
|
use crate::{
|
2020-06-08 09:30:39 +03:00
|
|
|
get_error,
|
|
|
|
get_output,
|
|
|
|
integers::u32::output_one,
|
2020-06-09 04:12:40 +03:00
|
|
|
parse_program,
|
2020-06-08 09:30:39 +03:00
|
|
|
EdwardsConstrainedValue,
|
2020-05-30 03:34:31 +03:00
|
|
|
EdwardsTestCompiler,
|
2020-05-20 22:45:40 +03:00
|
|
|
};
|
2020-05-20 07:59:00 +03:00
|
|
|
use leo_compiler::{
|
|
|
|
errors::{CompilerError, ExpressionError, FunctionError, StatementError},
|
2020-06-08 09:30:39 +03:00
|
|
|
ConstrainedCircuitMember,
|
|
|
|
ConstrainedValue,
|
2020-05-20 07:59:00 +03:00
|
|
|
};
|
2020-06-20 09:02:58 +03:00
|
|
|
use leo_types::{Expression, Function, Identifier, Integer, Span, Statement, Type};
|
2020-06-02 04:35:43 +03:00
|
|
|
|
2020-06-04 23:35:12 +03:00
|
|
|
use snarkos_models::gadgets::utilities::uint::UInt32;
|
2020-05-20 07:59:00 +03:00
|
|
|
|
|
|
|
// Circ { x: 1u32 }
|
2020-05-30 03:34:31 +03:00
|
|
|
fn output_circuit(program: EdwardsTestCompiler) {
|
2020-05-20 07:59:00 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
assert_eq!(
|
2020-05-30 03:34:31 +03:00
|
|
|
EdwardsConstrainedValue::Return(vec![ConstrainedValue::CircuitExpression(
|
2020-06-20 09:02:58 +03:00
|
|
|
Identifier {
|
|
|
|
name: "Circ".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
},
|
2020-05-30 01:55:57 +03:00
|
|
|
vec![ConstrainedCircuitMember(
|
2020-06-20 09:02:58 +03:00
|
|
|
Identifier {
|
|
|
|
name: "x".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
},
|
2020-05-30 01:55:57 +03:00
|
|
|
ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32)))
|
|
|
|
)]
|
2020-05-30 03:34:31 +03:00
|
|
|
)])
|
|
|
|
.to_string(),
|
|
|
|
output.to_string()
|
2020-05-20 07:59:00 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_expected_member(program: EdwardsTestCompiler) {
|
2020-05-20 07:59:00 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
2020-06-20 09:02:58 +03:00
|
|
|
ExpressionError::Error(_string),
|
2020-06-08 09:30:39 +03:00
|
|
|
))) => {}
|
2020-05-20 07:59:00 +03:00
|
|
|
error => panic!("Expected invalid circuit member error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 03:34:31 +03:00
|
|
|
fn fail_undefined_member(program: EdwardsTestCompiler) {
|
2020-05-20 22:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
2020-06-20 09:02:58 +03:00
|
|
|
ExpressionError::Error(_),
|
2020-06-08 09:30:39 +03:00
|
|
|
))) => {}
|
2020-05-20 22:45:40 +03:00
|
|
|
error => panic!("Expected undefined circuit member error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
// Expressions
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_inline() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("inline.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
output_circuit(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_inline_fail() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("inline_fail.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
fail_expected_member(program)
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_inline_undefined() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("inline_undefined.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
2020-06-20 09:02:58 +03:00
|
|
|
ExpressionError::Error(_),
|
2020-06-08 09:30:39 +03:00
|
|
|
))) => {}
|
2020-05-20 22:45:40 +03:00
|
|
|
error => panic!("Expected undefined circuit error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
// Members
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_member_field() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_field.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
output_one(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_member_field_fail() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_field_fail.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
fail_undefined_member(program);
|
|
|
|
}
|
|
|
|
|
2020-06-20 11:40:56 +03:00
|
|
|
#[test]
|
|
|
|
fn test_member_field_and_function() {
|
|
|
|
let bytes = include_bytes!("member_field_and_function.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
output_one(program);
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
#[test]
|
|
|
|
fn test_member_function() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_function.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
output_one(program);
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_member_function_fail() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_function_fail.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
fail_undefined_member(program);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_member_function_invalid() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_function_invalid.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
2020-06-20 09:02:58 +03:00
|
|
|
ExpressionError::Error(_),
|
2020-06-08 09:30:39 +03:00
|
|
|
))) => {}
|
2020-05-20 22:45:40 +03:00
|
|
|
error => panic!("Expected invalid function error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
#[test]
|
|
|
|
fn test_member_static_function() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_static_function.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 07:59:00 +03:00
|
|
|
output_one(program);
|
|
|
|
}
|
2020-05-20 22:45:40 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_member_static_function_undefined() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_static_function_undefined.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
2020-06-20 09:02:58 +03:00
|
|
|
ExpressionError::Error(_),
|
2020-06-08 09:30:39 +03:00
|
|
|
))) => {}
|
2020-05-20 22:45:40 +03:00
|
|
|
error => panic!("Expected undefined static function error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 04:12:40 +03:00
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
#[test]
|
|
|
|
fn test_member_static_function_invalid() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("member_static_function_invalid.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
match get_error(program) {
|
2020-06-08 09:30:39 +03:00
|
|
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
2020-06-20 09:02:58 +03:00
|
|
|
ExpressionError::Error(_),
|
2020-06-08 09:30:39 +03:00
|
|
|
))) => {}
|
2020-05-20 22:45:40 +03:00
|
|
|
error => panic!("Expected invalid static function error, got {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Self
|
|
|
|
#[test]
|
|
|
|
fn test_self() {
|
2020-06-09 04:12:40 +03:00
|
|
|
let bytes = include_bytes!("self.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
2020-05-20 22:45:40 +03:00
|
|
|
let output = get_output(program);
|
|
|
|
|
|
|
|
// circuit Circ {
|
|
|
|
// static function new() -> Self {
|
|
|
|
// return Self { }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
assert_eq!(
|
2020-05-30 03:34:31 +03:00
|
|
|
EdwardsConstrainedValue::Return(vec![ConstrainedValue::CircuitExpression(
|
2020-06-20 09:02:58 +03:00
|
|
|
Identifier {
|
|
|
|
name: "Circ".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
},
|
2020-05-30 01:55:57 +03:00
|
|
|
vec![ConstrainedCircuitMember(
|
2020-06-20 09:02:58 +03:00
|
|
|
Identifier {
|
|
|
|
name: "new".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
},
|
2020-05-30 01:55:57 +03:00
|
|
|
ConstrainedValue::Static(Box::new(ConstrainedValue::Function(
|
2020-06-20 09:02:58 +03:00
|
|
|
Some(Identifier {
|
|
|
|
name: "new".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
}),
|
2020-05-30 01:55:57 +03:00
|
|
|
Function {
|
2020-06-20 09:02:58 +03:00
|
|
|
function_name: Identifier {
|
|
|
|
name: "new".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
},
|
2020-05-30 01:55:57 +03:00
|
|
|
inputs: vec![],
|
|
|
|
returns: vec![Type::SelfType],
|
2020-06-20 09:02:58 +03:00
|
|
|
statements: vec![Statement::Return(
|
|
|
|
vec![Expression::Circuit(
|
|
|
|
Identifier {
|
|
|
|
name: "Self".to_string(),
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
vec![],
|
|
|
|
Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
)],
|
|
|
|
Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
|
|
|
)],
|
|
|
|
span: Span {
|
|
|
|
text: "".to_string(),
|
|
|
|
line: 0,
|
|
|
|
start: 0,
|
|
|
|
end: 0
|
|
|
|
}
|
2020-05-30 01:55:57 +03:00
|
|
|
}
|
|
|
|
)))
|
|
|
|
)]
|
2020-05-30 03:34:31 +03:00
|
|
|
)])
|
|
|
|
.to_string(),
|
|
|
|
output.to_string()
|
2020-05-20 22:45:40 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All
|
|
|
|
|
2020-06-23 04:28:30 +03:00
|
|
|
#[test]
|
|
|
|
fn test_pedersen_mock() {
|
|
|
|
use crate::integers::u32::output_zero;
|
|
|
|
|
|
|
|
let bytes = include_bytes!("pedersen_mock.leo");
|
|
|
|
let program = parse_program(bytes).unwrap();
|
|
|
|
|
|
|
|
output_zero(program);
|
|
|
|
}
|