impl group add, sub

This commit is contained in:
collin 2020-05-12 20:20:35 -07:00
parent 162c0064b2
commit c1df2b00b2
3 changed files with 44 additions and 0 deletions

View File

@ -51,6 +51,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
self.enforce_field_add(cs, fe_1, fe_2)?
}
(ConstrainedValue::GroupElement(ge_1), ConstrainedValue::GroupElement(ge_2)) => {
Self::evaluate_group_add(ge_1, ge_2)
}
(val_1, val_2) => {
return Err(ExpressionError::IncompatibleTypes(format!(
"{} + {}",
@ -73,6 +76,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
(ConstrainedValue::FieldElement(fe_1), ConstrainedValue::FieldElement(fe_2)) => {
self.enforce_field_sub(cs, fe_1, fe_2)?
}
(ConstrainedValue::GroupElement(ge_1), ConstrainedValue::GroupElement(ge_2)) => {
Self::evaluate_group_sub(ge_1, ge_2)
}
(val_1, val_2) => {
return Err(ExpressionError::IncompatibleTypes(format!(
"{} - {}",
@ -166,6 +172,9 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
// (ResolvedValue::FieldElement(fe_1), ResolvedValue::FieldElement(fe_2)) => {
// Self::field_eq(fe_1, fe_2)
// }
(ConstrainedValue::GroupElement(ge_1), ConstrainedValue::GroupElement(ge_2)) => {
Self::evaluate_group_eq(ge_1, ge_2)
}
(val_1, val_2) => {
return Err(ExpressionError::IncompatibleTypes(format!(
"{} == {}",

View File

@ -0,0 +1,32 @@
use crate::{ConstrainedProgram, ConstrainedValue};
use snarkos_models::{
curves::{Field, PrimeField, Group},
gadgets::{
r1cs::ConstraintSystem,
utilities::boolean::Boolean
},
};
impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgram<F, G, CS> {
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))
}
}

View File

@ -18,6 +18,9 @@ pub use integer::*;
pub mod field_element;
pub use field_element::*;
pub mod group_element;
pub use group_element::*;
pub mod program;
pub use program::*;