impl circuit member access with self.member

This commit is contained in:
collin 2020-07-03 13:35:46 -07:00
parent 36b2508de3
commit 3f10bcfe82
9 changed files with 84 additions and 38 deletions

View File

@ -46,7 +46,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
unresolved_identifier: Identifier,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
// Evaluate the identifier name in the current function scope
let variable_name = new_scope(function_scope, unresolved_identifier.to_string());
let variable_name = new_scope(function_scope.clone(), unresolved_identifier.to_string());
let identifier_name = new_scope(file_scope, unresolved_identifier.to_string());
let mut result_value = if let Some(value) = self.get(&variable_name) {
@ -701,7 +701,17 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
circuit_member: Identifier,
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
println!("access");
// access a circuit member using the `self` keyword
if let Expression::Identifier(ref identifier) = *circuit_identifier {
if identifier.is_self() {
let self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string());
let member_value =
self.evaluate_identifier(file_scope, self_keyword, &vec![], circuit_member.clone())?;
return Ok(member_value);
}
}
let (circuit_name, members) = match self.enforce_expression_value(
cs,
file_scope.clone(),
@ -731,7 +741,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
let self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string());
let field = new_scope(self_keyword, stored_member.0.to_string());
println!("storing");
self.store(field, stored_member.1.clone());
}
}

View File

@ -22,7 +22,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}
pub(crate) fn store(&mut self, name: String, value: ConstrainedValue<F, G>) {
println!("{}", name);
self.identifiers.insert(name, value);
}

View File

@ -40,7 +40,9 @@ pub enum ConstrainedValue<F: Field + PrimeField, G: GroupType<F>> {
Mutable(Box<ConstrainedValue<F, G>>),
Static(Box<ConstrainedValue<F, G>>),
Import(String, Box<ConstrainedValue<F, G>>),
Unresolved(String),
}

View File

@ -48,7 +48,7 @@ fn output_circuit(program: EdwardsTestCompiler) {
);
}
fn fail_expected_member(program: EdwardsTestCompiler) {
fn expect_fail(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_string),
@ -57,15 +57,6 @@ fn fail_expected_member(program: EdwardsTestCompiler) {
}
}
fn fail_undefined_member(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_),
))) => {}
error => panic!("Expected undefined circuit member error, got {}", error),
}
}
// Expressions
#[test]
@ -81,7 +72,7 @@ fn test_inline_fail() {
let bytes = include_bytes!("inline_fail.leo");
let program = parse_program(bytes).unwrap();
fail_expected_member(program)
expect_fail(program)
}
#[test]
@ -112,7 +103,7 @@ fn test_member_field_fail() {
let bytes = include_bytes!("member_field_fail.leo");
let program = parse_program(bytes).unwrap();
fail_undefined_member(program);
expect_fail(program);
}
#[test]
@ -136,7 +127,7 @@ fn test_member_function_fail() {
let bytes = include_bytes!("member_function_fail.leo");
let program = parse_program(bytes).unwrap();
fail_undefined_member(program);
expect_fail(program);
}
#[test]
@ -144,12 +135,7 @@ fn test_member_function_invalid() {
let bytes = include_bytes!("member_function_invalid.leo");
let program = parse_program(bytes).unwrap();
match get_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_),
))) => {}
error => panic!("Expected invalid function error, got {}", error),
}
expect_fail(program);
}
#[test]
@ -165,12 +151,7 @@ fn test_member_static_function_undefined() {
let bytes = include_bytes!("member_static_function_undefined.leo");
let program = parse_program(bytes).unwrap();
match get_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_),
))) => {}
error => panic!("Expected undefined static function error, got {}", error),
}
expect_fail(program)
}
#[test]
@ -178,18 +159,37 @@ fn test_member_static_function_invalid() {
let bytes = include_bytes!("member_static_function_invalid.leo");
let program = parse_program(bytes).unwrap();
match get_error(program) {
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
ExpressionError::Error(_),
))) => {}
error => panic!("Expected invalid static function error, got {}", error),
}
expect_fail(program)
}
// Self
#[test]
fn test_self() {
let bytes = include_bytes!("self.leo");
fn test_self_member() {
let bytes = include_bytes!("self_member.leo");
let program = parse_program(bytes).unwrap();
output_one(program);
}
#[test]
fn test_self_no_member_fail() {
let bytes = include_bytes!("self_no_member_fail.leo");
let program = parse_program(bytes).unwrap();
let _err = get_error(program);
}
#[test]
fn test_self_member_fail() {
let bytes = include_bytes!("self_member_fail.leo");
let program = parse_program(bytes).unwrap();
let _err = get_error(program);
}
#[test]
fn test_self_circuit() {
let bytes = include_bytes!("self_circuit.leo");
let program = parse_program(bytes).unwrap();
let output = get_output(program);

View File

@ -1,12 +1,14 @@
circuit PedersenHash {
parameters: u32[512]
static function new(parameters: u32[512]) -> Self {
return Self { parameters: parameters }
}
function hash(bits: bool[512]) -> u32 {
let mut digest: u32 = 0;
for i in 0..512 {
let base = if bits[i] ? parameters[i] : 0u32;
let base = if bits[i] ? self.parameters[i] : 0u32;
digest += base;
}
return digest

View File

@ -0,0 +1,12 @@
circuit Foo {
f: u32,
function bar() -> u32 {
return self.f
}
}
function main() -> u32 {
let circuit = Foo { f: 1u32 };
return circuit.bar()
}

View File

@ -0,0 +1,12 @@
circuit Foo {
f: u32,
function bar() -> u32 {
return f
}
}
function main() -> u32 {
let circuit = Foo { f: 1u32 };
return circuit.bar()
}

View File

@ -0,0 +1,10 @@
circuit Foo {
function bar() -> u32 {
return self.f
}
}
function main() -> u32 {
let circuit = Foo { };
return circuit.bar()
}