mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
impl group add for edwards
This commit is contained in:
parent
8a5d3e7718
commit
9ba3dfbb6b
@ -21,4 +21,5 @@ pest = { version = "2.0" }
|
||||
pest-ast = { version = "0.3.3" }
|
||||
pest_derive = { version = "2.0" }
|
||||
rand = { version = "0.7" }
|
||||
rand_xorshift = { version = "0.2" }
|
||||
sha2 = { version = "0.8" }
|
||||
|
@ -69,9 +69,9 @@ impl<
|
||||
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
|
||||
Ok(self.enforce_field_add(cs, fe_1, fe_2)?)
|
||||
}
|
||||
// (ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
|
||||
// Ok(Self::evaluate_group_add(ge_1, ge_2))
|
||||
// }
|
||||
(ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
|
||||
Ok(ConstrainedValue::Group(ge_1.add(cs, &ge_2)?))
|
||||
}
|
||||
(ConstrainedValue::Unresolved(string), val_2) => {
|
||||
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
|
||||
self.enforce_add_expression(cs, val_1, val_2)
|
||||
|
@ -1 +0,0 @@
|
||||
|
@ -18,9 +18,6 @@ pub use integer::*;
|
||||
pub mod field_element;
|
||||
pub use field_element::*;
|
||||
|
||||
pub mod group;
|
||||
pub use group::*;
|
||||
|
||||
pub mod program;
|
||||
pub use program::*;
|
||||
|
||||
|
@ -71,7 +71,10 @@ impl<NativeF: Field, F: Field + PrimeField, GType: GroupType<NativeF, F>>
|
||||
Type::FieldElement => Ok(ConstrainedValue::FieldElement(FieldElement::Constant(
|
||||
F::from_str(&value).unwrap_or_default(),
|
||||
))),
|
||||
Type::Group => Ok(ConstrainedValue::Group(GType::constant(value)?)),
|
||||
Type::Group => Ok(ConstrainedValue::Group(GType::constant(format!(
|
||||
"({}, 1)",
|
||||
value
|
||||
))?)),
|
||||
Type::Boolean => Ok(ConstrainedValue::Boolean(Boolean::Constant(
|
||||
value.parse::<bool>()?,
|
||||
))),
|
||||
|
@ -2,8 +2,11 @@ use crate::errors::GroupError;
|
||||
use crate::GroupType;
|
||||
|
||||
use snarkos_curves::edwards_bls12::{EdwardsAffine, EdwardsParameters, Fq};
|
||||
use snarkos_curves::templates::twisted_edwards_extended::GroupAffine;
|
||||
use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget;
|
||||
use snarkos_models::curves::ModelParameters;
|
||||
use snarkos_models::curves::{AffineCurve, ModelParameters};
|
||||
use snarkos_models::gadgets::curves::GroupGadget;
|
||||
use snarkos_models::gadgets::r1cs::ConstraintSystem;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@ -20,19 +23,30 @@ impl GroupType<<EdwardsParameters as ModelParameters>::BaseField, Fq> for Edward
|
||||
Ok(EdwardsGroupType::Constant(result))
|
||||
}
|
||||
|
||||
// fn add<CS: ConstraintSystem<Fq>>(&self, cs: CS, other: &Self) -> Result<Self, GroupElementError> {
|
||||
// match (self, other) {
|
||||
// (EdwardsGroupType::Constant(self_value), EdwardsGroupType::Constant(other_value)) =>
|
||||
// Ok(EdwardsGroupType::Constant(self_value.add(other_value))),
|
||||
//
|
||||
// (EdwardsGroupType::Allocated(self_value), EdwardsGroupType::Allocated(other_value)) => {
|
||||
// let result = <AffineGadget<EdwardsParameters, Fq, FqGadget> as GroupGadget<GroupAffine<EdwardsParameters>, Fq>>::add(self_value, cs, other_value)?;
|
||||
// Ok(EdwardsGroupType::Allocated(result))
|
||||
// }
|
||||
//
|
||||
// (EdwardsGroupType::Constant(constant_value), EdwardsGroupType::Allocated(allocated_value))
|
||||
// | (EdwardsGroupType::Allocated(allocated_value), EdwardsGroupType::Constant(constant_value)) =>
|
||||
// Ok(EdwardsGroupType::Allocated(allocated_value.add_constant(cs, constant_value)?)),
|
||||
// }
|
||||
// }
|
||||
fn add<CS: ConstraintSystem<Fq>>(&self, cs: CS, other: &Self) -> Result<Self, GroupError> {
|
||||
match (self, other) {
|
||||
(EdwardsGroupType::Constant(self_value), EdwardsGroupType::Constant(other_value)) => {
|
||||
Ok(EdwardsGroupType::Constant(self_value.add(other_value)))
|
||||
}
|
||||
|
||||
(EdwardsGroupType::Allocated(self_value), EdwardsGroupType::Allocated(other_value)) => {
|
||||
let result = <EdwardsBlsGadget as GroupGadget<
|
||||
GroupAffine<EdwardsParameters>,
|
||||
Fq,
|
||||
>>::add(self_value, cs, other_value)?;
|
||||
Ok(EdwardsGroupType::Allocated(result))
|
||||
}
|
||||
|
||||
(
|
||||
EdwardsGroupType::Constant(constant_value),
|
||||
EdwardsGroupType::Allocated(allocated_value),
|
||||
)
|
||||
| (
|
||||
EdwardsGroupType::Allocated(allocated_value),
|
||||
EdwardsGroupType::Constant(constant_value),
|
||||
) => Ok(EdwardsGroupType::Allocated(
|
||||
allocated_value.add_constant(cs, constant_value)?,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
use crate::errors::GroupError;
|
||||
use snarkos_models::curves::Field;
|
||||
use snarkos_models::gadgets::r1cs::ConstraintSystem;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub mod edwards_bls12;
|
||||
|
||||
pub trait GroupType<NativeF: Field, F: Field>: Sized + Clone + Debug {
|
||||
fn constant(string: String) -> Result<Self, GroupError>;
|
||||
|
||||
fn add<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, GroupError>;
|
||||
}
|
||||
|
@ -122,7 +122,8 @@ impl<'ast, F: Field + PrimeField> From<ast::Field<'ast>> for types::Expression<F
|
||||
|
||||
impl<'ast, F: Field + PrimeField> From<ast::Group<'ast>> for types::Expression<F> {
|
||||
fn from(group: ast::Group<'ast>) -> Self {
|
||||
types::Expression::Group(group.number.value)
|
||||
let string = format!("({}, 1)", group.number.value);
|
||||
types::Expression::Group(string)
|
||||
}
|
||||
}
|
||||
|
||||
|
4
compiler/tests/group/add.leo
Normal file
4
compiler/tests/group/add.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() -> group {
|
||||
let a: group = 0 + 0;
|
||||
return a
|
||||
}
|
@ -1 +1,30 @@
|
||||
use crate::{compile_program, get_output, EdwardsConstrainedValue, EdwardsTestCompiler};
|
||||
use leo_compiler::group::edwards_bls12::EdwardsGroupType;
|
||||
use leo_compiler::ConstrainedValue;
|
||||
use snarkos_curves::edwards_bls12::EdwardsAffine;
|
||||
use snarkos_models::curves::Group;
|
||||
|
||||
const DIRECTORY_NAME: &str = "tests/group/";
|
||||
|
||||
fn output_zero(program: EdwardsTestCompiler) {
|
||||
let output = get_output(program);
|
||||
assert_eq!(
|
||||
EdwardsConstrainedValue::Return(vec![ConstrainedValue::Group(EdwardsGroupType::Constant(
|
||||
EdwardsAffine::zero()
|
||||
))])
|
||||
.to_string(),
|
||||
output.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero() {
|
||||
let program = compile_program(DIRECTORY_NAME, "zero.leo").unwrap();
|
||||
output_zero(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add() {
|
||||
let program = compile_program(DIRECTORY_NAME, "add.leo").unwrap();
|
||||
output_zero(program);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user