mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 03:35:10 +03:00
fix index evaluation
This commit is contained in:
parent
eeaf1b4ae8
commit
5865149c06
@ -21,7 +21,7 @@ LINE_END = { ";" ~ NEWLINE* }
|
||||
mutable = { "mut" }
|
||||
|
||||
// Declared in common/range.rs
|
||||
range = { from_expression? ~ ".." ~ to_expression }
|
||||
range = { from_expression? ~ ".." ~ to_expression? }
|
||||
from_expression = { expression }
|
||||
to_expression = { expression }
|
||||
|
||||
|
@ -554,7 +554,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
index,
|
||||
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)),
|
||||
}
|
||||
}
|
||||
@ -584,11 +584,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
match index {
|
||||
RangeOrExpression::Range(from, to) => {
|
||||
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
|
||||
};
|
||||
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
|
||||
};
|
||||
Ok(ConstrainedValue::Array(array[from_resolved..to_resolved].to_owned()))
|
||||
|
@ -74,7 +74,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
// Modify the single value of the array in place
|
||||
match self.get_mutable_assignee(name, span.clone())? {
|
||||
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 selected_value = ConstrainedValue::conditionally_select(
|
||||
@ -94,11 +94,11 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
}
|
||||
RangeOrExpression::Range(from, to) => {
|
||||
let from_index = match from {
|
||||
Some(integer) => integer.to_usize(),
|
||||
Some(integer) => integer.to_usize(span.clone())?,
|
||||
None => 0usize,
|
||||
};
|
||||
let to_index_option = match to {
|
||||
Some(integer) => Some(integer.to_usize()),
|
||||
Some(integer) => Some(integer.to_usize(span.clone())?),
|
||||
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));
|
||||
}
|
||||
_ => {
|
||||
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 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 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 selected_value =
|
||||
@ -496,8 +496,10 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
span: Span,
|
||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
||||
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.
|
||||
// For loop scope is not implemented.
|
||||
let index_name = new_scope(function_scope.clone(), index.to_string());
|
||||
|
@ -46,7 +46,7 @@ pub enum ConstrainedValue<F: Field + PrimeField, G: GroupType<F>> {
|
||||
|
||||
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> {
|
||||
let other_type = other.to_type();
|
||||
let other_type = other.to_type(span.clone())?;
|
||||
|
||||
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 {
|
||||
match self {
|
||||
pub(crate) fn to_type(&self, span: Span) -> Result<Type, ValueError> {
|
||||
Ok(match self {
|
||||
ConstrainedValue::Integer(integer) => Type::IntegerType(integer.get_type()),
|
||||
ConstrainedValue::Field(_field) => Type::Field,
|
||||
ConstrainedValue::Group(_group) => Type::Group,
|
||||
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> {
|
||||
@ -95,8 +95,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
|
||||
match (&self, &other) {
|
||||
(ConstrainedValue::Unresolved(_), ConstrainedValue::Unresolved(_)) => Ok(()),
|
||||
(ConstrainedValue::Unresolved(_), _) => self.resolve_type(&vec![other.to_type()], span),
|
||||
(_, ConstrainedValue::Unresolved(_)) => other.resolve_type(&vec![self.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.clone())?], span),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
@ -141,7 +141,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.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>>()?;
|
||||
}
|
||||
@ -150,9 +152,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.map(|(i, member)| {
|
||||
member
|
||||
.1
|
||||
.allocate_value(cs.ns(|| format!("allocate circuit member {}", i)), span.clone())
|
||||
let unique_name = format!("allocate circuit member {} {}:{}", i, span.line, span.start);
|
||||
|
||||
member.1.allocate_value(cs.ns(|| unique_name), span.clone())
|
||||
})
|
||||
.collect::<Result<(), ValueError>>()?;
|
||||
}
|
||||
@ -161,7 +163,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.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>>()?;
|
||||
}
|
||||
@ -220,10 +224,10 @@ impl<F: Field + PrimeField, G: GroupType<F>> fmt::Display for ConstrainedValue<F
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
ConstrainedValue::CircuitDefinition(ref _definition) => {
|
||||
unimplemented!("cannot return circuit definition in program")
|
||||
ConstrainedValue::CircuitDefinition(ref circuit) => write!(f, "circuit {{ {} }}", circuit.identifier),
|
||||
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::Static(ref value) => write!(f, "static {}", 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::Mutable(first), _) => Self::conditionally_select(cs, cond, first, second)?,
|
||||
(_, ConstrainedValue::Mutable(second)) => Self::conditionally_select(cs, cond, first, second)?,
|
||||
(_, _) => return Err(SynthesisError::Unsatisfiable),
|
||||
})
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::errors::{BooleanError, ExpressionError, ValueError};
|
||||
use leo_types::{Error as FormattedError, Span};
|
||||
use leo_types::{Error as FormattedError, IntegerError, Span};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum StatementError {
|
||||
@ -12,6 +12,9 @@ pub enum StatementError {
|
||||
#[error("{}", _0)]
|
||||
ExpressionError(#[from] ExpressionError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
IntegerError(#[from] IntegerError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ValueError(#[from] ValueError),
|
||||
}
|
||||
|
@ -276,14 +276,12 @@ fn test_self() {
|
||||
|
||||
// All
|
||||
|
||||
// #[test]
|
||||
// fn test_pedersen_mock() {
|
||||
// use crate::integers::u32::output_zero;
|
||||
// let bytes = include_bytes!("pedersen_mock.leo");
|
||||
// let program = parse_program(bytes).unwrap();
|
||||
//
|
||||
// let err = get_error(program);
|
||||
//
|
||||
// println!("{}", err);
|
||||
// // output_zero(program);
|
||||
// }
|
||||
#[test]
|
||||
fn test_pedersen_mock() {
|
||||
use crate::integers::u32::output_zero;
|
||||
|
||||
let bytes = include_bytes!("pedersen_mock.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
output_zero(program);
|
||||
}
|
||||
|
13
compiler/tests/function/iteration.leo
Normal file
13
compiler/tests/function/iteration.leo
Normal 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
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
use crate::{
|
||||
boolean::output_true,
|
||||
get_error,
|
||||
get_output,
|
||||
integers::u32::output_one,
|
||||
integers::u32::{output_number, output_one},
|
||||
parse_program,
|
||||
EdwardsConstrainedValue,
|
||||
EdwardsTestCompiler,
|
||||
@ -11,7 +12,6 @@ use leo_compiler::{
|
||||
ConstrainedValue,
|
||||
};
|
||||
|
||||
use crate::boolean::output_true;
|
||||
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
||||
|
||||
pub(crate) fn output_empty(program: EdwardsTestCompiler) {
|
||||
@ -120,3 +120,18 @@ fn test_repeated_function_call() {
|
||||
|
||||
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);
|
||||
}
|
||||
|
13
compiler/tests/function/repeated_iteration.leo
Normal file
13
compiler/tests/function/repeated_iteration.leo
Normal 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()
|
||||
}
|
@ -65,7 +65,5 @@ fn test_num_returns_fail() {
|
||||
let bytes = include_bytes!("num_returns_fail.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
let error = get_error(program);
|
||||
|
||||
println!("{}", error);
|
||||
let _ = get_error(program);
|
||||
}
|
||||
|
0
examples/fibonacci/inputs/fibonacci.in
Normal file
0
examples/fibonacci/inputs/fibonacci.in
Normal file
@ -1,27 +1,16 @@
|
||||
function fibonacci(i: u32) -> u32 {
|
||||
if i == 0 {
|
||||
return 0
|
||||
} else if i == 1 {
|
||||
return 1
|
||||
} else {
|
||||
return fibonacci(i - 1) + fibonacci(i - 2)
|
||||
function fibonacci() -> u32 {
|
||||
let mut a = 0u32;
|
||||
let mut b = 1u32;
|
||||
|
||||
for i in 0..10 {
|
||||
a = b;
|
||||
b = a + b;
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// The 'fibonacci' main function.
|
||||
function main() -> u32 {
|
||||
return fibonacci(1)
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
return fibonacci()
|
||||
}
|
0
examples/hello_world/inputs/hello_world.in
Normal file
0
examples/hello_world/inputs/hello_world.in
Normal file
@ -1,5 +1,5 @@
|
||||
// The 'hello_world' main function.
|
||||
function main() -> u32 {
|
||||
let a = 1 + 1;
|
||||
let a: u32 = 1 + 1;
|
||||
return a
|
||||
}
|
||||
|
@ -34,7 +34,6 @@ impl LeoInputsParser {
|
||||
|
||||
// Build the abstract syntax tree
|
||||
let syntax_tree = files::File::from_pest(&mut file).map_err(|_| InputParserError::SyntaxTreeError)?;
|
||||
// println!("{:?}", syntax_tree);
|
||||
|
||||
Ok(syntax_tree)
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::{error::Error as FormattedError, Span};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@ -30,6 +31,13 @@ impl IntegerError {
|
||||
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 {
|
||||
let message = format!("expected integer input type, found `{}`", actual);
|
||||
|
||||
|
@ -230,9 +230,7 @@ impl<'ast> From<PostfixExpression<'ast>> for Expression {
|
||||
|
||||
// Handle function calls
|
||||
Access::Call(function) => {
|
||||
println!("old {:?}", function.span);
|
||||
let span = Span::from(function.span);
|
||||
println!("span {:?}", span);
|
||||
Expression::FunctionCall(
|
||||
Box::new(acc),
|
||||
function
|
||||
|
@ -108,8 +108,9 @@ impl Integer {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_usize(&self) -> usize {
|
||||
self.get_value().unwrap() as usize
|
||||
pub fn to_usize(&self, span: Span) -> Result<usize, IntegerError> {
|
||||
let value = self.get_value().ok_or(IntegerError::invalid_index(span))?;
|
||||
Ok(value as usize)
|
||||
}
|
||||
|
||||
pub fn get_type(&self) -> IntegerType {
|
||||
@ -216,67 +217,34 @@ impl Integer {
|
||||
other: Self,
|
||||
span: Span,
|
||||
) -> Result<Self, IntegerError> {
|
||||
let unique_namespace = format!("enforce {} + {} {}:{}", self, other, span.line, span.start);
|
||||
|
||||
Ok(match (self, other) {
|
||||
(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])
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
|
||||
|
||||
Integer::U8(result)
|
||||
}
|
||||
(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])
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
|
||||
|
||||
Integer::U16(result)
|
||||
}
|
||||
(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])
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
|
||||
|
||||
Integer::U32(result)
|
||||
}
|
||||
(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])
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
|
||||
|
||||
Integer::U64(result)
|
||||
}
|
||||
(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])
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("+"), e, span))?;
|
||||
|
||||
@ -292,15 +260,10 @@ impl Integer {
|
||||
other: Self,
|
||||
span: Span,
|
||||
) -> Result<Self, IntegerError> {
|
||||
let unique_namespace = format!("enforce {} - {} {}:{}", self, other, span.line, span.start);
|
||||
|
||||
Ok(match (self, other) {
|
||||
(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
|
||||
.sub(cs.ns(|| unique_namespace), &right_u8)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
|
||||
@ -308,13 +271,6 @@ impl Integer {
|
||||
Integer::U8(result)
|
||||
}
|
||||
(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
|
||||
.sub(cs.ns(|| unique_namespace), &right_u16)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
|
||||
@ -322,13 +278,6 @@ impl Integer {
|
||||
Integer::U16(result)
|
||||
}
|
||||
(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
|
||||
.sub(cs.ns(|| unique_namespace), &right_u32)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
|
||||
@ -336,13 +285,6 @@ impl Integer {
|
||||
Integer::U32(result)
|
||||
}
|
||||
(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
|
||||
.sub(cs.ns(|| unique_namespace), &right_u64)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
|
||||
@ -350,13 +292,6 @@ impl Integer {
|
||||
Integer::U64(result)
|
||||
}
|
||||
(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
|
||||
.sub(cs.ns(|| unique_namespace), &right_u128)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("-"), e, span))?;
|
||||
@ -373,15 +308,10 @@ impl Integer {
|
||||
other: Self,
|
||||
span: Span,
|
||||
) -> Result<Self, IntegerError> {
|
||||
let unique_namespace = format!("enforce {} * {} {}:{}", self, other, span.line, span.start);
|
||||
|
||||
Ok(match (self, other) {
|
||||
(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
|
||||
.mul(cs.ns(|| unique_namespace), &right_u8)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
|
||||
@ -389,13 +319,6 @@ impl Integer {
|
||||
Integer::U8(result)
|
||||
}
|
||||
(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
|
||||
.mul(cs.ns(|| unique_namespace), &right_u16)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
|
||||
@ -403,13 +326,6 @@ impl Integer {
|
||||
Integer::U16(result)
|
||||
}
|
||||
(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
|
||||
.mul(cs.ns(|| unique_namespace), &right_u32)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
|
||||
@ -417,13 +333,6 @@ impl Integer {
|
||||
Integer::U32(result)
|
||||
}
|
||||
(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
|
||||
.mul(cs.ns(|| unique_namespace), &right_u64)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
|
||||
@ -431,13 +340,6 @@ impl Integer {
|
||||
Integer::U64(result)
|
||||
}
|
||||
(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
|
||||
.mul(cs.ns(|| unique_namespace), &right_u128)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("*"), e, span))?;
|
||||
@ -454,15 +356,10 @@ impl Integer {
|
||||
other: Self,
|
||||
span: Span,
|
||||
) -> Result<Self, IntegerError> {
|
||||
let unique_namespace = format!("enforce {} ÷ {} {}:{}", self, other, span.line, span.start);
|
||||
|
||||
Ok(match (self, other) {
|
||||
(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
|
||||
.div(cs.ns(|| unique_namespace), &right_u8)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
|
||||
@ -470,13 +367,6 @@ impl Integer {
|
||||
Integer::U8(result)
|
||||
}
|
||||
(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
|
||||
.div(cs.ns(|| unique_namespace), &right_u16)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
|
||||
@ -484,13 +374,6 @@ impl Integer {
|
||||
Integer::U16(result)
|
||||
}
|
||||
(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
|
||||
.div(cs.ns(|| unique_namespace), &right_u32)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
|
||||
@ -498,13 +381,6 @@ impl Integer {
|
||||
Integer::U32(result)
|
||||
}
|
||||
(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
|
||||
.div(cs.ns(|| unique_namespace), &right_u64)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
|
||||
@ -512,13 +388,6 @@ impl Integer {
|
||||
Integer::U64(result)
|
||||
}
|
||||
(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
|
||||
.div(cs.ns(|| unique_namespace), &right_u128)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("÷"), e, span))?;
|
||||
@ -535,15 +404,10 @@ impl Integer {
|
||||
other: Self,
|
||||
span: Span,
|
||||
) -> Result<Self, IntegerError> {
|
||||
let unique_namespace = format!("enforce {} ** {} {}:{}", self, other, span.line, span.start);
|
||||
|
||||
Ok(match (self, other) {
|
||||
(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
|
||||
.pow(cs.ns(|| unique_namespace), &right_u8)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
|
||||
@ -551,13 +415,6 @@ impl Integer {
|
||||
Integer::U8(result)
|
||||
}
|
||||
(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
|
||||
.pow(cs.ns(|| unique_namespace), &right_u16)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
|
||||
@ -565,13 +422,6 @@ impl Integer {
|
||||
Integer::U16(result)
|
||||
}
|
||||
(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
|
||||
.pow(cs.ns(|| unique_namespace), &right_u32)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
|
||||
@ -579,13 +429,6 @@ impl Integer {
|
||||
Integer::U32(result)
|
||||
}
|
||||
(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
|
||||
.pow(cs.ns(|| unique_namespace), &right_u64)
|
||||
.map_err(|e| IntegerError::cannot_enforce(format!("**"), e, span))?;
|
||||
@ -593,13 +436,6 @@ impl Integer {
|
||||
Integer::U64(result)
|
||||
}
|
||||
(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
|
||||
.pow(cs.ns(|| unique_namespace), &right_u128)
|
||||
.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 {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let option = match self {
|
||||
Integer::U8(u8) => u8.value.map(|num| num as usize),
|
||||
Integer::U16(u16) => u16.value.map(|num| num as usize),
|
||||
Integer::U32(u32) => u32.value.map(|num| num as usize),
|
||||
Integer::U64(u64) => u64.value.map(|num| num as usize),
|
||||
Integer::U128(u128) => u128.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 u128),
|
||||
Integer::U32(u32) => u32.value.map(|num| num as u128),
|
||||
Integer::U64(u64) => u64.value.map(|num| num as u128),
|
||||
Integer::U128(u128) => u128.value.map(|num| num as u128),
|
||||
};
|
||||
match option {
|
||||
Some(number) => write!(f, "{}{}", number, self.get_type()),
|
||||
|
Loading…
Reference in New Issue
Block a user