From 822bab04895135675f7a599af29bdcf923be758f Mon Sep 17 00:00:00 2001 From: kilic Date: Wed, 3 Jul 2024 12:55:31 +0300 Subject: [PATCH] tidy --- Cargo.lock | 1 + Cargo.toml | 16 +++++++++------- rust-toolchain | 1 + src/ed_on_bn254_twist.rs | 25 ++++++++++++++----------- src/eddsa.rs | 21 +++++++++++++++++++-- src/lib.rs | 23 ++++++++++++++++++++--- src/poseidon.rs | 19 ------------------- 7 files changed, 64 insertions(+), 42 deletions(-) create mode 100644 rust-toolchain delete mode 100644 src/poseidon.rs diff --git a/Cargo.lock b/Cargo.lock index 697680e..8a746b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -255,6 +255,7 @@ dependencies = [ "ark-std", "blake2", "digest", + "hex", "rand", "rand_core", "sha2", diff --git a/Cargo.toml b/Cargo.toml index a2162ff..4eb6d9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,17 +1,18 @@ [package] edition = "2021" name = "arkeddsa" +rust-version = "1.75.0" version = "0.1.0" [dependencies] ark-crypto-primitives = {version = "^0.4.0", default-features = false, features = ["sponge", "crh"]} -ark-ec = "0.4" +ark-ec = "^0.4.0" ark-ed-on-bn254 = {version = "0.4.0"} -ark-ff = "0.4" -ark-serialize = "0.4" -ark-std = "0.4" -digest = "0.10.7" +ark-ff = "^0.4.0" +ark-serialize = {version = "^0.4.0", default-features = false} +ark-std = "^0.4.0" +digest = "0.10" rand = "0.8" rand_core = {version = "0.6", default-features = false} @@ -19,8 +20,9 @@ rand_core = {version = "0.6", default-features = false} ark-algebra-test-templates = "0.4.2" ark-ed-on-bls12-381 = {version = "0.4.0"} ark-ed-on-bls12-381-bandersnatch = {version = "0.4.0"} -blake2 = "0.10.6" -sha2 = "0.10.8" +blake2 = "0.10" +hex = "0.4.3" +sha2 = "0.10" [features] default = [] diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..7c7053a --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +1.75.0 diff --git a/src/ed_on_bn254_twist.rs b/src/ed_on_bn254_twist.rs index 0e9574b..747a61f 100644 --- a/src/ed_on_bn254_twist.rs +++ b/src/ed_on_bn254_twist.rs @@ -6,6 +6,10 @@ use ark_ff::MontFp; pub type EdwardsAffine = Affine; pub type EdwardsProjective = Projective; +pub use ark_ed_on_bn254::{Fq, Fr}; +pub type BaseField = ark_ed_on_bn254::Fq; +pub type ScalarField = ark_ed_on_bn254::Fr; + /// Twist of `Baby-JubJub` is a twist of twisted Edwards curve. These curves have equations of the /// form: ax² + y² = 1 + dx²y². /// over some base finite field BaseField. @@ -18,27 +22,27 @@ pub struct EdwardsConfig; ark_algebra_test_templates::test_group!(te; EdwardsProjective; te); impl CurveConfig for EdwardsConfig { - type BaseField = ark_ed_on_bn254::Fq; - type ScalarField = ark_ed_on_bn254::Fr; + type BaseField = BaseField; + type ScalarField = ScalarField; /// COFACTOR = 8 const COFACTOR: &'static [u64] = &[8]; /// COFACTOR^(-1) mod r = /// 2394026564107420727433200628387514462817212225638746351800188703329891451411 - const COFACTOR_INV: ark_ed_on_bn254::Fr = + const COFACTOR_INV: ScalarField = MontFp!("2394026564107420727433200628387514462817212225638746351800188703329891451411"); } impl TECurveConfig for EdwardsConfig { - const COEFF_A: ark_ed_on_bn254::Fq = MontFp!("168700"); + const COEFF_A: BaseField = MontFp!("168700"); #[inline(always)] fn mul_by_a(elem: Self::BaseField) -> Self::BaseField { elem * ::COEFF_A } - const COEFF_D: ark_ed_on_bn254::Fq = MontFp!("168696"); + const COEFF_D: BaseField = MontFp!("168696"); const GENERATOR: EdwardsAffine = EdwardsAffine::new_unchecked(GENERATOR_X, GENERATOR_Y); @@ -47,21 +51,21 @@ impl TECurveConfig for EdwardsConfig { impl MontCurveConfig for EdwardsConfig { /// COEFF_A = 168698 - const COEFF_A: ark_ed_on_bn254::Fq = MontFp!("168698"); + const COEFF_A: BaseField = MontFp!("168698"); /// COEFF_B = 168700 - const COEFF_B: ark_ed_on_bn254::Fq = MontFp!("1"); + const COEFF_B: BaseField = MontFp!("1"); type TECurveConfig = EdwardsConfig; } /// GENERATOR_X = /// 19698561148652590122159747500897617769866003486955115824547446575314762165298 -pub const GENERATOR_X: ark_ed_on_bn254::Fq = +pub const GENERATOR_X: BaseField = MontFp!("5299619240641551281634865583518297030282874472190772894086521144482721001553"); /// GENERATOR_Y = /// 19298250018296453272277890825869354524455968081175474282777126169995084727839 -pub const GENERATOR_Y: ark_ed_on_bn254::Fq = +pub const GENERATOR_Y: BaseField = MontFp!("16950150798460657717958625567821834550301663161624707787222815936182638968203"); #[test] @@ -77,7 +81,7 @@ fn test_twist() { fn untwist(curve: EdwardsAffine) -> ark_ed_on_bn254::EdwardsAffine { use ark_ff::Field; - const A: ark_ed_on_bn254::Fq = MontFp!("168700"); + const A: BaseField = MontFp!("168700"); let sqrt_a = A.sqrt().unwrap(); ark_ed_on_bn254::EdwardsAffine { x: curve.x * sqrt_a, @@ -86,7 +90,6 @@ fn test_twist() { } use ark_ec::{AffineRepr, CurveGroup}; - use ark_ed_on_bn254::Fr; use ark_std::UniformRand; use rand_core::OsRng; diff --git a/src/eddsa.rs b/src/eddsa.rs index 5c4965f..2898348 100644 --- a/src/eddsa.rs +++ b/src/eddsa.rs @@ -29,6 +29,14 @@ impl SecretKey { let x = prune_buffer(buffer); (x, hash_prefix) } + + pub fn to_bytes(&self) -> [u8; 32] { + self.0 + } + + pub fn from_bytes(bytes: [u8; 32]) -> Self { + Self(bytes) + } } #[derive(Copy, Clone, Debug)] @@ -37,6 +45,15 @@ pub struct PublicKey(A) where A::Config: TECurveConfig; +impl PublicKey +where + A::Config: TECurveConfig, +{ + pub fn xy(&self) -> (&A::BaseField, &A::BaseField) { + self.0.xy().unwrap() + } +} + #[derive(Copy, Clone, Debug)] /// `SigningKey` produces EdDSA signatures for given message pub struct SigningKey @@ -73,8 +90,8 @@ where Self::new::(&secret_key) } - pub fn public_key(&self) -> PublicKey { - self.public_key + pub fn public_key(&self) -> &PublicKey { + &self.public_key } pub fn sign( diff --git a/src/lib.rs b/src/lib.rs index 2eb9c38..a3d668b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ pub mod ed_on_bn254_twist; pub mod eddsa; -pub mod poseidon; pub mod signature; use ark_ff::PrimeField; @@ -21,20 +20,38 @@ pub enum Error { #[cfg(test)] mod test { + + use crate::SigningKey; + use ark_crypto_primitives::sponge::poseidon::{find_poseidon_ark_and_mds, PoseidonConfig}; use ark_crypto_primitives::sponge::Absorb; use ark_ec::{twisted_edwards::TECurveConfig, AffineRepr}; use ark_ff::PrimeField; use digest::Digest; use rand_core::OsRng; - use crate::SigningKey; + /// Generates poseidon constants and returns the config + pub fn poseidon_config( + rate: usize, + full_rounds: usize, + partial_rounds: usize, + ) -> PoseidonConfig { + let prime_bits = F::MODULUS_BIT_SIZE as u64; + let (ark, mds) = find_poseidon_ark_and_mds( + prime_bits, + rate, + full_rounds as u64, + partial_rounds as u64, + 0, + ); + PoseidonConfig::new(full_rounds, partial_rounds, 5, mds, ark, rate, 1) + } fn run_test() where A::BaseField: Absorb + PrimeField, A::Config: TECurveConfig, { - let poseidon = crate::poseidon::poseidon_config(4, 8, 55); + let poseidon = poseidon_config(4, 8, 55); let signing_key = SigningKey::::generate::(&mut OsRng).unwrap(); let message = b"xxx yyy <<< zzz >>> bunny"; let signature = signing_key.sign::(&poseidon, &message[..]); diff --git a/src/poseidon.rs b/src/poseidon.rs deleted file mode 100644 index f856b32..0000000 --- a/src/poseidon.rs +++ /dev/null @@ -1,19 +0,0 @@ -use ark_crypto_primitives::sponge::poseidon::{find_poseidon_ark_and_mds, PoseidonConfig}; -use ark_ff::PrimeField; - -/// Generates poseidon constants and returns the config -pub fn poseidon_config( - rate: usize, - full_rounds: usize, - partial_rounds: usize, -) -> PoseidonConfig { - let prime_bits = F::MODULUS_BIT_SIZE as u64; - let (ark, mds) = find_poseidon_ark_and_mds( - prime_bits, - rate, - full_rounds as u64, - partial_rounds as u64, - 0, - ); - PoseidonConfig::new(full_rounds, partial_rounds, 5, mds, ark, rate, 1) -}