mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
impl circuit function expressions
This commit is contained in:
parent
b766f96d23
commit
3fb6430e1f
@ -1,7 +1,8 @@
|
||||
circuit PedersenHash {
|
||||
static function new() {}
|
||||
parameters: group[1]
|
||||
|
||||
static function new() {
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
|
@ -6,7 +6,9 @@ use crate::{
|
||||
ConstrainedProgram, ConstrainedValue,
|
||||
},
|
||||
errors::ExpressionError,
|
||||
types::{CircuitMember, Expression, Identifier, RangeOrExpression, SpreadOrExpression},
|
||||
types::{
|
||||
CircuitMember, CircuitObject, Expression, Identifier, RangeOrExpression, SpreadOrExpression,
|
||||
},
|
||||
};
|
||||
|
||||
use snarkos_models::{
|
||||
@ -357,19 +359,26 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
self.get_mut_variable(&circuit_name)
|
||||
{
|
||||
let mut resolved_members = vec![];
|
||||
for (field, member) in circuit_definition
|
||||
.fields
|
||||
for (object, member) in circuit_definition
|
||||
.objects
|
||||
.clone()
|
||||
.into_iter()
|
||||
.zip(members.clone().into_iter())
|
||||
{
|
||||
if field.identifier != member.identifier {
|
||||
let (identifier, _type) = match object {
|
||||
CircuitObject::CircuitValue(identifier, _type) => (identifier, _type),
|
||||
CircuitObject::CircuitFunction(_, _) => {
|
||||
return Err(ExpressionError::InvalidCircuitValue)
|
||||
}
|
||||
};
|
||||
|
||||
if identifier != member.identifier {
|
||||
return Err(ExpressionError::InvalidCircuitObject(
|
||||
field.identifier.name,
|
||||
identifier.to_string(),
|
||||
member.identifier.name,
|
||||
));
|
||||
}
|
||||
// Resolve and enforce circuit fields
|
||||
// Resolve and enforce circuit object
|
||||
let member_value = self.enforce_expression(
|
||||
cs,
|
||||
file_scope.clone(),
|
||||
@ -377,8 +386,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
member.expression,
|
||||
)?;
|
||||
|
||||
// Check member types
|
||||
member_value.expect_type(&field._type)?;
|
||||
// Check member type
|
||||
member_value.expect_type(&_type)?;
|
||||
|
||||
resolved_members.push(ConstrainedCircuitObject(member.identifier, member_value))
|
||||
}
|
||||
|
@ -51,6 +51,9 @@ pub enum ExpressionError {
|
||||
#[error("Circuit object {} does not exist", _0)]
|
||||
UndefinedCircuitObject(String),
|
||||
|
||||
#[error("Cannot assign to circuit functions")]
|
||||
InvalidCircuitValue,
|
||||
|
||||
#[error("Expected circuit object {}, got {}", _0, _1)]
|
||||
InvalidCircuitObject(String, String),
|
||||
|
||||
|
@ -247,7 +247,7 @@ pub enum CircuitObject<F: Field + PrimeField, G: Group> {
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Circuit<F: Field + PrimeField, G: Group> {
|
||||
pub identifier: Identifier<F, G>,
|
||||
pub fields: Vec<CircuitObject<F, G>>,
|
||||
pub objects: Vec<CircuitObject<F, G>>,
|
||||
}
|
||||
|
||||
/// Function parameters
|
||||
|
@ -293,10 +293,10 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for CircuitObject<F, G> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
CircuitObject::CircuitValue(ref identifier, ref _type) => {
|
||||
write!(f, "{}: {}", self.identifier, self._type)
|
||||
write!(f, "{}: {}", identifier, _type)
|
||||
}
|
||||
CircuitObject::CircuitFunction(ref _static, ref function) => {
|
||||
if _static {
|
||||
if *_static {
|
||||
write!(f, "static ")?;
|
||||
}
|
||||
write!(f, "{}", function)
|
||||
@ -308,7 +308,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for CircuitObject<F, G> {
|
||||
impl<F: Field + PrimeField, G: Group> Circuit<F, G> {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "circuit {} {{ \n", self.identifier)?;
|
||||
for field in self.fields.iter() {
|
||||
for field in self.objects.iter() {
|
||||
write!(f, " {}\n", field)?;
|
||||
}
|
||||
write!(f, "}}")
|
||||
@ -395,11 +395,11 @@ impl<F: Field + PrimeField, G: Group> Function<F, G> {
|
||||
}
|
||||
}
|
||||
|
||||
// impl<F: Field + PrimeField, G: Group> fmt::Display for Function<F, G> {// uncomment when we no longer print out Program
|
||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// self.format(f)
|
||||
// }
|
||||
// }
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Function<F, G> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for Function<F, G> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
@ -742,7 +742,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Circuit<'ast>> for types::
|
||||
|
||||
types::Circuit {
|
||||
identifier: variable,
|
||||
fields,
|
||||
objects: fields,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user