Add Pallas and Vesta curves (#21)

Co-authored-by: Ying Tong Lai <yingtong@electriccoin.co>
Co-authored-by: Daira Hopwood <daira@jacaranda.org>
Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
Co-authored-by: therealyingtong <yingtong@z.cash>
This commit is contained in:
Daira Hopwood
2020-12-31 00:56:00 +00:00
committed by GitHub
parent e7d7d01a02
commit 39c58df3a6
25 changed files with 840 additions and 10 deletions

49
pallas/src/curves/mod.rs Normal file
View File

@@ -0,0 +1,49 @@
use crate::{fq::Fq, fr::Fr};
use ark_ec::{
models::{ModelParameters, SWModelParameters},
short_weierstrass_jacobian::{GroupAffine, GroupProjective},
};
use ark_ff::{field_new, Zero};
#[cfg(test)]
mod tests;
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct PallasParameters;
impl ModelParameters for PallasParameters {
type BaseField = Fq;
type ScalarField = Fr;
}
pub type Affine = GroupAffine<PallasParameters>;
pub type Projective = GroupProjective<PallasParameters>;
impl SWModelParameters for PallasParameters {
/// COEFF_A = 0
const COEFF_A: Fq = field_new!(Fq, "0");
/// COEFF_B = 5
const COEFF_B: Fq = field_new!(Fq, "5");
/// COFACTOR = 1
const COFACTOR: &'static [u64] = &[0x1];
/// COFACTOR_INV = 1
const COFACTOR_INV: Fr = field_new!(Fr, "1");
/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(G_GENERATOR_X, G_GENERATOR_Y);
#[inline(always)]
fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
Self::BaseField::zero()
}
}
/// G_GENERATOR_X = -1
pub const G_GENERATOR_X: Fq = field_new!(Fq, "-1");
/// G_GENERATOR_Y = 2
pub const G_GENERATOR_Y: Fq = field_new!(Fq, "2");

View File

@@ -0,0 +1,39 @@
#![allow(unused_imports)]
use ark_ff::{
fields::{Field, FpParameters, PrimeField, SquareRootField},
One, Zero,
};
use ark_serialize::CanonicalSerialize;
use ark_std::test_rng;
use ark_ec::{models::SWModelParameters, AffineCurve, PairingEngine, ProjectiveCurve};
use core::ops::{AddAssign, MulAssign};
use rand::Rng;
use crate::{Affine, PallasParameters, Projective};
use ark_curve_tests::{
curves::{curve_tests, sw_tests},
groups::group_test,
};
#[test]
fn test_projective_curve() {
curve_tests::<Projective>();
sw_tests::<PallasParameters>();
}
#[test]
fn test_projective_group() {
let mut rng = test_rng();
let a: Projective = rng.gen();
let b: Projective = rng.gen();
group_test(a, b);
}
#[test]
fn test_generator() {
let generator = Affine::prime_subgroup_generator();
assert!(generator.is_on_curve());
assert!(generator.is_in_correct_subgroup_assuming_on_curve());
}