impl group element values

This commit is contained in:
collin 2020-05-12 20:01:47 -07:00
parent 7e36d2b5fa
commit 6efc834948
25 changed files with 112 additions and 79 deletions

View File

@ -1,10 +1,3 @@
// Basic Pedersen hash function
// 1 window x 3 bits
function main() {
let arr1: u32[2][3] = [[0; 3]; 2];
let arr2: u32[2][3] = [[0, 0, 0], [0, 0, 0]];
assert_eq(arr1, arr2);
let g = 0group;
}

View File

@ -6,24 +6,23 @@ use snarkos_algorithms::snark::{
create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
};
use snarkos_curves::bls12_377::{Bls12_377, Fr};
use snarkos_curves::edwards_bls12::EdwardsProjective;
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::curves::Group;
use snarkos_models::{
curves::{Field, PrimeField},
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
};
use snarkos_models::curves::Group;
use std::{
fs,
marker::PhantomData,
time::{Duration, Instant},
};
use std::str::FromStr;
use snarkos_curves::edwards_bls12::EdwardsProjective;
#[derive(Clone)]
pub struct Benchmark<F: Field + PrimeField, G: Group> {
program: Program<F>,
parameters: Vec<Option<InputValue<F>>>,
program: Program<F, G>,
parameters: Vec<Option<InputValue<F, G>>>,
_group: PhantomData<G>,
_engine: PhantomData<F>,
}
@ -39,12 +38,6 @@ impl<F: Field + PrimeField, G: Group> Benchmark<F, G> {
}
pub fn evaluate_program(&mut self) -> Result<(), CompilerError> {
let scalar = G::ScalarField::from_str("2325446546544").unwrap_or_default();
println!("{}", scalar);
let other = G::default().mul(&G::ScalarField::one());
println!("{}", other);
assert_eq!(G::default(), G::default().double());
// Read in file as string
let unparsed_file = fs::read_to_string("simple.leo").expect("cannot read file");
@ -53,10 +46,9 @@ impl<F: Field + PrimeField, G: Group> Benchmark<F, G> {
// Build the abstract syntax tree
let syntax_tree = ast::File::from_pest(&mut file).expect("infallible");
// println!("{:#?}", syntax_tree);
// Build a leo program from the syntax tree
self.program = Program::<F>::from(syntax_tree, "simple".into());
self.program = Program::<F, G>::from(syntax_tree, "simple".into());
self.parameters = vec![None; self.program.num_parameters];
println!(" compiled: {:#?}\n", self.program);
@ -70,8 +62,7 @@ impl<F: Field + PrimeField, G: Group> ConstraintSynthesizer<F> for Benchmark<F,
self,
cs: &mut CS,
) -> Result<(), SynthesisError> {
let _res =
leo_compiler::generate_constraints(cs, self.program, self.parameters).unwrap();
let _res = leo_compiler::generate_constraints(cs, self.program, self.parameters).unwrap();
println!(" Result: {}", _res);
// Write results to file or something

View File

@ -259,6 +259,7 @@ impl<'ast> fmt::Display for Integer<'ast> {
#[pest_ast(rule(Rule::value_field))]
pub struct Field<'ast> {
pub number: Number<'ast>,
pub _type: FieldType<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
@ -273,6 +274,7 @@ impl<'ast> fmt::Display for Field<'ast> {
#[pest_ast(rule(Rule::value_group))]
pub struct Group<'ast> {
pub number: Number<'ast>,
pub _type: GroupType<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -9,7 +9,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
};

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, boolean::Boolean, eq::EqGadget},

View File

@ -10,7 +10,7 @@ use crate::{
};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};
@ -440,6 +440,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
// Values
Expression::Integer(integer) => Ok(Self::get_integer_constant(integer)),
Expression::FieldElement(fe) => Ok(Self::get_field_element_constant(fe)),
Expression::GroupElement(gr) => Ok(ConstrainedValue::GroupElement(gr)),
Expression::Boolean(bool) => Ok(Self::get_boolean_constant(bool)),
// Binary operations

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::r1cs::{ConstraintSystem, LinearCombination, Variable as R1CSVariable},
};

View File

@ -13,7 +13,7 @@ use crate::{
};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::r1cs::ConstraintSystem,
};

View File

@ -8,7 +8,7 @@ use crate::{
use from_pest::FromPest;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::r1cs::ConstraintSystem,
};
use std::fs;

View File

@ -8,7 +8,7 @@ use crate::{
};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean},
};

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, eq::EqGadget, uint128::UInt128},

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, eq::EqGadget, uint16::UInt16},

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, eq::EqGadget, uint32::UInt32},

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, eq::EqGadget, uint64::UInt64},

View File

@ -8,7 +8,7 @@ use crate::{
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{
r1cs::ConstraintSystem,
utilities::{alloc::AllocGadget, eq::EqGadget, uint8::UInt8},

View File

@ -33,7 +33,7 @@ use crate::{
};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::r1cs::ConstraintSystem,
};

View File

@ -3,7 +3,7 @@
use crate::{constraints::ConstrainedValue, types::Variable};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::r1cs::ConstraintSystem,
};
use std::{collections::HashMap, marker::PhantomData};
@ -31,7 +31,7 @@ pub fn new_variable_from_variable<F: Field + PrimeField, G: Group>(
Variable {
name: new_scope_from_variable(outer, inner),
_engine: PhantomData::<F>,
_group: PhantomData::<G>
_group: PhantomData::<G>,
}
}
@ -58,7 +58,11 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
self.resolved_names.insert(name, value);
}
pub(crate) fn store_variable(&mut self, variable: Variable<F, G>, value: ConstrainedValue<F, G>) {
pub(crate) fn store_variable(
&mut self,
variable: Variable<F, G>,
value: ConstrainedValue<F, G>,
) {
self.store(variable.name, value);
}

View File

@ -10,7 +10,7 @@ use crate::{
};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::{r1cs::ConstraintSystem, utilities::boolean::Boolean, utilities::uint32::UInt32},
};

View File

@ -7,13 +7,16 @@ use crate::{
};
use snarkos_models::{
curves::{Group, Field, PrimeField},
curves::{Field, Group, PrimeField},
gadgets::utilities::boolean::Boolean,
};
use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct ConstrainedStructMember<F: Field + PrimeField, G: Group>(pub Variable<F, G>, pub ConstrainedValue<F, G>);
pub struct ConstrainedStructMember<F: Field + PrimeField, G: Group>(
pub Variable<F, G>,
pub ConstrainedValue<F, G>,
);
#[derive(Clone, PartialEq, Eq)]
pub enum ConstrainedValue<F: Field + PrimeField, G: Group> {

View File

@ -2,7 +2,7 @@
use crate::Variable;
use snarkos_models::curves::{Field, PrimeField, Group};
use snarkos_models::curves::{Field, Group, PrimeField};
use std::fmt;
#[derive(Clone)]

View File

@ -72,7 +72,7 @@ type_integer = {
type_field = {"field"}
type_group = {"group"}
type_bool = {"bool"}
type_basic = { type_integer | type_field | type_group | type_bool }
type_basic = { type_field | type_group | type_bool | type_integer }
type_struct = { variable }
type_basic_or_struct = {type_basic | type_struct }
type_array = {type_basic ~ ("[" ~ value ~ "]")+ }
@ -83,10 +83,10 @@ type_list = _{(_type ~ ("," ~ _type)*)?}
value_number = @{ "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* }
value_integer = { value_number ~ type_integer? }
value_field = { value_number ~ "fe" }
value_group = { value_number ~ "gr" }
value_field = { value_number ~ type_field }
value_group = { value_number ~ type_group }
value_boolean = { "true" | "false" }
value = { value_field | value_boolean | value_integer }
value = { value_field | value_group | value_boolean | value_integer }
/// Variables

View File

@ -4,7 +4,7 @@
use crate::{errors::IntegerError, Import};
use crate::errors::ValueError;
use snarkos_models::curves::{Field, PrimeField, Group};
use snarkos_models::curves::{Field, Group, PrimeField};
use snarkos_models::gadgets::{
r1cs::Variable as R1CSVariable,
utilities::{
@ -135,7 +135,11 @@ pub enum Expression<F: Field + PrimeField, G: Group> {
Lt(Box<Expression<F, G>>, Box<Expression<F, G>>),
// Conditionals
IfElse(Box<Expression<F, G>>, Box<Expression<F, G>>, Box<Expression<F, G>>),
IfElse(
Box<Expression<F, G>>,
Box<Expression<F, G>>,
Box<Expression<F, G>>,
),
// Arrays
Array(Vec<Box<SpreadOrExpression<F, G>>>),

View File

@ -6,7 +6,7 @@ use crate::{
SpreadOrExpression, Statement, Struct, StructField, Type, Variable,
};
use snarkos_models::curves::{Group, Field, PrimeField};
use snarkos_models::curves::{Field, Group, PrimeField};
use std::fmt;
impl<F: Field + PrimeField, G: Group> fmt::Display for Variable<F, G> {
@ -34,7 +34,7 @@ impl<F: Field + PrimeField> FieldElement<F> {
if option.is_some() {
write!(f, "{}", option.unwrap())
} else {
write!(f, "allocated fe")
write!(f, "allocated field")
}
}
}
@ -314,6 +314,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for InputValue<F, G> {
match self {
InputValue::Integer(ref integer) => write!(f, "{}", integer),
InputValue::Field(ref field) => write!(f, "{}", field),
InputValue::Group(ref group) => write!(f, "{}", group),
InputValue::Boolean(ref bool) => write!(f, "{}", bool),
InputValue::Array(ref array) => {
write!(f, "[")?;

View File

@ -2,7 +2,7 @@
use crate::{ast, types, Import, ImportSymbol};
use snarkos_models::curves::{Field, PrimeField, Group};
use snarkos_models::curves::{Field, Group, PrimeField};
use snarkos_models::gadgets::utilities::{
boolean::Boolean, uint128::UInt128, uint16::UInt16, uint32::UInt32, uint64::UInt64,
uint8::UInt8,
@ -80,12 +80,14 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::RangeOrExpression<'ast>>
unimplemented!("Range bounds should be integers, found {}", expression)
}
});
let to = range.to.map(|to| match types::Expression::<F, G>::from(to.0) {
types::Expression::Integer(number) => number,
expression => {
unimplemented!("Range bounds should be integers, found {}", expression)
}
});
let to = range
.to
.map(|to| match types::Expression::<F, G>::from(to.0) {
types::Expression::Integer(number) => number,
expression => {
unimplemented!("Range bounds should be integers, found {}", expression)
}
});
types::RangeOrExpression::Range(from, to)
}
@ -133,14 +135,16 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Value<'ast>> for types::Ex
fn from(value: ast::Value<'ast>) -> Self {
match value {
ast::Value::Integer(num) => types::Expression::from(num),
ast::Value::Field(fe) => types::Expression::from(fe),
ast::Value::Group(group) => unimplemented!("no groups yet"),
ast::Value::Field(field) => types::Expression::from(field),
ast::Value::Group(group) => types::Expression::from(group),
ast::Value::Boolean(bool) => types::Expression::from(bool),
}
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::NotExpression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::NotExpression<'ast>>
for types::Expression<F, G>
{
fn from(expression: ast::NotExpression<'ast>) -> Self {
types::Expression::Not(Box::new(types::Expression::from(*expression.expression)))
}
@ -161,7 +165,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::SpreadOrExpression<'ast>>
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::BinaryExpression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::BinaryExpression<'ast>>
for types::Expression<F, G>
{
fn from(expression: ast::BinaryExpression<'ast>) -> Self {
match expression.operation {
// Boolean operations
@ -221,7 +227,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::BinaryExpression<'ast>> fo
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::TernaryExpression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::TernaryExpression<'ast>>
for types::Expression<F, G>
{
fn from(expression: ast::TernaryExpression<'ast>) -> Self {
types::Expression::IfElse(
Box::new(types::Expression::from(*expression.first)),
@ -231,7 +239,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::TernaryExpression<'ast>> f
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInlineExpression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInlineExpression<'ast>>
for types::Expression<F, G>
{
fn from(array: ast::ArrayInlineExpression<'ast>) -> Self {
types::Expression::Array(
array
@ -253,7 +263,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInitializerExpression
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineStructMember<'ast>> for types::StructMember<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineStructMember<'ast>>
for types::StructMember<F, G>
{
fn from(member: ast::InlineStructMember<'ast>) -> Self {
types::StructMember {
variable: types::Variable::from(member.variable),
@ -262,7 +274,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineStructMember<'ast>>
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructInlineExpression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructInlineExpression<'ast>>
for types::Expression<F, G>
{
fn from(expression: ast::StructInlineExpression<'ast>) -> Self {
let variable = types::Variable::from(expression.variable);
let members = expression
@ -275,7 +289,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructInlineExpression<'as
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::PostfixExpression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::PostfixExpression<'ast>>
for types::Expression<F, G>
{
fn from(expression: ast::PostfixExpression<'ast>) -> Self {
let variable = types::Expression::Variable(types::Variable::from(expression.variable));
@ -312,7 +328,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::PostfixExpression<'ast>> f
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Expression<'ast>> for types::Expression<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Expression<'ast>>
for types::Expression<F, G>
{
fn from(expression: ast::Expression<'ast>) -> Self {
match expression {
ast::Expression::Value(value) => types::Expression::from(value),
@ -396,7 +414,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types:
/// pest ast -> types::Statement
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ReturnStatement<'ast>> for types::Statement<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ReturnStatement<'ast>>
for types::Statement<F, G>
{
fn from(statement: ast::ReturnStatement<'ast>) -> Self {
types::Statement::Return(
statement
@ -408,17 +428,23 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ReturnStatement<'ast>> for
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::DefinitionStatement<'ast>> for types::Statement<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::DefinitionStatement<'ast>>
for types::Statement<F, G>
{
fn from(statement: ast::DefinitionStatement<'ast>) -> Self {
types::Statement::Definition(
types::Assignee::from(statement.variable),
statement._type.map(|_type| types::Type::<F, G>::from(_type)),
statement
._type
.map(|_type| types::Type::<F, G>::from(_type)),
types::Expression::from(statement.expression),
)
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssignStatement<'ast>> for types::Statement<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssignStatement<'ast>>
for types::Statement<F, G>
{
fn from(statement: ast::AssignStatement<'ast>) -> Self {
match statement.assign {
ast::OperationAssign::Assign(ref _assign) => types::Statement::Assign(
@ -535,7 +561,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ConditionalStatement<'ast>
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ForStatement<'ast>> for types::Statement<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ForStatement<'ast>>
for types::Statement<F, G>
{
fn from(statement: ast::ForStatement<'ast>) -> Self {
let from = match types::Expression::<F, G>::from(statement.start) {
types::Expression::Integer(number) => number,
@ -559,7 +587,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ForStatement<'ast>> for ty
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssertStatement<'ast>> for types::Statement<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssertStatement<'ast>>
for types::Statement<F, G>
{
fn from(statement: ast::AssertStatement<'ast>) -> Self {
match statement {
ast::AssertStatement::AssertEq(assert_eq) => types::Statement::AssertEq(
@ -570,7 +600,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::AssertStatement<'ast>> for
}
}
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ExpressionStatement<'ast>> for types::Statement<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::ExpressionStatement<'ast>>
for types::Statement<F, G>
{
fn from(statement: ast::ExpressionStatement<'ast>) -> Self {
types::Statement::Expression(types::Expression::from(statement.expression))
}
@ -610,7 +642,9 @@ impl From<ast::IntegerType> for types::IntegerType {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::BasicType<'ast>> for types::Type<F, G> {
fn from(basic_type: ast::BasicType<'ast>) -> Self {
match basic_type {
ast::BasicType::Integer(_type) => types::Type::IntegerType(types::IntegerType::from(_type)),
ast::BasicType::Integer(_type) => {
types::Type::IntegerType(types::IntegerType::from(_type))
}
ast::BasicType::Field(_type) => types::Type::FieldElement,
ast::BasicType::Group(_type) => unimplemented!(),
ast::BasicType::Boolean(_type) => types::Type::Boolean,
@ -649,7 +683,9 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Type<'ast>> for types::Typ
/// pest ast -> types::Struct
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructField<'ast>> for types::StructField<F, G> {
impl<'ast, F: Field + PrimeField, G: Group> From<ast::StructField<'ast>>
for types::StructField<F, G>
{
fn from(struct_field: ast::StructField<'ast>) -> Self {
types::StructField {
variable: types::Variable::from(struct_field.variable),

View File

@ -1,2 +0,0 @@
// 3 bit Pedersen hash function
fn main() ->