mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
Migrates integer, identifier, and input_value
This commit is contained in:
parent
54b531c27f
commit
b9baafd57f
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -517,6 +517,7 @@ dependencies = [
|
||||
"from-pest",
|
||||
"hex",
|
||||
"leo-ast",
|
||||
"leo-types",
|
||||
"log",
|
||||
"pest",
|
||||
"rand",
|
||||
@ -530,6 +531,16 @@ dependencies = [
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "leo-types"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"leo-ast",
|
||||
"snarkos-errors",
|
||||
"snarkos-models",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.67"
|
||||
|
@ -13,7 +13,7 @@ name = "leo"
|
||||
path = "leo/main.rs"
|
||||
|
||||
[workspace]
|
||||
members = [ "ast", "compiler" ]
|
||||
members = [ "ast", "compiler", "types" ]
|
||||
|
||||
[dependencies]
|
||||
leo-compiler = { path = "compiler", version = "0.1.0" }
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ast::Rule, common::Static, function::Function};
|
||||
use crate::{ast::Rule, common::Static, functions::Function};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
|
@ -5,41 +5,18 @@ extern crate pest_derive;
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod access;
|
||||
pub use access::*;
|
||||
|
||||
pub mod ast;
|
||||
pub use ast::*;
|
||||
|
||||
pub mod circuits;
|
||||
pub use circuits::*;
|
||||
|
||||
pub mod common;
|
||||
pub use common::*;
|
||||
|
||||
pub mod errors;
|
||||
pub use errors::*;
|
||||
|
||||
pub mod access;
|
||||
pub mod ast;
|
||||
pub mod circuits;
|
||||
pub mod common;
|
||||
pub mod expressions;
|
||||
pub use expressions::*;
|
||||
|
||||
pub mod files;
|
||||
pub use files::*;
|
||||
|
||||
pub mod functions;
|
||||
pub use functions::*;
|
||||
|
||||
pub mod imports;
|
||||
pub use imports::*;
|
||||
|
||||
pub mod operations;
|
||||
pub use operations::*;
|
||||
|
||||
pub mod statements;
|
||||
pub use statements::*;
|
||||
|
||||
pub mod values;
|
||||
pub use values::*;
|
||||
|
||||
pub mod types;
|
||||
pub use types::*;
|
||||
|
@ -6,6 +6,7 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
leo-ast = { path = "../ast", version = "0.1.0" }
|
||||
leo-types = { path = "../types", version = "0.1.0" }
|
||||
|
||||
snarkos-algorithms = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" }
|
||||
snarkos-curves = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" }
|
||||
|
@ -3,9 +3,10 @@
|
||||
use crate::{
|
||||
constraints::{generate_constraints, generate_test_constraints, ConstrainedValue},
|
||||
errors::CompilerError,
|
||||
GroupType, InputValue, Program,
|
||||
GroupType, Program,
|
||||
};
|
||||
use leo_ast::{ast, files::File};
|
||||
use leo_types::InputValue;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
|
@ -3,9 +3,9 @@
|
||||
use crate::{
|
||||
constraints::{ConstrainedProgram, ConstrainedValue},
|
||||
errors::BooleanError,
|
||||
types::InputValue,
|
||||
GroupType,
|
||||
};
|
||||
use leo_types::InputValue;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
|
@ -5,11 +5,12 @@ use crate::{
|
||||
errors::ExpressionError,
|
||||
new_scope,
|
||||
types::{
|
||||
CircuitFieldDefinition, CircuitMember, Expression, Identifier, RangeOrExpression,
|
||||
CircuitFieldDefinition, CircuitMember, Expression, RangeOrExpression,
|
||||
SpreadOrExpression,
|
||||
},
|
||||
FieldType, GroupType, Integer, IntegerType, Type,
|
||||
FieldType, GroupType, Type,
|
||||
};
|
||||
use leo_types::{Identifier, Integer, IntegerType};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -1,8 +1,9 @@
|
||||
//! Methods to enforce constraints on field elements in a resolved Leo program.
|
||||
|
||||
use crate::{
|
||||
constraints::ConstrainedValue, errors::FieldError, types::InputValue, FieldType, GroupType,
|
||||
constraints::ConstrainedValue, errors::FieldError, FieldType, GroupType,
|
||||
};
|
||||
use leo_types::InputValue;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
|
@ -5,9 +5,9 @@ use crate::{
|
||||
constraints::{new_scope, ConstrainedProgram, ConstrainedValue},
|
||||
errors::{FunctionError, ImportError},
|
||||
field_from_input, group_from_input,
|
||||
types::{Expression, Function, Identifier, InputValue, Program, Type},
|
||||
GroupType, Integer,
|
||||
};
|
||||
types::{Expression, Function, Program, Type},
|
||||
GroupType};
|
||||
use leo_types::{Identifier, InputValue, Integer};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::{errors::GroupError, ConstrainedValue, GroupType, InputValue};
|
||||
use crate::{errors::GroupError, ConstrainedValue, GroupType};
|
||||
use leo_types::InputValue;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
|
@ -1,4 +0,0 @@
|
||||
//! Module containing methods to enforce constraints on integers in a Leo program
|
||||
|
||||
pub mod integer;
|
||||
pub use integer::*;
|
@ -12,9 +12,6 @@ pub use expression::*;
|
||||
pub mod import;
|
||||
pub use import::*;
|
||||
|
||||
pub mod integer;
|
||||
pub use integer::*;
|
||||
|
||||
pub(crate) mod field;
|
||||
pub(crate) use field::*;
|
||||
|
||||
@ -32,9 +29,10 @@ pub use statement::*;
|
||||
|
||||
use crate::{
|
||||
errors::CompilerError,
|
||||
types::{InputValue, Program},
|
||||
types::Program,
|
||||
GroupType,
|
||||
};
|
||||
use leo_types::InputValue;
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -5,11 +5,12 @@ use crate::{
|
||||
errors::StatementError,
|
||||
new_scope,
|
||||
types::{
|
||||
Assignee, ConditionalNestedOrEnd, ConditionalStatement, Expression, Identifier, Integer,
|
||||
Assignee, ConditionalNestedOrEnd, ConditionalStatement, Expression,
|
||||
RangeOrExpression, Statement, Type,
|
||||
},
|
||||
GroupType, Variable,
|
||||
};
|
||||
use leo_types::{Identifier, Integer};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -2,9 +2,10 @@
|
||||
|
||||
use crate::{
|
||||
errors::ValueError,
|
||||
types::{Circuit, Function, Identifier, Integer, IntegerType, Type},
|
||||
types::{Circuit, Function, Type},
|
||||
FieldType, GroupType,
|
||||
};
|
||||
use leo_types::{Identifier, Integer, IntegerType};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::errors::{FunctionError, ImportError, IntegerError, };
|
||||
use crate::errors::{FunctionError, ImportError};
|
||||
use leo_ast::{ast::Rule, SyntaxError};
|
||||
use leo_types::IntegerError;
|
||||
|
||||
use pest::error::Error;
|
||||
use std::{io, path::PathBuf};
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::errors::{
|
||||
BooleanError, FieldError, FunctionError, GroupError, IntegerError, ValueError,
|
||||
BooleanError, FieldError, FunctionError, GroupError, ValueError,
|
||||
};
|
||||
use leo_types::IntegerError;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use std::num::ParseIntError;
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::errors::{
|
||||
BooleanError, ExpressionError, FieldError, GroupError, IntegerError, StatementError, ValueError,
|
||||
BooleanError, ExpressionError, FieldError, GroupError, StatementError, ValueError,
|
||||
};
|
||||
use leo_types::IntegerError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FunctionError {
|
||||
|
@ -12,9 +12,6 @@ pub use expression::*;
|
||||
pub mod import;
|
||||
pub use import::*;
|
||||
|
||||
pub mod integer;
|
||||
pub use integer::*;
|
||||
|
||||
pub mod field;
|
||||
pub use field::*;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::errors::{FieldError, GroupError, IntegerError};
|
||||
use crate::errors::{FieldError, GroupError};
|
||||
use leo_types::IntegerError;
|
||||
|
||||
use std::{num::ParseIntError, str::ParseBoolError};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! The Import type for a Leo program.
|
||||
|
||||
use crate::Identifier;
|
||||
use leo_types::Identifier;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
|
@ -2,29 +2,13 @@
|
||||
//! Each defined type consists of typed statements and expressions.
|
||||
|
||||
use crate::Import;
|
||||
use leo_types::{Identifier, Integer, IntegerType};
|
||||
|
||||
use snarkos_models::gadgets::utilities::{
|
||||
boolean::Boolean,
|
||||
uint::{UInt128, UInt16, UInt32, UInt64, UInt8},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// An identifier in the constrained program.
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Identifier {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Identifier {
|
||||
pub fn new(name: String) -> Self {
|
||||
Self { name }
|
||||
}
|
||||
|
||||
pub fn is_self(&self) -> bool {
|
||||
self.name == "Self"
|
||||
}
|
||||
}
|
||||
|
||||
/// A variable that is assigned to a value in the constrained program
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Variable {
|
||||
@ -33,16 +17,6 @@ pub struct Variable {
|
||||
pub _type: Option<Type>,
|
||||
}
|
||||
|
||||
/// An integer type enum wrapping the integer value. Used only in expressions.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
|
||||
pub enum Integer {
|
||||
U8(UInt8),
|
||||
U16(UInt16),
|
||||
U32(UInt32),
|
||||
U64(UInt64),
|
||||
U128(UInt128),
|
||||
}
|
||||
|
||||
/// Range or expression enum
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RangeOrExpression {
|
||||
@ -111,16 +85,6 @@ pub enum Assignee {
|
||||
CircuitField(Box<Assignee>, Identifier), // (circuit name, circuit field name)
|
||||
}
|
||||
|
||||
/// Explicit integer type
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum IntegerType {
|
||||
U8,
|
||||
U16,
|
||||
U32,
|
||||
U64,
|
||||
U128,
|
||||
}
|
||||
|
||||
/// Explicit type used for defining a variable or expression type
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Type {
|
||||
@ -217,15 +181,6 @@ pub struct InputModel {
|
||||
pub _type: Type,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum InputValue {
|
||||
Integer(u128),
|
||||
Field(String),
|
||||
Group(String),
|
||||
Boolean(bool),
|
||||
Array(Vec<InputValue>),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Function {
|
||||
pub function_name: Identifier,
|
||||
|
@ -2,22 +2,12 @@
|
||||
|
||||
use crate::{
|
||||
Assignee, Circuit, CircuitMember, ConditionalNestedOrEnd, ConditionalStatement, Expression,
|
||||
Function, Identifier, InputModel, InputValue, Integer, IntegerType, RangeOrExpression,
|
||||
Function, InputModel, RangeOrExpression,
|
||||
SpreadOrExpression, Statement, Type, Variable,
|
||||
};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
impl fmt::Display for Identifier {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.name)
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for Identifier {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Variable {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
@ -35,12 +25,6 @@ impl fmt::Display for Variable {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Integer {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}{}", self.to_usize(), self.get_type())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for RangeOrExpression {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
@ -234,18 +218,6 @@ impl fmt::Display for Statement {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for IntegerType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
IntegerType::U8 => write!(f, "u8"),
|
||||
IntegerType::U16 => write!(f, "u16"),
|
||||
IntegerType::U32 => write!(f, "u32"),
|
||||
IntegerType::U64 => write!(f, "u64"),
|
||||
IntegerType::U128 => write!(f, "u128"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
@ -320,26 +292,6 @@ impl fmt::Display for InputModel {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for InputValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
InputValue::Integer(ref integer) => write!(f, "{}", integer),
|
||||
InputValue::Field(ref field) => write!(f, "{}", field),
|
||||
InputValue::Group(ref group) => write!(f, "{}", group),
|
||||
InputValue::Boolean(ref bool) => write!(f, "{}", bool),
|
||||
InputValue::Array(ref array) => {
|
||||
write!(f, "[")?;
|
||||
for (i, e) in array.iter().enumerate() {
|
||||
write!(f, "{}", e)?;
|
||||
if i < array.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Function {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
use crate::{types, Import, ImportSymbol};
|
||||
use leo_ast::{
|
||||
File,
|
||||
access::{
|
||||
Access,
|
||||
AssigneeAccess,
|
||||
@ -16,7 +15,7 @@ use leo_ast::{
|
||||
},
|
||||
common::{
|
||||
Assignee,
|
||||
Identifier,
|
||||
Identifier as AstIdentifier,
|
||||
RangeOrExpression as AstRangeOrExpression,
|
||||
SpreadOrExpression as AstSpreadOrExpression,
|
||||
Variable as AstVariable,
|
||||
@ -33,6 +32,7 @@ use leo_ast::{
|
||||
PostfixExpression,
|
||||
TernaryExpression
|
||||
},
|
||||
files::File,
|
||||
functions::{
|
||||
Function,
|
||||
FunctionInput,
|
||||
@ -62,7 +62,6 @@ use leo_ast::{
|
||||
ArrayType,
|
||||
CircuitType,
|
||||
DataType,
|
||||
IntegerType,
|
||||
Type as AstType
|
||||
},
|
||||
values::{
|
||||
@ -70,29 +69,20 @@ use leo_ast::{
|
||||
FieldValue,
|
||||
GroupValue,
|
||||
IntegerValue,
|
||||
NumberValue,
|
||||
NumberImplicitValue,
|
||||
Value
|
||||
}
|
||||
};
|
||||
use leo_types::{Identifier, Integer, IntegerType};
|
||||
|
||||
use snarkos_models::gadgets::utilities::{
|
||||
boolean::Boolean,
|
||||
uint::{UInt128, UInt16, UInt32, UInt64, UInt8},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// pest ast -> types::Identifier
|
||||
|
||||
impl<'ast> From<Identifier<'ast>> for types::Identifier {
|
||||
fn from(identifier: Identifier<'ast>) -> Self {
|
||||
types::Identifier::new(identifier.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<Identifier<'ast>> for types::Expression {
|
||||
fn from(identifier: Identifier<'ast>) -> Self {
|
||||
types::Expression::Identifier(types::Identifier::from(identifier))
|
||||
impl<'ast> From<AstIdentifier<'ast>> for types::Expression {
|
||||
fn from(identifier: AstIdentifier<'ast>) -> Self {
|
||||
types::Expression::Identifier(Identifier::from(identifier))
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,49 +91,18 @@ impl<'ast> From<Identifier<'ast>> for types::Expression {
|
||||
impl<'ast> From<AstVariable<'ast>> for types::Variable {
|
||||
fn from(variable: AstVariable<'ast>) -> Self {
|
||||
types::Variable {
|
||||
identifier: types::Identifier::from(variable.identifier),
|
||||
identifier: Identifier::from(variable.identifier),
|
||||
mutable: variable.mutable.is_some(),
|
||||
_type: variable._type.map(|_type| types::Type::from(_type)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// pest ast - types::Integer
|
||||
|
||||
impl<'ast> types::Integer {
|
||||
pub(crate) fn from(number: NumberValue<'ast>, _type: IntegerType) -> Self {
|
||||
match _type {
|
||||
IntegerType::U8Type(_u8) => types::Integer::U8(UInt8::constant(
|
||||
number.value.parse::<u8>().expect("unable to parse u8"),
|
||||
)),
|
||||
IntegerType::U16Type(_u16) => types::Integer::U16(UInt16::constant(
|
||||
number.value.parse::<u16>().expect("unable to parse u16"),
|
||||
)),
|
||||
IntegerType::U32Type(_u32) => types::Integer::U32(UInt32::constant(
|
||||
number
|
||||
.value
|
||||
.parse::<u32>()
|
||||
.expect("unable to parse integers.u32"),
|
||||
)),
|
||||
IntegerType::U64Type(_u64) => types::Integer::U64(UInt64::constant(
|
||||
number.value.parse::<u64>().expect("unable to parse u64"),
|
||||
)),
|
||||
IntegerType::U128Type(_u128) => types::Integer::U128(UInt128::constant(
|
||||
number.value.parse::<u128>().expect("unable to parse u128"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_implicit(number: String) -> Self {
|
||||
types::Integer::U128(UInt128::constant(
|
||||
number.parse::<u128>().expect("unable to parse u128"),
|
||||
))
|
||||
}
|
||||
}
|
||||
/// pest ast - Integer
|
||||
|
||||
impl<'ast> From<IntegerValue<'ast>> for types::Expression {
|
||||
fn from(field: IntegerValue<'ast>) -> Self {
|
||||
types::Expression::Integer(types::Integer::from(field.number, field._type))
|
||||
types::Expression::Integer(Integer::from(field.number, field._type))
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,7 +115,7 @@ impl<'ast> From<AstRangeOrExpression<'ast>> for types::RangeOrExpression {
|
||||
.map(|from| match types::Expression::from(from.0) {
|
||||
types::Expression::Integer(number) => number,
|
||||
types::Expression::Implicit(string) => {
|
||||
types::Integer::from_implicit(string)
|
||||
Integer::from_implicit(string)
|
||||
}
|
||||
expression => {
|
||||
unimplemented!("Range bounds should be integers, found {}", expression)
|
||||
@ -164,7 +123,7 @@ impl<'ast> From<AstRangeOrExpression<'ast>> for types::RangeOrExpression {
|
||||
});
|
||||
let to = range.to.map(|to| match types::Expression::from(to.0) {
|
||||
types::Expression::Integer(number) => number,
|
||||
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
||||
types::Expression::Implicit(string) => Integer::from_implicit(string),
|
||||
expression => {
|
||||
unimplemented!("Range bounds should be integers, found {}", expression)
|
||||
}
|
||||
@ -342,7 +301,7 @@ impl<'ast> From<ArrayInitializerExpression<'ast>> for types::Expression {
|
||||
impl<'ast> From<CircuitField<'ast>> for types::CircuitFieldDefinition {
|
||||
fn from(member: CircuitField<'ast>) -> Self {
|
||||
types::CircuitFieldDefinition {
|
||||
identifier: types::Identifier::from(member.identifier),
|
||||
identifier: Identifier::from(member.identifier),
|
||||
expression: types::Expression::from(member.expression),
|
||||
}
|
||||
}
|
||||
@ -350,7 +309,7 @@ impl<'ast> From<CircuitField<'ast>> for types::CircuitFieldDefinition {
|
||||
|
||||
impl<'ast> From<CircuitInlineExpression<'ast>> for types::Expression {
|
||||
fn from(expression: CircuitInlineExpression<'ast>) -> Self {
|
||||
let variable = types::Identifier::from(expression.identifier);
|
||||
let variable = Identifier::from(expression.identifier);
|
||||
let members = expression
|
||||
.members
|
||||
.into_iter()
|
||||
@ -364,7 +323,7 @@ impl<'ast> From<CircuitInlineExpression<'ast>> for types::Expression {
|
||||
impl<'ast> From<PostfixExpression<'ast>> for types::Expression {
|
||||
fn from(expression: PostfixExpression<'ast>) -> Self {
|
||||
let variable =
|
||||
types::Expression::Identifier(types::Identifier::from(expression.identifier));
|
||||
types::Expression::Identifier(Identifier::from(expression.identifier));
|
||||
|
||||
// ast::PostFixExpression contains an array of "accesses": `a(34)[42]` is represented as `[a, [Call(34), Select(42)]]`, but Access call expressions
|
||||
// are recursive, so it is `Select(Call(a, 34), 42)`. We apply this transformation here
|
||||
@ -393,12 +352,12 @@ impl<'ast> From<PostfixExpression<'ast>> for types::Expression {
|
||||
// Handle circuit member accesses
|
||||
Access::Object(circuit_object) => types::Expression::CircuitMemberAccess(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_object.identifier),
|
||||
Identifier::from(circuit_object.identifier),
|
||||
),
|
||||
Access::StaticObject(circuit_object) => {
|
||||
types::Expression::CircuitStaticFunctionAccess(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_object.identifier),
|
||||
Identifier::from(circuit_object.identifier),
|
||||
)
|
||||
}
|
||||
})
|
||||
@ -442,7 +401,7 @@ impl<'ast> types::Expression {
|
||||
// Assignee -> types::Expression for operator assign statements
|
||||
impl<'ast> From<Assignee<'ast>> for types::Expression {
|
||||
fn from(assignee: Assignee<'ast>) -> Self {
|
||||
let variable = types::Expression::Identifier(types::Identifier::from(assignee.identifier));
|
||||
let variable = types::Expression::Identifier(Identifier::from(assignee.identifier));
|
||||
|
||||
// we start with the id, and we fold the array of accesses by wrapping the current value
|
||||
assignee
|
||||
@ -452,7 +411,7 @@ impl<'ast> From<Assignee<'ast>> for types::Expression {
|
||||
AssigneeAccess::Member(circuit_member) => {
|
||||
types::Expression::CircuitMemberAccess(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_member.identifier),
|
||||
Identifier::from(circuit_member.identifier),
|
||||
)
|
||||
}
|
||||
AssigneeAccess::Array(array) => types::Expression::ArrayAccess(
|
||||
@ -465,9 +424,9 @@ impl<'ast> From<Assignee<'ast>> for types::Expression {
|
||||
|
||||
/// pest ast -> types::Assignee
|
||||
|
||||
impl<'ast> From<Identifier<'ast>> for types::Assignee {
|
||||
fn from(variable: Identifier<'ast>) -> Self {
|
||||
types::Assignee::Identifier(types::Identifier::from(variable))
|
||||
impl<'ast> From<AstIdentifier<'ast>> for types::Assignee {
|
||||
fn from(variable: AstIdentifier<'ast>) -> Self {
|
||||
types::Assignee::Identifier(Identifier::from(variable))
|
||||
}
|
||||
}
|
||||
|
||||
@ -486,7 +445,7 @@ impl<'ast> From<Assignee<'ast>> for types::Assignee {
|
||||
),
|
||||
AssigneeAccess::Member(circuit_field) => types::Assignee::CircuitField(
|
||||
Box::new(acc),
|
||||
types::Identifier::from(circuit_field.identifier),
|
||||
Identifier::from(circuit_field.identifier),
|
||||
),
|
||||
})
|
||||
}
|
||||
@ -630,17 +589,17 @@ impl<'ast> From<ForStatement<'ast>> for types::Statement {
|
||||
fn from(statement: ForStatement<'ast>) -> Self {
|
||||
let from = match types::Expression::from(statement.start) {
|
||||
types::Expression::Integer(number) => number,
|
||||
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
||||
types::Expression::Implicit(string) => Integer::from_implicit(string),
|
||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||
};
|
||||
let to = match types::Expression::from(statement.stop) {
|
||||
types::Expression::Integer(number) => number,
|
||||
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
||||
types::Expression::Implicit(string) => Integer::from_implicit(string),
|
||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||
};
|
||||
|
||||
types::Statement::For(
|
||||
types::Identifier::from(statement.index),
|
||||
Identifier::from(statement.index),
|
||||
from,
|
||||
to,
|
||||
statement
|
||||
@ -688,23 +647,11 @@ impl<'ast> From<Statement<'ast>> for types::Statement {
|
||||
|
||||
/// pest ast -> Explicit types::Type for defining circuit members and function params
|
||||
|
||||
impl From<IntegerType> for types::IntegerType {
|
||||
fn from(integer_type: IntegerType) -> Self {
|
||||
match integer_type {
|
||||
IntegerType::U8Type(_type) => types::IntegerType::U8,
|
||||
IntegerType::U16Type(_type) => types::IntegerType::U16,
|
||||
IntegerType::U32Type(_type) => types::IntegerType::U32,
|
||||
IntegerType::U64Type(_type) => types::IntegerType::U64,
|
||||
IntegerType::U128Type(_type) => types::IntegerType::U128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DataType> for types::Type {
|
||||
fn from(basic_type: DataType) -> Self {
|
||||
match basic_type {
|
||||
DataType::Integer(_type) => {
|
||||
types::Type::IntegerType(types::IntegerType::from(_type))
|
||||
types::Type::IntegerType(IntegerType::from(_type))
|
||||
}
|
||||
DataType::Field(_type) => types::Type::Field,
|
||||
DataType::Group(_type) => types::Type::Group,
|
||||
@ -728,7 +675,7 @@ impl<'ast> From<ArrayType<'ast>> for types::Type {
|
||||
|
||||
impl<'ast> From<CircuitType<'ast>> for types::Type {
|
||||
fn from(circuit_type: CircuitType<'ast>) -> Self {
|
||||
types::Type::Circuit(types::Identifier::from(circuit_type.identifier))
|
||||
types::Type::Circuit(Identifier::from(circuit_type.identifier))
|
||||
}
|
||||
}
|
||||
|
||||
@ -748,7 +695,7 @@ impl<'ast> From<AstType<'ast>> for types::Type {
|
||||
impl<'ast> From<CircuitFieldDefinition<'ast>> for types::CircuitMember {
|
||||
fn from(circuit_value: CircuitFieldDefinition<'ast>) -> Self {
|
||||
types::CircuitMember::CircuitField(
|
||||
types::Identifier::from(circuit_value.identifier),
|
||||
Identifier::from(circuit_value.identifier),
|
||||
types::Type::from(circuit_value._type),
|
||||
)
|
||||
}
|
||||
@ -778,7 +725,7 @@ impl<'ast> From<CircuitMember<'ast>> for types::CircuitMember {
|
||||
|
||||
impl<'ast> From<Circuit<'ast>> for types::Circuit {
|
||||
fn from(circuit: Circuit<'ast>) -> Self {
|
||||
let variable = types::Identifier::from(circuit.identifier);
|
||||
let variable = Identifier::from(circuit.identifier);
|
||||
let members = circuit
|
||||
.members
|
||||
.into_iter()
|
||||
@ -797,7 +744,7 @@ impl<'ast> From<Circuit<'ast>> for types::Circuit {
|
||||
impl<'ast> From<FunctionInput<'ast>> for types::InputModel {
|
||||
fn from(parameter: FunctionInput<'ast>) -> Self {
|
||||
types::InputModel {
|
||||
identifier: types::Identifier::from(parameter.identifier),
|
||||
identifier: Identifier::from(parameter.identifier),
|
||||
mutable: parameter.mutable.is_some(),
|
||||
// private by default
|
||||
private: parameter.visibility.map_or(true, |visibility| {
|
||||
@ -812,7 +759,7 @@ impl<'ast> From<FunctionInput<'ast>> for types::InputModel {
|
||||
|
||||
impl<'ast> From<Function<'ast>> for types::Function {
|
||||
fn from(function_definition: Function<'ast>) -> Self {
|
||||
let function_name = types::Identifier::from(function_definition.function_name);
|
||||
let function_name = Identifier::from(function_definition.function_name);
|
||||
let parameters = function_definition
|
||||
.parameters
|
||||
.into_iter()
|
||||
@ -843,8 +790,8 @@ impl<'ast> From<Function<'ast>> for types::Function {
|
||||
impl<'ast> From<AstImportSymbol<'ast>> for ImportSymbol {
|
||||
fn from(symbol: AstImportSymbol<'ast>) -> Self {
|
||||
ImportSymbol {
|
||||
symbol: types::Identifier::from(symbol.value),
|
||||
alias: symbol.alias.map(|alias| types::Identifier::from(alias)),
|
||||
symbol: Identifier::from(symbol.value),
|
||||
alias: symbol.alias.map(|alias| Identifier::from(alias)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -887,29 +834,29 @@ impl<'ast> types::Program {
|
||||
|
||||
file.circuits.into_iter().for_each(|circuit| {
|
||||
circuits.insert(
|
||||
types::Identifier::from(circuit.identifier.clone()),
|
||||
Identifier::from(circuit.identifier.clone()),
|
||||
types::Circuit::from(circuit),
|
||||
);
|
||||
});
|
||||
file.functions.into_iter().for_each(|function_def| {
|
||||
functions.insert(
|
||||
types::Identifier::from(function_def.function_name.clone()),
|
||||
Identifier::from(function_def.function_name.clone()),
|
||||
types::Function::from(function_def),
|
||||
);
|
||||
});
|
||||
file.tests.into_iter().for_each(|test_def| {
|
||||
tests.insert(
|
||||
types::Identifier::from(test_def.function.function_name.clone()),
|
||||
Identifier::from(test_def.function.function_name.clone()),
|
||||
types::Test::from(test_def),
|
||||
);
|
||||
});
|
||||
|
||||
if let Some(main_function) = functions.get(&types::Identifier::new("main".into())) {
|
||||
if let Some(main_function) = functions.get(&Identifier::new("main".into())) {
|
||||
num_parameters = main_function.inputs.len();
|
||||
}
|
||||
|
||||
types::Program {
|
||||
name: types::Identifier::new(name),
|
||||
name: Identifier::new(name),
|
||||
num_parameters,
|
||||
imports,
|
||||
circuits,
|
||||
|
13
types/Cargo.toml
Normal file
13
types/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "leo-types"
|
||||
version = "0.1.0"
|
||||
authors = ["The Aleo Team <hello@aleo.org>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
leo-ast = { path = "../ast", version = "0.1.0" }
|
||||
|
||||
snarkos-errors = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" }
|
||||
snarkos-models = { git = "ssh://git@github.com/AleoHQ/snarkOS.git", version = "0.8.0" }
|
||||
|
||||
thiserror = { version = "1.0" }
|
2
types/src/errors/mod.rs
Normal file
2
types/src/errors/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod integer;
|
||||
pub use integer::*;
|
36
types/src/identifier.rs
Normal file
36
types/src/identifier.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use leo_ast::common::Identifier as AstIdentifier;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// An identifier in the constrained program.
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Identifier {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Identifier {
|
||||
pub fn new(name: String) -> Self {
|
||||
Self { name }
|
||||
}
|
||||
|
||||
pub fn is_self(&self) -> bool {
|
||||
self.name == "Self"
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstIdentifier<'ast>> for Identifier {
|
||||
fn from(identifier: AstIdentifier<'ast>) -> Self {
|
||||
Identifier::new(identifier.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Identifier {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.name)
|
||||
}
|
||||
}
|
||||
impl fmt::Debug for Identifier {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.name)
|
||||
}
|
||||
}
|
31
types/src/input_value.rs
Normal file
31
types/src/input_value.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum InputValue {
|
||||
Integer(u128),
|
||||
Field(String),
|
||||
Group(String),
|
||||
Boolean(bool),
|
||||
Array(Vec<InputValue>),
|
||||
}
|
||||
|
||||
impl fmt::Display for InputValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
InputValue::Integer(ref integer) => write!(f, "{}", integer),
|
||||
InputValue::Field(ref field) => write!(f, "{}", field),
|
||||
InputValue::Group(ref group) => write!(f, "{}", group),
|
||||
InputValue::Boolean(ref bool) => write!(f, "{}", bool),
|
||||
InputValue::Array(ref array) => {
|
||||
write!(f, "[")?;
|
||||
for (i, e) in array.iter().enumerate() {
|
||||
write!(f, "{}", e)?;
|
||||
if i < array.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
//! Methods to enforce constraints on integers in a resolved Leo program.
|
||||
//! Conversion of integer declarations to constraints in Leo.
|
||||
|
||||
use crate::{
|
||||
errors::IntegerError,
|
||||
types::{InputValue, Integer},
|
||||
IntegerType,
|
||||
};
|
||||
use crate::{errors::IntegerError, IntegerType, InputValue};
|
||||
use leo_ast::{types::IntegerType as AstIntegerType, values::NumberValue};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
@ -21,6 +18,49 @@ use snarkos_models::{
|
||||
},
|
||||
};
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// An integer type enum wrapping the integer value. Used only in expressions.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
|
||||
pub enum Integer {
|
||||
U8(UInt8),
|
||||
U16(UInt16),
|
||||
U32(UInt32),
|
||||
U64(UInt64),
|
||||
U128(UInt128),
|
||||
}
|
||||
|
||||
impl<'ast> Integer {
|
||||
pub fn from(number: NumberValue<'ast>, _type: AstIntegerType) -> Self {
|
||||
match _type {
|
||||
AstIntegerType::U8Type(_u8) => Integer::U8(UInt8::constant(
|
||||
number.value.parse::<u8>().expect("unable to parse u8"),
|
||||
)),
|
||||
AstIntegerType::U16Type(_u16) => Integer::U16(UInt16::constant(
|
||||
number.value.parse::<u16>().expect("unable to parse u16"),
|
||||
)),
|
||||
AstIntegerType::U32Type(_u32) => Integer::U32(UInt32::constant(
|
||||
number
|
||||
.value
|
||||
.parse::<u32>()
|
||||
.expect("unable to parse integers.u32"),
|
||||
)),
|
||||
AstIntegerType::U64Type(_u64) => Integer::U64(UInt64::constant(
|
||||
number.value.parse::<u64>().expect("unable to parse u64"),
|
||||
)),
|
||||
AstIntegerType::U128Type(_u128) => Integer::U128(UInt128::constant(
|
||||
number.value.parse::<u128>().expect("unable to parse u128"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_implicit(number: String) -> Self {
|
||||
Integer::U128(UInt128::constant(
|
||||
number.parse::<u128>().expect("unable to parse u128"),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Integer {
|
||||
pub fn to_usize(&self) -> usize {
|
||||
match self {
|
||||
@ -32,7 +72,7 @@ impl Integer {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_type(&self) -> IntegerType {
|
||||
pub fn get_type(&self) -> IntegerType {
|
||||
match self {
|
||||
Integer::U8(_u8) => IntegerType::U8,
|
||||
Integer::U16(_u16) => IntegerType::U16,
|
||||
@ -42,7 +82,7 @@ impl Integer {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_input<F: Field, CS: ConstraintSystem<F>>(
|
||||
pub fn from_input<F: Field, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
integer_type: IntegerType,
|
||||
name: String,
|
||||
@ -125,7 +165,7 @@ impl Integer {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn add<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
pub fn add<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
other: Self,
|
||||
@ -202,7 +242,7 @@ impl Integer {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn sub<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
pub fn sub<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
other: Self,
|
||||
@ -279,7 +319,7 @@ impl Integer {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn mul<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
pub fn mul<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
other: Self,
|
||||
@ -356,7 +396,7 @@ impl Integer {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn div<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
pub fn div<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
other: Self,
|
||||
@ -433,7 +473,7 @@ impl Integer {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn pow<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
pub fn pow<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
other: Self,
|
||||
@ -579,3 +619,9 @@ impl<F: Field + PrimeField> CondSelectGadget<F> for Integer {
|
||||
unimplemented!("Cannot calculate cost.")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Integer {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}{}", self.to_usize(), self.get_type())
|
||||
}
|
||||
}
|
37
types/src/integer_type.rs
Normal file
37
types/src/integer_type.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use leo_ast::types::IntegerType as AstIntegerType;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// Explicit integer type
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum IntegerType {
|
||||
U8,
|
||||
U16,
|
||||
U32,
|
||||
U64,
|
||||
U128,
|
||||
}
|
||||
|
||||
impl From<AstIntegerType> for IntegerType {
|
||||
fn from(integer_type: AstIntegerType) -> Self {
|
||||
match integer_type {
|
||||
AstIntegerType::U8Type(_type) => IntegerType::U8,
|
||||
AstIntegerType::U16Type(_type) => IntegerType::U16,
|
||||
AstIntegerType::U32Type(_type) => IntegerType::U32,
|
||||
AstIntegerType::U64Type(_type) => IntegerType::U64,
|
||||
AstIntegerType::U128Type(_type) => IntegerType::U128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for IntegerType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
IntegerType::U8 => write!(f, "u8"),
|
||||
IntegerType::U16 => write!(f, "u16"),
|
||||
IntegerType::U32 => write!(f, "u32"),
|
||||
IntegerType::U64 => write!(f, "u64"),
|
||||
IntegerType::U128 => write!(f, "u128"),
|
||||
}
|
||||
}
|
||||
}
|
17
types/src/lib.rs
Normal file
17
types/src/lib.rs
Normal file
@ -0,0 +1,17 @@
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod errors;
|
||||
pub use errors::*;
|
||||
|
||||
pub mod identifier;
|
||||
pub use identifier::*;
|
||||
|
||||
pub mod input_value;
|
||||
pub use input_value::*;
|
||||
|
||||
pub mod integer;
|
||||
pub use integer::*;
|
||||
|
||||
pub mod integer_type;
|
||||
pub use integer_type::*;
|
Loading…
Reference in New Issue
Block a user