add field module

This commit is contained in:
collin 2020-07-07 19:40:01 -07:00
parent d575a187db
commit a6092e9a2f
37 changed files with 58 additions and 56 deletions

View File

@ -115,9 +115,9 @@ function main() -> u32 {
### Field Elements
```js
function main() -> field {
function main() -> value.field {
let a = 1000field; // explicit type
let a: field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // explicit type
let a: value.field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // explicit type
let b = a + 1; // implicit type
let c = b - 1;
let d = c * 4;
@ -173,7 +173,7 @@ function main() -> u32[2] {
// initialize an array copying a slice from `c`
let d = c[1..3];
// initialize a field array
// initialize a value.field array
let e = [5field; 2];
// initialize a boolean array
@ -248,7 +248,7 @@ function test1(a : u32) -> u32 {
return a + 1
}
function test2(b: fe) -> field {
function test2(b: fe) -> value.field {
return b * 2field
}
@ -264,12 +264,12 @@ function main() -> u32 {
### Function Scope
```js
function foo() -> field {
function foo() -> value.field {
// return myGlobal <- not allowed
return 42field
}
function main() -> field {
function main() -> value.field {
let myGlobal = 42field;
return foo()
}
@ -293,7 +293,7 @@ function main() -> u32[3] {
Main function inputs are allocated private variables in the program's constraint system.
`a` is implicitly private.
```js
function main(a: field) -> field {
function main(a: value.field) -> value.field {
return a
}
```
@ -578,7 +578,7 @@ function main(a: u32, b: u32) -> u32 {
[main]
a: bool = true; // <- booleans
b: u8 = 2; // <- integers
c: field = 0; // <- fields
c: value.field = 0; // <- fields
d: group = (0, 1)group // <- group tuples
```

View File

@ -13,7 +13,7 @@ impl From<Error<Rule>> for SyntaxError {
error = error.renamed_rules(|rule| match *rule {
Rule::LINE_END => "`;`".to_owned(),
Rule::type_integer => "`u32`".to_owned(),
Rule::type_field => "`field`".to_owned(),
Rule::type_field => "`value.field`".to_owned(),
Rule::type_group => "`group`".to_owned(),
Rule::file => "an import, circuit, or function".to_owned(),
Rule::identifier => "a variable name".to_owned(),

View File

@ -111,7 +111,7 @@ type_u64 = { "u64" }
type_u128 = { "u128" }
// Declared in types/field_type.rs
type_field = { "field" }
type_field = { "value.field" }
// Declared in types/group_type.rs
type_group = { "group" }

View File

@ -3,9 +3,9 @@
use crate::{
address::Address,
constraints::{field::field_from_input, group::group_from_input, new_scope, ConstrainedProgram},
constraints::{group::group_from_input, new_scope, ConstrainedProgram},
errors::{FunctionError, StatementError},
value::{boolean::input::bool_from_input, ConstrainedValue},
value::{boolean::input::bool_from_input, field::input::field_from_input, ConstrainedValue},
GroupType,
Integer,
};

View File

@ -6,8 +6,6 @@ pub use self::function::*;
pub mod expression;
pub use self::expression::*;
pub mod field;
pub mod generate_constraints;
pub use self::generate_constraints::*;

View File

@ -147,7 +147,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
match self.get_mutable_assignee(circuit_name, span.clone())? {
ConstrainedValue::CircuitExpression(_variable, members) => {
// Modify the circuit field in place
// Modify the circuit value.field in place
let matched_field = members.into_iter().find(|object| object.0 == object_name);
match matched_field {

View File

@ -22,7 +22,7 @@ impl FieldError {
pub fn cannot_enforce(operation: String, error: SynthesisError, span: Span) -> Self {
let message = format!(
"the field binary operation `{}` failed due to synthesis error `{}`",
"the value.field binary operation `{}` failed due to synthesis error `{}`",
operation, error,
);
@ -30,25 +30,25 @@ impl FieldError {
}
pub fn invalid_field(actual: String, span: Span) -> Self {
let message = format!("expected field element input type, found `{}`", actual);
let message = format!("expected value.field element input type, found `{}`", actual);
Self::new_from_span(message, span)
}
pub fn missing_field(expected: String, span: Span) -> Self {
let message = format!("expected field input `{}` not found", expected);
let message = format!("expected value.field input `{}` not found", expected);
Self::new_from_span(message, span)
}
pub fn no_inverse(field: String, span: Span) -> Self {
let message = format!("no multiplicative inverse found for field `{}`", field);
let message = format!("no multiplicative inverse found for value.field `{}`", field);
Self::new_from_span(message, span)
}
pub fn synthesis_error(error: SynthesisError, span: Span) -> Self {
let message = format!("compilation failed due to field synthesis error `{}`", error);
let message = format!("compilation failed due to value.field synthesis error `{}`", error);
Self::new_from_span(message, span)
}

View File

@ -15,9 +15,6 @@ pub mod definitions;
pub mod errors;
pub mod field;
pub use self::field::*;
pub mod group;
pub use self::group::*;

View File

@ -1,4 +1,4 @@
//! A data type that represents a field value
//! A data type that represents a value.field value
use crate::{
comparator::{ComparatorGadget, EvaluateLtGadget},

View File

@ -1,4 +1,4 @@
//! Methods to enforce constraints on field elements in a resolved Leo program.
//! Methods to enforce constraints on value.field elements in a resolved Leo program.
use crate::{errors::FieldError, value::ConstrainedValue, FieldType, GroupType};
use leo_types::{InputValue, Span};
@ -15,7 +15,7 @@ pub(crate) fn allocate_field<F: Field + PrimeField, CS: ConstraintSystem<F>>(
option: Option<String>,
span: Span,
) -> Result<FieldType<F>, FieldError> {
let field_name = format!("{}: field", name);
let field_name = format!("{}: value.field", name);
let field_name_unique = format!("`{}` {}:{}", field_name, span.line, span.start);
FieldType::alloc(cs.ns(|| field_name_unique), || {

View File

@ -0,0 +1,4 @@
pub mod input;
pub mod field_type;
pub use self::field_type::*;

View File

@ -5,5 +5,8 @@ pub(crate) mod comparator;
pub mod integer;
pub use self::integer::*;
pub mod field;
pub use self::field::*;
pub mod value;
pub use self::value::*;

View File

@ -180,7 +180,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
}
ConstrainedValue::Field(field) => {
let gadget = field
.allocated(cs.ns(|| format!("allocate field {}:{}", span.line, span.start)))
.allocated(cs.ns(|| format!("allocate value.field {}:{}", span.line, span.start)))
.map_err(|error| ValueError::FieldError(FieldError::synthesis_error(error, span)))?;
*field = FieldType::Allocated(gadget)

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> field {
function main(a: value.field, b: value.field) -> value.field {
return a + b
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) {
function main(a: value.field, b: value.field) {
assert_eq!(a, b);
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> field {
function main(a: value.field, b: value.field) -> value.field {
return a / b
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> bool {
function main(a: value.field, b: value.field) -> bool {
return a == b
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> bool {
function main(a: value.field, b: value.field) -> bool {
return a >= b
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> bool {
function main(a: value.field, b: value.field) -> bool {
return a > b
}

View File

@ -1,3 +1,3 @@
function main(f: field) -> field{
function main(f: value.field) -> value.field{
return f
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> bool {
function main(a: value.field, b: value.field) -> bool {
return a <= b
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> bool {
function main(a: value.field, b: value.field) -> bool {
return a < b
}

View File

@ -55,7 +55,7 @@ fn output_one(program: EdwardsTestCompiler) {
fn fail_field(program: EdwardsTestCompiler) {
match get_error(program) {
CompilerError::FunctionError(FunctionError::FieldError(FieldError::Error(_string))) => {}
error => panic!("Expected invalid field error, got {}", error),
error => panic!("Expected invalid value.field error, got {}", error),
}
}
@ -450,7 +450,7 @@ fn test_ternary() {
let mut program_1 = parse_program(bytes).unwrap();
let mut program_2 = program_1.clone();
// true -> field 1
// true -> value.field 1
program_1.set_inputs(vec![
Some(InputValue::Boolean(true)),
Some(InputValue::Field(r1.to_string())),
@ -459,7 +459,7 @@ fn test_ternary() {
output_expected_allocated(program_1, g1);
// false -> field 2
// false -> value.field 2
program_2.set_inputs(vec![
Some(InputValue::Boolean(false)),
Some(InputValue::Field(r1.to_string())),

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> field {
function main(a: value.field, b: value.field) -> value.field {
return a * b
}

View File

@ -1,3 +1,3 @@
function main() -> field {
function main() -> value.field {
return 1field
}

View File

@ -1,3 +1,3 @@
function main(a: field, b: field) -> field {
function main(a: value.field, b: value.field) -> value.field {
return a - b
}

View File

@ -1,3 +1,3 @@
function main(b: bool, f1: field, f2: field) -> field {
function main(b: bool, f1: value.field, f2: value.field) -> value.field {
return if b ? f1 : f2
}

View File

@ -1,3 +1,3 @@
function main() -> field {
function main() -> value.field {
return 0field
}

View File

@ -1,8 +1,8 @@
function foo() -> field {
function foo() -> value.field {
return myGlobal
}
function main() -> field {
function main() -> value.field {
const myGlobal = 42field;
return foo()
}

View File

@ -364,7 +364,7 @@ macro_rules! test_uint {
let mut program_2 = program_1.clone();
// true -> field 1
// true -> value.field 1
program_1.set_inputs(vec![
Some(InputValue::Boolean(true)),
Some(InputValue::Integer($integer_type, r1 as u128)),
@ -373,7 +373,7 @@ macro_rules! test_uint {
output_expected_allocated(program_1, g1);
// false -> field 2
// false -> value.field 2
program_2.set_inputs(vec![
Some(InputValue::Boolean(false)),
Some(InputValue::Integer($integer_type, r1 as u128)),

View File

@ -1,4 +1,4 @@
// The program inputs for square_root/src/main.leo
[main]
a: field = 337;
b: field = 113569;
a: value.field = 337;
b: value.field = 113569;

View File

@ -1,5 +1,5 @@
// The 'square_root' main function.
// prove knowledge of the square root `a` of a number `b`:
function main(a: field, b: field) -> bool {
function main(a: value.field, b: value.field) -> bool {
return a * a == b
}

View File

@ -13,7 +13,7 @@ impl From<Error<Rule>> for SyntaxError {
error = error.renamed_rules(|rule| match *rule {
Rule::LINE_END => "`;`".to_owned(),
Rule::type_integer => "`u32`".to_owned(),
Rule::type_field => "`field`".to_owned(),
Rule::type_field => "`value.field`".to_owned(),
Rule::type_group => "`group`".to_owned(),
Rule::file => "an import, circuit, or function".to_owned(),
Rule::identifier => "a variable name".to_owned(),

View File

@ -28,7 +28,7 @@ type_u64 = { "u64" }
type_u128 = { "u128" }
// Declared in types/field_type.rs
type_field = { "field" }
type_field = { "value.field" }
// Declared in types/group_type.rs
type_group = { "group" }

View File

@ -21,7 +21,7 @@ impl std::fmt::Display for DataType {
match self {
DataType::Address(_) => write!(f, "address"),
DataType::Boolean(_) => write!(f, "bool"),
DataType::Field(_) => write!(f, "field"),
DataType::Field(_) => write!(f, "value.field"),
DataType::Group(_) => write!(f, "group"),
DataType::Integer(ref integer) => write!(f, "{}", integer),
}

View File

@ -12,7 +12,7 @@ use std::fmt;
pub enum Assignee {
Identifier(Identifier),
Array(Box<Assignee>, RangeOrExpression),
CircuitField(Box<Assignee>, Identifier), // (circuit name, circuit field name)
CircuitField(Box<Assignee>, Identifier), // (circuit name, circuit value.field name)
}
impl<'ast> From<AstIdentifier<'ast>> for Assignee {

View File

@ -113,7 +113,7 @@ impl fmt::Display for Type {
match *self {
Type::Address => write!(f, "address"),
Type::Boolean => write!(f, "bool"),
Type::Field => write!(f, "field"),
Type::Field => write!(f, "value.field"),
Type::Group => write!(f, "group"),
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
Type::Circuit(ref variable) => write!(f, "circuit {}", variable),