fix index evaluation

This commit is contained in:
collin 2020-06-22 18:28:30 -07:00
parent eeaf1b4ae8
commit 5865149c06
18 changed files with 127 additions and 249 deletions

View File

@ -21,7 +21,7 @@ LINE_END = { ";" ~ NEWLINE* }
mutable = { "mut" } mutable = { "mut" }
// Declared in common/range.rs // Declared in common/range.rs
range = { from_expression? ~ ".." ~ to_expression } range = { from_expression? ~ ".." ~ to_expression? }
from_expression = { expression } from_expression = { expression }
to_expression = { expression } to_expression = { expression }

View File

@ -554,7 +554,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
index, index,
span.clone(), span.clone(),
)? { )? {
ConstrainedValue::Integer(number) => Ok(number.to_usize()), ConstrainedValue::Integer(number) => Ok(number.to_usize(span.clone())?),
value => Err(ExpressionError::invalid_index(value.to_string(), span)), value => Err(ExpressionError::invalid_index(value.to_string(), span)),
} }
} }
@ -584,11 +584,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
match index { match index {
RangeOrExpression::Range(from, to) => { RangeOrExpression::Range(from, to) => {
let from_resolved = match from { let from_resolved = match from {
Some(from_index) => from_index.to_usize(), Some(from_index) => from_index.to_usize(span.clone())?,
None => 0usize, // Array slice starts at index 0 None => 0usize, // Array slice starts at index 0
}; };
let to_resolved = match to { let to_resolved = match to {
Some(to_index) => to_index.to_usize(), Some(to_index) => to_index.to_usize(span.clone())?,
None => array.len(), // Array slice ends at array length None => array.len(), // Array slice ends at array length
}; };
Ok(ConstrainedValue::Array(array[from_resolved..to_resolved].to_owned())) Ok(ConstrainedValue::Array(array[from_resolved..to_resolved].to_owned()))

View File

@ -74,7 +74,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
// Modify the single value of the array in place // Modify the single value of the array in place
match self.get_mutable_assignee(name, span.clone())? { match self.get_mutable_assignee(name, span.clone())? {
ConstrainedValue::Array(old) => { ConstrainedValue::Array(old) => {
new_value.resolve_type(&vec![old[index].to_type()], span.clone())?; new_value.resolve_type(&vec![old[index].to_type(span.clone())?], span.clone())?;
let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let selected_value = ConstrainedValue::conditionally_select( let selected_value = ConstrainedValue::conditionally_select(
@ -94,11 +94,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
} }
RangeOrExpression::Range(from, to) => { RangeOrExpression::Range(from, to) => {
let from_index = match from { let from_index = match from {
Some(integer) => integer.to_usize(), Some(integer) => integer.to_usize(span.clone())?,
None => 0usize, None => 0usize,
}; };
let to_index_option = match to { let to_index_option = match to {
Some(integer) => Some(integer.to_usize()), Some(integer) => Some(integer.to_usize(span.clone())?),
None => None, None => None,
}; };
@ -153,7 +153,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
return Err(StatementError::immutable_circuit_function("static".into(), span)); return Err(StatementError::immutable_circuit_function("static".into(), span));
} }
_ => { _ => {
new_value.resolve_type(&vec![object.1.to_type()], span.clone())?; new_value.resolve_type(&vec![object.1.to_type(span.clone())?], span.clone())?;
let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let selected_value = ConstrainedValue::conditionally_select( let selected_value = ConstrainedValue::conditionally_select(
@ -201,7 +201,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
let condition = indicator.unwrap_or(Boolean::Constant(true)); let condition = indicator.unwrap_or(Boolean::Constant(true));
let old_value = self.get_mutable_assignee(variable_name.clone(), span.clone())?; let old_value = self.get_mutable_assignee(variable_name.clone(), span.clone())?;
new_value.resolve_type(&vec![old_value.to_type()], span.clone())?; new_value.resolve_type(&vec![old_value.to_type(span.clone())?], span.clone())?;
let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let selected_value = let selected_value =
@ -496,8 +496,10 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span: Span, span: Span,
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> { ) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
let mut res = None; let mut res = None;
let from = start.to_usize(span.clone())?;
let to = stop.to_usize(span.clone())?;
for i in start.to_usize()..stop.to_usize() { for i in from..to {
// Store index in current function scope. // Store index in current function scope.
// For loop scope is not implemented. // For loop scope is not implemented.
let index_name = new_scope(function_scope.clone(), index.to_string()); let index_name = new_scope(function_scope.clone(), index.to_string());

View File

@ -46,7 +46,7 @@ pub enum ConstrainedValue<F: Field + PrimeField, G: GroupType<F>> {
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> { impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
pub(crate) fn from_other(value: String, other: &ConstrainedValue<F, G>, span: Span) -> Result<Self, ValueError> { pub(crate) fn from_other(value: String, other: &ConstrainedValue<F, G>, span: Span) -> Result<Self, ValueError> {
let other_type = other.to_type(); let other_type = other.to_type(span.clone())?;
ConstrainedValue::from_type(value, &other_type, span) ConstrainedValue::from_type(value, &other_type, span)
} }
@ -66,14 +66,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
} }
} }
pub(crate) fn to_type(&self) -> Type { pub(crate) fn to_type(&self, span: Span) -> Result<Type, ValueError> {
match self { Ok(match self {
ConstrainedValue::Integer(integer) => Type::IntegerType(integer.get_type()), ConstrainedValue::Integer(integer) => Type::IntegerType(integer.get_type()),
ConstrainedValue::Field(_field) => Type::Field, ConstrainedValue::Field(_field) => Type::Field,
ConstrainedValue::Group(_group) => Type::Group, ConstrainedValue::Group(_group) => Type::Group,
ConstrainedValue::Boolean(_bool) => Type::Boolean, ConstrainedValue::Boolean(_bool) => Type::Boolean,
_ => unimplemented!("to type only implemented for primitives"), value => return Err(ValueError::implicit(value.to_string(), span)),
} })
} }
pub(crate) fn resolve_type(&mut self, types: &Vec<Type>, span: Span) -> Result<(), ValueError> { pub(crate) fn resolve_type(&mut self, types: &Vec<Type>, span: Span) -> Result<(), ValueError> {
@ -95,8 +95,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
match (&self, &other) { match (&self, &other) {
(ConstrainedValue::Unresolved(_), ConstrainedValue::Unresolved(_)) => Ok(()), (ConstrainedValue::Unresolved(_), ConstrainedValue::Unresolved(_)) => Ok(()),
(ConstrainedValue::Unresolved(_), _) => self.resolve_type(&vec![other.to_type()], span), (ConstrainedValue::Unresolved(_), _) => self.resolve_type(&vec![other.to_type(span.clone())?], span),
(_, ConstrainedValue::Unresolved(_)) => other.resolve_type(&vec![self.to_type()], span), (_, ConstrainedValue::Unresolved(_)) => other.resolve_type(&vec![self.to_type(span.clone())?], span),
_ => Ok(()), _ => Ok(()),
} }
} }
@ -141,7 +141,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
.iter_mut() .iter_mut()
.enumerate() .enumerate()
.map(|(i, value)| { .map(|(i, value)| {
value.allocate_value(cs.ns(|| format!("allocate array member {}", i)), span.clone()) let unique_name = format!("allocate array member {} {}:{}", i, span.line, span.start);
value.allocate_value(cs.ns(|| unique_name), span.clone())
}) })
.collect::<Result<(), ValueError>>()?; .collect::<Result<(), ValueError>>()?;
} }
@ -150,9 +152,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
.iter_mut() .iter_mut()
.enumerate() .enumerate()
.map(|(i, member)| { .map(|(i, member)| {
member let unique_name = format!("allocate circuit member {} {}:{}", i, span.line, span.start);
.1
.allocate_value(cs.ns(|| format!("allocate circuit member {}", i)), span.clone()) member.1.allocate_value(cs.ns(|| unique_name), span.clone())
}) })
.collect::<Result<(), ValueError>>()?; .collect::<Result<(), ValueError>>()?;
} }
@ -161,7 +163,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
.iter_mut() .iter_mut()
.enumerate() .enumerate()
.map(|(i, value)| { .map(|(i, value)| {
value.allocate_value(cs.ns(|| format!("allocate return member {}", i)), span.clone()) let unique_name = format!("allocate return member {} {}:{}", i, span.line, span.start);
value.allocate_value(cs.ns(|| unique_name), span.clone())
}) })
.collect::<Result<(), ValueError>>()?; .collect::<Result<(), ValueError>>()?;
} }
@ -220,10 +224,10 @@ impl<F: Field + PrimeField, G: GroupType<F>> fmt::Display for ConstrainedValue<F
} }
write!(f, "]") write!(f, "]")
} }
ConstrainedValue::CircuitDefinition(ref _definition) => { ConstrainedValue::CircuitDefinition(ref circuit) => write!(f, "circuit {{ {} }}", circuit.identifier),
unimplemented!("cannot return circuit definition in program") ConstrainedValue::Function(ref _circuit_option, ref function) => {
write!(f, "function {{ {}() }}", function.function_name)
} }
ConstrainedValue::Function(ref _circuit_option, ref function) => write!(f, "{}", function),
ConstrainedValue::Mutable(ref value) => write!(f, "mut {}", value), ConstrainedValue::Mutable(ref value) => write!(f, "mut {}", value),
ConstrainedValue::Static(ref value) => write!(f, "static {}", value), ConstrainedValue::Static(ref value) => write!(f, "static {}", value),
ConstrainedValue::Unresolved(ref value) => write!(f, "unresolved {}", value), ConstrainedValue::Unresolved(ref value) => write!(f, "unresolved {}", value),
@ -343,6 +347,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> CondSelectGadget<F> for Constrained
} }
ConstrainedValue::Array(array) ConstrainedValue::Array(array)
} }
(ConstrainedValue::Mutable(first), _) => Self::conditionally_select(cs, cond, first, second)?,
(_, ConstrainedValue::Mutable(second)) => Self::conditionally_select(cs, cond, first, second)?,
(_, _) => return Err(SynthesisError::Unsatisfiable), (_, _) => return Err(SynthesisError::Unsatisfiable),
}) })
} }

View File

@ -1,5 +1,5 @@
use crate::errors::{BooleanError, ExpressionError, ValueError}; use crate::errors::{BooleanError, ExpressionError, ValueError};
use leo_types::{Error as FormattedError, Span}; use leo_types::{Error as FormattedError, IntegerError, Span};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum StatementError { pub enum StatementError {
@ -12,6 +12,9 @@ pub enum StatementError {
#[error("{}", _0)] #[error("{}", _0)]
ExpressionError(#[from] ExpressionError), ExpressionError(#[from] ExpressionError),
#[error("{}", _0)]
IntegerError(#[from] IntegerError),
#[error("{}", _0)] #[error("{}", _0)]
ValueError(#[from] ValueError), ValueError(#[from] ValueError),
} }

View File

@ -276,14 +276,12 @@ fn test_self() {
// All // All
// #[test] #[test]
// fn test_pedersen_mock() { fn test_pedersen_mock() {
// use crate::integers::u32::output_zero; use crate::integers::u32::output_zero;
// let bytes = include_bytes!("pedersen_mock.leo");
// let program = parse_program(bytes).unwrap(); let bytes = include_bytes!("pedersen_mock.leo");
// let program = parse_program(bytes).unwrap();
// let err = get_error(program);
// output_zero(program);
// println!("{}", err); }
// // output_zero(program);
// }

View File

@ -0,0 +1,13 @@
function one() -> u32 {
return 1u32
}
function main() -> u32 {
let mut a = 0u32;
for i in 0..10 {
a += one();
}
return a
}

View File

@ -1,7 +1,8 @@
use crate::{ use crate::{
boolean::output_true,
get_error, get_error,
get_output, get_output,
integers::u32::output_one, integers::u32::{output_number, output_one},
parse_program, parse_program,
EdwardsConstrainedValue, EdwardsConstrainedValue,
EdwardsTestCompiler, EdwardsTestCompiler,
@ -11,7 +12,6 @@ use leo_compiler::{
ConstrainedValue, ConstrainedValue,
}; };
use crate::boolean::output_true;
use snarkos_models::gadgets::utilities::boolean::Boolean; use snarkos_models::gadgets::utilities::boolean::Boolean;
pub(crate) fn output_empty(program: EdwardsTestCompiler) { pub(crate) fn output_empty(program: EdwardsTestCompiler) {
@ -120,3 +120,18 @@ fn test_repeated_function_call() {
output_true(program); output_true(program);
} }
#[test]
fn test_iteration() {
let bytes = include_bytes!("iteration.leo");
let program = parse_program(bytes).unwrap();
output_number(program, 10);
}
#[test]
fn test_repeated_iteration() {
let bytes = include_bytes!("repeated_iteration.leo");
let program = parse_program(bytes).unwrap();
output_number(program, 20u32);
}

View File

@ -0,0 +1,13 @@
function iteration() -> u32 {
let mut a = 0u32;
for i in 0..10 {
a += 1;
}
return a
}
function main() -> u32 {
return iteration() + iteration()
}

View File

@ -65,7 +65,5 @@ fn test_num_returns_fail() {
let bytes = include_bytes!("num_returns_fail.leo"); let bytes = include_bytes!("num_returns_fail.leo");
let program = parse_program(bytes).unwrap(); let program = parse_program(bytes).unwrap();
let error = get_error(program); let _ = get_error(program);
println!("{}", error);
} }

View File

View File

@ -1,27 +1,16 @@
function fibonacci(i: u32) -> u32 { function fibonacci() -> u32 {
if i == 0 { let mut a = 0u32;
return 0 let mut b = 1u32;
} else if i == 1 {
return 1 for i in 0..10 {
} else { a = b;
return fibonacci(i - 1) + fibonacci(i - 2) b = a + b;
} }
return a
} }
// The 'fibonacci' main function. // The 'fibonacci' main function.
function main() -> u32 { function main() -> u32 {
return fibonacci(1) return fibonacci()
}
Function mutateNoLet(b: bool) {
let mut a = 5;
if b {
// must be turned into statements.conditional expression
a = 0;
// a = if b ? 0 : a;
} else {
a = 3;
// a = if b ? a : 3;
}
} }

View File

@ -1,5 +1,5 @@
// The 'hello_world' main function. // The 'hello_world' main function.
function main() -> u32 { function main() -> u32 {
let a = 1 + 1; let a: u32 = 1 + 1;
return a return a
} }

View File

@ -34,7 +34,6 @@ impl LeoInputsParser {
// Build the abstract syntax tree // Build the abstract syntax tree
let syntax_tree = files::File::from_pest(&mut file).map_err(|_| InputParserError::SyntaxTreeError)?; let syntax_tree = files::File::from_pest(&mut file).map_err(|_| InputParserError::SyntaxTreeError)?;
// println!("{:?}", syntax_tree);
Ok(syntax_tree) Ok(syntax_tree)
} }

View File

@ -1,4 +1,5 @@
use crate::{error::Error as FormattedError, Span}; use crate::{error::Error as FormattedError, Span};
use snarkos_errors::gadgets::SynthesisError; use snarkos_errors::gadgets::SynthesisError;
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -30,6 +31,13 @@ impl IntegerError {
Self::new_from_span(message, span) Self::new_from_span(message, span)
} }
pub fn invalid_index(span: Span) -> Self {
let message =
format!("index must be a constant value integer. allocated indices produce a circuit of unknown size");
Self::new_from_span(message, span)
}
pub fn invalid_integer(actual: String, span: Span) -> Self { pub fn invalid_integer(actual: String, span: Span) -> Self {
let message = format!("expected integer input type, found `{}`", actual); let message = format!("expected integer input type, found `{}`", actual);

View File

@ -230,9 +230,7 @@ impl<'ast> From<PostfixExpression<'ast>> for Expression {
// Handle function calls // Handle function calls
Access::Call(function) => { Access::Call(function) => {
println!("old {:?}", function.span);
let span = Span::from(function.span); let span = Span::from(function.span);
println!("span {:?}", span);
Expression::FunctionCall( Expression::FunctionCall(
Box::new(acc), Box::new(acc),
function function

View File

@ -108,8 +108,9 @@ impl Integer {
} }
} }
pub fn to_usize(&self) -> usize { pub fn to_usize(&self, span: Span) -> Result<usize, IntegerError> {
self.get_value().unwrap() as usize let value = self.get_value().ok_or(IntegerError::invalid_index(span))?;
Ok(value as usize)
} }
pub fn get_type(&self) -> IntegerType { pub fn get_type(&self) -> IntegerType {
@ -216,67 +217,34 @@ impl Integer {
other: Self, other: Self,
span: Span, span: Span,
) -> Result<Self, IntegerError> { ) -> Result<Self, IntegerError> {
let unique_namespace = format!("enforce {} + {} {}:{}", self, other, span.line, span.start);
Ok(match (self, other) { Ok(match (self, other) {
(Integer::U8(left_u8), Integer::U8(right_u8)) => { (Integer::U8(left_u8), Integer::U8(right_u8)) => {
let unique_namespace = format!(
"enforce {} + {} {}:{}",
left_u8.value.unwrap(),
right_u8.value.unwrap(),
span.line,
span.start
);
let result = UInt8::addmany(cs.ns(|| unique_namespace), &[left_u8, right_u8]) let result = UInt8::addmany(cs.ns(|| unique_namespace), &[left_u8, right_u8])
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
Integer::U8(result) Integer::U8(result)
} }
(Integer::U16(left_u16), Integer::U16(right_u16)) => { (Integer::U16(left_u16), Integer::U16(right_u16)) => {
let unique_namespace = format!(
"enforce {} + {} {}:{}",
left_u16.value.unwrap(),
right_u16.value.unwrap(),
span.line,
span.start
);
let result = UInt16::addmany(cs.ns(|| unique_namespace), &[left_u16, right_u16]) let result = UInt16::addmany(cs.ns(|| unique_namespace), &[left_u16, right_u16])
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
Integer::U16(result) Integer::U16(result)
} }
(Integer::U32(left_u32), Integer::U32(right_u32)) => { (Integer::U32(left_u32), Integer::U32(right_u32)) => {
let unique_namespace = format!(
"enforce {} + {} {}:{}",
left_u32.value.unwrap(),
right_u32.value.unwrap(),
span.line,
span.start
);
let result = UInt32::addmany(cs.ns(|| unique_namespace), &[left_u32, right_u32]) let result = UInt32::addmany(cs.ns(|| unique_namespace), &[left_u32, right_u32])
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
Integer::U32(result) Integer::U32(result)
} }
(Integer::U64(left_u64), Integer::U64(right_u64)) => { (Integer::U64(left_u64), Integer::U64(right_u64)) => {
let unique_namespace = format!(
"enforce {} + {} {}:{}",
left_u64.value.unwrap(),
right_u64.value.unwrap(),
span.line,
span.start
);
let result = UInt64::addmany(cs.ns(|| unique_namespace), &[left_u64, right_u64]) let result = UInt64::addmany(cs.ns(|| unique_namespace), &[left_u64, right_u64])
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
Integer::U64(result) Integer::U64(result)
} }
(Integer::U128(left_u128), Integer::U128(right_u128)) => { (Integer::U128(left_u128), Integer::U128(right_u128)) => {
let unique_namespace = format!(
"enforce {} + {} {}:{}",
left_u128.value.unwrap(),
right_u128.value.unwrap(),
span.line,
span.start
);
let result = UInt128::addmany(cs.ns(|| unique_namespace), &[left_u128, right_u128]) let result = UInt128::addmany(cs.ns(|| unique_namespace), &[left_u128, right_u128])
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
@ -292,15 +260,10 @@ impl Integer {
other: Self, other: Self,
span: Span, span: Span,
) -> Result<Self, IntegerError> { ) -> Result<Self, IntegerError> {
let unique_namespace = format!("enforce {} - {} {}:{}", self, other, span.line, span.start);
Ok(match (self, other) { Ok(match (self, other) {
(Integer::U8(left_u8), Integer::U8(right_u8)) => { (Integer::U8(left_u8), Integer::U8(right_u8)) => {
let unique_namespace = format!(
"enforce {} - {} {}:{}",
left_u8.value.unwrap(),
right_u8.value.unwrap(),
span.line,
span.start
);
let result = left_u8 let result = left_u8
.sub(cs.ns(|| unique_namespace), &right_u8) .sub(cs.ns(|| unique_namespace), &right_u8)
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
@ -308,13 +271,6 @@ impl Integer {
Integer::U8(result) Integer::U8(result)
} }
(Integer::U16(left_u16), Integer::U16(right_u16)) => { (Integer::U16(left_u16), Integer::U16(right_u16)) => {
let unique_namespace = format!(
"enforce {} - {} {}:{}",
left_u16.value.unwrap(),
right_u16.value.unwrap(),
span.line,
span.start
);
let result = left_u16 let result = left_u16
.sub(cs.ns(|| unique_namespace), &right_u16) .sub(cs.ns(|| unique_namespace), &right_u16)
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
@ -322,13 +278,6 @@ impl Integer {
Integer::U16(result) Integer::U16(result)
} }
(Integer::U32(left_u32), Integer::U32(right_u32)) => { (Integer::U32(left_u32), Integer::U32(right_u32)) => {
let unique_namespace = format!(
"enforce {} - {} {}:{}",
left_u32.value.unwrap(),
right_u32.value.unwrap(),
span.line,
span.start
);
let result = left_u32 let result = left_u32
.sub(cs.ns(|| unique_namespace), &right_u32) .sub(cs.ns(|| unique_namespace), &right_u32)
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
@ -336,13 +285,6 @@ impl Integer {
Integer::U32(result) Integer::U32(result)
} }
(Integer::U64(left_u64), Integer::U64(right_u64)) => { (Integer::U64(left_u64), Integer::U64(right_u64)) => {
let unique_namespace = format!(
"enforce {} - {} {}:{}",
left_u64.value.unwrap(),
right_u64.value.unwrap(),
span.line,
span.start
);
let result = left_u64 let result = left_u64
.sub(cs.ns(|| unique_namespace), &right_u64) .sub(cs.ns(|| unique_namespace), &right_u64)
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
@ -350,13 +292,6 @@ impl Integer {
Integer::U64(result) Integer::U64(result)
} }
(Integer::U128(left_u128), Integer::U128(right_u128)) => { (Integer::U128(left_u128), Integer::U128(right_u128)) => {
let unique_namespace = format!(
"enforce {} - {} {}:{}",
left_u128.value.unwrap(),
right_u128.value.unwrap(),
span.line,
span.start
);
let result = left_u128 let result = left_u128
.sub(cs.ns(|| unique_namespace), &right_u128) .sub(cs.ns(|| unique_namespace), &right_u128)
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
@ -373,15 +308,10 @@ impl Integer {
other: Self, other: Self,
span: Span, span: Span,
) -> Result<Self, IntegerError> { ) -> Result<Self, IntegerError> {
let unique_namespace = format!("enforce {} * {} {}:{}", self, other, span.line, span.start);
Ok(match (self, other) { Ok(match (self, other) {
(Integer::U8(left_u8), Integer::U8(right_u8)) => { (Integer::U8(left_u8), Integer::U8(right_u8)) => {
let unique_namespace = format!(
"enforce {} * {} {}:{}",
left_u8.value.unwrap(),
right_u8.value.unwrap(),
span.line,
span.start
);
let result = left_u8 let result = left_u8
.mul(cs.ns(|| unique_namespace), &right_u8) .mul(cs.ns(|| unique_namespace), &right_u8)
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
@ -389,13 +319,6 @@ impl Integer {
Integer::U8(result) Integer::U8(result)
} }
(Integer::U16(left_u16), Integer::U16(right_u16)) => { (Integer::U16(left_u16), Integer::U16(right_u16)) => {
let unique_namespace = format!(
"enforce {} * {} {}:{}",
left_u16.value.unwrap(),
right_u16.value.unwrap(),
span.line,
span.start
);
let result = left_u16 let result = left_u16
.mul(cs.ns(|| unique_namespace), &right_u16) .mul(cs.ns(|| unique_namespace), &right_u16)
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
@ -403,13 +326,6 @@ impl Integer {
Integer::U16(result) Integer::U16(result)
} }
(Integer::U32(left_u32), Integer::U32(right_u32)) => { (Integer::U32(left_u32), Integer::U32(right_u32)) => {
let unique_namespace = format!(
"enforce {} * {} {}:{}",
left_u32.value.unwrap(),
right_u32.value.unwrap(),
span.line,
span.start
);
let result = left_u32 let result = left_u32
.mul(cs.ns(|| unique_namespace), &right_u32) .mul(cs.ns(|| unique_namespace), &right_u32)
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
@ -417,13 +333,6 @@ impl Integer {
Integer::U32(result) Integer::U32(result)
} }
(Integer::U64(left_u64), Integer::U64(right_u64)) => { (Integer::U64(left_u64), Integer::U64(right_u64)) => {
let unique_namespace = format!(
"enforce {} * {} {}:{}",
left_u64.value.unwrap(),
right_u64.value.unwrap(),
span.line,
span.start
);
let result = left_u64 let result = left_u64
.mul(cs.ns(|| unique_namespace), &right_u64) .mul(cs.ns(|| unique_namespace), &right_u64)
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
@ -431,13 +340,6 @@ impl Integer {
Integer::U64(result) Integer::U64(result)
} }
(Integer::U128(left_u128), Integer::U128(right_u128)) => { (Integer::U128(left_u128), Integer::U128(right_u128)) => {
let unique_namespace = format!(
"enforce {} * {} {}:{}",
left_u128.value.unwrap(),
right_u128.value.unwrap(),
span.line,
span.start
);
let result = left_u128 let result = left_u128
.mul(cs.ns(|| unique_namespace), &right_u128) .mul(cs.ns(|| unique_namespace), &right_u128)
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
@ -454,15 +356,10 @@ impl Integer {
other: Self, other: Self,
span: Span, span: Span,
) -> Result<Self, IntegerError> { ) -> Result<Self, IntegerError> {
let unique_namespace = format!("enforce {} ÷ {} {}:{}", self, other, span.line, span.start);
Ok(match (self, other) { Ok(match (self, other) {
(Integer::U8(left_u8), Integer::U8(right_u8)) => { (Integer::U8(left_u8), Integer::U8(right_u8)) => {
let unique_namespace = format!(
"enforce {} ÷ {} {}:{}",
left_u8.value.unwrap(),
right_u8.value.unwrap(),
span.line,
span.start
);
let result = left_u8 let result = left_u8
.div(cs.ns(|| unique_namespace), &right_u8) .div(cs.ns(|| unique_namespace), &right_u8)
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
@ -470,13 +367,6 @@ impl Integer {
Integer::U8(result) Integer::U8(result)
} }
(Integer::U16(left_u16), Integer::U16(right_u16)) => { (Integer::U16(left_u16), Integer::U16(right_u16)) => {
let unique_namespace = format!(
"enforce {} ÷ {} {}:{}",
left_u16.value.unwrap(),
right_u16.value.unwrap(),
span.line,
span.start
);
let result = left_u16 let result = left_u16
.div(cs.ns(|| unique_namespace), &right_u16) .div(cs.ns(|| unique_namespace), &right_u16)
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
@ -484,13 +374,6 @@ impl Integer {
Integer::U16(result) Integer::U16(result)
} }
(Integer::U32(left_u32), Integer::U32(right_u32)) => { (Integer::U32(left_u32), Integer::U32(right_u32)) => {
let unique_namespace = format!(
"enforce {} ÷ {} {}:{}",
left_u32.value.unwrap(),
right_u32.value.unwrap(),
span.line,
span.start
);
let result = left_u32 let result = left_u32
.div(cs.ns(|| unique_namespace), &right_u32) .div(cs.ns(|| unique_namespace), &right_u32)
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
@ -498,13 +381,6 @@ impl Integer {
Integer::U32(result) Integer::U32(result)
} }
(Integer::U64(left_u64), Integer::U64(right_u64)) => { (Integer::U64(left_u64), Integer::U64(right_u64)) => {
let unique_namespace = format!(
"enforce {} ÷ {} {}:{}",
left_u64.value.unwrap(),
right_u64.value.unwrap(),
span.line,
span.start
);
let result = left_u64 let result = left_u64
.div(cs.ns(|| unique_namespace), &right_u64) .div(cs.ns(|| unique_namespace), &right_u64)
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
@ -512,13 +388,6 @@ impl Integer {
Integer::U64(result) Integer::U64(result)
} }
(Integer::U128(left_u128), Integer::U128(right_u128)) => { (Integer::U128(left_u128), Integer::U128(right_u128)) => {
let unique_namespace = format!(
"enforce {} ÷ {} {}:{}",
left_u128.value.unwrap(),
right_u128.value.unwrap(),
span.line,
span.start
);
let result = left_u128 let result = left_u128
.div(cs.ns(|| unique_namespace), &right_u128) .div(cs.ns(|| unique_namespace), &right_u128)
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
@ -535,15 +404,10 @@ impl Integer {
other: Self, other: Self,
span: Span, span: Span,
) -> Result<Self, IntegerError> { ) -> Result<Self, IntegerError> {
let unique_namespace = format!("enforce {} ** {} {}:{}", self, other, span.line, span.start);
Ok(match (self, other) { Ok(match (self, other) {
(Integer::U8(left_u8), Integer::U8(right_u8)) => { (Integer::U8(left_u8), Integer::U8(right_u8)) => {
let unique_namespace = format!(
"enforce {} ** {} {}:{}",
left_u8.value.unwrap(),
right_u8.value.unwrap(),
span.line,
span.start
);
let result = left_u8 let result = left_u8
.pow(cs.ns(|| unique_namespace), &right_u8) .pow(cs.ns(|| unique_namespace), &right_u8)
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
@ -551,13 +415,6 @@ impl Integer {
Integer::U8(result) Integer::U8(result)
} }
(Integer::U16(left_u16), Integer::U16(right_u16)) => { (Integer::U16(left_u16), Integer::U16(right_u16)) => {
let unique_namespace = format!(
"enforce {} ** {} {}:{}",
left_u16.value.unwrap(),
right_u16.value.unwrap(),
span.line,
span.start
);
let result = left_u16 let result = left_u16
.pow(cs.ns(|| unique_namespace), &right_u16) .pow(cs.ns(|| unique_namespace), &right_u16)
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
@ -565,13 +422,6 @@ impl Integer {
Integer::U16(result) Integer::U16(result)
} }
(Integer::U32(left_u32), Integer::U32(right_u32)) => { (Integer::U32(left_u32), Integer::U32(right_u32)) => {
let unique_namespace = format!(
"enforce {} ** {} {}:{}",
left_u32.value.unwrap(),
right_u32.value.unwrap(),
span.line,
span.start
);
let result = left_u32 let result = left_u32
.pow(cs.ns(|| unique_namespace), &right_u32) .pow(cs.ns(|| unique_namespace), &right_u32)
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
@ -579,13 +429,6 @@ impl Integer {
Integer::U32(result) Integer::U32(result)
} }
(Integer::U64(left_u64), Integer::U64(right_u64)) => { (Integer::U64(left_u64), Integer::U64(right_u64)) => {
let unique_namespace = format!(
"enforce {} ** {} {}:{}",
left_u64.value.unwrap(),
right_u64.value.unwrap(),
span.line,
span.start
);
let result = left_u64 let result = left_u64
.pow(cs.ns(|| unique_namespace), &right_u64) .pow(cs.ns(|| unique_namespace), &right_u64)
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
@ -593,13 +436,6 @@ impl Integer {
Integer::U64(result) Integer::U64(result)
} }
(Integer::U128(left_u128), Integer::U128(right_u128)) => { (Integer::U128(left_u128), Integer::U128(right_u128)) => {
let unique_namespace = format!(
"enforce {} ** {} {}:{}",
left_u128.value.unwrap(),
right_u128.value.unwrap(),
span.line,
span.start
);
let result = left_u128 let result = left_u128
.pow(cs.ns(|| unique_namespace), &right_u128) .pow(cs.ns(|| unique_namespace), &right_u128)
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?; .map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
@ -691,11 +527,11 @@ impl<F: Field + PrimeField> CondSelectGadget<F> for Integer {
impl fmt::Display for Integer { impl fmt::Display for Integer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let option = match self { let option = match self {
Integer::U8(u8) => u8.value.map(|num| num as usize), Integer::U8(u8) => u8.value.map(|num| num as u128),
Integer::U16(u16) => u16.value.map(|num| num as usize), Integer::U16(u16) => u16.value.map(|num| num as u128),
Integer::U32(u32) => u32.value.map(|num| num as usize), Integer::U32(u32) => u32.value.map(|num| num as u128),
Integer::U64(u64) => u64.value.map(|num| num as usize), Integer::U64(u64) => u64.value.map(|num| num as u128),
Integer::U128(u128) => u128.value.map(|num| num as usize), Integer::U128(u128) => u128.value.map(|num| num as u128),
}; };
match option { match option {
Some(number) => write!(f, "{}{}", number, self.get_type()), Some(number) => write!(f, "{}{}", number, self.get_type()),