impl group sub for edwards

This commit is contained in:
collin 2020-05-30 17:19:26 -07:00
parent faf726e3b1
commit d1e448d630
5 changed files with 52 additions and 6 deletions

View File

@ -100,9 +100,9 @@ impl<
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
Ok(self.enforce_field_sub(cs, fe_1, fe_2)?)
}
// (ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
// Ok(Self::evaluate_group_sub(ge_1, ge_2))
// }
(ConstrainedValue::Group(ge_1), ConstrainedValue::Group(ge_2)) => {
Ok(ConstrainedValue::Group(ge_1.sub(cs, &ge_2)?))
}
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
self.enforce_sub_expression(cs, val_1, val_2)
@ -131,9 +131,6 @@ impl<
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
Ok(self.enforce_field_mul(cs, fe_1, fe_2)?)
}
// (ConstrainedValue::GroupElement(group), ConstrainedValue::FieldElement(scalar)) => {
// Ok(Self::evaluate_group_mul(group, scalar))
// }
(ConstrainedValue::Unresolved(string), val_2) => {
let val_1 = ConstrainedValue::from_other(string, &val_2)?;
self.enforce_mul_expression(cs, val_1, val_2)

View File

@ -7,6 +7,7 @@ use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget;
use snarkos_models::curves::{AffineCurve, ModelParameters};
use snarkos_models::gadgets::curves::GroupGadget;
use snarkos_models::gadgets::r1cs::ConstraintSystem;
use std::ops::Sub;
use std::str::FromStr;
#[derive(Clone, Debug)]
@ -55,4 +56,31 @@ impl GroupType<<EdwardsParameters as ModelParameters>::BaseField, Fq> for Edward
)),
}
}
fn sub<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.sub(other_value)))
}
(EdwardsGroupType::Allocated(self_value), EdwardsGroupType::Allocated(other_value)) => {
let result = <EdwardsBlsGadget as GroupGadget<
GroupAffine<EdwardsParameters>,
Fq,
>>::sub(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.sub_constant(cs, constant_value)?,
)),
}
}
}

View File

@ -9,4 +9,6 @@ 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>;
fn sub<CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, GroupError>;
}

View File

@ -52,3 +52,16 @@ fn test_add() {
let program = compile_program(DIRECTORY_NAME, "add.leo").unwrap();
output_expected(program, sum);
}
#[test]
fn test_sub() {
use std::ops::Sub;
let point_1 = EdwardsAffine::from_str(TEST_POINT_1).unwrap();
let point_2 = EdwardsAffine::from_str(TEST_POINT_2).unwrap();
let sum = point_1.sub(&point_2);
let program = compile_program(DIRECTORY_NAME, "sub.leo").unwrap();
output_expected(program, sum);
}

View File

@ -0,0 +1,6 @@
function main() -> group {
let point_1 = (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group;
let point_2 = (1005842117974384149622370061042978581211342111653966059496918451529532134799, 79389132189982034519597104273449021362784864778548730890166152019533697186)group;
return point_1 - point_2
}