mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 22:36:05 +03:00
remove snarkos group trait from compiler
This commit is contained in:
parent
dfcf2d3dbd
commit
19f6e64c48
@ -6,9 +6,7 @@ use snarkos_algorithms::snark::{
|
|||||||
create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
|
create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
|
||||||
};
|
};
|
||||||
use snarkos_curves::bls12_377::{Bls12_377, Fr};
|
use snarkos_curves::bls12_377::{Bls12_377, Fr};
|
||||||
use snarkos_curves::edwards_bls12::EdwardsProjective;
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::curves::Group;
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
|
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
|
||||||
@ -20,19 +18,17 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Benchmark<F: Field + PrimeField, G: Group> {
|
pub struct Benchmark<F: Field + PrimeField> {
|
||||||
program: Program<F, G>,
|
program: Program<F>,
|
||||||
parameters: Vec<Option<InputValue<F, G>>>,
|
parameters: Vec<Option<InputValue<F>>>,
|
||||||
_group: PhantomData<G>,
|
|
||||||
_engine: PhantomData<F>,
|
_engine: PhantomData<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Benchmark<F, G> {
|
impl<F: Field + PrimeField> Benchmark<F> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
program: Program::new(),
|
program: Program::new(),
|
||||||
parameters: vec![],
|
parameters: vec![],
|
||||||
_group: PhantomData,
|
|
||||||
_engine: PhantomData,
|
_engine: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,7 +44,7 @@ impl<F: Field + PrimeField, G: Group> Benchmark<F, G> {
|
|||||||
let syntax_tree = ast::File::from_pest(&mut file).expect("infallible");
|
let syntax_tree = ast::File::from_pest(&mut file).expect("infallible");
|
||||||
|
|
||||||
// Build a leo program from the syntax tree
|
// Build a leo program from the syntax tree
|
||||||
self.program = Program::<F, G>::from(syntax_tree, "simple".into());
|
self.program = Program::<F>::from(syntax_tree, "simple".into());
|
||||||
self.parameters = vec![None; self.program.num_parameters];
|
self.parameters = vec![None; self.program.num_parameters];
|
||||||
|
|
||||||
println!(" compiled: {:#?}\n", self.program);
|
println!(" compiled: {:#?}\n", self.program);
|
||||||
@ -57,7 +53,7 @@ impl<F: Field + PrimeField, G: Group> Benchmark<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> ConstraintSynthesizer<F> for Benchmark<F, G> {
|
impl<F: Field + PrimeField> ConstraintSynthesizer<F> for Benchmark<F> {
|
||||||
fn generate_constraints<CS: ConstraintSystem<F>>(
|
fn generate_constraints<CS: ConstraintSystem<F>>(
|
||||||
self,
|
self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
@ -81,7 +77,7 @@ fn main() {
|
|||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
// Load and compile program
|
// Load and compile program
|
||||||
let mut program = Benchmark::<Fr, EdwardsProjective>::new();
|
let mut program = Benchmark::<Fr>::new();
|
||||||
program.evaluate_program().unwrap();
|
program.evaluate_program().unwrap();
|
||||||
|
|
||||||
// Generate proof parameters
|
// Generate proof parameters
|
||||||
|
@ -9,7 +9,7 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
|
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -18,16 +18,16 @@ use sha2::{Digest, Sha256};
|
|||||||
use std::{fs, marker::PhantomData, path::PathBuf};
|
use std::{fs, marker::PhantomData, path::PathBuf};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Compiler<F: Field + PrimeField, G: Group> {
|
pub struct Compiler<F: Field + PrimeField> {
|
||||||
package_name: String,
|
package_name: String,
|
||||||
main_file_path: PathBuf,
|
main_file_path: PathBuf,
|
||||||
program: Program<F, G>,
|
program: Program<F>,
|
||||||
program_inputs: Vec<Option<InputValue<F, G>>>,
|
program_inputs: Vec<Option<InputValue<F>>>,
|
||||||
output: Option<ConstrainedValue<F, G>>,
|
output: Option<ConstrainedValue<F>>,
|
||||||
_engine: PhantomData<F>,
|
_engine: PhantomData<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Compiler<F, G> {
|
impl<F: Field + PrimeField> Compiler<F> {
|
||||||
pub fn init(package_name: String, main_file_path: PathBuf) -> Result<Self, CompilerError> {
|
pub fn init(package_name: String, main_file_path: PathBuf) -> Result<Self, CompilerError> {
|
||||||
let mut program = Self {
|
let mut program = Self {
|
||||||
package_name,
|
package_name,
|
||||||
@ -44,7 +44,7 @@ impl<F: Field + PrimeField, G: Group> Compiler<F, G> {
|
|||||||
Ok(program)
|
Ok(program)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_inputs(&mut self, program_inputs: Vec<Option<InputValue<F, G>>>) {
|
pub fn set_inputs(&mut self, program_inputs: Vec<Option<InputValue<F>>>) {
|
||||||
self.program_inputs = program_inputs;
|
self.program_inputs = program_inputs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ impl<F: Field + PrimeField, G: Group> Compiler<F, G> {
|
|||||||
pub fn compile_constraints<CS: ConstraintSystem<F>>(
|
pub fn compile_constraints<CS: ConstraintSystem<F>>(
|
||||||
self,
|
self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
) -> Result<ConstrainedValue<F, G>, CompilerError> {
|
) -> Result<ConstrainedValue<F>, CompilerError> {
|
||||||
generate_constraints(cs, self.program, self.program_inputs)
|
generate_constraints(cs, self.program, self.program_inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ impl<F: Field + PrimeField, G: Group> Compiler<F, G> {
|
|||||||
// Build program from abstract syntax tree
|
// Build program from abstract syntax tree
|
||||||
let package_name = self.package_name.clone();
|
let package_name = self.package_name.clone();
|
||||||
|
|
||||||
self.program = Program::<F, G>::from(syntax_tree, package_name);
|
self.program = Program::<F>::from(syntax_tree, package_name);
|
||||||
self.program_inputs = vec![None; self.program.num_parameters];
|
self.program_inputs = vec![None; self.program.num_parameters];
|
||||||
|
|
||||||
log::debug!("Program parsing complete\n{:#?}", self.program);
|
log::debug!("Program parsing complete\n{:#?}", self.program);
|
||||||
@ -107,7 +107,7 @@ impl<F: Field + PrimeField, G: Group> Compiler<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> ConstraintSynthesizer<F> for Compiler<F, G> {
|
impl<F: Field + PrimeField> ConstraintSynthesizer<F> for Compiler<F> {
|
||||||
fn generate_constraints<CS: ConstraintSystem<F>>(
|
fn generate_constraints<CS: ConstraintSystem<F>>(
|
||||||
self,
|
self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
|
@ -8,21 +8,21 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, boolean::Boolean, eq::EqGadget},
|
utilities::{alloc::AllocGadget, boolean::Boolean, eq::EqGadget},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn bool_from_input(
|
pub(crate) fn bool_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
input_value: Option<InputValue<F, G>>,
|
input_value: Option<InputValue<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
) -> Result<ConstrainedValue<F>, BooleanError> {
|
||||||
// Check that the input value is the correct type
|
// Check that the input value is the correct type
|
||||||
let bool_value = match input_value {
|
let bool_value = match input_value {
|
||||||
Some(input) => {
|
Some(input) => {
|
||||||
@ -49,13 +49,13 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
Ok(ConstrainedValue::Boolean(number))
|
Ok(ConstrainedValue::Boolean(number))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_boolean_constant(bool: Boolean) -> ConstrainedValue<F, G> {
|
pub(crate) fn get_boolean_constant(bool: Boolean) -> ConstrainedValue<F> {
|
||||||
ConstrainedValue::Boolean(bool)
|
ConstrainedValue::Boolean(bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn evaluate_not(
|
pub(crate) fn evaluate_not(
|
||||||
value: ConstrainedValue<F, G>,
|
value: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
) -> Result<ConstrainedValue<F>, BooleanError> {
|
||||||
match value {
|
match value {
|
||||||
ConstrainedValue::Boolean(boolean) => Ok(ConstrainedValue::Boolean(boolean.not())),
|
ConstrainedValue::Boolean(boolean) => Ok(ConstrainedValue::Boolean(boolean.not())),
|
||||||
value => Err(BooleanError::CannotEvaluate(format!("!{}", value))),
|
value => Err(BooleanError::CannotEvaluate(format!("!{}", value))),
|
||||||
@ -65,9 +65,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
pub(crate) fn enforce_or(
|
pub(crate) fn enforce_or(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
) -> Result<ConstrainedValue<F>, BooleanError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) => Ok(
|
(ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) => Ok(
|
||||||
ConstrainedValue::Boolean(Boolean::or(cs, &left_bool, &right_bool)?),
|
ConstrainedValue::Boolean(Boolean::or(cs, &left_bool, &right_bool)?),
|
||||||
@ -82,9 +82,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
pub(crate) fn enforce_and(
|
pub(crate) fn enforce_and(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, BooleanError> {
|
) -> Result<ConstrainedValue<F>, BooleanError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) => Ok(
|
(ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) => Ok(
|
||||||
ConstrainedValue::Boolean(Boolean::and(cs, &left_bool, &right_bool)?),
|
ConstrainedValue::Boolean(Boolean::and(cs, &left_bool, &right_bool)?),
|
||||||
@ -96,7 +96,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn boolean_eq(left: Boolean, right: Boolean) -> ConstrainedValue<F, G> {
|
pub(crate) fn boolean_eq(left: Boolean, right: Boolean) -> ConstrainedValue<F> {
|
||||||
ConstrainedValue::Boolean(Boolean::Constant(left.eq(&right)))
|
ConstrainedValue::Boolean(Boolean::Constant(left.eq(&right)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,22 +12,22 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{boolean::Boolean, select::CondSelectGadget},
|
utilities::{boolean::Boolean, select::CondSelectGadget},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
/// Enforce a variable expression by getting the resolved value
|
/// Enforce a variable expression by getting the resolved value
|
||||||
pub(crate) fn evaluate_identifier(
|
pub(crate) fn evaluate_identifier(
|
||||||
&mut self,
|
&mut self,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
unresolved_identifier: Identifier<F, G>,
|
unresolved_identifier: Identifier<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
// Evaluate the identifier name in the current function scope
|
// Evaluate the identifier name in the current function scope
|
||||||
let variable_name = new_scope(function_scope, unresolved_identifier.to_string());
|
let variable_name = new_scope(function_scope, unresolved_identifier.to_string());
|
||||||
let identifier_name = new_scope(file_scope, unresolved_identifier.to_string());
|
let identifier_name = new_scope(file_scope, unresolved_identifier.to_string());
|
||||||
@ -53,9 +53,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn enforce_add_expression(
|
fn enforce_add_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
||||||
Ok(Self::enforce_integer_add(cs, num_1, num_2)?)
|
Ok(Self::enforce_integer_add(cs, num_1, num_2)?)
|
||||||
@ -63,9 +63,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
|
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
|
||||||
Ok(self.enforce_field_add(cs, fe_1, fe_2)?)
|
Ok(self.enforce_field_add(cs, fe_1, fe_2)?)
|
||||||
}
|
}
|
||||||
(ConstrainedValue::GroupElement(ge_1), ConstrainedValue::GroupElement(ge_2)) => {
|
// (ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
|
||||||
Ok(Self::evaluate_group_add(ge_1, ge_2))
|
// Ok(Self::evaluate_group_add(ge_1, ge_2))
|
||||||
}
|
// }
|
||||||
(ConstrainedValue::Unresolved(string), val_2) => {
|
(ConstrainedValue::Unresolved(string), val_2) => {
|
||||||
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
||||||
self.enforce_add_expression(cs, val_1, val_2)
|
self.enforce_add_expression(cs, val_1, val_2)
|
||||||
@ -84,9 +84,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn enforce_sub_expression(
|
fn enforce_sub_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
||||||
Ok(Self::enforce_integer_sub(cs, num_1, num_2)?)
|
Ok(Self::enforce_integer_sub(cs, num_1, num_2)?)
|
||||||
@ -94,9 +94,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
|
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
|
||||||
Ok(self.enforce_field_sub(cs, fe_1, fe_2)?)
|
Ok(self.enforce_field_sub(cs, fe_1, fe_2)?)
|
||||||
}
|
}
|
||||||
(ConstrainedValue::GroupElement(ge_1), ConstrainedValue::GroupElement(ge_2)) => {
|
// (ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
|
||||||
Ok(Self::evaluate_group_sub(ge_1, ge_2))
|
// Ok(Self::evaluate_group_sub(ge_1, ge_2))
|
||||||
}
|
// }
|
||||||
(ConstrainedValue::Unresolved(string), val_2) => {
|
(ConstrainedValue::Unresolved(string), val_2) => {
|
||||||
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
||||||
self.enforce_sub_expression(cs, val_1, val_2)
|
self.enforce_sub_expression(cs, val_1, val_2)
|
||||||
@ -115,9 +115,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn enforce_mul_expression(
|
fn enforce_mul_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
||||||
Ok(Self::enforce_integer_mul(cs, num_1, num_2)?)
|
Ok(Self::enforce_integer_mul(cs, num_1, num_2)?)
|
||||||
@ -148,9 +148,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn enforce_div_expression(
|
fn enforce_div_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
||||||
Ok(Self::enforce_integer_div(cs, num_1, num_2)?)
|
Ok(Self::enforce_integer_div(cs, num_1, num_2)?)
|
||||||
@ -177,9 +177,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn enforce_pow_expression(
|
fn enforce_pow_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
(ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => {
|
||||||
Ok(Self::enforce_integer_pow(cs, num_1, num_2)?)
|
Ok(Self::enforce_integer_pow(cs, num_1, num_2)?)
|
||||||
@ -208,9 +208,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
/// Evaluate Boolean operations
|
/// Evaluate Boolean operations
|
||||||
fn evaluate_eq_expression(
|
fn evaluate_eq_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(ConstrainedValue::Boolean(bool_1), ConstrainedValue::Boolean(bool_2)) => {
|
(ConstrainedValue::Boolean(bool_1), ConstrainedValue::Boolean(bool_2)) => {
|
||||||
Ok(Self::boolean_eq(bool_1, bool_2))
|
Ok(Self::boolean_eq(bool_1, bool_2))
|
||||||
@ -221,9 +221,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
||||||
// Self::field_eq(fe_1, fe_2)
|
// Self::field_eq(fe_1, fe_2)
|
||||||
// }
|
// }
|
||||||
(ConstrainedValue::GroupElement(ge_1), ConstrainedValue::GroupElement(ge_2)) => {
|
// (ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
|
||||||
Ok(Self::evaluate_group_eq(ge_1, ge_2))
|
// Ok(Self::evaluate_group_eq(ge_1, ge_2))
|
||||||
}
|
// }
|
||||||
(ConstrainedValue::Unresolved(string), val_2) => {
|
(ConstrainedValue::Unresolved(string), val_2) => {
|
||||||
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
||||||
self.evaluate_eq_expression(val_1, val_2)
|
self.evaluate_eq_expression(val_1, val_2)
|
||||||
@ -241,9 +241,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
|
|
||||||
fn evaluate_geq_expression(
|
fn evaluate_geq_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
||||||
// Self::field_geq(fe_1, fe_2)
|
// Self::field_geq(fe_1, fe_2)
|
||||||
@ -265,9 +265,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
|
|
||||||
fn evaluate_gt_expression(
|
fn evaluate_gt_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
||||||
// Self::field_gt(fe_1, fe_2)
|
// Self::field_gt(fe_1, fe_2)
|
||||||
@ -289,9 +289,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
|
|
||||||
fn evaluate_leq_expression(
|
fn evaluate_leq_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
||||||
// Self::field_leq(fe_1, fe_2)
|
// Self::field_leq(fe_1, fe_2)
|
||||||
@ -313,9 +313,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
|
|
||||||
fn evaluate_lt_expression(
|
fn evaluate_lt_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
|
||||||
// Self::field_lt(fe_1, fe_2)
|
// Self::field_lt(fe_1, fe_2)
|
||||||
@ -341,11 +341,11 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
first: Expression<F, G>,
|
first: Expression<F>,
|
||||||
second: Expression<F, G>,
|
second: Expression<F>,
|
||||||
third: Expression<F, G>,
|
third: Expression<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
let resolved_first = match self.enforce_expression(
|
let resolved_first = match self.enforce_expression(
|
||||||
cs,
|
cs,
|
||||||
file_scope.clone(),
|
file_scope.clone(),
|
||||||
@ -392,9 +392,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
array: Vec<Box<SpreadOrExpression<F, G>>>,
|
array: Vec<Box<SpreadOrExpression<F>>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
// Check explicit array type dimension if given
|
// Check explicit array type dimension if given
|
||||||
let mut expected_types = expected_types.clone();
|
let mut expected_types = expected_types.clone();
|
||||||
let expected_dimensions = vec![];
|
let expected_dimensions = vec![];
|
||||||
@ -456,7 +456,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
index: Expression<F, G>,
|
index: Expression<F>,
|
||||||
) -> Result<usize, ExpressionError> {
|
) -> Result<usize, ExpressionError> {
|
||||||
let expected_types = vec![Type::IntegerType(IntegerType::U32)];
|
let expected_types = vec![Type::IntegerType(IntegerType::U32)];
|
||||||
match self.enforce_branch(
|
match self.enforce_branch(
|
||||||
@ -476,10 +476,10 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
array: Box<Expression<F, G>>,
|
array: Box<Expression<F>>,
|
||||||
index: RangeOrExpression<F, G>,
|
index: RangeOrExpression<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
let array = match self.enforce_branch(
|
let array = match self.enforce_branch(
|
||||||
cs,
|
cs,
|
||||||
file_scope.clone(),
|
file_scope.clone(),
|
||||||
@ -517,9 +517,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
identifier: Identifier<F, G>,
|
identifier: Identifier<F>,
|
||||||
members: Vec<CircuitFieldDefinition<F, G>>,
|
members: Vec<CircuitFieldDefinition<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
let mut program_identifier = new_scope(file_scope.clone(), identifier.to_string());
|
let mut program_identifier = new_scope(file_scope.clone(), identifier.to_string());
|
||||||
|
|
||||||
if identifier.is_self() {
|
if identifier.is_self() {
|
||||||
@ -591,10 +591,10 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
circuit_identifier: Box<Expression<F, G>>,
|
circuit_identifier: Box<Expression<F>>,
|
||||||
circuit_member: Identifier<F, G>,
|
circuit_member: Identifier<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
let (circuit_name, members) = match self.enforce_branch(
|
let (circuit_name, members) = match self.enforce_branch(
|
||||||
cs,
|
cs,
|
||||||
file_scope.clone(),
|
file_scope.clone(),
|
||||||
@ -652,10 +652,10 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
circuit_identifier: Box<Expression<F, G>>,
|
circuit_identifier: Box<Expression<F>>,
|
||||||
circuit_member: Identifier<F, G>,
|
circuit_member: Identifier<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
// Get defined circuit
|
// Get defined circuit
|
||||||
let circuit = match self.enforce_expression(
|
let circuit = match self.enforce_expression(
|
||||||
cs,
|
cs,
|
||||||
@ -706,10 +706,10 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
function: Box<Expression<F, G>>,
|
function: Box<Expression<F>>,
|
||||||
arguments: Vec<Expression<F, G>>,
|
arguments: Vec<Expression<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
let function_value = self.enforce_expression(
|
let function_value = self.enforce_expression(
|
||||||
cs,
|
cs,
|
||||||
file_scope.clone(),
|
file_scope.clone(),
|
||||||
@ -745,9 +745,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn enforce_number_implicit(
|
pub(crate) fn enforce_number_implicit(
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
value: String,
|
value: String,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
if expected_types.len() == 1 {
|
if expected_types.len() == 1 {
|
||||||
return Ok(ConstrainedValue::from_type(value, &expected_types[0])?);
|
return Ok(ConstrainedValue::from_type(value, &expected_types[0])?);
|
||||||
}
|
}
|
||||||
@ -763,9 +763,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
expression: Expression<F, G>,
|
expression: Expression<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
let mut branch =
|
let mut branch =
|
||||||
self.enforce_expression(cs, file_scope, function_scope, expected_types, expression)?;
|
self.enforce_expression(cs, file_scope, function_scope, expected_types, expression)?;
|
||||||
|
|
||||||
@ -780,10 +780,10 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
left: Expression<F, G>,
|
left: Expression<F>,
|
||||||
right: Expression<F, G>,
|
right: Expression<F>,
|
||||||
) -> Result<(ConstrainedValue<F, G>, ConstrainedValue<F, G>), ExpressionError> {
|
) -> Result<(ConstrainedValue<F>, ConstrainedValue<F>), ExpressionError> {
|
||||||
let resolved_left = self.enforce_branch(
|
let resolved_left = self.enforce_branch(
|
||||||
cs,
|
cs,
|
||||||
file_scope.clone(),
|
file_scope.clone(),
|
||||||
@ -807,9 +807,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expected_types: &Vec<Type<F, G>>,
|
expected_types: &Vec<Type<F>>,
|
||||||
expression: Expression<F, G>,
|
expression: Expression<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
) -> Result<ConstrainedValue<F>, ExpressionError> {
|
||||||
match expression {
|
match expression {
|
||||||
// Variables
|
// Variables
|
||||||
Expression::Identifier(unresolved_variable) => self.evaluate_identifier(
|
Expression::Identifier(unresolved_variable) => self.evaluate_identifier(
|
||||||
@ -822,7 +822,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
// Values
|
// Values
|
||||||
Expression::Integer(integer) => Ok(Self::get_integer_constant(integer)),
|
Expression::Integer(integer) => Ok(Self::get_integer_constant(integer)),
|
||||||
Expression::FieldElement(fe) => Ok(Self::get_field_element_constant(fe)),
|
Expression::FieldElement(fe) => Ok(Self::get_field_element_constant(fe)),
|
||||||
Expression::GroupElement(gr) => Ok(ConstrainedValue::GroupElement(gr)),
|
Expression::Group(gr) => Ok(ConstrainedValue::Group(gr)),
|
||||||
Expression::Boolean(bool) => Ok(Self::get_boolean_constant(bool)),
|
Expression::Boolean(bool) => Ok(Self::get_boolean_constant(bool)),
|
||||||
Expression::Implicit(value) => Self::enforce_number_implicit(expected_types, value),
|
Expression::Implicit(value) => Self::enforce_number_implicit(expected_types, value),
|
||||||
|
|
||||||
|
@ -8,18 +8,18 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::{ConstraintSystem, LinearCombination, Variable as R1CSVariable},
|
gadgets::r1cs::{ConstraintSystem, LinearCombination, Variable as R1CSVariable},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn field_element_from_input(
|
pub(crate) fn field_element_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
input_value: Option<InputValue<F, G>>,
|
input_value: Option<InputValue<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
) -> Result<ConstrainedValue<F>, FieldElementError> {
|
||||||
// Check that the parameter value is the correct type
|
// Check that the parameter value is the correct type
|
||||||
let field_option = match input_value {
|
let field_option = match input_value {
|
||||||
Some(input) => {
|
Some(input) => {
|
||||||
@ -51,7 +51,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_field_element_constant(fe: FieldElement<F>) -> ConstrainedValue<F, G> {
|
pub(crate) fn get_field_element_constant(fe: FieldElement<F>) -> ConstrainedValue<F> {
|
||||||
ConstrainedValue::FieldElement(fe)
|
ConstrainedValue::FieldElement(fe)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
fe_1: FieldElement<F>,
|
fe_1: FieldElement<F>,
|
||||||
fe_2: FieldElement<F>,
|
fe_2: FieldElement<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
) -> Result<ConstrainedValue<F>, FieldElementError> {
|
||||||
Ok(match (fe_1, fe_2) {
|
Ok(match (fe_1, fe_2) {
|
||||||
// if both constants, then return a constant result
|
// if both constants, then return a constant result
|
||||||
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
||||||
@ -201,7 +201,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
fe_1: FieldElement<F>,
|
fe_1: FieldElement<F>,
|
||||||
fe_2: FieldElement<F>,
|
fe_2: FieldElement<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
) -> Result<ConstrainedValue<F>, FieldElementError> {
|
||||||
Ok(match (fe_1, fe_2) {
|
Ok(match (fe_1, fe_2) {
|
||||||
// if both constants, then return a constant result
|
// if both constants, then return a constant result
|
||||||
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
||||||
@ -278,7 +278,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
fe_1: FieldElement<F>,
|
fe_1: FieldElement<F>,
|
||||||
fe_2: FieldElement<F>,
|
fe_2: FieldElement<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
) -> Result<ConstrainedValue<F>, FieldElementError> {
|
||||||
Ok(match (fe_1, fe_2) {
|
Ok(match (fe_1, fe_2) {
|
||||||
// if both constants, then return a constant result
|
// if both constants, then return a constant result
|
||||||
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
||||||
@ -355,7 +355,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
fe_1: FieldElement<F>,
|
fe_1: FieldElement<F>,
|
||||||
fe_2: FieldElement<F>,
|
fe_2: FieldElement<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
) -> Result<ConstrainedValue<F>, FieldElementError> {
|
||||||
Ok(match (fe_1, fe_2) {
|
Ok(match (fe_1, fe_2) {
|
||||||
// if both constants, then return a constant result
|
// if both constants, then return a constant result
|
||||||
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
(FieldElement::Constant(fe_1_constant), FieldElement::Constant(fe_2_constant)) => {
|
||||||
@ -443,7 +443,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
fe_1: FieldElement<F>,
|
fe_1: FieldElement<F>,
|
||||||
num: Integer,
|
num: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, FieldElementError> {
|
) -> Result<ConstrainedValue<F>, FieldElementError> {
|
||||||
Ok(match fe_1 {
|
Ok(match fe_1 {
|
||||||
// if both constants, then return a constant result
|
// if both constants, then return a constant result
|
||||||
FieldElement::Constant(fe_1_constant) => ConstrainedValue::FieldElement(
|
FieldElement::Constant(fe_1_constant) => ConstrainedValue::FieldElement(
|
||||||
|
@ -8,11 +8,11 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::ConstraintSystem,
|
gadgets::r1cs::ConstraintSystem,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
fn check_arguments_length(expected: usize, actual: usize) -> Result<(), FunctionError> {
|
fn check_arguments_length(expected: usize, actual: usize) -> Result<(), FunctionError> {
|
||||||
// Make sure we are given the correct number of arguments
|
// Make sure we are given the correct number of arguments
|
||||||
if expected != actual {
|
if expected != actual {
|
||||||
@ -28,9 +28,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
scope: String,
|
scope: String,
|
||||||
caller_scope: String,
|
caller_scope: String,
|
||||||
function_name: String,
|
function_name: String,
|
||||||
expected_types: Vec<Type<F, G>>,
|
expected_types: Vec<Type<F>>,
|
||||||
input: Expression<F, G>,
|
input: Expression<F>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
) -> Result<ConstrainedValue<F>, FunctionError> {
|
||||||
// Evaluate the function input value as pass by value from the caller or
|
// Evaluate the function input value as pass by value from the caller or
|
||||||
// evaluate as an expression in the current function scope
|
// evaluate as an expression in the current function scope
|
||||||
match input {
|
match input {
|
||||||
@ -55,9 +55,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
scope: String,
|
scope: String,
|
||||||
caller_scope: String,
|
caller_scope: String,
|
||||||
function: Function<F, G>,
|
function: Function<F>,
|
||||||
inputs: Vec<Expression<F, G>>,
|
inputs: Vec<Expression<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
) -> Result<ConstrainedValue<F>, FunctionError> {
|
||||||
let function_name = new_scope(scope.clone(), function.get_name());
|
let function_name = new_scope(scope.clone(), function.get_name());
|
||||||
|
|
||||||
// Make sure we are given the correct number of inputs
|
// Make sure we are given the correct number of inputs
|
||||||
@ -116,10 +116,10 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
array_type: Type<F, G>,
|
array_type: Type<F>,
|
||||||
array_dimensions: Vec<usize>,
|
array_dimensions: Vec<usize>,
|
||||||
input_value: Option<InputValue<F, G>>,
|
input_value: Option<InputValue<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
) -> Result<ConstrainedValue<F>, FunctionError> {
|
||||||
let expected_length = array_dimensions[0];
|
let expected_length = array_dimensions[0];
|
||||||
let mut array_value = vec![];
|
let mut array_value = vec![];
|
||||||
|
|
||||||
@ -168,11 +168,11 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn allocate_main_function_input(
|
fn allocate_main_function_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
_type: Type<F, G>,
|
_type: Type<F>,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
input_value: Option<InputValue<F, G>>,
|
input_value: Option<InputValue<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
) -> Result<ConstrainedValue<F>, FunctionError> {
|
||||||
match _type {
|
match _type {
|
||||||
Type::IntegerType(integer_type) => {
|
Type::IntegerType(integer_type) => {
|
||||||
Ok(self.integer_from_parameter(cs, integer_type, name, private, input_value)?)
|
Ok(self.integer_from_parameter(cs, integer_type, name, private, input_value)?)
|
||||||
@ -180,9 +180,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
Type::FieldElement => {
|
Type::FieldElement => {
|
||||||
Ok(self.field_element_from_input(cs, name, private, input_value)?)
|
Ok(self.field_element_from_input(cs, name, private, input_value)?)
|
||||||
}
|
}
|
||||||
Type::GroupElement => {
|
// Type::Group => {
|
||||||
Ok(self.group_element_from_input(cs, name, private, input_value)?)
|
// Ok(self.group_element_from_input(cs, name, private, input_value)?)
|
||||||
}
|
// }
|
||||||
Type::Boolean => Ok(self.bool_from_input(cs, name, private, input_value)?),
|
Type::Boolean => Ok(self.bool_from_input(cs, name, private, input_value)?),
|
||||||
Type::Array(_type, dimensions) => {
|
Type::Array(_type, dimensions) => {
|
||||||
self.allocate_array(cs, name, private, *_type, dimensions, input_value)
|
self.allocate_array(cs, name, private, *_type, dimensions, input_value)
|
||||||
@ -195,9 +195,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
scope: String,
|
scope: String,
|
||||||
function: Function<F, G>,
|
function: Function<F>,
|
||||||
inputs: Vec<Option<InputValue<F, G>>>,
|
inputs: Vec<Option<InputValue<F>>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, FunctionError> {
|
) -> Result<ConstrainedValue<F>, FunctionError> {
|
||||||
let function_name = new_scope(scope.clone(), function.get_name());
|
let function_name = new_scope(scope.clone(), function.get_name());
|
||||||
|
|
||||||
// Make sure we are given the correct number of inputs
|
// Make sure we are given the correct number of inputs
|
||||||
@ -231,7 +231,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
pub(crate) fn resolve_definitions(
|
pub(crate) fn resolve_definitions(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
program: Program<F, G>,
|
program: Program<F>,
|
||||||
) -> Result<(), ImportError> {
|
) -> Result<(), ImportError> {
|
||||||
let program_name = program.name.clone();
|
let program_name = program.name.clone();
|
||||||
|
|
||||||
|
1
compiler/src/constraints/group.rs
Normal file
1
compiler/src/constraints/group.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
@ -1,67 +0,0 @@
|
|||||||
use crate::errors::GroupElementError;
|
|
||||||
use crate::{ConstrainedProgram, ConstrainedValue, InputValue};
|
|
||||||
|
|
||||||
use snarkos_models::{
|
|
||||||
curves::{Field, Group, PrimeField},
|
|
||||||
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
|
|
||||||
};
|
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
|
||||||
pub(crate) fn group_element_from_input(
|
|
||||||
&mut self,
|
|
||||||
_cs: &mut CS,
|
|
||||||
_name: String,
|
|
||||||
_private: bool,
|
|
||||||
input_value: Option<InputValue<F, G>>,
|
|
||||||
) -> Result<ConstrainedValue<F, G>, GroupElementError> {
|
|
||||||
// Check that the parameter value is the correct type
|
|
||||||
// let group_option = match input_value {
|
|
||||||
// Some(input) => {
|
|
||||||
// if let InputValue::Group(group) = input {
|
|
||||||
// Some(group)
|
|
||||||
// } else {
|
|
||||||
// return Err(GroupElementError::InvalidGroup(input.to_string()));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// None => None,
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// // Check visibility of parameter
|
|
||||||
// let group_value = if private {
|
|
||||||
// cs.alloc(
|
|
||||||
// || name,
|
|
||||||
// || group_option.ok_or(SynthesisError::AssignmentMissing),
|
|
||||||
// )?
|
|
||||||
// } else {
|
|
||||||
// cs.alloc_input(
|
|
||||||
// || name,
|
|
||||||
// || group_option.ok_or(SynthesisError::AssignmentMissing),
|
|
||||||
// )?
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// Ok(ConstrainedValue::GroupElement())
|
|
||||||
|
|
||||||
// TODO: use group gadget to allocate groups
|
|
||||||
if let Some(InputValue::Group(group)) = input_value {
|
|
||||||
return Ok(ConstrainedValue::GroupElement(group));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ConstrainedValue::GroupElement(G::default()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn evaluate_group_eq(group_element_1: G, group_element_2: G) -> ConstrainedValue<F, G> {
|
|
||||||
ConstrainedValue::Boolean(Boolean::constant(group_element_1.eq(&group_element_2)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn evaluate_group_add(group_element_1: G, group_element_2: G) -> ConstrainedValue<F, G> {
|
|
||||||
ConstrainedValue::GroupElement(group_element_1.add(&group_element_2))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn evaluate_group_sub(group_element_1: G, group_element_2: G) -> ConstrainedValue<F, G> {
|
|
||||||
ConstrainedValue::GroupElement(group_element_1.sub(&group_element_2))
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// pub fn evaluate_group_mul(group_element: G, scalar_field: G::ScalarField) -> ConstrainedValue<F, G> {
|
|
||||||
// ConstrainedValue::GroupElement(group_element.mul(&scalar_field))
|
|
||||||
// }
|
|
||||||
}
|
|
@ -9,18 +9,18 @@ use crate::{
|
|||||||
|
|
||||||
use from_pest::FromPest;
|
use from_pest::FromPest;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::ConstraintSystem,
|
gadgets::r1cs::ConstraintSystem,
|
||||||
};
|
};
|
||||||
use std::env::current_dir;
|
use std::env::current_dir;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub fn enforce_import(
|
pub fn enforce_import(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
scope: String,
|
scope: String,
|
||||||
import: Import<F, G>,
|
import: Import<F>,
|
||||||
) -> Result<(), ImportError> {
|
) -> Result<(), ImportError> {
|
||||||
let path = current_dir().map_err(|error| ImportError::DirectoryError(error))?;
|
let path = current_dir().map_err(|error| ImportError::DirectoryError(error))?;
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{
|
utilities::{
|
||||||
@ -73,15 +73,15 @@ impl<F: Field + PrimeField> CondSelectGadget<F> for Integer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn get_integer_constant(integer: Integer) -> ConstrainedValue<F, G> {
|
pub(crate) fn get_integer_constant(integer: Integer) -> ConstrainedValue<F> {
|
||||||
ConstrainedValue::Integer(integer)
|
ConstrainedValue::Integer(integer)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn evaluate_integer_eq(
|
pub(crate) fn evaluate_integer_eq(
|
||||||
left: Integer,
|
left: Integer,
|
||||||
right: Integer,
|
right: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
Ok(ConstrainedValue::Boolean(Boolean::Constant(
|
Ok(ConstrainedValue::Boolean(Boolean::Constant(
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(Integer::U8(left_u8), Integer::U8(right_u8)) => left_u8.eq(&right_u8),
|
(Integer::U8(left_u8), Integer::U8(right_u8)) => left_u8.eq(&right_u8),
|
||||||
@ -105,8 +105,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
integer_type: IntegerType,
|
integer_type: IntegerType,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
integer_value: Option<InputValue<F, G>>,
|
integer_value: Option<InputValue<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
// Check that the input value is the correct type
|
// Check that the input value is the correct type
|
||||||
let integer_option = match integer_value {
|
let integer_option = match integer_value {
|
||||||
Some(input) => {
|
Some(input) => {
|
||||||
@ -162,7 +162,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: Integer,
|
left: Integer,
|
||||||
right: Integer,
|
right: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
Ok(ConstrainedValue::Integer(match (left, right) {
|
Ok(ConstrainedValue::Integer(match (left, right) {
|
||||||
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
||||||
Integer::U8(Self::enforce_u8_add(cs, left_u8, right_u8)?)
|
Integer::U8(Self::enforce_u8_add(cs, left_u8, right_u8)?)
|
||||||
@ -188,7 +188,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: Integer,
|
left: Integer,
|
||||||
right: Integer,
|
right: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
Ok(ConstrainedValue::Integer(match (left, right) {
|
Ok(ConstrainedValue::Integer(match (left, right) {
|
||||||
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
||||||
Integer::U8(Self::enforce_u8_sub(cs, left_u8, right_u8)?)
|
Integer::U8(Self::enforce_u8_sub(cs, left_u8, right_u8)?)
|
||||||
@ -214,7 +214,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: Integer,
|
left: Integer,
|
||||||
right: Integer,
|
right: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
Ok(ConstrainedValue::Integer(match (left, right) {
|
Ok(ConstrainedValue::Integer(match (left, right) {
|
||||||
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
||||||
Integer::U8(Self::enforce_u8_mul(cs, left_u8, right_u8)?)
|
Integer::U8(Self::enforce_u8_mul(cs, left_u8, right_u8)?)
|
||||||
@ -240,7 +240,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: Integer,
|
left: Integer,
|
||||||
right: Integer,
|
right: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
Ok(ConstrainedValue::Integer(match (left, right) {
|
Ok(ConstrainedValue::Integer(match (left, right) {
|
||||||
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
||||||
Integer::U8(Self::enforce_u8_div(cs, left_u8, right_u8)?)
|
Integer::U8(Self::enforce_u8_div(cs, left_u8, right_u8)?)
|
||||||
@ -266,7 +266,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: Integer,
|
left: Integer,
|
||||||
right: Integer,
|
right: Integer,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
Ok(ConstrainedValue::Integer(match (left, right) {
|
Ok(ConstrainedValue::Integer(match (left, right) {
|
||||||
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
(Integer::U8(left_u8), Integer::U8(right_u8)) => {
|
||||||
Integer::U8(Self::enforce_u8_pow(cs, left_u8, right_u8)?)
|
Integer::U8(Self::enforce_u8_pow(cs, left_u8, right_u8)?)
|
||||||
|
@ -8,21 +8,21 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, eq::EqGadget, uint128::UInt128},
|
utilities::{alloc::AllocGadget, eq::EqGadget, uint128::UInt128},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn u128_from_input(
|
pub(crate) fn u128_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
integer_option: Option<usize>,
|
integer_option: Option<usize>,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
// Type cast to u128 in rust.
|
// Type cast to u128 in rust.
|
||||||
// If this fails should we return our own error?
|
// If this fails should we return our own error?
|
||||||
let u128_option = integer_option.map(|integer| integer as u128);
|
let u128_option = integer_option.map(|integer| integer as u128);
|
||||||
|
@ -8,21 +8,21 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, eq::EqGadget, uint16::UInt16},
|
utilities::{alloc::AllocGadget, eq::EqGadget, uint16::UInt16},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn u16_from_input(
|
pub(crate) fn u16_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
integer_option: Option<usize>,
|
integer_option: Option<usize>,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
// Type cast to u16 in rust.
|
// Type cast to u16 in rust.
|
||||||
// If this fails should we return our own error?
|
// If this fails should we return our own error?
|
||||||
let u16_option = integer_option.map(|integer| integer as u16);
|
let u16_option = integer_option.map(|integer| integer as u16);
|
||||||
|
@ -8,21 +8,21 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, eq::EqGadget, uint32::UInt32},
|
utilities::{alloc::AllocGadget, eq::EqGadget, uint32::UInt32},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn u32_from_input(
|
pub(crate) fn u32_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
integer_option: Option<usize>,
|
integer_option: Option<usize>,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
// Type cast to integers.u32 in rust.
|
// Type cast to integers.u32 in rust.
|
||||||
// If this fails should we return our own error?
|
// If this fails should we return our own error?
|
||||||
let u32_option = integer_option.map(|integer| integer as u32);
|
let u32_option = integer_option.map(|integer| integer as u32);
|
||||||
|
@ -8,21 +8,21 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, eq::EqGadget, uint64::UInt64},
|
utilities::{alloc::AllocGadget, eq::EqGadget, uint64::UInt64},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn u64_from_input(
|
pub(crate) fn u64_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
integer_option: Option<usize>,
|
integer_option: Option<usize>,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
// Type cast to u64 in rust.
|
// Type cast to u64 in rust.
|
||||||
// If this fails should we return our own error?
|
// If this fails should we return our own error?
|
||||||
let u64_option = integer_option.map(|integer| integer as u64);
|
let u64_option = integer_option.map(|integer| integer as u64);
|
||||||
|
@ -8,21 +8,21 @@ use crate::{
|
|||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{
|
gadgets::{
|
||||||
r1cs::ConstraintSystem,
|
r1cs::ConstraintSystem,
|
||||||
utilities::{alloc::AllocGadget, eq::EqGadget, uint8::UInt8},
|
utilities::{alloc::AllocGadget, eq::EqGadget, uint8::UInt8},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub(crate) fn u8_from_input(
|
pub(crate) fn u8_from_input(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
name: String,
|
name: String,
|
||||||
private: bool,
|
private: bool,
|
||||||
integer_option: Option<usize>,
|
integer_option: Option<usize>,
|
||||||
) -> Result<ConstrainedValue<F, G>, IntegerError> {
|
) -> Result<ConstrainedValue<F>, IntegerError> {
|
||||||
// Type cast to u8 in rust.
|
// Type cast to u8 in rust.
|
||||||
// If this fails should we return our own error?
|
// If this fails should we return our own error?
|
||||||
let u8_option = integer_option.map(|integer| integer as u8);
|
let u8_option = integer_option.map(|integer| integer as u8);
|
||||||
|
@ -18,8 +18,8 @@ pub use integer::*;
|
|||||||
pub mod field_element;
|
pub mod field_element;
|
||||||
pub use field_element::*;
|
pub use field_element::*;
|
||||||
|
|
||||||
pub mod group_element;
|
pub mod group;
|
||||||
pub use group_element::*;
|
pub use group::*;
|
||||||
|
|
||||||
pub mod program;
|
pub mod program;
|
||||||
pub use program::*;
|
pub use program::*;
|
||||||
@ -36,15 +36,15 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::ConstraintSystem,
|
gadgets::r1cs::ConstraintSystem,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn generate_constraints<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>>(
|
pub fn generate_constraints<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
program: Program<F, G>,
|
program: Program<F>,
|
||||||
parameters: Vec<Option<InputValue<F, G>>>,
|
parameters: Vec<Option<InputValue<F>>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, CompilerError> {
|
) -> Result<ConstrainedValue<F>, CompilerError> {
|
||||||
let mut resolved_program = ConstrainedProgram::new();
|
let mut resolved_program = ConstrainedProgram::new();
|
||||||
let program_name = program.get_name();
|
let program_name = program.get_name();
|
||||||
let main_function_name = new_scope(program_name.clone(), "main".into());
|
let main_function_name = new_scope(program_name.clone(), "main".into());
|
||||||
|
@ -3,13 +3,13 @@
|
|||||||
use crate::constraints::ConstrainedValue;
|
use crate::constraints::ConstrainedValue;
|
||||||
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::r1cs::ConstraintSystem,
|
gadgets::r1cs::ConstraintSystem,
|
||||||
};
|
};
|
||||||
use std::{collections::HashMap, marker::PhantomData};
|
use std::{collections::HashMap, marker::PhantomData};
|
||||||
|
|
||||||
pub struct ConstrainedProgram<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> {
|
pub struct ConstrainedProgram<F: Field + PrimeField, CS: ConstraintSystem<F>> {
|
||||||
pub identifiers: HashMap<String, ConstrainedValue<F, G>>,
|
pub identifiers: HashMap<String, ConstrainedValue<F>>,
|
||||||
pub _cs: PhantomData<CS>,
|
pub _cs: PhantomData<CS>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ pub fn new_scope(outer: String, inner: String) -> String {
|
|||||||
format!("{}_{}", outer, inner)
|
format!("{}_{}", outer, inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
identifiers: HashMap::new(),
|
identifiers: HashMap::new(),
|
||||||
@ -25,15 +25,15 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn store(&mut self, name: String, value: ConstrainedValue<F, G>) {
|
pub(crate) fn store(&mut self, name: String, value: ConstrainedValue<F>) {
|
||||||
self.identifiers.insert(name, value);
|
self.identifiers.insert(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get(&self, name: &String) -> Option<&ConstrainedValue<F, G>> {
|
pub(crate) fn get(&self, name: &String) -> Option<&ConstrainedValue<F>> {
|
||||||
self.identifiers.get(name)
|
self.identifiers.get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_mut(&mut self, name: &String) -> Option<&mut ConstrainedValue<F, G>> {
|
pub(crate) fn get_mut(&mut self, name: &String) -> Option<&mut ConstrainedValue<F>> {
|
||||||
self.identifiers.get_mut(name)
|
self.identifiers.get_mut(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,12 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean, utilities::uint32::UInt32},
|
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean, utilities::uint32::UInt32},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
|
impl<F: Field + PrimeField, CS: ConstraintSystem<F>> ConstrainedProgram<F, CS> {
|
||||||
fn resolve_assignee(&mut self, scope: String, assignee: Assignee<F, G>) -> String {
|
fn resolve_assignee(&mut self, scope: String, assignee: Assignee<F>) -> String {
|
||||||
match assignee {
|
match assignee {
|
||||||
Assignee::Identifier(name) => new_scope(scope, name.to_string()),
|
Assignee::Identifier(name) => new_scope(scope, name.to_string()),
|
||||||
Assignee::Array(array, _index) => self.resolve_assignee(scope, *array),
|
Assignee::Array(array, _index) => self.resolve_assignee(scope, *array),
|
||||||
@ -30,7 +30,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn get_mutable_assignee(
|
fn get_mutable_assignee(
|
||||||
&mut self,
|
&mut self,
|
||||||
name: String,
|
name: String,
|
||||||
) -> Result<&mut ConstrainedValue<F, G>, StatementError> {
|
) -> Result<&mut ConstrainedValue<F>, StatementError> {
|
||||||
// Check that assignee exists and is mutable
|
// Check that assignee exists and is mutable
|
||||||
Ok(match self.get_mut(&name) {
|
Ok(match self.get_mut(&name) {
|
||||||
Some(value) => match value {
|
Some(value) => match value {
|
||||||
@ -47,8 +47,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
name: String,
|
name: String,
|
||||||
range_or_expression: RangeOrExpression<F, G>,
|
range_or_expression: RangeOrExpression<F>,
|
||||||
new_value: ConstrainedValue<F, G>,
|
new_value: ConstrainedValue<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
// Resolve index so we know if we are assigning to a single value or a range of values
|
// Resolve index so we know if we are assigning to a single value or a range of values
|
||||||
match range_or_expression {
|
match range_or_expression {
|
||||||
@ -91,8 +91,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn mutute_circuit_field(
|
fn mutute_circuit_field(
|
||||||
&mut self,
|
&mut self,
|
||||||
circuit_name: String,
|
circuit_name: String,
|
||||||
object_name: Identifier<F, G>,
|
object_name: Identifier<F>,
|
||||||
new_value: ConstrainedValue<F, G>,
|
new_value: ConstrainedValue<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
match self.get_mutable_assignee(circuit_name)? {
|
match self.get_mutable_assignee(circuit_name)? {
|
||||||
ConstrainedValue::CircuitExpression(_variable, members) => {
|
ConstrainedValue::CircuitExpression(_variable, members) => {
|
||||||
@ -129,8 +129,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
assignee: Assignee<F, G>,
|
assignee: Assignee<F>,
|
||||||
expression: Expression<F, G>,
|
expression: Expression<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
// Get the name of the variable we are assigning to
|
// Get the name of the variable we are assigning to
|
||||||
let variable_name = self.resolve_assignee(function_scope.clone(), assignee.clone());
|
let variable_name = self.resolve_assignee(function_scope.clone(), assignee.clone());
|
||||||
@ -170,8 +170,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn store_definition(
|
fn store_definition(
|
||||||
&mut self,
|
&mut self,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
variable: Variable<F, G>,
|
variable: Variable<F>,
|
||||||
mut value: ConstrainedValue<F, G>,
|
mut value: ConstrainedValue<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
// Store with given mutability
|
// Store with given mutability
|
||||||
if variable.mutable {
|
if variable.mutable {
|
||||||
@ -190,8 +190,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
variable: Variable<F, G>,
|
variable: Variable<F>,
|
||||||
expression: Expression<F, G>,
|
expression: Expression<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
let mut expected_types = vec![];
|
let mut expected_types = vec![];
|
||||||
if let Some(ref _type) = variable._type {
|
if let Some(ref _type) = variable._type {
|
||||||
@ -213,8 +213,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
variables: Vec<Variable<F, G>>,
|
variables: Vec<Variable<F>>,
|
||||||
function: Expression<F, G>,
|
function: Expression<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
let mut expected_types = vec![];
|
let mut expected_types = vec![];
|
||||||
for variable in variables.iter() {
|
for variable in variables.iter() {
|
||||||
@ -256,9 +256,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
expressions: Vec<Expression<F, G>>,
|
expressions: Vec<Expression<F>>,
|
||||||
return_types: Vec<Type<F, G>>,
|
return_types: Vec<Type<F>>,
|
||||||
) -> Result<ConstrainedValue<F, G>, StatementError> {
|
) -> Result<ConstrainedValue<F>, StatementError> {
|
||||||
// Make sure we return the correct number of values
|
// Make sure we return the correct number of values
|
||||||
if return_types.len() != expressions.len() {
|
if return_types.len() != expressions.len() {
|
||||||
return Err(StatementError::InvalidNumberOfReturns(
|
return Err(StatementError::InvalidNumberOfReturns(
|
||||||
@ -289,9 +289,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
statements: Vec<Statement<F, G>>,
|
statements: Vec<Statement<F>>,
|
||||||
return_types: Vec<Type<F, G>>,
|
return_types: Vec<Type<F>>,
|
||||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
) -> Result<Option<ConstrainedValue<F>>, StatementError> {
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
// Evaluate statements and possibly return early
|
// Evaluate statements and possibly return early
|
||||||
for statement in statements.iter() {
|
for statement in statements.iter() {
|
||||||
@ -315,9 +315,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
statement: ConditionalStatement<F, G>,
|
statement: ConditionalStatement<F>,
|
||||||
return_types: Vec<Type<F, G>>,
|
return_types: Vec<Type<F>>,
|
||||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
) -> Result<Option<ConstrainedValue<F>>, StatementError> {
|
||||||
let expected_types = vec![Type::Boolean];
|
let expected_types = vec![Type::Boolean];
|
||||||
let condition = match self.enforce_expression(
|
let condition = match self.enforce_expression(
|
||||||
cs,
|
cs,
|
||||||
@ -367,12 +367,12 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
index: Identifier<F, G>,
|
index: Identifier<F>,
|
||||||
start: Integer,
|
start: Integer,
|
||||||
stop: Integer,
|
stop: Integer,
|
||||||
statements: Vec<Statement<F, G>>,
|
statements: Vec<Statement<F>>,
|
||||||
return_types: Vec<Type<F, G>>,
|
return_types: Vec<Type<F>>,
|
||||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
) -> Result<Option<ConstrainedValue<F>>, StatementError> {
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
|
|
||||||
for i in start.to_usize()..stop.to_usize() {
|
for i in start.to_usize()..stop.to_usize() {
|
||||||
@ -403,8 +403,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
fn enforce_assert_eq_statement(
|
fn enforce_assert_eq_statement(
|
||||||
&mut self,
|
&mut self,
|
||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
left: ConstrainedValue<F, G>,
|
left: ConstrainedValue<F>,
|
||||||
right: ConstrainedValue<F, G>,
|
right: ConstrainedValue<F>,
|
||||||
) -> Result<(), StatementError> {
|
) -> Result<(), StatementError> {
|
||||||
Ok(match (left, right) {
|
Ok(match (left, right) {
|
||||||
(ConstrainedValue::Boolean(bool_1), ConstrainedValue::Boolean(bool_2)) => {
|
(ConstrainedValue::Boolean(bool_1), ConstrainedValue::Boolean(bool_2)) => {
|
||||||
@ -440,9 +440,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
|||||||
cs: &mut CS,
|
cs: &mut CS,
|
||||||
file_scope: String,
|
file_scope: String,
|
||||||
function_scope: String,
|
function_scope: String,
|
||||||
statement: Statement<F, G>,
|
statement: Statement<F>,
|
||||||
return_types: Vec<Type<F, G>>,
|
return_types: Vec<Type<F>>,
|
||||||
) -> Result<Option<ConstrainedValue<F, G>>, StatementError> {
|
) -> Result<Option<ConstrainedValue<F>>, StatementError> {
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
match statement {
|
match statement {
|
||||||
Statement::Return(expressions) => {
|
Statement::Return(expressions) => {
|
||||||
|
@ -6,7 +6,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
curves::{Field, Group, PrimeField},
|
curves::{Field, PrimeField},
|
||||||
gadgets::utilities::{
|
gadgets::utilities::{
|
||||||
boolean::Boolean, uint128::UInt128, uint16::UInt16, uint32::UInt32, uint64::UInt64,
|
boolean::Boolean, uint128::UInt128, uint16::UInt16, uint32::UInt32, uint64::UInt64,
|
||||||
uint8::UInt8,
|
uint8::UInt8,
|
||||||
@ -15,42 +15,42 @@ use snarkos_models::{
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct ConstrainedCircuitMember<F: Field + PrimeField, G: Group>(
|
pub struct ConstrainedCircuitMember<F: Field + PrimeField>(
|
||||||
pub Identifier<F, G>,
|
pub Identifier<F>,
|
||||||
pub ConstrainedValue<F, G>,
|
pub ConstrainedValue<F>,
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum ConstrainedValue<F: Field + PrimeField, G: Group> {
|
pub enum ConstrainedValue<F: Field + PrimeField> {
|
||||||
Integer(Integer),
|
Integer(Integer),
|
||||||
FieldElement(FieldElement<F>),
|
FieldElement(FieldElement<F>),
|
||||||
GroupElement(G),
|
Group(String),
|
||||||
Boolean(Boolean),
|
Boolean(Boolean),
|
||||||
|
|
||||||
Array(Vec<ConstrainedValue<F, G>>),
|
Array(Vec<ConstrainedValue<F>>),
|
||||||
|
|
||||||
CircuitDefinition(Circuit<F, G>),
|
CircuitDefinition(Circuit<F>),
|
||||||
CircuitExpression(Identifier<F, G>, Vec<ConstrainedCircuitMember<F, G>>),
|
CircuitExpression(Identifier<F>, Vec<ConstrainedCircuitMember<F>>),
|
||||||
|
|
||||||
Function(Option<Identifier<F, G>>, Function<F, G>), // (optional circuit identifier, function definition)
|
Function(Option<Identifier<F>>, Function<F>), // (optional circuit identifier, function definition)
|
||||||
Return(Vec<ConstrainedValue<F, G>>),
|
Return(Vec<ConstrainedValue<F>>),
|
||||||
|
|
||||||
Mutable(Box<ConstrainedValue<F, G>>),
|
Mutable(Box<ConstrainedValue<F>>),
|
||||||
Static(Box<ConstrainedValue<F, G>>),
|
Static(Box<ConstrainedValue<F>>),
|
||||||
Unresolved(String),
|
Unresolved(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> ConstrainedValue<F, G> {
|
impl<F: Field + PrimeField> ConstrainedValue<F> {
|
||||||
pub(crate) fn from_other(
|
pub(crate) fn from_other(
|
||||||
value: String,
|
value: String,
|
||||||
other: &ConstrainedValue<F, G>,
|
other: &ConstrainedValue<F>,
|
||||||
) -> Result<Self, ValueError> {
|
) -> Result<Self, ValueError> {
|
||||||
let other_type = other.to_type();
|
let other_type = other.to_type();
|
||||||
|
|
||||||
ConstrainedValue::from_type(value, &other_type)
|
ConstrainedValue::from_type(value, &other_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_type(value: String, _type: &Type<F, G>) -> Result<Self, ValueError> {
|
pub(crate) fn from_type(value: String, _type: &Type<F>) -> Result<Self, ValueError> {
|
||||||
match _type {
|
match _type {
|
||||||
Type::IntegerType(integer_type) => Ok(ConstrainedValue::Integer(match integer_type {
|
Type::IntegerType(integer_type) => Ok(ConstrainedValue::Integer(match integer_type {
|
||||||
IntegerType::U8 => Integer::U8(UInt8::constant(value.parse::<u8>()?)),
|
IntegerType::U8 => Integer::U8(UInt8::constant(value.parse::<u8>()?)),
|
||||||
@ -62,13 +62,7 @@ impl<F: Field + PrimeField, G: Group> ConstrainedValue<F, G> {
|
|||||||
Type::FieldElement => Ok(ConstrainedValue::FieldElement(FieldElement::Constant(
|
Type::FieldElement => Ok(ConstrainedValue::FieldElement(FieldElement::Constant(
|
||||||
F::from_str(&value).unwrap_or_default(),
|
F::from_str(&value).unwrap_or_default(),
|
||||||
))),
|
))),
|
||||||
Type::GroupElement => Ok(ConstrainedValue::GroupElement({
|
Type::Group => Ok(ConstrainedValue::Group(value)),
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
let scalar = G::ScalarField::from_str(&value).unwrap_or_default();
|
|
||||||
let point = G::default().mul(&scalar);
|
|
||||||
point
|
|
||||||
})),
|
|
||||||
Type::Boolean => Ok(ConstrainedValue::Boolean(Boolean::Constant(
|
Type::Boolean => Ok(ConstrainedValue::Boolean(Boolean::Constant(
|
||||||
value.parse::<bool>()?,
|
value.parse::<bool>()?,
|
||||||
))),
|
))),
|
||||||
@ -77,17 +71,17 @@ impl<F: Field + PrimeField, G: Group> ConstrainedValue<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn to_type(&self) -> Type<F, G> {
|
pub(crate) fn to_type(&self) -> Type<F> {
|
||||||
match self {
|
match self {
|
||||||
ConstrainedValue::Integer(integer) => Type::IntegerType(integer.get_type()),
|
ConstrainedValue::Integer(integer) => Type::IntegerType(integer.get_type()),
|
||||||
ConstrainedValue::FieldElement(_field) => Type::FieldElement,
|
ConstrainedValue::FieldElement(_field) => Type::FieldElement,
|
||||||
ConstrainedValue::GroupElement(_group) => Type::GroupElement,
|
ConstrainedValue::Group(_group) => Type::Group,
|
||||||
ConstrainedValue::Boolean(_bool) => Type::Boolean,
|
ConstrainedValue::Boolean(_bool) => Type::Boolean,
|
||||||
_ => unimplemented!("to type only implemented for primitives"),
|
_ => unimplemented!("to type only implemented for primitives"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_type(&mut self, types: &Vec<Type<F, G>>) -> Result<(), ValueError> {
|
pub(crate) fn resolve_type(&mut self, types: &Vec<Type<F>>) -> Result<(), ValueError> {
|
||||||
if let ConstrainedValue::Unresolved(ref string) = self {
|
if let ConstrainedValue::Unresolved(ref string) = self {
|
||||||
if !types.is_empty() {
|
if !types.is_empty() {
|
||||||
*self = ConstrainedValue::from_type(string.clone(), &types[0])?
|
*self = ConstrainedValue::from_type(string.clone(), &types[0])?
|
||||||
@ -104,12 +98,12 @@ impl<F: Field + PrimeField, G: Group> ConstrainedValue<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for ConstrainedValue<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for ConstrainedValue<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
ConstrainedValue::Integer(ref value) => write!(f, "{}", value),
|
ConstrainedValue::Integer(ref value) => write!(f, "{}", value),
|
||||||
ConstrainedValue::FieldElement(ref value) => write!(f, "{}", value),
|
ConstrainedValue::FieldElement(ref value) => write!(f, "{}", value),
|
||||||
ConstrainedValue::GroupElement(ref value) => write!(f, "{}", value),
|
ConstrainedValue::Group(ref value) => write!(f, "{}", value),
|
||||||
ConstrainedValue::Boolean(ref value) => write!(f, "{}", value.get_value().unwrap()),
|
ConstrainedValue::Boolean(ref value) => write!(f, "{}", value.get_value().unwrap()),
|
||||||
ConstrainedValue::Array(ref array) => {
|
ConstrainedValue::Array(ref array) => {
|
||||||
write!(f, "[")?;
|
write!(f, "[")?;
|
||||||
@ -154,7 +148,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for ConstrainedValue<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for ConstrainedValue<F, G> {
|
impl<F: Field + PrimeField> fmt::Debug for ConstrainedValue<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self)
|
write!(f, "{}", self)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::errors::{
|
use crate::errors::{
|
||||||
BooleanError, ExpressionError, FieldElementError, GroupElementError, IntegerError,
|
BooleanError, ExpressionError, FieldElementError, GroupError, IntegerError, StatementError,
|
||||||
StatementError, ValueError,
|
ValueError,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
@ -30,7 +30,7 @@ pub enum FunctionError {
|
|||||||
FieldElementError(FieldElementError),
|
FieldElementError(FieldElementError),
|
||||||
|
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
GroupElementError(GroupElementError),
|
GroupError(GroupError),
|
||||||
|
|
||||||
#[error("{}", _0)]
|
#[error("{}", _0)]
|
||||||
BooleanError(BooleanError),
|
BooleanError(BooleanError),
|
||||||
@ -60,9 +60,9 @@ impl From<FieldElementError> for FunctionError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<GroupElementError> for FunctionError {
|
impl From<GroupError> for FunctionError {
|
||||||
fn from(error: GroupElementError) -> Self {
|
fn from(error: GroupError) -> Self {
|
||||||
FunctionError::GroupElementError(error)
|
FunctionError::GroupError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum GroupElementError {
|
pub enum GroupError {
|
||||||
#[error("Expected group element parameter, got {}", _0)]
|
#[error("Expected group element parameter, got {}", _0)]
|
||||||
InvalidGroup(String),
|
InvalidGroup(String),
|
||||||
|
|
||||||
@ -9,8 +9,8 @@ pub enum GroupElementError {
|
|||||||
SynthesisError(SynthesisError),
|
SynthesisError(SynthesisError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SynthesisError> for GroupElementError {
|
impl From<SynthesisError> for GroupError {
|
||||||
fn from(error: SynthesisError) -> Self {
|
fn from(error: SynthesisError) -> Self {
|
||||||
GroupElementError::SynthesisError(error)
|
GroupError::SynthesisError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,8 +18,8 @@ pub use integer::*;
|
|||||||
pub mod field_element;
|
pub mod field_element;
|
||||||
pub use field_element::*;
|
pub use field_element::*;
|
||||||
|
|
||||||
pub mod group_element;
|
pub mod group;
|
||||||
pub use group_element::*;
|
pub use group::*;
|
||||||
|
|
||||||
pub mod value;
|
pub mod value;
|
||||||
pub use value::*;
|
pub use value::*;
|
||||||
|
@ -2,23 +2,23 @@
|
|||||||
|
|
||||||
use crate::Identifier;
|
use crate::Identifier;
|
||||||
|
|
||||||
use snarkos_models::curves::{Field, Group, PrimeField};
|
use snarkos_models::curves::{Field, PrimeField};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ImportSymbol<F: Field + PrimeField, G: Group> {
|
pub struct ImportSymbol<F: Field + PrimeField> {
|
||||||
pub symbol: Identifier<F, G>,
|
pub symbol: Identifier<F>,
|
||||||
pub alias: Option<Identifier<F, G>>,
|
pub alias: Option<Identifier<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Import<F: Field + PrimeField, G: Group> {
|
pub struct Import<F: Field + PrimeField> {
|
||||||
pub path_string: String,
|
pub path_string: String,
|
||||||
pub symbols: Vec<ImportSymbol<F, G>>,
|
pub symbols: Vec<ImportSymbol<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Import<F, G> {
|
impl<F: Field + PrimeField> Import<F> {
|
||||||
pub fn new(source: String, symbols: Vec<ImportSymbol<F, G>>) -> Import<F, G> {
|
pub fn new(source: String, symbols: Vec<ImportSymbol<F>>) -> Import<F> {
|
||||||
Import {
|
Import {
|
||||||
path_string: source,
|
path_string: source,
|
||||||
symbols,
|
symbols,
|
||||||
@ -51,7 +51,7 @@ impl<F: Field + PrimeField, G: Group> Import<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for ImportSymbol<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for ImportSymbol<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if self.alias.is_some() {
|
if self.alias.is_some() {
|
||||||
write!(f, "\t{} as {}", self.symbol, self.alias.as_ref().unwrap())
|
write!(f, "\t{} as {}", self.symbol, self.alias.as_ref().unwrap())
|
||||||
@ -61,13 +61,13 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for ImportSymbol<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Import<F, G> {
|
impl<'ast, F: Field + PrimeField> fmt::Display for Import<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.format(f)
|
self.format(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> fmt::Debug for Import<F, G> {
|
impl<'ast, F: Field + PrimeField> fmt::Debug for Import<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.format(f)
|
self.format(f)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use crate::Import;
|
use crate::Import;
|
||||||
|
|
||||||
use snarkos_models::curves::{Field, Group, PrimeField};
|
use snarkos_models::curves::{Field, PrimeField};
|
||||||
use snarkos_models::gadgets::{
|
use snarkos_models::gadgets::{
|
||||||
r1cs::Variable as R1CSVariable,
|
r1cs::Variable as R1CSVariable,
|
||||||
utilities::{
|
utilities::{
|
||||||
@ -16,17 +16,15 @@ use std::marker::PhantomData;
|
|||||||
|
|
||||||
/// An identifier in the constrained program.
|
/// An identifier in the constrained program.
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct Identifier<F: Field + PrimeField, G: Group> {
|
pub struct Identifier<F: Field + PrimeField> {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub(crate) _group: PhantomData<G>,
|
|
||||||
pub(crate) _engine: PhantomData<F>,
|
pub(crate) _engine: PhantomData<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Identifier<F, G> {
|
impl<F: Field + PrimeField> Identifier<F> {
|
||||||
pub fn new(name: String) -> Self {
|
pub fn new(name: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name,
|
name,
|
||||||
_group: PhantomData::<G>,
|
|
||||||
_engine: PhantomData::<F>,
|
_engine: PhantomData::<F>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,10 +36,10 @@ impl<F: Field + PrimeField, G: Group> Identifier<F, G> {
|
|||||||
|
|
||||||
/// A variable that is assigned to a value in the constrained program
|
/// A variable that is assigned to a value in the constrained program
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct Variable<F: Field + PrimeField, G: Group> {
|
pub struct Variable<F: Field + PrimeField> {
|
||||||
pub identifier: Identifier<F, G>,
|
pub identifier: Identifier<F>,
|
||||||
pub mutable: bool,
|
pub mutable: bool,
|
||||||
pub _type: Option<Type<F, G>>,
|
pub _type: Option<Type<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An integer type enum wrapping the integer value. Used only in expressions.
|
/// An integer type enum wrapping the integer value. Used only in expressions.
|
||||||
@ -63,74 +61,70 @@ pub enum FieldElement<F: Field + PrimeField> {
|
|||||||
|
|
||||||
/// Range or expression enum
|
/// Range or expression enum
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum RangeOrExpression<F: Field + PrimeField, G: Group> {
|
pub enum RangeOrExpression<F: Field + PrimeField> {
|
||||||
Range(Option<Integer>, Option<Integer>),
|
Range(Option<Integer>, Option<Integer>),
|
||||||
Expression(Expression<F, G>),
|
Expression(Expression<F>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spread or expression
|
/// Spread or expression
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum SpreadOrExpression<F: Field + PrimeField, G: Group> {
|
pub enum SpreadOrExpression<F: Field + PrimeField> {
|
||||||
Spread(Expression<F, G>),
|
Spread(Expression<F>),
|
||||||
Expression(Expression<F, G>),
|
Expression(Expression<F>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expression that evaluates to a value
|
/// Expression that evaluates to a value
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum Expression<F: Field + PrimeField, G: Group> {
|
pub enum Expression<F: Field + PrimeField> {
|
||||||
// Identifier
|
// Identifier
|
||||||
Identifier(Identifier<F, G>),
|
Identifier(Identifier<F>),
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
Integer(Integer),
|
Integer(Integer),
|
||||||
FieldElement(FieldElement<F>),
|
FieldElement(FieldElement<F>),
|
||||||
GroupElement(G),
|
Group(String),
|
||||||
Boolean(Boolean),
|
Boolean(Boolean),
|
||||||
Implicit(String),
|
Implicit(String),
|
||||||
|
|
||||||
// Number operations
|
// Number operations
|
||||||
Add(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Add(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Sub(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Sub(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Mul(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Mul(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Div(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Div(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Pow(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Pow(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
|
|
||||||
// Boolean operations
|
// Boolean operations
|
||||||
Not(Box<Expression<F, G>>),
|
Not(Box<Expression<F>>),
|
||||||
Or(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Or(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
And(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
And(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Eq(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Eq(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Geq(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Geq(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Gt(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Gt(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Leq(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Leq(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Lt(Box<Expression<F, G>>, Box<Expression<F, G>>),
|
Lt(Box<Expression<F>>, Box<Expression<F>>),
|
||||||
|
|
||||||
// Conditionals
|
// Conditionals
|
||||||
IfElse(
|
IfElse(Box<Expression<F>>, Box<Expression<F>>, Box<Expression<F>>),
|
||||||
Box<Expression<F, G>>,
|
|
||||||
Box<Expression<F, G>>,
|
|
||||||
Box<Expression<F, G>>,
|
|
||||||
),
|
|
||||||
|
|
||||||
// Arrays
|
// Arrays
|
||||||
Array(Vec<Box<SpreadOrExpression<F, G>>>),
|
Array(Vec<Box<SpreadOrExpression<F>>>),
|
||||||
ArrayAccess(Box<Expression<F, G>>, Box<RangeOrExpression<F, G>>), // (array name, range)
|
ArrayAccess(Box<Expression<F>>, Box<RangeOrExpression<F>>), // (array name, range)
|
||||||
|
|
||||||
// Circuits
|
// Circuits
|
||||||
Circuit(Identifier<F, G>, Vec<CircuitFieldDefinition<F, G>>),
|
Circuit(Identifier<F>, Vec<CircuitFieldDefinition<F>>),
|
||||||
CircuitMemberAccess(Box<Expression<F, G>>, Identifier<F, G>), // (declared circuit name, circuit member name)
|
CircuitMemberAccess(Box<Expression<F>>, Identifier<F>), // (declared circuit name, circuit member name)
|
||||||
CircuitStaticFunctionAccess(Box<Expression<F, G>>, Identifier<F, G>), // (defined circuit name, circuit static member name)
|
CircuitStaticFunctionAccess(Box<Expression<F>>, Identifier<F>), // (defined circuit name, circuit static member name)
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
FunctionCall(Box<Expression<F, G>>, Vec<Expression<F, G>>),
|
FunctionCall(Box<Expression<F>>, Vec<Expression<F>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Definition assignee: v, arr[0..2], Point p.x
|
/// Definition assignee: v, arr[0..2], Point p.x
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum Assignee<F: Field + PrimeField, G: Group> {
|
pub enum Assignee<F: Field + PrimeField> {
|
||||||
Identifier(Identifier<F, G>),
|
Identifier(Identifier<F>),
|
||||||
Array(Box<Assignee<F, G>>, RangeOrExpression<F, G>),
|
Array(Box<Assignee<F>>, RangeOrExpression<F>),
|
||||||
CircuitField(Box<Assignee<F, G>>, Identifier<F, G>), // (circuit name, circuit field name)
|
CircuitField(Box<Assignee<F>>, Identifier<F>), // (circuit name, circuit field name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Explicit integer type
|
/// Explicit integer type
|
||||||
@ -145,17 +139,17 @@ pub enum IntegerType {
|
|||||||
|
|
||||||
/// Explicit type used for defining a variable or expression type
|
/// Explicit type used for defining a variable or expression type
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum Type<F: Field + PrimeField, G: Group> {
|
pub enum Type<F: Field + PrimeField> {
|
||||||
IntegerType(IntegerType),
|
IntegerType(IntegerType),
|
||||||
FieldElement,
|
FieldElement,
|
||||||
GroupElement,
|
Group,
|
||||||
Boolean,
|
Boolean,
|
||||||
Array(Box<Type<F, G>>, Vec<usize>),
|
Array(Box<Type<F>>, Vec<usize>),
|
||||||
Circuit(Identifier<F, G>),
|
Circuit(Identifier<F>),
|
||||||
SelfType,
|
SelfType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Type<F, G> {
|
impl<F: Field + PrimeField> Type<F> {
|
||||||
pub fn outer_dimension(&self, dimensions: &Vec<usize>) -> Self {
|
pub fn outer_dimension(&self, dimensions: &Vec<usize>) -> Self {
|
||||||
let _type = self.clone();
|
let _type = self.clone();
|
||||||
|
|
||||||
@ -184,79 +178,79 @@ impl<F: Field + PrimeField, G: Group> Type<F, G> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum ConditionalNestedOrEnd<F: Field + PrimeField, G: Group> {
|
pub enum ConditionalNestedOrEnd<F: Field + PrimeField> {
|
||||||
Nested(Box<ConditionalStatement<F, G>>),
|
Nested(Box<ConditionalStatement<F>>),
|
||||||
End(Vec<Statement<F, G>>),
|
End(Vec<Statement<F>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct ConditionalStatement<F: Field + PrimeField, G: Group> {
|
pub struct ConditionalStatement<F: Field + PrimeField> {
|
||||||
pub condition: Expression<F, G>,
|
pub condition: Expression<F>,
|
||||||
pub statements: Vec<Statement<F, G>>,
|
pub statements: Vec<Statement<F>>,
|
||||||
pub next: Option<ConditionalNestedOrEnd<F, G>>,
|
pub next: Option<ConditionalNestedOrEnd<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Program statement that defines some action (or expression) to be carried out.
|
/// Program statement that defines some action (or expression) to be carried out.
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum Statement<F: Field + PrimeField, G: Group> {
|
pub enum Statement<F: Field + PrimeField> {
|
||||||
Return(Vec<Expression<F, G>>),
|
Return(Vec<Expression<F>>),
|
||||||
Definition(Variable<F, G>, Expression<F, G>),
|
Definition(Variable<F>, Expression<F>),
|
||||||
Assign(Assignee<F, G>, Expression<F, G>),
|
Assign(Assignee<F>, Expression<F>),
|
||||||
MultipleAssign(Vec<Variable<F, G>>, Expression<F, G>),
|
MultipleAssign(Vec<Variable<F>>, Expression<F>),
|
||||||
Conditional(ConditionalStatement<F, G>),
|
Conditional(ConditionalStatement<F>),
|
||||||
For(Identifier<F, G>, Integer, Integer, Vec<Statement<F, G>>),
|
For(Identifier<F>, Integer, Integer, Vec<Statement<F>>),
|
||||||
AssertEq(Expression<F, G>, Expression<F, G>),
|
AssertEq(Expression<F>, Expression<F>),
|
||||||
Expression(Expression<F, G>),
|
Expression(Expression<F>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Circuits
|
/// Circuits
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct CircuitFieldDefinition<F: Field + PrimeField, G: Group> {
|
pub struct CircuitFieldDefinition<F: Field + PrimeField> {
|
||||||
pub identifier: Identifier<F, G>,
|
pub identifier: Identifier<F>,
|
||||||
pub expression: Expression<F, G>,
|
pub expression: Expression<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum CircuitMember<F: Field + PrimeField, G: Group> {
|
pub enum CircuitMember<F: Field + PrimeField> {
|
||||||
CircuitField(Identifier<F, G>, Type<F, G>),
|
CircuitField(Identifier<F>, Type<F>),
|
||||||
CircuitFunction(bool, Function<F, G>),
|
CircuitFunction(bool, Function<F>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct Circuit<F: Field + PrimeField, G: Group> {
|
pub struct Circuit<F: Field + PrimeField> {
|
||||||
pub identifier: Identifier<F, G>,
|
pub identifier: Identifier<F>,
|
||||||
pub members: Vec<CircuitMember<F, G>>,
|
pub members: Vec<CircuitMember<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Function parameters
|
/// Function parameters
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct InputModel<F: Field + PrimeField, G: Group> {
|
pub struct InputModel<F: Field + PrimeField> {
|
||||||
pub identifier: Identifier<F, G>,
|
pub identifier: Identifier<F>,
|
||||||
pub mutable: bool,
|
pub mutable: bool,
|
||||||
pub private: bool,
|
pub private: bool,
|
||||||
pub _type: Type<F, G>,
|
pub _type: Type<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum InputValue<F: Field + PrimeField, G: Group> {
|
pub enum InputValue<F: Field + PrimeField> {
|
||||||
Integer(usize),
|
Integer(usize),
|
||||||
Field(F),
|
Field(F),
|
||||||
Group(G),
|
Group(String),
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
Array(Vec<InputValue<F, G>>),
|
Array(Vec<InputValue<F>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct Function<F: Field + PrimeField, G: Group> {
|
pub struct Function<F: Field + PrimeField> {
|
||||||
pub function_name: Identifier<F, G>,
|
pub function_name: Identifier<F>,
|
||||||
pub inputs: Vec<InputModel<F, G>>,
|
pub inputs: Vec<InputModel<F>>,
|
||||||
pub returns: Vec<Type<F, G>>,
|
pub returns: Vec<Type<F>>,
|
||||||
pub statements: Vec<Statement<F, G>>,
|
pub statements: Vec<Statement<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Function<F, G> {
|
impl<F: Field + PrimeField> Function<F> {
|
||||||
pub fn get_name(&self) -> String {
|
pub fn get_name(&self) -> String {
|
||||||
self.function_name.name.clone()
|
self.function_name.name.clone()
|
||||||
}
|
}
|
||||||
@ -264,15 +258,15 @@ impl<F: Field + PrimeField, G: Group> Function<F, G> {
|
|||||||
|
|
||||||
/// A simple program with statement expressions, program arguments and program returns.
|
/// A simple program with statement expressions, program arguments and program returns.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Program<F: Field + PrimeField, G: Group> {
|
pub struct Program<F: Field + PrimeField> {
|
||||||
pub name: Identifier<F, G>,
|
pub name: Identifier<F>,
|
||||||
pub num_parameters: usize,
|
pub num_parameters: usize,
|
||||||
pub imports: Vec<Import<F, G>>,
|
pub imports: Vec<Import<F>>,
|
||||||
pub circuits: HashMap<Identifier<F, G>, Circuit<F, G>>,
|
pub circuits: HashMap<Identifier<F>, Circuit<F>>,
|
||||||
pub functions: HashMap<Identifier<F, G>, Function<F, G>>,
|
pub functions: HashMap<Identifier<F>, Function<F>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> Program<F, G> {
|
impl<'ast, F: Field + PrimeField> Program<F> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: Identifier::new("".into()),
|
name: Identifier::new("".into()),
|
||||||
|
@ -6,21 +6,21 @@ use crate::{
|
|||||||
RangeOrExpression, SpreadOrExpression, Statement, Type, Variable,
|
RangeOrExpression, SpreadOrExpression, Statement, Type, Variable,
|
||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::curves::{Field, Group, PrimeField};
|
use snarkos_models::curves::{Field, PrimeField};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Identifier<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for Identifier<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.name)
|
write!(f, "{}", self.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for Identifier<F, G> {
|
impl<F: Field + PrimeField> fmt::Debug for Identifier<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.name)
|
write!(f, "{}", self.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Variable<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for Variable<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if self.mutable {
|
if self.mutable {
|
||||||
write!(f, "mut ")?;
|
write!(f, "mut ")?;
|
||||||
@ -69,7 +69,7 @@ impl<F: Field + PrimeField> fmt::Debug for FieldElement<F> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for RangeOrExpression<F, G> {
|
impl<'ast, F: Field + PrimeField> fmt::Display for RangeOrExpression<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
RangeOrExpression::Range(ref from, ref to) => write!(
|
RangeOrExpression::Range(ref from, ref to) => write!(
|
||||||
@ -87,7 +87,7 @@ impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for RangeOrExpression<F
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for SpreadOrExpression<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for SpreadOrExpression<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
SpreadOrExpression::Spread(ref spread) => write!(f, "...{}", spread),
|
SpreadOrExpression::Spread(ref spread) => write!(f, "...{}", spread),
|
||||||
@ -96,7 +96,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for SpreadOrExpression<F, G>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> fmt::Display for Expression<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
// Variables
|
// Variables
|
||||||
@ -105,7 +105,7 @@ impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Expression<F, G> {
|
|||||||
// Values
|
// Values
|
||||||
Expression::Integer(ref integer) => write!(f, "{}", integer),
|
Expression::Integer(ref integer) => write!(f, "{}", integer),
|
||||||
Expression::FieldElement(ref field) => write!(f, "{}", field),
|
Expression::FieldElement(ref field) => write!(f, "{}", field),
|
||||||
Expression::GroupElement(ref group) => write!(f, "{}", group),
|
Expression::Group(ref group) => write!(f, "{}", group),
|
||||||
Expression::Boolean(ref bool) => write!(f, "{}", bool.get_value().unwrap()),
|
Expression::Boolean(ref bool) => write!(f, "{}", bool.get_value().unwrap()),
|
||||||
Expression::Implicit(ref value) => write!(f, "{}", value),
|
Expression::Implicit(ref value) => write!(f, "{}", value),
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Expression<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Assignee<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for Assignee<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Assignee::Identifier(ref variable) => write!(f, "{}", variable),
|
Assignee::Identifier(ref variable) => write!(f, "{}", variable),
|
||||||
@ -189,7 +189,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for Assignee<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for ConditionalNestedOrEnd<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for ConditionalNestedOrEnd<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
ConditionalNestedOrEnd::Nested(ref nested) => write!(f, "else {}", nested),
|
ConditionalNestedOrEnd::Nested(ref nested) => write!(f, "else {}", nested),
|
||||||
@ -204,7 +204,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for ConditionalNestedOrEnd<F,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for ConditionalStatement<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for ConditionalStatement<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "if ({}) {{\n", self.condition)?;
|
write!(f, "if ({}) {{\n", self.condition)?;
|
||||||
for statement in self.statements.iter() {
|
for statement in self.statements.iter() {
|
||||||
@ -217,7 +217,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for ConditionalStatement<F, G
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Statement<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for Statement<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Statement::Return(ref statements) => {
|
Statement::Return(ref statements) => {
|
||||||
@ -274,12 +274,12 @@ impl fmt::Display for IntegerType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Type<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for Type<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
|
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
|
||||||
Type::FieldElement => write!(f, "field"),
|
Type::FieldElement => write!(f, "field"),
|
||||||
Type::GroupElement => write!(f, "group"),
|
Type::Group => write!(f, "group"),
|
||||||
Type::Boolean => write!(f, "bool"),
|
Type::Boolean => write!(f, "bool"),
|
||||||
Type::Circuit(ref variable) => write!(f, "{}", variable),
|
Type::Circuit(ref variable) => write!(f, "{}", variable),
|
||||||
Type::SelfType => write!(f, "Self"),
|
Type::SelfType => write!(f, "Self"),
|
||||||
@ -294,7 +294,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for Type<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for CircuitMember<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for CircuitMember<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
CircuitMember::CircuitField(ref identifier, ref _type) => {
|
CircuitMember::CircuitField(ref identifier, ref _type) => {
|
||||||
@ -310,7 +310,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for CircuitMember<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Circuit<F, G> {
|
impl<F: Field + PrimeField> Circuit<F> {
|
||||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "circuit {} {{ \n", self.identifier)?;
|
write!(f, "circuit {} {{ \n", self.identifier)?;
|
||||||
for field in self.members.iter() {
|
for field in self.members.iter() {
|
||||||
@ -320,19 +320,19 @@ impl<F: Field + PrimeField, G: Group> Circuit<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl<F: Field + PrimeField, G: Group> fmt::Display for Circuit<F, G> {// uncomment when we no longer print out Program
|
// impl<F: Field + PrimeField> fmt::Display for Circuit<F> {// uncomment when we no longer print out Program
|
||||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
// self.format(f)
|
// self.format(f)
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for Circuit<F, G> {
|
impl<F: Field + PrimeField> fmt::Debug for Circuit<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.format(f)
|
self.format(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for InputModel<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for InputModel<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
// mut var: private bool
|
// mut var: private bool
|
||||||
if self.mutable {
|
if self.mutable {
|
||||||
@ -348,7 +348,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for InputModel<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for InputValue<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for InputValue<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
InputValue::Integer(ref integer) => write!(f, "{}", integer),
|
InputValue::Integer(ref integer) => write!(f, "{}", integer),
|
||||||
@ -369,7 +369,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for InputValue<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> Function<F, G> {
|
impl<F: Field + PrimeField> Function<F> {
|
||||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "function {}", self.function_name)?;
|
write!(f, "function {}", self.function_name)?;
|
||||||
let parameters = self
|
let parameters = self
|
||||||
@ -400,13 +400,13 @@ impl<F: Field + PrimeField, G: Group> Function<F, G> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Display for Function<F, G> {
|
impl<F: Field + PrimeField> fmt::Display for Function<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.format(f)
|
self.format(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for Function<F, G> {
|
impl<F: Field + PrimeField> fmt::Debug for Function<F> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.format(f)
|
self.format(f)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::{ast, types, Import, ImportSymbol};
|
use crate::{ast, types, Import, ImportSymbol};
|
||||||
|
|
||||||
use snarkos_models::curves::{Field, Group, PrimeField};
|
use snarkos_models::curves::{Field, PrimeField};
|
||||||
use snarkos_models::gadgets::utilities::{
|
use snarkos_models::gadgets::utilities::{
|
||||||
boolean::Boolean, uint128::UInt128, uint16::UInt16, uint32::UInt32, uint64::UInt64,
|
boolean::Boolean, uint128::UInt128, uint16::UInt16, uint32::UInt32, uint64::UInt64,
|
||||||
uint8::UInt8,
|
uint8::UInt8,
|
||||||
@ -11,17 +11,13 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
/// pest ast -> types::Identifier
|
/// pest ast -> types::Identifier
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Identifier<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::Identifier<'ast>> for types::Identifier<F> {
|
||||||
for types::Identifier<F, G>
|
|
||||||
{
|
|
||||||
fn from(identifier: ast::Identifier<'ast>) -> Self {
|
fn from(identifier: ast::Identifier<'ast>) -> Self {
|
||||||
types::Identifier::new(identifier.value)
|
types::Identifier::new(identifier.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Identifier<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::Identifier<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(identifier: ast::Identifier<'ast>) -> Self {
|
fn from(identifier: ast::Identifier<'ast>) -> Self {
|
||||||
types::Expression::Identifier(types::Identifier::from(identifier))
|
types::Expression::Identifier(types::Identifier::from(identifier))
|
||||||
}
|
}
|
||||||
@ -29,7 +25,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Identifier<'ast>>
|
|||||||
|
|
||||||
/// pest ast -> types::Variable
|
/// pest ast -> types::Variable
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Variable<'ast>> for types::Variable<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Variable<'ast>> for types::Variable<F> {
|
||||||
fn from(variable: ast::Variable<'ast>) -> Self {
|
fn from(variable: ast::Variable<'ast>) -> Self {
|
||||||
types::Variable {
|
types::Variable {
|
||||||
identifier: types::Identifier::from(variable.identifier),
|
identifier: types::Identifier::from(variable.identifier),
|
||||||
@ -72,21 +68,21 @@ impl<'ast> types::Integer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Integer<'ast>> for types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Integer<'ast>> for types::Expression<F> {
|
||||||
fn from(field: ast::Integer<'ast>) -> Self {
|
fn from(field: ast::Integer<'ast>) -> Self {
|
||||||
types::Expression::Integer(types::Integer::from(field.number, field._type))
|
types::Expression::Integer(types::Integer::from(field.number, field._type))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::RangeOrExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::RangeOrExpression<'ast>>
|
||||||
for types::RangeOrExpression<F, G>
|
for types::RangeOrExpression<F>
|
||||||
{
|
{
|
||||||
fn from(range_or_expression: ast::RangeOrExpression<'ast>) -> Self {
|
fn from(range_or_expression: ast::RangeOrExpression<'ast>) -> Self {
|
||||||
match range_or_expression {
|
match range_or_expression {
|
||||||
ast::RangeOrExpression::Range(range) => {
|
ast::RangeOrExpression::Range(range) => {
|
||||||
let from = range
|
let from = range
|
||||||
.from
|
.from
|
||||||
.map(|from| match types::Expression::<F, G>::from(from.0) {
|
.map(|from| match types::Expression::<F>::from(from.0) {
|
||||||
types::Expression::Integer(number) => number,
|
types::Expression::Integer(number) => number,
|
||||||
types::Expression::Implicit(string) => {
|
types::Expression::Implicit(string) => {
|
||||||
types::Integer::from_implicit(string)
|
types::Integer::from_implicit(string)
|
||||||
@ -95,13 +91,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::RangeOrExpression<'ast>>
|
|||||||
unimplemented!("Range bounds should be integers, found {}", expression)
|
unimplemented!("Range bounds should be integers, found {}", expression)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let to = range
|
let to = range.to.map(|to| match types::Expression::<F>::from(to.0) {
|
||||||
.to
|
|
||||||
.map(|to| match types::Expression::<F, G>::from(to.0) {
|
|
||||||
types::Expression::Integer(number) => number,
|
types::Expression::Integer(number) => number,
|
||||||
types::Expression::Implicit(string) => {
|
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
||||||
types::Integer::from_implicit(string)
|
|
||||||
}
|
|
||||||
expression => {
|
expression => {
|
||||||
unimplemented!("Range bounds should be integers, found {}", expression)
|
unimplemented!("Range bounds should be integers, found {}", expression)
|
||||||
}
|
}
|
||||||
@ -118,7 +110,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::RangeOrExpression<'ast>>
|
|||||||
|
|
||||||
/// pest ast -> types::Field
|
/// pest ast -> types::Field
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Field<'ast>> for types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Field<'ast>> for types::Expression<F> {
|
||||||
fn from(field: ast::Field<'ast>) -> Self {
|
fn from(field: ast::Field<'ast>) -> Self {
|
||||||
types::Expression::FieldElement(types::FieldElement::Constant(
|
types::Expression::FieldElement(types::FieldElement::Constant(
|
||||||
F::from_str(&field.number.value).unwrap_or_default(),
|
F::from_str(&field.number.value).unwrap_or_default(),
|
||||||
@ -128,20 +120,15 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Field<'ast>> for types::Ex
|
|||||||
|
|
||||||
/// pest ast -> types::Group
|
/// pest ast -> types::Group
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Group<'ast>> for types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Group<'ast>> for types::Expression<F> {
|
||||||
fn from(group: ast::Group<'ast>) -> Self {
|
fn from(group: ast::Group<'ast>) -> Self {
|
||||||
use std::str::FromStr;
|
types::Expression::Group(group.number.value)
|
||||||
|
|
||||||
let scalar = G::ScalarField::from_str(&group.number.value).unwrap_or_default();
|
|
||||||
let point = G::default().mul(&scalar);
|
|
||||||
|
|
||||||
types::Expression::GroupElement(point)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// pest ast -> types::Boolean
|
/// pest ast -> types::Boolean
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Boolean<'ast>> for types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Boolean<'ast>> for types::Expression<F> {
|
||||||
fn from(boolean: ast::Boolean<'ast>) -> Self {
|
fn from(boolean: ast::Boolean<'ast>) -> Self {
|
||||||
types::Expression::Boolean(Boolean::Constant(
|
types::Expression::Boolean(Boolean::Constant(
|
||||||
boolean
|
boolean
|
||||||
@ -154,9 +141,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Boolean<'ast>> for types::
|
|||||||
|
|
||||||
/// pest ast -> types::NumberImplicit
|
/// pest ast -> types::NumberImplicit
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::NumberImplicit<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::NumberImplicit<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(number: ast::NumberImplicit<'ast>) -> Self {
|
fn from(number: ast::NumberImplicit<'ast>) -> Self {
|
||||||
types::Expression::Implicit(number.number.value)
|
types::Expression::Implicit(number.number.value)
|
||||||
}
|
}
|
||||||
@ -164,7 +149,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::NumberImplicit<'ast>>
|
|||||||
|
|
||||||
/// pest ast -> types::Expression
|
/// pest ast -> types::Expression
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Value<'ast>> for types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Value<'ast>> for types::Expression<F> {
|
||||||
fn from(value: ast::Value<'ast>) -> Self {
|
fn from(value: ast::Value<'ast>) -> Self {
|
||||||
match value {
|
match value {
|
||||||
ast::Value::Integer(num) => types::Expression::from(num),
|
ast::Value::Integer(num) => types::Expression::from(num),
|
||||||
@ -176,16 +161,14 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Value<'ast>> for types::Ex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::NotExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::NotExpression<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(expression: ast::NotExpression<'ast>) -> Self {
|
fn from(expression: ast::NotExpression<'ast>) -> Self {
|
||||||
types::Expression::Not(Box::new(types::Expression::from(*expression.expression)))
|
types::Expression::Not(Box::new(types::Expression::from(*expression.expression)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::SpreadOrExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::SpreadOrExpression<'ast>>
|
||||||
for types::SpreadOrExpression<F, G>
|
for types::SpreadOrExpression<F>
|
||||||
{
|
{
|
||||||
fn from(s_or_e: ast::SpreadOrExpression<'ast>) -> Self {
|
fn from(s_or_e: ast::SpreadOrExpression<'ast>) -> Self {
|
||||||
match s_or_e {
|
match s_or_e {
|
||||||
@ -199,9 +182,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::SpreadOrExpression<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::BinaryExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::BinaryExpression<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(expression: ast::BinaryExpression<'ast>) -> Self {
|
fn from(expression: ast::BinaryExpression<'ast>) -> Self {
|
||||||
match expression.operation {
|
match expression.operation {
|
||||||
// Boolean operations
|
// Boolean operations
|
||||||
@ -261,9 +242,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::BinaryExpression<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::TernaryExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::TernaryExpression<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(expression: ast::TernaryExpression<'ast>) -> Self {
|
fn from(expression: ast::TernaryExpression<'ast>) -> Self {
|
||||||
types::Expression::IfElse(
|
types::Expression::IfElse(
|
||||||
Box::new(types::Expression::from(*expression.first)),
|
Box::new(types::Expression::from(*expression.first)),
|
||||||
@ -273,9 +252,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::TernaryExpression<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInlineExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ArrayInlineExpression<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(array: ast::ArrayInlineExpression<'ast>) -> Self {
|
fn from(array: ast::ArrayInlineExpression<'ast>) -> Self {
|
||||||
types::Expression::Array(
|
types::Expression::Array(
|
||||||
array
|
array
|
||||||
@ -286,19 +263,19 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInlineExpression<'ast
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInitializerExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ArrayInitializerExpression<'ast>>
|
||||||
for types::Expression<F, G>
|
for types::Expression<F>
|
||||||
{
|
{
|
||||||
fn from(array: ast::ArrayInitializerExpression<'ast>) -> Self {
|
fn from(array: ast::ArrayInitializerExpression<'ast>) -> Self {
|
||||||
let count = types::Expression::<F, G>::get_count(array.count);
|
let count = types::Expression::<F>::get_count(array.count);
|
||||||
let expression = Box::new(types::SpreadOrExpression::from(*array.expression));
|
let expression = Box::new(types::SpreadOrExpression::from(*array.expression));
|
||||||
|
|
||||||
types::Expression::Array(vec![expression; count])
|
types::Expression::Array(vec![expression; count])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitField<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::CircuitField<'ast>>
|
||||||
for types::CircuitFieldDefinition<F, G>
|
for types::CircuitFieldDefinition<F>
|
||||||
{
|
{
|
||||||
fn from(member: ast::CircuitField<'ast>) -> Self {
|
fn from(member: ast::CircuitField<'ast>) -> Self {
|
||||||
types::CircuitFieldDefinition {
|
types::CircuitFieldDefinition {
|
||||||
@ -308,8 +285,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitField<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitInlineExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::CircuitInlineExpression<'ast>>
|
||||||
for types::Expression<F, G>
|
for types::Expression<F>
|
||||||
{
|
{
|
||||||
fn from(expression: ast::CircuitInlineExpression<'ast>) -> Self {
|
fn from(expression: ast::CircuitInlineExpression<'ast>) -> Self {
|
||||||
let variable = types::Identifier::from(expression.identifier);
|
let variable = types::Identifier::from(expression.identifier);
|
||||||
@ -317,15 +294,13 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitInlineExpression<'a
|
|||||||
.members
|
.members
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|member| types::CircuitFieldDefinition::from(member))
|
.map(|member| types::CircuitFieldDefinition::from(member))
|
||||||
.collect::<Vec<types::CircuitFieldDefinition<F, G>>>();
|
.collect::<Vec<types::CircuitFieldDefinition<F>>>();
|
||||||
|
|
||||||
types::Expression::Circuit(variable, members)
|
types::Expression::Circuit(variable, members)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::PostfixExpression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::PostfixExpression<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(expression: ast::PostfixExpression<'ast>) -> Self {
|
fn from(expression: ast::PostfixExpression<'ast>) -> Self {
|
||||||
let variable =
|
let variable =
|
||||||
types::Expression::Identifier(types::Identifier::from(expression.identifier));
|
types::Expression::Identifier(types::Identifier::from(expression.identifier));
|
||||||
@ -369,9 +344,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::PostfixExpression<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Expression<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::Expression<'ast>> for types::Expression<F> {
|
||||||
for types::Expression<F, G>
|
|
||||||
{
|
|
||||||
fn from(expression: ast::Expression<'ast>) -> Self {
|
fn from(expression: ast::Expression<'ast>) -> Self {
|
||||||
match expression {
|
match expression {
|
||||||
ast::Expression::Value(value) => types::Expression::from(value),
|
ast::Expression::Value(value) => types::Expression::from(value),
|
||||||
@ -387,7 +360,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Expression<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> types::Expression<F> {
|
||||||
fn get_count(count: ast::Value<'ast>) -> usize {
|
fn get_count(count: ast::Value<'ast>) -> usize {
|
||||||
match count {
|
match count {
|
||||||
ast::Value::Integer(integer) => integer
|
ast::Value::Integer(integer) => integer
|
||||||
@ -406,7 +379,7 @@ impl<'ast, F: Field + PrimeField, G: Group> types::Expression<F, G> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ast::Assignee -> types::Expression for operator assign statements
|
// ast::Assignee -> types::Expression for operator assign statements
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types::Expression<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Assignee<'ast>> for types::Expression<F> {
|
||||||
fn from(assignee: ast::Assignee<'ast>) -> Self {
|
fn from(assignee: ast::Assignee<'ast>) -> Self {
|
||||||
let variable = types::Expression::Identifier(types::Identifier::from(assignee.identifier));
|
let variable = types::Expression::Identifier(types::Identifier::from(assignee.identifier));
|
||||||
|
|
||||||
@ -431,13 +404,13 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types:
|
|||||||
|
|
||||||
/// pest ast -> types::Assignee
|
/// pest ast -> types::Assignee
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Identifier<'ast>> for types::Assignee<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Identifier<'ast>> for types::Assignee<F> {
|
||||||
fn from(variable: ast::Identifier<'ast>) -> Self {
|
fn from(variable: ast::Identifier<'ast>) -> Self {
|
||||||
types::Assignee::Identifier(types::Identifier::from(variable))
|
types::Assignee::Identifier(types::Identifier::from(variable))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types::Assignee<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Assignee<'ast>> for types::Assignee<F> {
|
||||||
fn from(assignee: ast::Assignee<'ast>) -> Self {
|
fn from(assignee: ast::Assignee<'ast>) -> Self {
|
||||||
let variable = types::Assignee::from(assignee.identifier);
|
let variable = types::Assignee::from(assignee.identifier);
|
||||||
|
|
||||||
@ -460,9 +433,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types:
|
|||||||
|
|
||||||
/// pest ast -> types::Statement
|
/// pest ast -> types::Statement
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ReturnStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ReturnStatement<'ast>> for types::Statement<F> {
|
||||||
for types::Statement<F, G>
|
|
||||||
{
|
|
||||||
fn from(statement: ast::ReturnStatement<'ast>) -> Self {
|
fn from(statement: ast::ReturnStatement<'ast>) -> Self {
|
||||||
types::Statement::Return(
|
types::Statement::Return(
|
||||||
statement
|
statement
|
||||||
@ -474,9 +445,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ReturnStatement<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::DefinitionStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::DefinitionStatement<'ast>> for types::Statement<F> {
|
||||||
for types::Statement<F, G>
|
|
||||||
{
|
|
||||||
fn from(statement: ast::DefinitionStatement<'ast>) -> Self {
|
fn from(statement: ast::DefinitionStatement<'ast>) -> Self {
|
||||||
types::Statement::Definition(
|
types::Statement::Definition(
|
||||||
types::Variable::from(statement.variable),
|
types::Variable::from(statement.variable),
|
||||||
@ -485,9 +454,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::DefinitionStatement<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssignStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::AssignStatement<'ast>> for types::Statement<F> {
|
||||||
for types::Statement<F, G>
|
|
||||||
{
|
|
||||||
fn from(statement: ast::AssignStatement<'ast>) -> Self {
|
fn from(statement: ast::AssignStatement<'ast>) -> Self {
|
||||||
match statement.assign {
|
match statement.assign {
|
||||||
ast::OperationAssign::Assign(ref _assign) => types::Statement::Assign(
|
ast::OperationAssign::Assign(ref _assign) => types::Statement::Assign(
|
||||||
@ -543,8 +510,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssignStatement<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::MultipleAssignmentStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::MultipleAssignmentStatement<'ast>>
|
||||||
for types::Statement<F, G>
|
for types::Statement<F>
|
||||||
{
|
{
|
||||||
fn from(statement: ast::MultipleAssignmentStatement<'ast>) -> Self {
|
fn from(statement: ast::MultipleAssignmentStatement<'ast>) -> Self {
|
||||||
let variables = statement
|
let variables = statement
|
||||||
@ -567,8 +534,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::MultipleAssignmentStatemen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ConditionalNestedOrEnd<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ConditionalNestedOrEnd<'ast>>
|
||||||
for types::ConditionalNestedOrEnd<F, G>
|
for types::ConditionalNestedOrEnd<F>
|
||||||
{
|
{
|
||||||
fn from(statement: ast::ConditionalNestedOrEnd<'ast>) -> Self {
|
fn from(statement: ast::ConditionalNestedOrEnd<'ast>) -> Self {
|
||||||
match statement {
|
match statement {
|
||||||
@ -585,8 +552,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ConditionalNestedOrEnd<'as
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ConditionalStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ConditionalStatement<'ast>>
|
||||||
for types::ConditionalStatement<F, G>
|
for types::ConditionalStatement<F>
|
||||||
{
|
{
|
||||||
fn from(statement: ast::ConditionalStatement<'ast>) -> Self {
|
fn from(statement: ast::ConditionalStatement<'ast>) -> Self {
|
||||||
types::ConditionalStatement {
|
types::ConditionalStatement {
|
||||||
@ -604,16 +571,14 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ConditionalStatement<'ast>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ForStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ForStatement<'ast>> for types::Statement<F> {
|
||||||
for types::Statement<F, G>
|
|
||||||
{
|
|
||||||
fn from(statement: ast::ForStatement<'ast>) -> Self {
|
fn from(statement: ast::ForStatement<'ast>) -> Self {
|
||||||
let from = match types::Expression::<F, G>::from(statement.start) {
|
let from = match types::Expression::<F>::from(statement.start) {
|
||||||
types::Expression::Integer(number) => number,
|
types::Expression::Integer(number) => number,
|
||||||
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
||||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||||
};
|
};
|
||||||
let to = match types::Expression::<F, G>::from(statement.stop) {
|
let to = match types::Expression::<F>::from(statement.stop) {
|
||||||
types::Expression::Integer(number) => number,
|
types::Expression::Integer(number) => number,
|
||||||
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
types::Expression::Implicit(string) => types::Integer::from_implicit(string),
|
||||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||||
@ -632,9 +597,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ForStatement<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssertStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::AssertStatement<'ast>> for types::Statement<F> {
|
||||||
for types::Statement<F, G>
|
|
||||||
{
|
|
||||||
fn from(statement: ast::AssertStatement<'ast>) -> Self {
|
fn from(statement: ast::AssertStatement<'ast>) -> Self {
|
||||||
match statement {
|
match statement {
|
||||||
ast::AssertStatement::AssertEq(assert_eq) => types::Statement::AssertEq(
|
ast::AssertStatement::AssertEq(assert_eq) => types::Statement::AssertEq(
|
||||||
@ -645,15 +608,13 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssertStatement<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ExpressionStatement<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::ExpressionStatement<'ast>> for types::Statement<F> {
|
||||||
for types::Statement<F, G>
|
|
||||||
{
|
|
||||||
fn from(statement: ast::ExpressionStatement<'ast>) -> Self {
|
fn from(statement: ast::ExpressionStatement<'ast>) -> Self {
|
||||||
types::Statement::Expression(types::Expression::from(statement.expression))
|
types::Statement::Expression(types::Expression::from(statement.expression))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Statement<'ast>> for types::Statement<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Statement<'ast>> for types::Statement<F> {
|
||||||
fn from(statement: ast::Statement<'ast>) -> Self {
|
fn from(statement: ast::Statement<'ast>) -> Self {
|
||||||
match statement {
|
match statement {
|
||||||
ast::Statement::Return(statement) => types::Statement::from(statement),
|
ast::Statement::Return(statement) => types::Statement::from(statement),
|
||||||
@ -684,39 +645,39 @@ impl From<ast::IntegerType> for types::IntegerType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field + PrimeField, G: Group> From<ast::BasicType> for types::Type<F, G> {
|
impl<F: Field + PrimeField> From<ast::BasicType> for types::Type<F> {
|
||||||
fn from(basic_type: ast::BasicType) -> Self {
|
fn from(basic_type: ast::BasicType) -> Self {
|
||||||
match basic_type {
|
match basic_type {
|
||||||
ast::BasicType::Integer(_type) => {
|
ast::BasicType::Integer(_type) => {
|
||||||
types::Type::IntegerType(types::IntegerType::from(_type))
|
types::Type::IntegerType(types::IntegerType::from(_type))
|
||||||
}
|
}
|
||||||
ast::BasicType::Field(_type) => types::Type::FieldElement,
|
ast::BasicType::Field(_type) => types::Type::FieldElement,
|
||||||
ast::BasicType::Group(_type) => types::Type::GroupElement,
|
ast::BasicType::Group(_type) => types::Type::Group,
|
||||||
ast::BasicType::Boolean(_type) => types::Type::Boolean,
|
ast::BasicType::Boolean(_type) => types::Type::Boolean,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayType<'ast>> for types::Type<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::ArrayType<'ast>> for types::Type<F> {
|
||||||
fn from(array_type: ast::ArrayType<'ast>) -> Self {
|
fn from(array_type: ast::ArrayType<'ast>) -> Self {
|
||||||
let element_type = Box::new(types::Type::from(array_type._type));
|
let element_type = Box::new(types::Type::from(array_type._type));
|
||||||
let dimensions = array_type
|
let dimensions = array_type
|
||||||
.dimensions
|
.dimensions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|row| types::Expression::<F, G>::get_count(row))
|
.map(|row| types::Expression::<F>::get_count(row))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
types::Type::Array(element_type, dimensions)
|
types::Type::Array(element_type, dimensions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitType<'ast>> for types::Type<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::CircuitType<'ast>> for types::Type<F> {
|
||||||
fn from(circuit_type: ast::CircuitType<'ast>) -> Self {
|
fn from(circuit_type: ast::CircuitType<'ast>) -> Self {
|
||||||
types::Type::Circuit(types::Identifier::from(circuit_type.identifier))
|
types::Type::Circuit(types::Identifier::from(circuit_type.identifier))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Type<'ast>> for types::Type<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Type<'ast>> for types::Type<F> {
|
||||||
fn from(_type: ast::Type<'ast>) -> Self {
|
fn from(_type: ast::Type<'ast>) -> Self {
|
||||||
match _type {
|
match _type {
|
||||||
ast::Type::Basic(_type) => types::Type::from(_type),
|
ast::Type::Basic(_type) => types::Type::from(_type),
|
||||||
@ -729,8 +690,8 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Type<'ast>> for types::Typ
|
|||||||
|
|
||||||
/// pest ast -> types::Circuit
|
/// pest ast -> types::Circuit
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitFieldDefinition<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::CircuitFieldDefinition<'ast>>
|
||||||
for types::CircuitMember<F, G>
|
for types::CircuitMember<F>
|
||||||
{
|
{
|
||||||
fn from(circuit_value: ast::CircuitFieldDefinition<'ast>) -> Self {
|
fn from(circuit_value: ast::CircuitFieldDefinition<'ast>) -> Self {
|
||||||
types::CircuitMember::CircuitField(
|
types::CircuitMember::CircuitField(
|
||||||
@ -740,9 +701,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitFieldDefinition<'as
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitFunction<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::CircuitFunction<'ast>> for types::CircuitMember<F> {
|
||||||
for types::CircuitMember<F, G>
|
|
||||||
{
|
|
||||||
fn from(circuit_function: ast::CircuitFunction<'ast>) -> Self {
|
fn from(circuit_function: ast::CircuitFunction<'ast>) -> Self {
|
||||||
types::CircuitMember::CircuitFunction(
|
types::CircuitMember::CircuitFunction(
|
||||||
circuit_function._static.is_some(),
|
circuit_function._static.is_some(),
|
||||||
@ -751,9 +710,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitFunction<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitMember<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::CircuitMember<'ast>> for types::CircuitMember<F> {
|
||||||
for types::CircuitMember<F, G>
|
|
||||||
{
|
|
||||||
fn from(object: ast::CircuitMember<'ast>) -> Self {
|
fn from(object: ast::CircuitMember<'ast>) -> Self {
|
||||||
match object {
|
match object {
|
||||||
ast::CircuitMember::CircuitFieldDefinition(circuit_value) => {
|
ast::CircuitMember::CircuitFieldDefinition(circuit_value) => {
|
||||||
@ -766,7 +723,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitMember<'ast>>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Circuit<'ast>> for types::Circuit<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Circuit<'ast>> for types::Circuit<F> {
|
||||||
fn from(circuit: ast::Circuit<'ast>) -> Self {
|
fn from(circuit: ast::Circuit<'ast>) -> Self {
|
||||||
let variable = types::Identifier::from(circuit.identifier);
|
let variable = types::Identifier::from(circuit.identifier);
|
||||||
let members = circuit
|
let members = circuit
|
||||||
@ -784,9 +741,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Circuit<'ast>> for types::
|
|||||||
|
|
||||||
/// pest ast -> function types::Parameters
|
/// pest ast -> function types::Parameters
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::InputModel<'ast>>
|
impl<'ast, F: Field + PrimeField> From<ast::InputModel<'ast>> for types::InputModel<F> {
|
||||||
for types::InputModel<F, G>
|
|
||||||
{
|
|
||||||
fn from(parameter: ast::InputModel<'ast>) -> Self {
|
fn from(parameter: ast::InputModel<'ast>) -> Self {
|
||||||
types::InputModel {
|
types::InputModel {
|
||||||
identifier: types::Identifier::from(parameter.identifier),
|
identifier: types::Identifier::from(parameter.identifier),
|
||||||
@ -802,7 +757,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::InputModel<'ast>>
|
|||||||
|
|
||||||
/// pest ast -> types::Function
|
/// pest ast -> types::Function
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Function<'ast>> for types::Function<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Function<'ast>> for types::Function<F> {
|
||||||
fn from(function_definition: ast::Function<'ast>) -> Self {
|
fn from(function_definition: ast::Function<'ast>) -> Self {
|
||||||
let function_name = types::Identifier::from(function_definition.function_name);
|
let function_name = types::Identifier::from(function_definition.function_name);
|
||||||
let parameters = function_definition
|
let parameters = function_definition
|
||||||
@ -832,7 +787,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Function<'ast>> for types:
|
|||||||
|
|
||||||
/// pest ast -> Import
|
/// pest ast -> Import
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ImportSymbol<'ast>> for ImportSymbol<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::ImportSymbol<'ast>> for ImportSymbol<F> {
|
||||||
fn from(symbol: ast::ImportSymbol<'ast>) -> Self {
|
fn from(symbol: ast::ImportSymbol<'ast>) -> Self {
|
||||||
ImportSymbol {
|
ImportSymbol {
|
||||||
symbol: types::Identifier::from(symbol.value),
|
symbol: types::Identifier::from(symbol.value),
|
||||||
@ -841,7 +796,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ImportSymbol<'ast>> for Im
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Import<'ast>> for Import<F, G> {
|
impl<'ast, F: Field + PrimeField> From<ast::Import<'ast>> for Import<F> {
|
||||||
fn from(import: ast::Import<'ast>) -> Self {
|
fn from(import: ast::Import<'ast>) -> Self {
|
||||||
Import {
|
Import {
|
||||||
path_string: import.source.value,
|
path_string: import.source.value,
|
||||||
@ -856,14 +811,14 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Import<'ast>> for Import<F
|
|||||||
|
|
||||||
/// pest ast -> types::Program
|
/// pest ast -> types::Program
|
||||||
|
|
||||||
impl<'ast, F: Field + PrimeField, G: Group> types::Program<F, G> {
|
impl<'ast, F: Field + PrimeField> types::Program<F> {
|
||||||
pub fn from(file: ast::File<'ast>, name: String) -> Self {
|
pub fn from(file: ast::File<'ast>, name: String) -> Self {
|
||||||
// Compiled ast -> aleo program representation
|
// Compiled ast -> aleo program representation
|
||||||
let imports = file
|
let imports = file
|
||||||
.imports
|
.imports
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|import| Import::from(import))
|
.map(|import| Import::from(import))
|
||||||
.collect::<Vec<Import<F, G>>>();
|
.collect::<Vec<Import<F>>>();
|
||||||
|
|
||||||
let mut circuits = HashMap::new();
|
let mut circuits = HashMap::new();
|
||||||
let mut functions = HashMap::new();
|
let mut functions = HashMap::new();
|
||||||
|
@ -6,16 +6,16 @@ use leo_compiler::{
|
|||||||
errors::{CompilerError, FunctionError},
|
errors::{CompilerError, FunctionError},
|
||||||
ConstrainedValue, InputValue, Integer,
|
ConstrainedValue, InputValue, Integer,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/array/";
|
const DIRECTORY_NAME: &str = "tests/array/";
|
||||||
|
|
||||||
// [1, 1, 1]
|
// [1, 1, 1]
|
||||||
fn output_ones(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_ones(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Array(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Array(
|
||||||
vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32))); 3]
|
vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32))); 3]
|
||||||
)]),
|
)]),
|
||||||
output
|
output
|
||||||
@ -24,30 +24,27 @@ fn output_ones(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
|
|
||||||
// [[0, 0, 0],
|
// [[0, 0, 0],
|
||||||
// [0, 0, 0]]
|
// [0, 0, 0]]
|
||||||
fn output_multi(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_multi(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Array(vec![
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Array(vec![
|
||||||
ConstrainedValue::Array(vec![
|
ConstrainedValue::Array(
|
||||||
ConstrainedValue::Integer(Integer::U32(
|
vec![ConstrainedValue::Integer(Integer::U32(UInt32::constant(0u32))); 3]
|
||||||
UInt32::constant(0u32)
|
);
|
||||||
));
|
|
||||||
3
|
|
||||||
]);
|
|
||||||
2
|
2
|
||||||
])]),
|
])]),
|
||||||
output
|
output
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_array(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_array(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::InvalidArray(_string)) => {}
|
CompilerError::FunctionError(FunctionError::InvalidArray(_string)) => {}
|
||||||
error => panic!("Expected invalid array error, got {}", error),
|
error => panic!("Expected invalid array error, got {}", error),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_synthesis(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::IntegerError(
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
||||||
IntegerError::SynthesisError(_string),
|
IntegerError::SynthesisError(_string),
|
||||||
|
@ -6,32 +6,28 @@ use leo_compiler::{
|
|||||||
errors::{CompilerError, FunctionError, StatementError},
|
errors::{CompilerError, FunctionError, StatementError},
|
||||||
ConstrainedValue, InputValue,
|
ConstrainedValue, InputValue,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/boolean/";
|
const DIRECTORY_NAME: &str = "tests/boolean/";
|
||||||
|
|
||||||
fn output_true(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_true(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Boolean(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Boolean(Boolean::Constant(true))]),
|
||||||
Boolean::Constant(true)
|
|
||||||
)]),
|
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn output_false(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_false(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Boolean(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Boolean(Boolean::Constant(false))]),
|
||||||
Boolean::Constant(false)
|
|
||||||
)]),
|
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_evaluate(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_evaluate(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(
|
CompilerError::FunctionError(FunctionError::StatementError(
|
||||||
StatementError::ExpressionError(ExpressionError::BooleanError(
|
StatementError::ExpressionError(ExpressionError::BooleanError(
|
||||||
@ -42,7 +38,7 @@ fn fail_evaluate(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_enforce(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_enforce(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(
|
CompilerError::FunctionError(FunctionError::StatementError(
|
||||||
StatementError::ExpressionError(ExpressionError::BooleanError(
|
StatementError::ExpressionError(ExpressionError::BooleanError(
|
||||||
@ -53,7 +49,7 @@ fn fail_enforce(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_boolean(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_boolean(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::BooleanError(
|
CompilerError::FunctionError(FunctionError::BooleanError(
|
||||||
BooleanError::InvalidBoolean(_string),
|
BooleanError::InvalidBoolean(_string),
|
||||||
@ -62,7 +58,7 @@ fn fail_boolean(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_synthesis(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::BooleanError(
|
CompilerError::FunctionError(FunctionError::BooleanError(
|
||||||
BooleanError::SynthesisError(_string),
|
BooleanError::SynthesisError(_string),
|
||||||
|
@ -12,29 +12,27 @@ use leo_compiler::{
|
|||||||
ConstrainedCircuitMember, ConstrainedValue, Expression, Function, Identifier, Integer,
|
ConstrainedCircuitMember, ConstrainedValue, Expression, Function, Identifier, Integer,
|
||||||
Statement, Type,
|
Statement, Type,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/circuit/";
|
const DIRECTORY_NAME: &str = "tests/circuit/";
|
||||||
|
|
||||||
// Circ { x: 1u32 }
|
// Circ { x: 1u32 }
|
||||||
fn output_circuit(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_circuit(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::CircuitExpression(
|
||||||
ConstrainedValue::CircuitExpression(
|
|
||||||
Identifier::new("Circ".into()),
|
Identifier::new("Circ".into()),
|
||||||
vec![ConstrainedCircuitMember(
|
vec![ConstrainedCircuitMember(
|
||||||
Identifier::new("x".into()),
|
Identifier::new("x".into()),
|
||||||
ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32)))
|
ConstrainedValue::Integer(Integer::U32(UInt32::constant(1u32)))
|
||||||
)]
|
)]
|
||||||
)
|
)]),
|
||||||
]),
|
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_expected_member(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_expected_member(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(
|
CompilerError::FunctionError(FunctionError::StatementError(
|
||||||
StatementError::ExpressionError(ExpressionError::ExpectedCircuitMember(_string)),
|
StatementError::ExpressionError(ExpressionError::ExpectedCircuitMember(_string)),
|
||||||
@ -43,7 +41,7 @@ fn fail_expected_member(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_undefined_member(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_undefined_member(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(
|
CompilerError::FunctionError(FunctionError::StatementError(
|
||||||
StatementError::ExpressionError(ExpressionError::UndefinedMemberAccess(_, _)),
|
StatementError::ExpressionError(ExpressionError::UndefinedMemberAccess(_, _)),
|
||||||
@ -153,8 +151,7 @@ fn test_self() {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::CircuitExpression(
|
||||||
ConstrainedValue::CircuitExpression(
|
|
||||||
Identifier::new("Circ".into()),
|
Identifier::new("Circ".into()),
|
||||||
vec![ConstrainedCircuitMember(
|
vec![ConstrainedCircuitMember(
|
||||||
Identifier::new("new".into()),
|
Identifier::new("new".into()),
|
||||||
@ -171,8 +168,7 @@ fn test_self() {
|
|||||||
}
|
}
|
||||||
)))
|
)))
|
||||||
)]
|
)]
|
||||||
)
|
)]),
|
||||||
]),
|
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -6,32 +6,32 @@ use leo_compiler::{
|
|||||||
errors::{CompilerError, FunctionError},
|
errors::{CompilerError, FunctionError},
|
||||||
ConstrainedValue, FieldElement, InputValue,
|
ConstrainedValue, FieldElement, InputValue,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::curves::Field;
|
use snarkos_models::curves::Field;
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/field_element/";
|
const DIRECTORY_NAME: &str = "tests/field_element/";
|
||||||
|
|
||||||
fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_zero(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::FieldElement(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::FieldElement(
|
||||||
FieldElement::Constant(Fr::zero())
|
FieldElement::Constant(Fr::zero())
|
||||||
)]),
|
)]),
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn output_one(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_one(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::FieldElement(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::FieldElement(
|
||||||
FieldElement::Constant(Fr::one())
|
FieldElement::Constant(Fr::one())
|
||||||
)]),
|
)]),
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_field(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_field(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::FieldElementError(
|
CompilerError::FunctionError(FunctionError::FieldElementError(
|
||||||
FieldElementError::InvalidField(_string),
|
FieldElementError::InvalidField(_string),
|
||||||
@ -40,7 +40,7 @@ fn fail_field(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_synthesis(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::FieldElementError(
|
CompilerError::FunctionError(FunctionError::FieldElementError(
|
||||||
FieldElementError::SynthesisError(_string),
|
FieldElementError::SynthesisError(_string),
|
||||||
|
@ -5,24 +5,21 @@ use leo_compiler::{
|
|||||||
errors::{CompilerError, ExpressionError, FunctionError, StatementError},
|
errors::{CompilerError, ExpressionError, FunctionError, StatementError},
|
||||||
ConstrainedValue,
|
ConstrainedValue,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
use snarkos_models::gadgets::utilities::boolean::Boolean;
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/function/";
|
const DIRECTORY_NAME: &str = "tests/function/";
|
||||||
|
|
||||||
pub(crate) fn output_empty(program: Compiler<Fr, EdwardsProjective>) {
|
pub(crate) fn output_empty(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(ConstrainedValue::<Fr>::Return(vec![]), output);
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![]),
|
|
||||||
output
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// (true, false)
|
// (true, false)
|
||||||
pub(crate) fn output_multiple(program: Compiler<Fr, EdwardsProjective>) {
|
pub(crate) fn output_multiple(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![
|
ConstrainedValue::<Fr>::Return(vec![
|
||||||
ConstrainedValue::Boolean(Boolean::Constant(true)),
|
ConstrainedValue::Boolean(Boolean::Constant(true)),
|
||||||
ConstrainedValue::Boolean(Boolean::Constant(false))
|
ConstrainedValue::Boolean(Boolean::Constant(false))
|
||||||
]),
|
]),
|
||||||
@ -30,7 +27,7 @@ pub(crate) fn output_multiple(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_undefined_identifier(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_undefined_identifier(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::StatementError(
|
CompilerError::FunctionError(FunctionError::StatementError(
|
||||||
StatementError::ExpressionError(ExpressionError::UndefinedIdentifier(_)),
|
StatementError::ExpressionError(ExpressionError::UndefinedIdentifier(_)),
|
||||||
|
1
compiler/tests/group/mod.rs
Normal file
1
compiler/tests/group/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
@ -1,23 +0,0 @@
|
|||||||
use crate::{compile_program, get_output};
|
|
||||||
|
|
||||||
use leo_compiler::{compiler::Compiler, ConstrainedValue};
|
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
|
||||||
use snarkos_models::curves::Group;
|
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/group_element/";
|
|
||||||
|
|
||||||
pub(crate) fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
|
|
||||||
let output = get_output(program);
|
|
||||||
assert_eq!(
|
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::GroupElement(
|
|
||||||
EdwardsProjective::zero()
|
|
||||||
)]),
|
|
||||||
output
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_zero() {
|
|
||||||
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
|
|
||||||
output_zero(program);
|
|
||||||
}
|
|
@ -3,42 +3,42 @@ use crate::{compile_program, get_error, get_output};
|
|||||||
use leo_compiler::{compiler::Compiler, types::Integer, ConstrainedValue, InputValue};
|
use leo_compiler::{compiler::Compiler, types::Integer, ConstrainedValue, InputValue};
|
||||||
|
|
||||||
use leo_compiler::errors::{CompilerError, FunctionError, IntegerError};
|
use leo_compiler::errors::{CompilerError, FunctionError, IntegerError};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
use snarkos_models::gadgets::utilities::uint32::UInt32;
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/integer/u32/";
|
const DIRECTORY_NAME: &str = "tests/integer/u32/";
|
||||||
|
|
||||||
pub(crate) fn output_zero(program: Compiler<Fr, EdwardsProjective>) {
|
pub(crate) fn output_zero(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Integer(Integer::U32(
|
||||||
Integer::U32(UInt32::constant(0u32))
|
UInt32::constant(0u32)
|
||||||
)]),
|
))]),
|
||||||
output
|
output
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn output_one(program: Compiler<Fr, EdwardsProjective>) {
|
pub(crate) fn output_one(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Integer(Integer::U32(
|
||||||
Integer::U32(UInt32::constant(1u32))
|
UInt32::constant(1u32)
|
||||||
)]),
|
))]),
|
||||||
output
|
output
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn output_two(program: Compiler<Fr, EdwardsProjective>) {
|
fn output_two(program: Compiler<Fr>) {
|
||||||
let output = get_output(program);
|
let output = get_output(program);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Integer(Integer::U32(
|
||||||
Integer::U32(UInt32::constant(2u32))
|
UInt32::constant(2u32)
|
||||||
)]),
|
))]),
|
||||||
output
|
output
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_integer(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_integer(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::IntegerError(
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
||||||
IntegerError::InvalidInteger(_string),
|
IntegerError::InvalidInteger(_string),
|
||||||
@ -47,7 +47,7 @@ fn fail_integer(program: Compiler<Fr, EdwardsProjective>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_synthesis(program: Compiler<Fr, EdwardsProjective>) {
|
fn fail_synthesis(program: Compiler<Fr>) {
|
||||||
match get_error(program) {
|
match get_error(program) {
|
||||||
CompilerError::FunctionError(FunctionError::IntegerError(
|
CompilerError::FunctionError(FunctionError::IntegerError(
|
||||||
IntegerError::SynthesisError(_string),
|
IntegerError::SynthesisError(_string),
|
||||||
|
@ -3,7 +3,7 @@ pub mod boolean;
|
|||||||
pub mod circuit;
|
pub mod circuit;
|
||||||
pub mod field_element;
|
pub mod field_element;
|
||||||
pub mod function;
|
pub mod function;
|
||||||
pub mod group_element;
|
pub mod group;
|
||||||
pub mod import;
|
pub mod import;
|
||||||
pub mod integer;
|
pub mod integer;
|
||||||
pub mod mutability;
|
pub mod mutability;
|
||||||
@ -11,21 +11,18 @@ pub mod statement;
|
|||||||
|
|
||||||
use leo_compiler::{compiler::Compiler, errors::CompilerError, ConstrainedValue};
|
use leo_compiler::{compiler::Compiler, errors::CompilerError, ConstrainedValue};
|
||||||
|
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
|
|
||||||
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
|
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
|
||||||
use std::env::current_dir;
|
use std::env::current_dir;
|
||||||
|
|
||||||
pub(crate) fn get_output(
|
pub(crate) fn get_output(program: Compiler<Fr>) -> ConstrainedValue<Fr> {
|
||||||
program: Compiler<Fr, EdwardsProjective>,
|
|
||||||
) -> ConstrainedValue<Fr, EdwardsProjective> {
|
|
||||||
let mut cs = TestConstraintSystem::<Fr>::new();
|
let mut cs = TestConstraintSystem::<Fr>::new();
|
||||||
let output = program.compile_constraints(&mut cs).unwrap();
|
let output = program.compile_constraints(&mut cs).unwrap();
|
||||||
assert!(cs.is_satisfied());
|
assert!(cs.is_satisfied());
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_error(program: Compiler<Fr, EdwardsProjective>) -> CompilerError {
|
pub(crate) fn get_error(program: Compiler<Fr>) -> CompilerError {
|
||||||
let mut cs = TestConstraintSystem::<Fr>::new();
|
let mut cs = TestConstraintSystem::<Fr>::new();
|
||||||
program.compile_constraints(&mut cs).unwrap_err()
|
program.compile_constraints(&mut cs).unwrap_err()
|
||||||
}
|
}
|
||||||
@ -33,7 +30,7 @@ pub(crate) fn get_error(program: Compiler<Fr, EdwardsProjective>) -> CompilerErr
|
|||||||
pub(crate) fn compile_program(
|
pub(crate) fn compile_program(
|
||||||
directory_name: &str,
|
directory_name: &str,
|
||||||
file_name: &str,
|
file_name: &str,
|
||||||
) -> Result<Compiler<Fr, EdwardsProjective>, CompilerError> {
|
) -> Result<Compiler<Fr>, CompilerError> {
|
||||||
let path = current_dir().map_err(|error| CompilerError::DirectoryError(error))?;
|
let path = current_dir().map_err(|error| CompilerError::DirectoryError(error))?;
|
||||||
|
|
||||||
// Sanitize the package path to the test directory
|
// Sanitize the package path to the test directory
|
||||||
@ -50,5 +47,5 @@ pub(crate) fn compile_program(
|
|||||||
println!("Compiling file - {:?}", main_file_path);
|
println!("Compiling file - {:?}", main_file_path);
|
||||||
|
|
||||||
// Compile from the main file path
|
// Compile from the main file path
|
||||||
Compiler::<Fr, EdwardsProjective>::init(file_name.to_string(), main_file_path)
|
Compiler::<Fr>::init(file_name.to_string(), main_file_path)
|
||||||
}
|
}
|
||||||
|
@ -6,25 +6,25 @@ use leo_compiler::{
|
|||||||
types::{InputValue, Integer},
|
types::{InputValue, Integer},
|
||||||
ConstrainedValue,
|
ConstrainedValue,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{bls12_377::Fr, edwards_bls12::EdwardsProjective};
|
use snarkos_curves::bls12_377::Fr;
|
||||||
use snarkos_models::gadgets::{r1cs::TestConstraintSystem, utilities::uint32::UInt32};
|
use snarkos_models::gadgets::{r1cs::TestConstraintSystem, utilities::uint32::UInt32};
|
||||||
|
|
||||||
const DIRECTORY_NAME: &str = "tests/mutability/";
|
const DIRECTORY_NAME: &str = "tests/mutability/";
|
||||||
|
|
||||||
fn mut_success(program: Compiler<Fr, EdwardsProjective>) {
|
fn mut_success(program: Compiler<Fr>) {
|
||||||
let mut cs = TestConstraintSystem::<Fr>::new();
|
let mut cs = TestConstraintSystem::<Fr>::new();
|
||||||
let output = program.compile_constraints(&mut cs).unwrap();
|
let output = program.compile_constraints(&mut cs).unwrap();
|
||||||
|
|
||||||
assert!(cs.is_satisfied());
|
assert!(cs.is_satisfied());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ConstrainedValue::<Fr, EdwardsProjective>::Return(vec![ConstrainedValue::Integer(
|
ConstrainedValue::<Fr>::Return(vec![ConstrainedValue::Integer(Integer::U32(
|
||||||
Integer::U32(UInt32::constant(0))
|
UInt32::constant(0)
|
||||||
)]),
|
))]),
|
||||||
output
|
output
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mut_fail(program: Compiler<Fr, EdwardsProjective>) {
|
fn mut_fail(program: Compiler<Fr>) {
|
||||||
let mut cs = TestConstraintSystem::<Fr>::new();
|
let mut cs = TestConstraintSystem::<Fr>::new();
|
||||||
let err = program.compile_constraints(&mut cs).unwrap_err();
|
let err = program.compile_constraints(&mut cs).unwrap_err();
|
||||||
|
|
||||||
|
@ -5,10 +5,7 @@ use crate::{cli::*, cli_types::*};
|
|||||||
use leo_compiler::compiler::Compiler;
|
use leo_compiler::compiler::Compiler;
|
||||||
|
|
||||||
use snarkos_algorithms::snark::KeypairAssembly;
|
use snarkos_algorithms::snark::KeypairAssembly;
|
||||||
use snarkos_curves::{
|
use snarkos_curves::bls12_377::{Bls12_377, Fr};
|
||||||
bls12_377::{Bls12_377, Fr},
|
|
||||||
edwards_bls12::EdwardsProjective
|
|
||||||
};
|
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
@ -19,7 +16,7 @@ pub struct BuildCommand;
|
|||||||
|
|
||||||
impl CLI for BuildCommand {
|
impl CLI for BuildCommand {
|
||||||
type Options = ();
|
type Options = ();
|
||||||
type Output = (Compiler<Fr, EdwardsProjective>, bool);
|
type Output = (Compiler<Fr>, bool);
|
||||||
|
|
||||||
const NAME: NameType = "build";
|
const NAME: NameType = "build";
|
||||||
const ABOUT: AboutType = "Compile the current package as a program";
|
const ABOUT: AboutType = "Compile the current package as a program";
|
||||||
@ -63,7 +60,7 @@ impl CLI for BuildCommand {
|
|||||||
main_file_path.push(MAIN_FILE_NAME);
|
main_file_path.push(MAIN_FILE_NAME);
|
||||||
|
|
||||||
// Compute the current program checksum
|
// Compute the current program checksum
|
||||||
let program = Compiler::<Fr, EdwardsProjective>::init(package_name.clone(), main_file_path.clone())?;
|
let program = Compiler::<Fr>::init(package_name.clone(), main_file_path.clone())?;
|
||||||
let program_checksum = program.checksum()?;
|
let program_checksum = program.checksum()?;
|
||||||
|
|
||||||
// Generate the program on the constraint system and verify correctness
|
// Generate the program on the constraint system and verify correctness
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
|
||||||
use crate::commands::BuildCommand;
|
use crate::commands::BuildCommand;
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
use crate::files::Manifest;
|
use crate::files::Manifest;
|
||||||
|
use crate::{cli::*, cli_types::*};
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
|
||||||
use crate::commands::BuildCommand;
|
use crate::commands::BuildCommand;
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
use crate::files::Manifest;
|
use crate::files::Manifest;
|
||||||
|
use crate::{cli::*, cli_types::*};
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
|
||||||
use crate::commands::SetupCommand;
|
use crate::commands::SetupCommand;
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
use crate::files::{Manifest, ProofFile};
|
use crate::files::{Manifest, ProofFile};
|
||||||
|
use crate::{cli::*, cli_types::*};
|
||||||
|
|
||||||
use snarkos_algorithms::snark::{create_random_proof, Proof};
|
use snarkos_algorithms::snark::{create_random_proof, Proof};
|
||||||
use snarkos_curves::bls12_377::Bls12_377;
|
use snarkos_curves::bls12_377::Bls12_377;
|
||||||
@ -45,7 +45,10 @@ impl CLI for ProveCommand {
|
|||||||
let rng = &mut thread_rng();
|
let rng = &mut thread_rng();
|
||||||
let program_proof = create_random_proof(program, ¶meters, rng).unwrap();
|
let program_proof = create_random_proof(program, ¶meters, rng).unwrap();
|
||||||
|
|
||||||
log::info!("Prover completed in {:?} milliseconds", start.elapsed().as_millis());
|
log::info!(
|
||||||
|
"Prover completed in {:?} milliseconds",
|
||||||
|
start.elapsed().as_millis()
|
||||||
|
);
|
||||||
|
|
||||||
// Write the proof file to the outputs directory
|
// Write the proof file to the outputs directory
|
||||||
let mut proof = vec![];
|
let mut proof = vec![];
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
|
||||||
use crate::commands::BuildCommand;
|
use crate::commands::BuildCommand;
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
use crate::files::Manifest;
|
use crate::files::Manifest;
|
||||||
|
use crate::{cli::*, cli_types::*};
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::commands::{SetupCommand, ProveCommand};
|
use crate::commands::{ProveCommand, SetupCommand};
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
use crate::{cli::*, cli_types::*};
|
use crate::{cli::*, cli_types::*};
|
||||||
|
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
|
||||||
use crate::commands::BuildCommand;
|
use crate::commands::BuildCommand;
|
||||||
use crate::errors::{CLIError, VerificationKeyFileError};
|
use crate::errors::{CLIError, VerificationKeyFileError};
|
||||||
use crate::files::{Manifest, ProvingKeyFile, VerificationKeyFile};
|
use crate::files::{Manifest, ProvingKeyFile, VerificationKeyFile};
|
||||||
|
use crate::{cli::*, cli_types::*};
|
||||||
use leo_compiler::compiler::Compiler;
|
use leo_compiler::compiler::Compiler;
|
||||||
|
|
||||||
use snarkos_algorithms::snark::{
|
use snarkos_algorithms::snark::{
|
||||||
generate_random_parameters, prepare_verifying_key, Parameters, PreparedVerifyingKey,
|
generate_random_parameters, prepare_verifying_key, Parameters, PreparedVerifyingKey,
|
||||||
};
|
};
|
||||||
use snarkos_curves::{
|
use snarkos_curves::bls12_377::{Bls12_377, Fr};
|
||||||
bls12_377::{Bls12_377, Fr},
|
|
||||||
edwards_bls12::EdwardsProjective
|
|
||||||
};
|
|
||||||
use snarkos_utilities::bytes::ToBytes;
|
use snarkos_utilities::bytes::ToBytes;
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
@ -25,7 +22,7 @@ pub struct SetupCommand;
|
|||||||
impl CLI for SetupCommand {
|
impl CLI for SetupCommand {
|
||||||
type Options = ();
|
type Options = ();
|
||||||
type Output = (
|
type Output = (
|
||||||
Compiler<Fr, EdwardsProjective>,
|
Compiler<Fr>,
|
||||||
Parameters<Bls12_377>,
|
Parameters<Bls12_377>,
|
||||||
PreparedVerifyingKey<Bls12_377>,
|
PreparedVerifyingKey<Bls12_377>,
|
||||||
);
|
);
|
||||||
@ -66,7 +63,10 @@ impl CLI for SetupCommand {
|
|||||||
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(¶meters.vk);
|
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(¶meters.vk);
|
||||||
|
|
||||||
// End the timer
|
// End the timer
|
||||||
log::info!("Setup completed in {:?} milliseconds", start.elapsed().as_millis());
|
log::info!(
|
||||||
|
"Setup completed in {:?} milliseconds",
|
||||||
|
start.elapsed().as_millis()
|
||||||
|
);
|
||||||
|
|
||||||
// TODO (howardwu): Convert parameters to a 'proving key' struct for serialization.
|
// TODO (howardwu): Convert parameters to a 'proving key' struct for serialization.
|
||||||
// Write the proving key file to the outputs directory
|
// Write the proving key file to the outputs directory
|
||||||
@ -95,10 +95,13 @@ impl CLI for SetupCommand {
|
|||||||
prepared_verifying_key.write(&mut verification_key)?;
|
prepared_verifying_key.write(&mut verification_key)?;
|
||||||
|
|
||||||
// Check that the constructed prepared verification key matches the stored verification key
|
// Check that the constructed prepared verification key matches the stored verification key
|
||||||
let compare: Vec<(u8, u8)> = verification_key.into_iter().zip(stored_vk.into_iter()).collect();
|
let compare: Vec<(u8, u8)> = verification_key
|
||||||
|
.into_iter()
|
||||||
|
.zip(stored_vk.into_iter())
|
||||||
|
.collect();
|
||||||
for (a, b) in compare {
|
for (a, b) in compare {
|
||||||
if a != b {
|
if a != b {
|
||||||
return Err(VerificationKeyFileError::IncorrectVerificationKey.into())
|
return Err(VerificationKeyFileError::IncorrectVerificationKey.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::{cli::*, cli_types::*};
|
|
||||||
use crate::commands::BuildCommand;
|
use crate::commands::BuildCommand;
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
use crate::files::Manifest;
|
use crate::files::Manifest;
|
||||||
|
use crate::{cli::*, cli_types::*};
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
@ -31,7 +31,10 @@ impl ChecksumFile {
|
|||||||
pub fn read_from(&self, path: &PathBuf) -> Result<String, ChecksumFileError> {
|
pub fn read_from(&self, path: &PathBuf) -> Result<String, ChecksumFileError> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
Ok(fs::read_to_string(&path).map_err(|_| ChecksumFileError::FileReadError(path.clone()))?)
|
Ok(
|
||||||
|
fs::read_to_string(&path)
|
||||||
|
.map_err(|_| ChecksumFileError::FileReadError(path.clone()))?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given checksum to a file.
|
/// Writes the given checksum to a file.
|
||||||
@ -52,7 +55,10 @@ impl ChecksumFile {
|
|||||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||||
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
||||||
}
|
}
|
||||||
path.push(PathBuf::from(format!("{}{}", self.package_name, CHECKSUM_FILE_EXTENSION)));
|
path.push(PathBuf::from(format!(
|
||||||
|
"{}{}",
|
||||||
|
self.package_name, CHECKSUM_FILE_EXTENSION
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,8 @@ impl ProofFile {
|
|||||||
pub fn read_from(&self, path: &PathBuf) -> Result<String, ProofFileError> {
|
pub fn read_from(&self, path: &PathBuf) -> Result<String, ProofFileError> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let proof = fs::read_to_string(&path).map_err(|_| ProofFileError::FileReadError(path.clone()))?;
|
let proof =
|
||||||
|
fs::read_to_string(&path).map_err(|_| ProofFileError::FileReadError(path.clone()))?;
|
||||||
Ok(proof)
|
Ok(proof)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +54,10 @@ impl ProofFile {
|
|||||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||||
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
||||||
}
|
}
|
||||||
path.push(PathBuf::from(format!("{}{}", self.package_name, PROOF_FILE_EXTENSION)));
|
path.push(PathBuf::from(format!(
|
||||||
|
"{}{}",
|
||||||
|
self.package_name, PROOF_FILE_EXTENSION
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,10 @@ impl ProvingKeyFile {
|
|||||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||||
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
||||||
}
|
}
|
||||||
path.push(PathBuf::from(format!("{}{}", self.package_name, PROVING_KEY_FILE_EXTENSION)));
|
path.push(PathBuf::from(format!(
|
||||||
|
"{}{}",
|
||||||
|
self.package_name, PROVING_KEY_FILE_EXTENSION
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,11 @@ impl VerificationKeyFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the given verification key to a file.
|
/// Writes the given verification key to a file.
|
||||||
pub fn write_to(&self, path: &PathBuf, verification_key: &[u8]) -> Result<(), VerificationKeyFileError> {
|
pub fn write_to(
|
||||||
|
&self,
|
||||||
|
path: &PathBuf,
|
||||||
|
verification_key: &[u8],
|
||||||
|
) -> Result<(), VerificationKeyFileError> {
|
||||||
let path = self.setup_file_path(path);
|
let path = self.setup_file_path(path);
|
||||||
|
|
||||||
let mut file = File::create(&path)?;
|
let mut file = File::create(&path)?;
|
||||||
@ -52,7 +56,10 @@ impl VerificationKeyFile {
|
|||||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||||
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
||||||
}
|
}
|
||||||
path.push(PathBuf::from(format!("{}{}", self.package_name, VERIFICATION_KEY_FILE_EXTENSION)));
|
path.push(PathBuf::from(format!(
|
||||||
|
"{}{}",
|
||||||
|
self.package_name, VERIFICATION_KEY_FILE_EXTENSION
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user