Skip to content

Commit

Permalink
chore: do heavy lifting in trait default impl, cut scope in concrete …
Browse files Browse the repository at this point in the history
…impls (#8)

* chore: do heavy lifting in trait default impl, cut scope in concrete impls

* remove unused imports

* fix(ci): better rust caching
  • Loading branch information
rymnc authored May 25, 2024
1 parent f9c0334 commit f95dd17
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 213 deletions.
24 changes: 3 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ jobs:
toolchain: stable
override: true

- uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: Swatinem/rust-cache@v2

- name: Check (All features)
run: cargo check --all-features
Expand All @@ -45,13 +39,7 @@ jobs:
toolchain: stable
override: true

- uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: Swatinem/rust-cache@v2

- name: Lint
run: cargo fmt --all --check
Expand All @@ -69,13 +57,7 @@ jobs:
toolchain: stable
override: true

- uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: Swatinem/rust-cache@v2

- name: Test
# need the release flag to test rln integration appropriately
Expand Down
28 changes: 2 additions & 26 deletions src/bls12_377_impl.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_bls12_377::g1::{G1_GENERATOR_X, G1_GENERATOR_Y};
use ark_bls12_377::{Fq, Fr, G1Affine, G1Projective};
use crate::stealth_commitments::StealthAddressOnCurve;

#[allow(non_camel_case_types)]
pub struct Bls12_377_G1Affine(G1Affine);
impl AffineWrapper for Bls12_377_G1Affine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
Bls12_377_G1Affine(G1Affine::new(x, y))
}

fn get_generator_x() -> Self::Fq {
G1_GENERATOR_X
}

fn get_generator_y() -> Self::Fq {
G1_GENERATOR_Y
}
}

impl From<Bls12_377_G1Affine> for G1Projective {
fn from(value: Bls12_377_G1Affine) -> Self {
G1Projective::from(value.0)
}
}
use ark_bls12_377::{Fr, G1Projective};

impl StealthAddressOnCurve for ark_bls12_377::Bls12_377 {
type Projective = G1Projective;
type Affine = Bls12_377_G1Affine;
type Fr = Fr;
}

Expand Down
28 changes: 2 additions & 26 deletions src/bls12_381_impl.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_bls12_381::g1::{G1_GENERATOR_X, G1_GENERATOR_Y};
use ark_bls12_381::{Fq, Fr, G1Affine, G1Projective};
use crate::stealth_commitments::StealthAddressOnCurve;

#[allow(non_camel_case_types)]
pub struct Bls12_381_G1Affine(G1Affine);
impl AffineWrapper for Bls12_381_G1Affine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
Bls12_381_G1Affine(G1Affine::new(x, y))
}

fn get_generator_x() -> Self::Fq {
G1_GENERATOR_X
}

fn get_generator_y() -> Self::Fq {
G1_GENERATOR_Y
}
}

impl From<Bls12_381_G1Affine> for G1Projective {
fn from(value: Bls12_381_G1Affine) -> Self {
G1Projective::from(value.0)
}
}
use ark_bls12_381::{Fr, G1Projective};

impl StealthAddressOnCurve for ark_bls12_381::Bls12_381 {
type Projective = G1Projective;
type Affine = Bls12_381_G1Affine;
type Fr = Fr;
}

Expand Down
22 changes: 3 additions & 19 deletions src/bn254_impl.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_bn254::g1::{G1_GENERATOR_X, G1_GENERATOR_Y};
use ark_bn254::{Fq, Fr, G1Affine, G1Projective};
use rln::hashers::{hash_to_field, poseidon_hash};

impl AffineWrapper for G1Affine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
G1Affine::new(x, y)
}

fn get_generator_x() -> Self::Fq {
G1_GENERATOR_X
}
use crate::stealth_commitments::StealthAddressOnCurve;

fn get_generator_y() -> Self::Fq {
G1_GENERATOR_Y
}
}
use ark_bn254::{Fr, G1Projective};
use rln::hashers::{hash_to_field, poseidon_hash};

impl StealthAddressOnCurve for ark_bn254::Bn254 {
type Projective = G1Projective;
type Affine = G1Affine;
type Fr = Fr;

fn hash_to_fr(input: &[u8]) -> Self::Fr {
Expand Down
28 changes: 2 additions & 26 deletions src/pallas_impl.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_pallas::{Affine, Fq, Fr, Projective, G_GENERATOR_X, G_GENERATOR_Y};

#[allow(non_camel_case_types)]
pub struct PallasAffine(Affine);
impl AffineWrapper for PallasAffine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
PallasAffine(Affine::new(x, y))
}

fn get_generator_x() -> Self::Fq {
G_GENERATOR_X
}

fn get_generator_y() -> Self::Fq {
G_GENERATOR_Y
}
}

impl From<PallasAffine> for Projective {
fn from(value: PallasAffine) -> Self {
Projective::from(value.0)
}
}
use crate::stealth_commitments::StealthAddressOnCurve;
use ark_pallas::{Fr, Projective};

pub struct Pallas;

impl StealthAddressOnCurve for Pallas {
type Projective = Projective;
type Affine = PallasAffine;
type Fr = Fr;
}

Expand Down
29 changes: 2 additions & 27 deletions src/secp256k1_impl.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_secp256k1::{Affine, Fq, Fr, Projective};
use ark_secp256k1::{G_GENERATOR_X, G_GENERATOR_Y};

#[allow(non_camel_case_types)]
pub struct Secp256k1_Affine(Affine);
impl AffineWrapper for Secp256k1_Affine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
Secp256k1_Affine(Affine::new(x, y))
}

fn get_generator_x() -> Self::Fq {
G_GENERATOR_X
}

fn get_generator_y() -> Self::Fq {
G_GENERATOR_Y
}
}

impl From<Secp256k1_Affine> for Projective {
fn from(value: Secp256k1_Affine) -> Self {
Projective::from(value.0)
}
}
use crate::stealth_commitments::StealthAddressOnCurve;
use ark_secp256k1::{Fr, Projective};

pub struct Secp256k1;

impl StealthAddressOnCurve for Secp256k1 {
type Projective = Projective;
type Affine = Secp256k1_Affine;
type Fr = Fr;
}

Expand Down
29 changes: 2 additions & 27 deletions src/secp256r1_impl.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_secp256r1::{Affine, Fq, Fr, Projective};
use ark_secp256r1::{G_GENERATOR_X, G_GENERATOR_Y};

#[allow(non_camel_case_types)]
pub struct Secp256r1_Affine(Affine);
impl AffineWrapper for Secp256r1_Affine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
Secp256r1_Affine(Affine::new(x, y))
}

fn get_generator_x() -> Self::Fq {
G_GENERATOR_X
}

fn get_generator_y() -> Self::Fq {
G_GENERATOR_Y
}
}

impl From<Secp256r1_Affine> for Projective {
fn from(value: Secp256r1_Affine) -> Self {
Projective::from(value.0)
}
}
use crate::stealth_commitments::StealthAddressOnCurve;
use ark_secp256r1::{Fr, Projective};

pub struct Secp256r1;

impl StealthAddressOnCurve for Secp256r1 {
type Projective = Projective;
type Affine = Secp256r1_Affine;
type Fr = Fr;
}

Expand Down
19 changes: 4 additions & 15 deletions src/stealth_commitments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ark_ec::CurveGroup;
use ark_ec::{CurveGroup, Group};
use ark_ff::{Fp, FpConfig, PrimeField};
use ark_serialize::CanonicalSerialize;
use ark_std::rand::rngs::OsRng;
Expand All @@ -8,13 +8,6 @@ use std::ops::{Add, Mul};

use tiny_keccak::{Hasher, Keccak};

pub trait AffineWrapper {
type Fq: ark_ff::PrimeField;
fn new(x: Self::Fq, y: Self::Fq) -> Self;
fn get_generator_x() -> Self::Fq;
fn get_generator_y() -> Self::Fq;
}

pub trait HasViewTag {
fn get_view_tag(&self) -> u64;
}
Expand Down Expand Up @@ -47,16 +40,12 @@ pub trait StealthAddressOnCurve {
type Projective: Display
+ Add<Output = Self::Projective>
+ Mul<Self::Fr, Output = Self::Projective>
+ From<Self::Affine>
+ From<<Self::Projective as CurveGroup>::Affine>
+ ark_ec::CurveGroup;
type Affine: AffineWrapper;
type Fr: Add<Self::Fr, Output = Self::Fr> + ark_ff::PrimeField + HasViewTag;

fn derive_public_key(private_key: &Self::Fr) -> Self::Projective {
let generator_affine = Self::Affine::new(
Self::Affine::get_generator_x(),
Self::Affine::get_generator_y(),
);
(Self::Projective::from(generator_affine)) * *private_key
Self::Projective::generator() * *private_key
}

fn random_keypair() -> (Self::Fr, Self::Projective) {
Expand Down
28 changes: 2 additions & 26 deletions src/vesta_impl.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
use crate::define_curve_tests;
use crate::stealth_commitments::{AffineWrapper, StealthAddressOnCurve};
use ark_vesta::{Affine, Fq, Fr, Projective, G_GENERATOR_X, G_GENERATOR_Y};

#[allow(non_camel_case_types)]
pub struct VestaAffine(Affine);
impl AffineWrapper for VestaAffine {
type Fq = Fq;
fn new(x: Self::Fq, y: Self::Fq) -> Self {
VestaAffine(Affine::new(x, y))
}

fn get_generator_x() -> Self::Fq {
G_GENERATOR_X
}

fn get_generator_y() -> Self::Fq {
G_GENERATOR_Y
}
}

impl From<VestaAffine> for Projective {
fn from(value: VestaAffine) -> Self {
Projective::from(value.0)
}
}
use crate::stealth_commitments::StealthAddressOnCurve;
use ark_vesta::{Fr, Projective};

pub struct Vesta;

impl StealthAddressOnCurve for Vesta {
type Projective = Projective;
type Affine = VestaAffine;
type Fr = Fr;
}

Expand Down

0 comments on commit f95dd17

Please sign in to comment.