mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-28 01:01:53 +03:00
impl circuit member access with self.member
This commit is contained in:
parent
36b2508de3
commit
3f10bcfe82
@ -46,7 +46,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
|||||||
unresolved_identifier: Identifier,
|
unresolved_identifier: Identifier,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
||||||
// Evaluate the identifier name in the current function scope
|
// 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 identifier_name = new_scope(file_scope, unresolved_identifier.to_string());
|
||||||
|
|
||||||
let mut result_value = if let Some(value) = self.get(&variable_name) {
|
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,
|
circuit_member: Identifier,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> 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(
|
let (circuit_name, members) = match self.enforce_expression_value(
|
||||||
cs,
|
cs,
|
||||||
file_scope.clone(),
|
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 self_keyword = new_scope(function_scope, SELF_KEYWORD.to_string());
|
||||||
let field = new_scope(self_keyword, stored_member.0.to_string());
|
let field = new_scope(self_keyword, stored_member.0.to_string());
|
||||||
|
|
||||||
println!("storing");
|
|
||||||
self.store(field, stored_member.1.clone());
|
self.store(field, stored_member.1.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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>) {
|
pub(crate) fn store(&mut self, name: String, value: ConstrainedValue<F, G>) {
|
||||||
println!("{}", name);
|
|
||||||
self.identifiers.insert(name, value);
|
self.identifiers.insert(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,9 @@ pub enum ConstrainedValue<F: Field + PrimeField, G: GroupType<F>> {
|
|||||||
|
|
||||||
Mutable(Box<ConstrainedValue<F, G>>),
|
Mutable(Box<ConstrainedValue<F, G>>),
|
||||||
Static(Box<ConstrainedValue<F, G>>),
|
Static(Box<ConstrainedValue<F, G>>),
|
||||||
|
|
||||||
Import(String, Box<ConstrainedValue<F, G>>),
|
Import(String, Box<ConstrainedValue<F, G>>),
|
||||||
|
|
||||||
Unresolved(String),
|
Unresolved(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ fn output_circuit(program: EdwardsTestCompiler) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_expected_member(program: EdwardsTestCompiler) {
|
fn expect_fail(program: EdwardsTestCompiler) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
||||||
ExpressionError::Error(_string),
|
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
|
// Expressions
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -81,7 +72,7 @@ fn test_inline_fail() {
|
|||||||
let bytes = include_bytes!("inline_fail.leo");
|
let bytes = include_bytes!("inline_fail.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
fail_expected_member(program)
|
expect_fail(program)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -112,7 +103,7 @@ fn test_member_field_fail() {
|
|||||||
let bytes = include_bytes!("member_field_fail.leo");
|
let bytes = include_bytes!("member_field_fail.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
fail_undefined_member(program);
|
expect_fail(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -136,7 +127,7 @@ fn test_member_function_fail() {
|
|||||||
let bytes = include_bytes!("member_function_fail.leo");
|
let bytes = include_bytes!("member_function_fail.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
fail_undefined_member(program);
|
expect_fail(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -144,12 +135,7 @@ fn test_member_function_invalid() {
|
|||||||
let bytes = include_bytes!("member_function_invalid.leo");
|
let bytes = include_bytes!("member_function_invalid.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
match get_error(program) {
|
expect_fail(program);
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
|
||||||
ExpressionError::Error(_),
|
|
||||||
))) => {}
|
|
||||||
error => panic!("Expected invalid function error, got {}", error),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -165,12 +151,7 @@ fn test_member_static_function_undefined() {
|
|||||||
let bytes = include_bytes!("member_static_function_undefined.leo");
|
let bytes = include_bytes!("member_static_function_undefined.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
match get_error(program) {
|
expect_fail(program)
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
|
||||||
ExpressionError::Error(_),
|
|
||||||
))) => {}
|
|
||||||
error => panic!("Expected undefined static function error, got {}", error),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -178,18 +159,37 @@ fn test_member_static_function_invalid() {
|
|||||||
let bytes = include_bytes!("member_static_function_invalid.leo");
|
let bytes = include_bytes!("member_static_function_invalid.leo");
|
||||||
let program = parse_program(bytes).unwrap();
|
let program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
match get_error(program) {
|
expect_fail(program)
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(StatementError::ExpressionError(
|
|
||||||
ExpressionError::Error(_),
|
|
||||||
))) => {}
|
|
||||||
error => panic!("Expected invalid static function error, got {}", error),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Self
|
// Self
|
||||||
#[test]
|
#[test]
|
||||||
fn test_self() {
|
fn test_self_member() {
|
||||||
let bytes = include_bytes!("self.leo");
|
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 program = parse_program(bytes).unwrap();
|
||||||
|
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
circuit PedersenHash {
|
circuit PedersenHash {
|
||||||
parameters: u32[512]
|
parameters: u32[512]
|
||||||
|
|
||||||
static function new(parameters: u32[512]) -> Self {
|
static function new(parameters: u32[512]) -> Self {
|
||||||
return Self { parameters: parameters }
|
return Self { parameters: parameters }
|
||||||
}
|
}
|
||||||
|
|
||||||
function hash(bits: bool[512]) -> u32 {
|
function hash(bits: bool[512]) -> u32 {
|
||||||
let mut digest: u32 = 0;
|
let mut digest: u32 = 0;
|
||||||
for i in 0..512 {
|
for i in 0..512 {
|
||||||
let base = if bits[i] ? parameters[i] : 0u32;
|
let base = if bits[i] ? self.parameters[i] : 0u32;
|
||||||
digest += base;
|
digest += base;
|
||||||
}
|
}
|
||||||
return digest
|
return digest
|
||||||
|
12
compiler/tests/circuits/self_member.leo
Normal file
12
compiler/tests/circuits/self_member.leo
Normal 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()
|
||||||
|
}
|
12
compiler/tests/circuits/self_member_fail.leo
Normal file
12
compiler/tests/circuits/self_member_fail.leo
Normal 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()
|
||||||
|
}
|
10
compiler/tests/circuits/self_no_member_fail.leo
Normal file
10
compiler/tests/circuits/self_no_member_fail.leo
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
circuit Foo {
|
||||||
|
function bar() -> u32 {
|
||||||
|
return self.f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() -> u32 {
|
||||||
|
let circuit = Foo { };
|
||||||
|
return circuit.bar()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user