Merge pull request #103 from AleoHQ/fix/group-one

Fix/group one
This commit is contained in:
Howard Wu 2020-07-07 16:40:08 -07:00 committed by GitHub
commit 3e9b963d5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 7 deletions

View File

@ -8,7 +8,7 @@ use snarkos_curves::{
use snarkos_errors::gadgets::SynthesisError; use snarkos_errors::gadgets::SynthesisError;
use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget; use snarkos_gadgets::curves::edwards_bls12::EdwardsBlsGadget;
use snarkos_models::{ use snarkos_models::{
curves::AffineCurve, curves::{AffineCurve, One, TEModelParameters},
gadgets::{ gadgets::{
curves::{FieldGadget, FpGadget, GroupGadget}, curves::{FieldGadget, FpGadget, GroupGadget},
r1cs::ConstraintSystem, r1cs::ConstraintSystem,
@ -33,6 +33,11 @@ pub enum EdwardsGroupType {
impl GroupType<Fq> for EdwardsGroupType { impl GroupType<Fq> for EdwardsGroupType {
fn constant(string: String, span: Span) -> Result<Self, GroupError> { fn constant(string: String, span: Span) -> Result<Self, GroupError> {
// 1group = generator
if string.eq("1") {
return Ok(Self::one());
}
let value = let value =
Self::edwards_affine_from_str(string.clone()).map_err(|_| GroupError::invalid_group(string, span))?; Self::edwards_affine_from_str(string.clone()).map_err(|_| GroupError::invalid_group(string, span))?;
@ -104,7 +109,7 @@ impl GroupType<Fq> for EdwardsGroupType {
impl EdwardsGroupType { impl EdwardsGroupType {
pub fn edwards_affine_from_str(string: String) -> Result<EdwardsAffine, SynthesisError> { pub fn edwards_affine_from_str(string: String) -> Result<EdwardsAffine, SynthesisError> {
// 0 or (0, 1) // x or (x, y)
match Fq::from_str(&string).ok() { match Fq::from_str(&string).ok() {
Some(x) => EdwardsAffine::get_point_from_x(x, false).ok_or(SynthesisError::AssignmentMissing), Some(x) => EdwardsAffine::get_point_from_x(x, false).ok_or(SynthesisError::AssignmentMissing),
None => EdwardsAffine::from_str(&string).map_err(|_| SynthesisError::AssignmentMissing), None => EdwardsAffine::from_str(&string).map_err(|_| SynthesisError::AssignmentMissing),
@ -122,7 +127,12 @@ impl EdwardsGroupType {
_ => Err(SynthesisError::AssignmentMissing), _ => 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<CS: ConstraintSystem<Fq>>(&self, mut cs: CS) -> Result<EdwardsBlsGadget, SynthesisError> { pub fn allocated<CS: ConstraintSystem<Fq>>(&self, mut cs: CS) -> Result<EdwardsBlsGadget, SynthesisError> {
@ -317,6 +327,24 @@ impl ToBytesGadget<Fq> for EdwardsGroupType {
} }
} }
fn edwards_affine_one() -> GroupAffine<EdwardsParameters> {
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 { impl std::fmt::Display for EdwardsGroupType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {

View File

@ -4,7 +4,7 @@ use crate::errors::GroupError;
use leo_types::Span; use leo_types::Span;
use snarkos_models::{ use snarkos_models::{
curves::Field, curves::{Field, One},
gadgets::{ gadgets::{
r1cs::ConstraintSystem, r1cs::ConstraintSystem,
utilities::{ utilities::{
@ -25,6 +25,7 @@ pub trait GroupType<F: Field>:
+ Clone + Clone
+ Debug + Debug
+ Display + Display
+ One
+ EvaluateEqGadget<F> + EvaluateEqGadget<F>
+ EqGadget<F> + EqGadget<F>
+ ConditionalEqGadget<F> + ConditionalEqGadget<F>

View File

@ -9,10 +9,10 @@ use crate::{
use leo_compiler::{group::edwards_bls12::EdwardsGroupType, ConstrainedValue}; use leo_compiler::{group::edwards_bls12::EdwardsGroupType, ConstrainedValue};
use leo_types::InputValue; 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_gadgets::curves::edwards_bls12::EdwardsBlsGadget;
use snarkos_models::{ use snarkos_models::{
curves::Zero, curves::{TEModelParameters, Zero},
gadgets::{r1cs::TestConstraintSystem, utilities::alloc::AllocGadget}, gadgets::{r1cs::TestConstraintSystem, utilities::alloc::AllocGadget},
}; };
use std::str::FromStr; use std::str::FromStr;
@ -47,6 +47,13 @@ fn output_zero(program: EdwardsTestCompiler) {
output_expected_constant(program, EdwardsAffine::zero()) 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] #[test]
fn test_zero() { fn test_zero() {
let bytes = include_bytes!("zero.leo"); let bytes = include_bytes!("zero.leo");
@ -55,6 +62,14 @@ fn test_zero() {
output_zero(program); output_zero(program);
} }
#[test]
fn test_one() {
let bytes = include_bytes!("one.leo");
let program = parse_program(bytes).unwrap();
output_one(program)
}
#[test] #[test]
fn test_point() { fn test_point() {
let point = EdwardsAffine::from_str(TEST_POINT_1).unwrap(); let point = EdwardsAffine::from_str(TEST_POINT_1).unwrap();

View File

@ -0,0 +1,3 @@
function main() -> group {
return 1group
}

View File

@ -19,7 +19,7 @@ circuit PedersenHash {
// The 'pedersen_hash' main function. // The 'pedersen_hash' main function.
function main() -> group { function main() -> group {
const parameters = [0group; 256]; const parameters = [1group; 256];
const pedersen = PedersenHash::new(parameters); const pedersen = PedersenHash::new(parameters);
let input: bool[256] = [true; 256]; let input: bool[256] = [true; 256];
return pedersen.hash(input) return pedersen.hash(input)