mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
remove Field trait parameter from InputValue
This commit is contained in:
parent
a2dfc5e9fc
commit
674f0dc83d
@ -22,7 +22,7 @@ pub struct Compiler<F: Field + PrimeField, G: GroupType<F>> {
|
||||
package_name: String,
|
||||
main_file_path: PathBuf,
|
||||
program: Program<F>,
|
||||
program_inputs: Vec<Option<InputValue<F>>>,
|
||||
program_inputs: Vec<Option<InputValue>>,
|
||||
output: Option<ConstrainedValue<F, G>>,
|
||||
_engine: PhantomData<F>,
|
||||
}
|
||||
@ -44,7 +44,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
||||
Ok(program)
|
||||
}
|
||||
|
||||
pub fn set_inputs(&mut self, program_inputs: Vec<Option<InputValue<F>>>) {
|
||||
pub fn set_inputs(&mut self, program_inputs: Vec<Option<InputValue>>) {
|
||||
self.program_inputs = program_inputs;
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
private: bool,
|
||||
input_value: Option<InputValue<F>>,
|
||||
input_value: Option<InputValue>,
|
||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
||||
// Check that the input value is the correct type
|
||||
let bool_value = match input_value {
|
||||
|
@ -189,9 +189,6 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
let val_2 = ConstrainedValue::from_other(string, &val_1)?;
|
||||
self.enforce_pow_expression(cs, val_1, val_2)
|
||||
}
|
||||
(_, ConstrainedValue::Field(num_2)) => {
|
||||
Err(ExpressionError::InvalidExponent(num_2.to_string()))
|
||||
}
|
||||
(val_1, val_2) => Err(ExpressionError::IncompatibleTypes(format!(
|
||||
"{} * {}",
|
||||
val_1, val_2,
|
||||
@ -212,7 +209,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
||||
Ok(Self::evaluate_integer_eq(num_1, num_2)?)
|
||||
}
|
||||
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
|
||||
(ConstrainedValue::Field(fe_1), ConstrainedValue::Field(fe_2)) => {
|
||||
Ok(ConstrainedValue::Boolean(Boolean::Constant(fe_1.eq(&fe_2))))
|
||||
}
|
||||
(ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
|
||||
|
@ -1,8 +1,7 @@
|
||||
//! Methods to enforce constraints on field elements in a resolved Leo program.
|
||||
|
||||
use crate::{
|
||||
constraints::ConstrainedValue, errors::FieldElementError, types::InputValue, FieldType,
|
||||
GroupType,
|
||||
constraints::ConstrainedValue, errors::FieldError, types::InputValue, FieldType, GroupType,
|
||||
};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
@ -15,15 +14,15 @@ pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
private: bool,
|
||||
input_value: Option<InputValue<F>>,
|
||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
||||
input_value: Option<InputValue>,
|
||||
) -> Result<ConstrainedValue<F, G>, FieldError> {
|
||||
// Check that the parameter value is the correct type
|
||||
let field_option = match input_value {
|
||||
Some(input) => {
|
||||
if let InputValue::Field(field_string) = input {
|
||||
Some(field_string)
|
||||
} else {
|
||||
return Err(FieldElementError::Invalid(input.to_string()));
|
||||
return Err(FieldError::Invalid(input.to_string()));
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
|
@ -120,7 +120,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
private: bool,
|
||||
array_type: Type<F>,
|
||||
array_dimensions: Vec<usize>,
|
||||
input_value: Option<InputValue<F>>,
|
||||
input_value: Option<InputValue>,
|
||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
||||
let expected_length = array_dimensions[0];
|
||||
let mut array_value = vec![];
|
||||
@ -173,7 +173,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
_type: Type<F>,
|
||||
name: String,
|
||||
private: bool,
|
||||
input_value: Option<InputValue<F>>,
|
||||
input_value: Option<InputValue>,
|
||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
||||
match _type {
|
||||
Type::IntegerType(integer_type) => {
|
||||
@ -194,7 +194,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
cs: &mut CS,
|
||||
scope: String,
|
||||
function: Function<F>,
|
||||
inputs: Vec<Option<InputValue<F>>>,
|
||||
inputs: Vec<Option<InputValue>>,
|
||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
||||
let function_name = new_scope(scope.clone(), function.get_name());
|
||||
|
||||
|
@ -10,7 +10,7 @@ pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: Const
|
||||
cs: &mut CS,
|
||||
name: String,
|
||||
private: bool,
|
||||
input_value: Option<InputValue<F>>,
|
||||
input_value: Option<InputValue>,
|
||||
) -> Result<ConstrainedValue<F, G>, GroupError> {
|
||||
// Check that the parameter value is the correct type
|
||||
let group_option = match input_value {
|
||||
|
@ -105,7 +105,7 @@ impl<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>> Constraine
|
||||
integer_type: IntegerType,
|
||||
name: String,
|
||||
private: bool,
|
||||
integer_value: Option<InputValue<F>>,
|
||||
integer_value: Option<InputValue>,
|
||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
||||
// Check that the input value is the correct type
|
||||
let integer_option = match integer_value {
|
||||
|
@ -44,7 +44,7 @@ use snarkos_models::{
|
||||
pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
|
||||
cs: &mut CS,
|
||||
program: Program<F>,
|
||||
parameters: Vec<Option<InputValue<F>>>,
|
||||
parameters: Vec<Option<InputValue>>,
|
||||
) -> Result<ConstrainedValue<F, G>, CompilerError> {
|
||||
let mut resolved_program = ConstrainedProgram::new();
|
||||
let program_name = program.get_name();
|
||||
|
@ -101,7 +101,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> fmt::Display for ConstrainedValue<F
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ConstrainedValue::Integer(ref value) => write!(f, "{}", value),
|
||||
ConstrainedValue::Field(ref value) => write!(f, "{}", value),
|
||||
ConstrainedValue::Field(ref value) => write!(f, "{:?}", value),
|
||||
ConstrainedValue::Group(ref value) => write!(f, "{:?}", value),
|
||||
ConstrainedValue::Boolean(ref value) => write!(f, "{}", value.get_value().unwrap()),
|
||||
ConstrainedValue::Array(ref array) => {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::errors::{
|
||||
BooleanError, FieldElementError, FunctionError, GroupError, IntegerError, ValueError,
|
||||
BooleanError, FieldError, FunctionError, GroupError, IntegerError, ValueError,
|
||||
};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
@ -28,7 +28,7 @@ pub enum ExpressionError {
|
||||
ParseIntError(ParseIntError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FieldElementError(FieldElementError),
|
||||
FieldError(FieldError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
GroupError(#[from] GroupError),
|
||||
@ -128,9 +128,9 @@ impl From<ParseIntError> for ExpressionError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FieldElementError> for ExpressionError {
|
||||
fn from(error: FieldElementError) -> Self {
|
||||
ExpressionError::FieldElementError(error)
|
||||
impl From<FieldError> for ExpressionError {
|
||||
fn from(error: FieldError) -> Self {
|
||||
ExpressionError::FieldError(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FieldElementError {
|
||||
pub enum FieldError {
|
||||
#[error("Expected field element parameter, got {}", _0)]
|
||||
Invalid(String),
|
||||
|
||||
@ -9,8 +9,8 @@ pub enum FieldElementError {
|
||||
SynthesisError(SynthesisError),
|
||||
}
|
||||
|
||||
impl From<SynthesisError> for FieldElementError {
|
||||
impl From<SynthesisError> for FieldError {
|
||||
fn from(error: SynthesisError) -> Self {
|
||||
FieldElementError::SynthesisError(error)
|
||||
FieldError::SynthesisError(error)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::errors::{
|
||||
BooleanError, ExpressionError, FieldElementError, GroupError, IntegerError, StatementError,
|
||||
ValueError,
|
||||
BooleanError, ExpressionError, FieldError, GroupError, IntegerError, StatementError, ValueError,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@ -27,7 +26,7 @@ pub enum FunctionError {
|
||||
IntegerError(IntegerError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FieldElementError(FieldElementError),
|
||||
FieldElementError(FieldError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
GroupError(GroupError),
|
||||
@ -54,8 +53,8 @@ impl From<IntegerError> for FunctionError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FieldElementError> for FunctionError {
|
||||
fn from(error: FieldElementError) -> Self {
|
||||
impl From<FieldError> for FunctionError {
|
||||
fn from(error: FieldError) -> Self {
|
||||
FunctionError::FieldElementError(error)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::errors::{BooleanError, ExpressionError, FieldElementError, IntegerError, ValueError};
|
||||
use crate::errors::{BooleanError, ExpressionError, FieldError, IntegerError, ValueError};
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
|
||||
@ -14,7 +14,7 @@ pub enum StatementError {
|
||||
IntegerError(IntegerError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FieldElementError(FieldElementError),
|
||||
FieldError(FieldError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
BooleanError(BooleanError),
|
||||
@ -81,9 +81,9 @@ impl From<IntegerError> for StatementError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FieldElementError> for StatementError {
|
||||
fn from(error: FieldElementError) -> Self {
|
||||
StatementError::FieldElementError(error)
|
||||
impl From<FieldError> for StatementError {
|
||||
fn from(error: FieldError) -> Self {
|
||||
StatementError::FieldError(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::errors::{GroupError, IntegerError};
|
||||
use crate::errors::{FieldError, GroupError, IntegerError};
|
||||
|
||||
use std::{num::ParseIntError, str::ParseBoolError};
|
||||
|
||||
@ -29,6 +29,9 @@ pub enum ValueError {
|
||||
|
||||
#[error("{}", _0)]
|
||||
GroupError(#[from] GroupError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
FieldError(#[from] FieldError),
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for ValueError {
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! A data type that represents a field value
|
||||
|
||||
use crate::errors::FieldElementError;
|
||||
use crate::errors::FieldError;
|
||||
|
||||
use snarkos_errors::gadgets::SynthesisError;
|
||||
use snarkos_models::{
|
||||
@ -20,54 +20,35 @@ use snarkos_models::{
|
||||
};
|
||||
use std::{borrow::Borrow, str::FromStr};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum FieldType<F: Field + PrimeField> {
|
||||
Constant(F),
|
||||
Allocated(FpGadget<F>),
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField> FieldType<F> {
|
||||
pub fn constant(string: String) -> Result<Self, FieldElementError> {
|
||||
let value = F::from_str(&string).map_err(|_| FieldElementError::Invalid(string))?;
|
||||
pub fn constant(string: String) -> Result<Self, FieldError> {
|
||||
let value = F::from_str(&string).map_err(|_| FieldError::Invalid(string))?;
|
||||
Ok(FieldType::Constant(value))
|
||||
}
|
||||
|
||||
pub fn add<CS: ConstraintSystem<F>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
other: &Self,
|
||||
) -> Result<Self, FieldElementError> {
|
||||
pub fn add<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, FieldError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn sub<CS: ConstraintSystem<F>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
other: &Self,
|
||||
) -> Result<Self, FieldElementError> {
|
||||
pub fn sub<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, FieldError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn mul<CS: ConstraintSystem<F>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
other: &Self,
|
||||
) -> Result<Self, FieldElementError> {
|
||||
pub fn mul<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, FieldError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn div<CS: ConstraintSystem<F>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
other: &Self,
|
||||
) -> Result<Self, FieldElementError> {
|
||||
pub fn div<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, FieldError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn pow<CS: ConstraintSystem<F>>(
|
||||
&self,
|
||||
cs: CS,
|
||||
other: &Self,
|
||||
) -> Result<Self, FieldElementError> {
|
||||
pub fn pow<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, FieldError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
@ -97,8 +78,12 @@ impl<F: Field + PrimeField> ConditionalEqGadget<F> for FieldType<F> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField> AllocGadget<F, F> for FieldType<F> {
|
||||
fn alloc<Fn: FnOnce() -> Result<T, SynthesisError>, T: Borrow<F>, CS: ConstraintSystem<F>>(
|
||||
impl<F: Field + PrimeField> AllocGadget<String, F> for FieldType<F> {
|
||||
fn alloc<
|
||||
Fn: FnOnce() -> Result<T, SynthesisError>,
|
||||
T: Borrow<String>,
|
||||
CS: ConstraintSystem<F>,
|
||||
>(
|
||||
cs: CS,
|
||||
f: Fn,
|
||||
) -> Result<Self, SynthesisError> {
|
||||
@ -107,7 +92,7 @@ impl<F: Field + PrimeField> AllocGadget<F, F> for FieldType<F> {
|
||||
|
||||
fn alloc_input<
|
||||
Fn: FnOnce() -> Result<T, SynthesisError>,
|
||||
T: Borrow<F>,
|
||||
T: Borrow<String>,
|
||||
CS: ConstraintSystem<F>,
|
||||
>(
|
||||
cs: CS,
|
||||
|
@ -235,12 +235,12 @@ pub struct InputModel<F: Field + PrimeField> {
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub enum InputValue<F: Field + PrimeField> {
|
||||
pub enum InputValue {
|
||||
Integer(usize),
|
||||
Field(String),
|
||||
Group(String),
|
||||
Boolean(bool),
|
||||
Array(Vec<InputValue<F>>),
|
||||
Array(Vec<InputValue>),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
|
@ -321,7 +321,7 @@ impl<F: Field + PrimeField> fmt::Display for InputModel<F> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField> fmt::Display for InputValue<F> {
|
||||
impl fmt::Display for InputValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
InputValue::Integer(ref integer) => write!(f, "{}", integer),
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{compile_program, get_error, get_output, EdwardsConstrainedValue, EdwardsTestCompiler};
|
||||
use leo_compiler::{
|
||||
errors::{CompilerError, FieldElementError, FunctionError},
|
||||
errors::{CompilerError, FieldError, FunctionError},
|
||||
ConstrainedValue, FieldElement, InputValue,
|
||||
};
|
||||
|
||||
@ -33,9 +33,9 @@ fn output_one(program: EdwardsTestCompiler) {
|
||||
|
||||
fn fail_field(program: EdwardsTestCompiler) {
|
||||
match get_error(program) {
|
||||
CompilerError::FunctionError(FunctionError::FieldElementError(
|
||||
FieldElementError::Invalid(_string),
|
||||
)) => {}
|
||||
CompilerError::FunctionError(FunctionError::FieldElementError(FieldError::Invalid(
|
||||
_string,
|
||||
))) => {}
|
||||
error => panic!("Expected invalid field error, got {}", error),
|
||||
}
|
||||
}
|
||||
@ -43,7 +43,7 @@ fn fail_field(program: EdwardsTestCompiler) {
|
||||
fn fail_synthesis(program: EdwardsTestCompiler) {
|
||||
match get_error(program) {
|
||||
CompilerError::FunctionError(FunctionError::FieldElementError(
|
||||
FieldElementError::SynthesisError(_string),
|
||||
FieldError::SynthesisError(_string),
|
||||
)) => {}
|
||||
error => panic!("Expected synthesis error, got {}", error),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user