mirror of
https://github.com/arnaucube/arkeddsa.git
synced 2026-01-12 16:21:29 +01:00
tidy
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -255,6 +255,7 @@ dependencies = [
|
|||||||
"ark-std",
|
"ark-std",
|
||||||
"blake2",
|
"blake2",
|
||||||
"digest",
|
"digest",
|
||||||
|
"hex",
|
||||||
"rand",
|
"rand",
|
||||||
"rand_core",
|
"rand_core",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
|||||||
16
Cargo.toml
16
Cargo.toml
@@ -1,17 +1,18 @@
|
|||||||
[package]
|
[package]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
name = "arkeddsa"
|
name = "arkeddsa"
|
||||||
|
rust-version = "1.75.0"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
ark-crypto-primitives = {version = "^0.4.0", default-features = false, features = ["sponge", "crh"]}
|
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-ed-on-bn254 = {version = "0.4.0"}
|
||||||
ark-ff = "0.4"
|
ark-ff = "^0.4.0"
|
||||||
ark-serialize = "0.4"
|
ark-serialize = {version = "^0.4.0", default-features = false}
|
||||||
ark-std = "0.4"
|
ark-std = "^0.4.0"
|
||||||
digest = "0.10.7"
|
digest = "0.10"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rand_core = {version = "0.6", default-features = false}
|
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-algebra-test-templates = "0.4.2"
|
||||||
ark-ed-on-bls12-381 = {version = "0.4.0"}
|
ark-ed-on-bls12-381 = {version = "0.4.0"}
|
||||||
ark-ed-on-bls12-381-bandersnatch = {version = "0.4.0"}
|
ark-ed-on-bls12-381-bandersnatch = {version = "0.4.0"}
|
||||||
blake2 = "0.10.6"
|
blake2 = "0.10"
|
||||||
sha2 = "0.10.8"
|
hex = "0.4.3"
|
||||||
|
sha2 = "0.10"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|||||||
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
@@ -0,0 +1 @@
|
|||||||
|
1.75.0
|
||||||
@@ -6,6 +6,10 @@ use ark_ff::MontFp;
|
|||||||
pub type EdwardsAffine = Affine<EdwardsConfig>;
|
pub type EdwardsAffine = Affine<EdwardsConfig>;
|
||||||
pub type EdwardsProjective = Projective<EdwardsConfig>;
|
pub type EdwardsProjective = Projective<EdwardsConfig>;
|
||||||
|
|
||||||
|
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
|
/// Twist of `Baby-JubJub` is a twist of twisted Edwards curve. These curves have equations of the
|
||||||
/// form: ax² + y² = 1 + dx²y².
|
/// form: ax² + y² = 1 + dx²y².
|
||||||
/// over some base finite field BaseField.
|
/// over some base finite field BaseField.
|
||||||
@@ -18,27 +22,27 @@ pub struct EdwardsConfig;
|
|||||||
ark_algebra_test_templates::test_group!(te; EdwardsProjective; te);
|
ark_algebra_test_templates::test_group!(te; EdwardsProjective; te);
|
||||||
|
|
||||||
impl CurveConfig for EdwardsConfig {
|
impl CurveConfig for EdwardsConfig {
|
||||||
type BaseField = ark_ed_on_bn254::Fq;
|
type BaseField = BaseField;
|
||||||
type ScalarField = ark_ed_on_bn254::Fr;
|
type ScalarField = ScalarField;
|
||||||
|
|
||||||
/// COFACTOR = 8
|
/// COFACTOR = 8
|
||||||
const COFACTOR: &'static [u64] = &[8];
|
const COFACTOR: &'static [u64] = &[8];
|
||||||
|
|
||||||
/// COFACTOR^(-1) mod r =
|
/// COFACTOR^(-1) mod r =
|
||||||
/// 2394026564107420727433200628387514462817212225638746351800188703329891451411
|
/// 2394026564107420727433200628387514462817212225638746351800188703329891451411
|
||||||
const COFACTOR_INV: ark_ed_on_bn254::Fr =
|
const COFACTOR_INV: ScalarField =
|
||||||
MontFp!("2394026564107420727433200628387514462817212225638746351800188703329891451411");
|
MontFp!("2394026564107420727433200628387514462817212225638746351800188703329891451411");
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TECurveConfig for EdwardsConfig {
|
impl TECurveConfig for EdwardsConfig {
|
||||||
const COEFF_A: ark_ed_on_bn254::Fq = MontFp!("168700");
|
const COEFF_A: BaseField = MontFp!("168700");
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn mul_by_a(elem: Self::BaseField) -> Self::BaseField {
|
fn mul_by_a(elem: Self::BaseField) -> Self::BaseField {
|
||||||
elem * <Self as TECurveConfig>::COEFF_A
|
elem * <Self as TECurveConfig>::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);
|
const GENERATOR: EdwardsAffine = EdwardsAffine::new_unchecked(GENERATOR_X, GENERATOR_Y);
|
||||||
|
|
||||||
@@ -47,21 +51,21 @@ impl TECurveConfig for EdwardsConfig {
|
|||||||
|
|
||||||
impl MontCurveConfig for EdwardsConfig {
|
impl MontCurveConfig for EdwardsConfig {
|
||||||
/// COEFF_A = 168698
|
/// COEFF_A = 168698
|
||||||
const COEFF_A: ark_ed_on_bn254::Fq = MontFp!("168698");
|
const COEFF_A: BaseField = MontFp!("168698");
|
||||||
/// COEFF_B = 168700
|
/// COEFF_B = 168700
|
||||||
const COEFF_B: ark_ed_on_bn254::Fq = MontFp!("1");
|
const COEFF_B: BaseField = MontFp!("1");
|
||||||
|
|
||||||
type TECurveConfig = EdwardsConfig;
|
type TECurveConfig = EdwardsConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// GENERATOR_X =
|
/// GENERATOR_X =
|
||||||
/// 19698561148652590122159747500897617769866003486955115824547446575314762165298
|
/// 19698561148652590122159747500897617769866003486955115824547446575314762165298
|
||||||
pub const GENERATOR_X: ark_ed_on_bn254::Fq =
|
pub const GENERATOR_X: BaseField =
|
||||||
MontFp!("5299619240641551281634865583518297030282874472190772894086521144482721001553");
|
MontFp!("5299619240641551281634865583518297030282874472190772894086521144482721001553");
|
||||||
|
|
||||||
/// GENERATOR_Y =
|
/// GENERATOR_Y =
|
||||||
/// 19298250018296453272277890825869354524455968081175474282777126169995084727839
|
/// 19298250018296453272277890825869354524455968081175474282777126169995084727839
|
||||||
pub const GENERATOR_Y: ark_ed_on_bn254::Fq =
|
pub const GENERATOR_Y: BaseField =
|
||||||
MontFp!("16950150798460657717958625567821834550301663161624707787222815936182638968203");
|
MontFp!("16950150798460657717958625567821834550301663161624707787222815936182638968203");
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -77,7 +81,7 @@ fn test_twist() {
|
|||||||
|
|
||||||
fn untwist(curve: EdwardsAffine) -> ark_ed_on_bn254::EdwardsAffine {
|
fn untwist(curve: EdwardsAffine) -> ark_ed_on_bn254::EdwardsAffine {
|
||||||
use ark_ff::Field;
|
use ark_ff::Field;
|
||||||
const A: ark_ed_on_bn254::Fq = MontFp!("168700");
|
const A: BaseField = MontFp!("168700");
|
||||||
let sqrt_a = A.sqrt().unwrap();
|
let sqrt_a = A.sqrt().unwrap();
|
||||||
ark_ed_on_bn254::EdwardsAffine {
|
ark_ed_on_bn254::EdwardsAffine {
|
||||||
x: curve.x * sqrt_a,
|
x: curve.x * sqrt_a,
|
||||||
@@ -86,7 +90,6 @@ fn test_twist() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use ark_ec::{AffineRepr, CurveGroup};
|
use ark_ec::{AffineRepr, CurveGroup};
|
||||||
use ark_ed_on_bn254::Fr;
|
|
||||||
use ark_std::UniformRand;
|
use ark_std::UniformRand;
|
||||||
use rand_core::OsRng;
|
use rand_core::OsRng;
|
||||||
|
|
||||||
|
|||||||
21
src/eddsa.rs
21
src/eddsa.rs
@@ -29,6 +29,14 @@ impl SecretKey {
|
|||||||
let x = prune_buffer(buffer);
|
let x = prune_buffer(buffer);
|
||||||
(x, hash_prefix)
|
(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)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
@@ -37,6 +45,15 @@ pub struct PublicKey<A: AffineRepr>(A)
|
|||||||
where
|
where
|
||||||
A::Config: TECurveConfig;
|
A::Config: TECurveConfig;
|
||||||
|
|
||||||
|
impl<A: AffineRepr> PublicKey<A>
|
||||||
|
where
|
||||||
|
A::Config: TECurveConfig,
|
||||||
|
{
|
||||||
|
pub fn xy(&self) -> (&A::BaseField, &A::BaseField) {
|
||||||
|
self.0.xy().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
/// `SigningKey` produces EdDSA signatures for given message
|
/// `SigningKey` produces EdDSA signatures for given message
|
||||||
pub struct SigningKey<A: AffineRepr>
|
pub struct SigningKey<A: AffineRepr>
|
||||||
@@ -73,8 +90,8 @@ where
|
|||||||
Self::new::<D>(&secret_key)
|
Self::new::<D>(&secret_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn public_key(&self) -> PublicKey<A> {
|
pub fn public_key(&self) -> &PublicKey<A> {
|
||||||
self.public_key
|
&self.public_key
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sign<D: Digest, E: Absorb>(
|
pub fn sign<D: Digest, E: Absorb>(
|
||||||
|
|||||||
23
src/lib.rs
23
src/lib.rs
@@ -1,6 +1,5 @@
|
|||||||
pub mod ed_on_bn254_twist;
|
pub mod ed_on_bn254_twist;
|
||||||
pub mod eddsa;
|
pub mod eddsa;
|
||||||
pub mod poseidon;
|
|
||||||
pub mod signature;
|
pub mod signature;
|
||||||
|
|
||||||
use ark_ff::PrimeField;
|
use ark_ff::PrimeField;
|
||||||
@@ -21,20 +20,38 @@ pub enum Error {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod 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_crypto_primitives::sponge::Absorb;
|
||||||
use ark_ec::{twisted_edwards::TECurveConfig, AffineRepr};
|
use ark_ec::{twisted_edwards::TECurveConfig, AffineRepr};
|
||||||
use ark_ff::PrimeField;
|
use ark_ff::PrimeField;
|
||||||
use digest::Digest;
|
use digest::Digest;
|
||||||
use rand_core::OsRng;
|
use rand_core::OsRng;
|
||||||
|
|
||||||
use crate::SigningKey;
|
/// Generates poseidon constants and returns the config
|
||||||
|
pub fn poseidon_config<F: PrimeField>(
|
||||||
|
rate: usize,
|
||||||
|
full_rounds: usize,
|
||||||
|
partial_rounds: usize,
|
||||||
|
) -> PoseidonConfig<F> {
|
||||||
|
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<A: AffineRepr, D: Digest>()
|
fn run_test<A: AffineRepr, D: Digest>()
|
||||||
where
|
where
|
||||||
A::BaseField: Absorb + PrimeField,
|
A::BaseField: Absorb + PrimeField,
|
||||||
A::Config: TECurveConfig,
|
A::Config: TECurveConfig,
|
||||||
{
|
{
|
||||||
let poseidon = crate::poseidon::poseidon_config(4, 8, 55);
|
let poseidon = poseidon_config(4, 8, 55);
|
||||||
let signing_key = SigningKey::<A>::generate::<D>(&mut OsRng).unwrap();
|
let signing_key = SigningKey::<A>::generate::<D>(&mut OsRng).unwrap();
|
||||||
let message = b"xxx yyy <<< zzz >>> bunny";
|
let message = b"xxx yyy <<< zzz >>> bunny";
|
||||||
let signature = signing_key.sign::<D, _>(&poseidon, &message[..]);
|
let signature = signing_key.sign::<D, _>(&poseidon, &message[..]);
|
||||||
|
|||||||
@@ -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<F: PrimeField>(
|
|
||||||
rate: usize,
|
|
||||||
full_rounds: usize,
|
|
||||||
partial_rounds: usize,
|
|
||||||
) -> PoseidonConfig<F> {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user