Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Protryon committed Jan 20, 2021
1 parent ba2a7a6 commit 7c03cc6
Show file tree
Hide file tree
Showing 68 changed files with 548 additions and 1,386 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions asg/src/checks/return_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ impl MonoidalReducerStatement<BoolAnd> for ReturnPathReducer {
}

fn reduce_conditional_statement(&mut self, input: &ConditionalStatement, condition: BoolAnd, if_true: BoolAnd, if_false: Option<BoolAnd>) -> BoolAnd {
if if_false.as_ref().map(|x| x.0).unwrap_or(false) != if_true.0 {
self.record_error(input.span(), "cannot have asymmetrical return in if statement".to_string());
}
if_true.append(if_false.unwrap_or_else(|| BoolAnd(false)))
}

Expand Down
12 changes: 12 additions & 0 deletions asg/src/const_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{ Type, IntegerType, AsgConvertError, Span };
use std::convert::TryInto;
use num_bigint::BigInt;
use std::fmt;

#[derive(Clone, Debug, PartialEq)]
pub enum ConstInt {
Expand All @@ -24,6 +25,17 @@ pub enum GroupCoordinate {
Inferred,
}

impl fmt::Display for GroupCoordinate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GroupCoordinate::Number(number) => write!(f, "{}", number),
GroupCoordinate::SignHigh => write!(f, "+"),
GroupCoordinate::SignLow => write!(f, "-"),
GroupCoordinate::Inferred => write!(f, "_"),
}
}
}

impl From<&leo_ast::GroupCoordinate> for GroupCoordinate {
fn from(other: &leo_ast::GroupCoordinate) -> GroupCoordinate {
use leo_ast::GroupCoordinate::*;
Expand Down
1 change: 1 addition & 0 deletions asg/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Input {
state_leaf,
container_circuit: container_circuit.clone(),
container: Arc::new(RefCell::new(crate::InnerVariable {
id: uuid::Uuid::new_v4(),
name: Identifier::new("input".to_string()),
type_: Type::Circuit(container_circuit),
mutable: false,
Expand Down
26 changes: 13 additions & 13 deletions asg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#[macro_use]
extern crate thiserror;

mod node;
pub mod node;
pub use node::*;

mod type_;
pub mod type_;
pub use type_::*;

mod program;
pub mod program;
pub use program::*;

mod expression;
pub mod expression;
pub use expression::*;

mod statement;
pub mod statement;
pub use statement::*;

mod variable;
pub mod variable;
pub use variable::*;

mod scope;
pub mod scope;
pub use scope::*;

mod error;
pub mod error;
pub use error::*;

mod import;
pub mod import;
pub use import::*;

mod const_value;
pub mod const_value;
pub use const_value::*;

mod input;
pub use input::*;

mod prelude;
pub mod prelude;
pub use prelude::*;

mod reducer;
pub mod reducer;
pub use reducer::*;

mod checks;
pub mod checks;
pub use checks::*;

pub use leo_ast::{ Span, Identifier };
Expand Down
2 changes: 2 additions & 0 deletions asg/src/program/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl FunctionBody {
if function.qualifier != FunctionQualifier::Static {
let circuit = function.circuit.borrow();
let self_variable = Arc::new(RefCell::new(crate::InnerVariable {
id: Uuid::new_v4(),
name: Identifier::new("self".to_string()),
type_: Type::Circuit(circuit.as_ref().unwrap().upgrade().clone().unwrap()),
mutable: function.qualifier == FunctionQualifier::MutSelfRef,
Expand All @@ -119,6 +120,7 @@ impl FunctionBody {
FunctionInput::MutSelfKeyword(_) => {},
FunctionInput::Variable(leo_ast::FunctionInputVariable { identifier, mutable, type_, span: _span }) => {
let variable = Arc::new(RefCell::new(crate::InnerVariable {
id: Uuid::new_v4(),
name: identifier.clone(),
type_: scope_borrow.resolve_ast_type(&type_)?,
mutable: *mutable,
Expand Down
1 change: 1 addition & 0 deletions asg/src/statement/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl FromAst<leo_ast::DefinitionStatement> for DefinitionStatement {
return Err(AsgConvertError::illegal_ast_structure("cannot have const mut"));
}
variables.push(Arc::new(RefCell::new(InnerVariable {
id: uuid::Uuid::new_v4(),
name: variable.identifier.clone(),
type_: type_
.ok_or_else(|| AsgConvertError::unresolved_type(&variable.identifier.name, &statement.span))?,
Expand Down
1 change: 1 addition & 0 deletions asg/src/statement/iteration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl FromAst<leo_ast::IterationStatement> for IterationStatement {
let start = Arc::<Expression>::from_ast(scope, &statement.start, expected_index_type.clone())?;
let stop = Arc::<Expression>::from_ast(scope, &statement.stop, expected_index_type)?;
let variable = Arc::new(RefCell::new(InnerVariable {
id: uuid::Uuid::new_v4(),
name: statement.variable.clone(),
type_: start.get_type().ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?,
mutable: false,
Expand Down
2 changes: 2 additions & 0 deletions asg/src/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use leo_ast::Identifier;
use crate::{ Type, Expression, ConstValue, Statement };
use std::sync::{ Arc, Weak };
use std::cell::RefCell;
use uuid::Uuid;

//todo: fill out
pub enum VariableDeclaration {
Expand All @@ -13,6 +14,7 @@ pub enum VariableDeclaration {
}

pub struct InnerVariable {
pub id: Uuid,
pub name: Identifier,
pub type_: Type,
pub mutable: bool,
Expand Down
4 changes: 4 additions & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ version = "1.0"
[dependencies.tracing]
version = "0.1"

[dependencies.uuid]
version = "0.8"
features = ["v4", "serde"]

[dev-dependencies.num-bigint]
version = "0.3"

Expand Down
12 changes: 6 additions & 6 deletions compiler/src/console/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use crate::{
value::ConstrainedValue,
GroupType,
};
use leo_ast::{Expression, Span, Type};
use leo_asg::{Expression, Span, Type};
use std::sync::Arc;

use snarkvm_models::{
curves::{Field, PrimeField},
Expand All @@ -37,14 +38,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
file_scope: &str,
function_scope: &str,
indicator: &Boolean,
expression: Expression,
expression: &Arc<Expression>,
span: &Span,
) -> Result<(), ConsoleError> {
let expected_type = Some(Type::Boolean);
let expression_string = expression.to_string();

// Evaluate assert expression
let assert_expression = self.enforce_expression(cs, file_scope, function_scope, expected_type, expression)?;
let assert_expression = self.enforce_expression(cs, file_scope, function_scope, expression)?;

// If the indicator bit is false, do not evaluate the assertion
// This is okay since we are not enforcing any constraints
Expand All @@ -57,15 +57,15 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
ConstrainedValue::Boolean(boolean) => boolean.get_value(),
_ => {
return Err(ConsoleError::assertion_must_be_boolean(
expression_string,
span.text.clone(),
span.to_owned(),
));
}
};
let result_bool = result_option.ok_or_else(|| ConsoleError::assertion_depends_on_input(span.to_owned()))?;

if !result_bool {
return Err(ConsoleError::assertion_failed(expression_string, span.to_owned()));
return Err(ConsoleError::assertion_failed(span.text.clone(), span.to_owned()));
}

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/console/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Evaluates a macro in a compiled Leo program.

use crate::{errors::ConsoleError, program::ConstrainedProgram, statement::get_indicator_value, GroupType};
use leo_ast::{ConsoleFunction, ConsoleStatement};
use leo_asg::{ConsoleFunction, ConsoleStatement};

use snarkvm_models::{
curves::{Field, PrimeField},
Expand All @@ -31,11 +31,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
file_scope: &str,
function_scope: &str,
indicator: &Boolean,
console: ConsoleStatement,
console: &ConsoleStatement,
) -> Result<(), ConsoleError> {
match console.function {
match &console.function {
ConsoleFunction::Assert(expression) => {
self.evaluate_console_assert(cs, file_scope, function_scope, indicator, expression, &console.span)?;
self.evaluate_console_assert(cs, file_scope, function_scope, indicator, expression, &console.span.clone().unwrap_or_default())?;
}
ConsoleFunction::Debug(string) => {
let string = self.format(cs, file_scope, function_scope, string)?;
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/console/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Evaluates a formatted string in a compiled Leo program.

use crate::{errors::ConsoleError, program::ConstrainedProgram, GroupType};
use leo_ast::FormattedString;
use leo_asg::FormattedString;

use snarkvm_models::{
curves::{Field, PrimeField},
Expand All @@ -30,7 +30,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
cs: &mut CS,
file_scope: &str,
function_scope: &str,
formatted: FormattedString,
formatted: &FormattedString,
) -> Result<String, ConsoleError> {
// Check that containers and parameters match
if formatted.containers.len() != formatted.parameters.len() {
Expand All @@ -51,8 +51,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
// Insert the parameter for each container `{}`
let mut result = string.to_string();

for parameter in formatted.parameters.into_iter() {
let parameter_value = self.enforce_expression(cs, file_scope, function_scope, None, parameter)?;
for parameter in formatted.parameters.iter() {
let parameter_value = self.enforce_expression(cs, file_scope, function_scope, parameter)?;

result = result.replacen("{}", &parameter_value.to_string(), 1);
}
Expand Down
17 changes: 6 additions & 11 deletions compiler/src/constraints/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

use crate::{
errors::CompilerError,
new_scope,
ConstrainedProgram,
ConstrainedValue,
GroupType,
OutputBytes,
OutputFile,
Expand All @@ -43,15 +41,15 @@ pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: Constrai
) -> Result<OutputBytes, CompilerError> {
let mut resolved_program = ConstrainedProgram::<F, G>::new();
let program_name = program.borrow().name.clone();
let main_function_name = new_scope(&program_name, "main");

resolved_program.store_definitions(program)?;

let main = resolved_program.get(&main_function_name).ok_or(CompilerError::NoMain)?;
let main = {
let program = program.borrow();
program.functions.get("main").cloned()
};

match main.clone() {
ConstrainedValue::Function(_circuit_identifier, function) => {
let result = resolved_program.enforce_main_function(cs, &program_name, function, input)?;
Some(function) => {
let result = resolved_program.enforce_main_function(cs, &program_name, &function, input)?;
Ok(result)
}
_ => Err(CompilerError::NoMainFunction),
Expand All @@ -67,9 +65,6 @@ pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
let mut resolved_program = ConstrainedProgram::<F, G>::new();
let program_name = program.borrow().name.clone();

// Store definitions
resolved_program.store_definitions(&program)?;

// Get default input
let default = input.pairs.get(&program_name);

Expand Down
14 changes: 6 additions & 8 deletions compiler/src/definition/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@
//! Stores all defined names in a compiled Leo program.

use crate::{
program::{new_scope, ConstrainedProgram},
program::{ConstrainedProgram},
value::ConstrainedValue,
GroupType,
};
use leo_ast::Identifier;
use leo_asg::Variable;

use snarkvm_models::curves::{Field, PrimeField};

impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn store_definition(
&mut self,
function_scope: &str,
mutable: bool,
identifier: Identifier,
variable: &Variable,
mut value: ConstrainedValue<F, G>,
) {
let variable = variable.borrow();
// Store with given mutability
if mutable {
if variable.mutable {
value = ConstrainedValue::Mutable(Box::new(value));
}

let variable_program_identifier = new_scope(function_scope, &identifier.name);

self.store(variable_program_identifier, value);
self.store(variable.id.clone(), value);
}
}
Loading

0 comments on commit 7c03cc6

Please sign in to comment.