From efd885c6a7f92a8ec1a92a0234efd2902b6e8b25 Mon Sep 17 00:00:00 2001 From: collin Date: Tue, 7 Jul 2020 02:08:48 -0700 Subject: [PATCH 1/2] impl 1group and add test --- compiler/src/group/edwards_bls12.rs | 34 ++++++++++++++++++++++++++--- compiler/tests/group/mod.rs | 19 ++++++++++++++-- compiler/tests/group/one.leo | 3 +++ examples/pedersen_hash/src/main.leo | 2 +- 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 compiler/tests/group/one.leo diff --git a/compiler/src/group/edwards_bls12.rs b/compiler/src/group/edwards_bls12.rs index 9ea55eb730..f45a590291 100644 --- a/compiler/src/group/edwards_bls12.rs +++ b/compiler/src/group/edwards_bls12.rs @@ -8,7 +8,7 @@ use snarkos_curves::{ use snarkos_errors::gadgets::SynthesisError; use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget; use snarkos_models::{ - curves::AffineCurve, + curves::{AffineCurve, One, TEModelParameters}, gadgets::{ curves::{FieldGadget, FpGadget, GroupGadget}, r1cs::ConstraintSystem, @@ -33,6 +33,11 @@ pub enum EdwardsGroupType { impl GroupType for EdwardsGroupType { fn constant(string: String, span: Span) -> Result { + // 1group = generator + if string.eq("1") { + return Ok(Self::one()); + } + let value = Self::edwards_affine_from_str(string.clone()).map_err(|_| GroupError::invalid_group(string, span))?; @@ -104,7 +109,7 @@ impl GroupType for EdwardsGroupType { impl EdwardsGroupType { pub fn edwards_affine_from_str(string: String) -> Result { - // 0 or (0, 1) + // x or (x, y) match Fq::from_str(&string).ok() { Some(x) => EdwardsAffine::get_point_from_x(x, false).ok_or(SynthesisError::AssignmentMissing), None => EdwardsAffine::from_str(&string).map_err(|_| SynthesisError::AssignmentMissing), @@ -122,7 +127,12 @@ impl EdwardsGroupType { _ => Err(SynthesisError::AssignmentMissing), }?; - Self::edwards_affine_from_str(affine_string) + // 1group = generator + if affine_string.eq("1") { + Ok(edwards_affine_one()) + } else { + Self::edwards_affine_from_str(affine_string) + } } pub fn allocated>(&self, mut cs: CS) -> Result { @@ -317,6 +327,24 @@ impl ToBytesGadget for EdwardsGroupType { } } +fn edwards_affine_one() -> GroupAffine { + let (x, y) = EdwardsParameters::AFFINE_GENERATOR_COEFFS; + + EdwardsAffine::new(x, y) +} + +impl One for EdwardsGroupType { + fn one() -> Self { + let one = edwards_affine_one(); + + Self::Constant(one) + } + + fn is_one(&self) -> bool { + self.eq(&Self::one()) + } +} + impl std::fmt::Display for EdwardsGroupType { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { diff --git a/compiler/tests/group/mod.rs b/compiler/tests/group/mod.rs index f0c32488b1..072e3f94b3 100644 --- a/compiler/tests/group/mod.rs +++ b/compiler/tests/group/mod.rs @@ -9,10 +9,10 @@ use crate::{ use leo_compiler::{group::edwards_bls12::EdwardsGroupType, ConstrainedValue}; use leo_types::InputValue; -use snarkos_curves::edwards_bls12::{EdwardsAffine, Fq}; +use snarkos_curves::edwards_bls12::{EdwardsAffine, EdwardsParameters, Fq}; use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget; use snarkos_models::{ - curves::Zero, + curves::{TEModelParameters, Zero}, gadgets::{r1cs::TestConstraintSystem, utilities::alloc::AllocGadget}, }; use std::str::FromStr; @@ -47,6 +47,13 @@ fn output_zero(program: EdwardsTestCompiler) { output_expected_constant(program, EdwardsAffine::zero()) } +fn output_one(program: EdwardsTestCompiler) { + let (x, y) = EdwardsParameters::AFFINE_GENERATOR_COEFFS; + let one = EdwardsAffine::new(x, y); + + output_expected_constant(program, one) +} + #[test] fn test_zero() { let bytes = include_bytes!("zero.leo"); @@ -55,6 +62,14 @@ fn test_zero() { output_zero(program); } +#[test] +fn test_one() { + let bytes = include_bytes!("one.leo"); + let program = parse_program(bytes).unwrap(); + + output_one(program) +} + #[test] fn test_point() { let point = EdwardsAffine::from_str(TEST_POINT_1).unwrap(); diff --git a/compiler/tests/group/one.leo b/compiler/tests/group/one.leo new file mode 100644 index 0000000000..1dc4eac9e4 --- /dev/null +++ b/compiler/tests/group/one.leo @@ -0,0 +1,3 @@ +function main() -> group { + return 1group +} \ No newline at end of file diff --git a/examples/pedersen_hash/src/main.leo b/examples/pedersen_hash/src/main.leo index 69cc316443..da62f8ebd5 100644 --- a/examples/pedersen_hash/src/main.leo +++ b/examples/pedersen_hash/src/main.leo @@ -19,7 +19,7 @@ circuit PedersenHash { // The 'pedersen_hash' main function. function main() -> group { - const parameters = [0group; 256]; + const parameters = [1group; 256]; const pedersen = PedersenHash::new(parameters); let input: bool[256] = [true; 256]; return pedersen.hash(input) From a20d4ab22de87e04acf8d46365f13834125a2b09 Mon Sep 17 00:00:00 2001 From: collin Date: Tue, 7 Jul 2020 02:10:39 -0700 Subject: [PATCH 2/2] add one trait to group type trait --- compiler/src/group/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/src/group/mod.rs b/compiler/src/group/mod.rs index c35b3aac9a..b4a4fbc325 100644 --- a/compiler/src/group/mod.rs +++ b/compiler/src/group/mod.rs @@ -4,7 +4,7 @@ use crate::errors::GroupError; use leo_types::Span; use snarkos_models::{ - curves::Field, + curves::{Field, One}, gadgets::{ r1cs::ConstraintSystem, utilities::{ @@ -25,6 +25,7 @@ pub trait GroupType: + Clone + Debug + Display + + One + EvaluateEqGadget + EqGadget + ConditionalEqGadget