Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed May 27, 2024
1 parent 66a5f27 commit a2abae9
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 2,174 deletions.
2,354 changes: 269 additions & 2,085 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.PHONY: deps clean example

deps:
cargo install cross --git https://github.com/cross-rs/cross.git --rev 1511a28
@cargo install cross --git https://github.com/cross-rs/cross.git --rev 1511a28
clean:
cargo clean
@cargo clean
example:
@cargo run -p stealth_address_kit_example
58 changes: 52 additions & 6 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,76 @@ use stealth_address_kit::StealthAddressOnCurve;

type Curve = Secp256r1;

fn print_discriminator() {
println!("{}", "+".repeat(100));
}

fn main() {
let (spending_key, spending_public_key) = Curve::random_keypair();
let (viewing_key, viewing_public_key) = Curve::random_keypair();

print_discriminator();
println!("BOB PRE-COMPUTATION");
print_discriminator();

println!("Spending Key: {}", &spending_key.to_string());
println!("Spending Public Key: {}", &spending_public_key.to_string());
println!("Viewing Key: {}", &viewing_key.to_string());
println!("Viewing Public Key: {}", &viewing_public_key.to_string());

print_discriminator();

// generate ephemeral keypair
let (ephemeral_private_key, ephemeral_public_key) = Curve::random_keypair();

let (stealth_address, view_tag) = Curve::generate_stealth_address(
print_discriminator();
println!("ALICE COMPUTATION");
print_discriminator();

println!(
"Ephemeral Private Key: {}",
&ephemeral_private_key.to_string()
);
println!(
"Ephemeral Public Key: {}",
&ephemeral_public_key.to_string()
);

let (stealth_public_key, view_tag) = Curve::generate_stealth_address(
viewing_public_key,
spending_public_key,
ephemeral_private_key,
);

println!("Stealth Public Key: {}", &stealth_public_key.to_string());
println!("View Tag: {}", &view_tag.to_string());

print_discriminator();

print_discriminator();
println!("BOB COMPUTATION AFTER RECEIVING BROADCASTED KEY MATERIAL");
print_discriminator();

let stealth_private_key_opt = Curve::generate_stealth_private_key(
ephemeral_public_key,
viewing_key,
spending_key,
view_tag,
);

if stealth_private_key_opt.is_none() {
panic!("View tags did not match");
}
if let Some(stealth_private_key) = stealth_private_key_opt {
let derived_stealth_public_key = Curve::derive_public_key(&stealth_private_key);

println!(
"Derived Stealth Public Key: {}",
&derived_stealth_public_key.to_string()
);
println!("Stealth Private Key: {}", &stealth_private_key.to_string());

let derived_stealth_address = Curve::derive_public_key(&stealth_private_key_opt.unwrap());
assert_eq!(derived_stealth_address, stealth_address);
assert_eq!(derived_stealth_public_key, stealth_public_key);

print_discriminator();
} else {
panic!("View tags did not match");
};
}
6 changes: 3 additions & 3 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ all = ["ffi", "secp256k1", "bls12_381", "bls12_377", "bn254", "secp256r1", "pall
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rln = "0.3.4"
ark-std = "0.4.0"
num-bigint = "0.4.3"
num-traits = "0.2.15"
Expand All @@ -41,10 +40,11 @@ ark-pallas = "0.4.0"
ark-vesta = "0.4.0"
ark-bw6-761 = "0.4.0"
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
ark-ec = "0.4.1"
ark-serialize = "0.4.1"
ark-ec = "0.4.2"
ark-serialize = "0.4.2"
cfg-if = "1.0.0"
paste = "1.0.0"
rand = { version = "0.8.5", features = ["getrandom"] }

[dev-dependencies]
serde_json = "1.0.96"
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/bls12_377_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};

use ark_bls12_377::{Bls12_377, Fr, G1Projective};

Expand All @@ -8,6 +8,8 @@ impl StealthAddressOnCurve for Bls12_377 {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(bls12_377, Bls12_377, Fr, G1Projective, 32, 48);
define_curve_tests!(ark_bls12_377::Bls12_377);
4 changes: 3 additions & 1 deletion sdk/src/bls12_381_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};

use ark_bls12_381::{Bls12_381, Fr, G1Projective};

Expand All @@ -8,6 +8,8 @@ impl StealthAddressOnCurve for Bls12_381 {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(bls12_381, Bls12_381, Fr, G1Projective, 32, 48);
define_curve_tests!(Bls12_381);
71 changes: 0 additions & 71 deletions sdk/src/bn254_impl.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,12 @@
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_bn254::{Bn254, Fr, G1Projective};
#[allow(unused_imports)]
use rln::ffi::*;
use rln::hashers::{hash_to_field, poseidon_hash};

impl StealthAddressOnCurve for Bn254 {
type Projective = G1Projective;
type Fr = Fr;

fn hash_to_fr(input: &[u8]) -> Self::Fr {
poseidon_hash(&[hash_to_field(input)])
}
}

#[cfg(feature = "ffi")]
define_curve_ffi!(bn254, Bn254, Fr, G1Projective, 32, 32);
define_curve_tests!(Bn254);

#[cfg(test)]
mod rln_tests {
use super::*;
use ark_std::rand::thread_rng;
use ark_std::UniformRand;
use color_eyre::{Report, Result};
use rln::public::RLN;
use rln::utils::fr_to_bytes_le;
use serde_json::json;
use std::io::Cursor;

type Curve = ark_bn254::Bn254;

// this can only be tested for bn254 since that is the curve supported by RLN
#[test]
fn apply_stealth_membership_from_one_tree_to_another() -> Result<()> {
let test_tree_height = 20;
let resources = Cursor::new(json!({"resources_folder": "tree_height_20"}).to_string());
let mut rln = RLN::new(test_tree_height, resources.clone())?;

let alice_leaf = Fr::rand(&mut thread_rng());
let (alice_known_spending_sk, alice_known_spending_pk) = Curve::random_keypair();
let alice_leaf_buffer = Cursor::new(fr_to_bytes_le(&alice_leaf));
rln.set_leaf(0, alice_leaf_buffer)?;

// now the application sees that a user has been inserted into the tree
let mut rln_app_tree = RLN::new(test_tree_height, resources)?;
// the application generates a stealth address for alice
let (ephemeral_private_key, ephemeral_public_key) = Curve::random_keypair();
let (alice_stealth_address, view_tag) = Curve::generate_stealth_address(
alice_known_spending_pk,
alice_known_spending_pk,
ephemeral_private_key,
);

let parts = [alice_stealth_address.x, alice_stealth_address.y];
let fr_parts = parts.map(|x| Fr::from(x.0));
let alice_stealth_address_buffer = Cursor::new(fr_to_bytes_le(&poseidon_hash(&fr_parts)));
rln_app_tree.set_leaf(0, alice_stealth_address_buffer)?;

// now alice's stealth address has been inserted into the tree, but alice has not
// yet derived the secret for it -
let alice_stealth_private_key_opt = Curve::generate_stealth_private_key(
ephemeral_public_key,
alice_known_spending_sk,
alice_known_spending_sk,
view_tag,
);
if alice_stealth_private_key_opt.is_none() {
return Err(Report::msg("Invalid view tag"));
}
let alice_stealth_private_key = alice_stealth_private_key_opt.unwrap();

assert_eq!(
Curve::derive_public_key(&alice_stealth_private_key),
alice_stealth_address
);

// now alice may generate valid rln proofs for the rln app tree, using a address
// derived from her address on the other tree
Ok(())
}
}
4 changes: 3 additions & 1 deletion sdk/src/bw6_761_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};

use ark_bw6_761::{Fr, G1Projective, BW6_761};

Expand All @@ -8,6 +8,8 @@ impl StealthAddressOnCurve for BW6_761 {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(bw6_761, BW6_761, Fr, G1Projective, 48, 96);
define_curve_tests!(BW6_761);
4 changes: 3 additions & 1 deletion sdk/src/pallas_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_pallas::{Fr, Projective};

pub struct Pallas;
Expand All @@ -9,6 +9,8 @@ impl StealthAddressOnCurve for Pallas {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(pallas, Pallas, Fr, Projective, 32, 33);
define_curve_tests!(Pallas);
4 changes: 3 additions & 1 deletion sdk/src/secp256k1_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_secp256k1::{Fr, Projective};

pub struct Secp256k1;
Expand All @@ -9,6 +9,8 @@ impl StealthAddressOnCurve for Secp256k1 {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(secp256k1, Secp256k1, Fr, Projective, 32, 33);
define_curve_tests!(Secp256k1);
4 changes: 3 additions & 1 deletion sdk/src/secp256r1_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_secp256r1::{Fr, Projective};

pub struct Secp256r1;
Expand All @@ -9,6 +9,8 @@ impl StealthAddressOnCurve for Secp256r1 {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(secp256r1, Secp256r1, Fr, Projective, 32, 33);
define_curve_tests!(Secp256r1);
4 changes: 3 additions & 1 deletion sdk/src/vesta_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::define_curve_tests;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_vesta::{Fr, Projective};

pub struct Vesta;
Expand All @@ -9,6 +9,8 @@ impl StealthAddressOnCurve for Vesta {
type Fr = Fr;
}

#[cfg(feature = "ffi")]
use crate::define_curve_ffi;
#[cfg(feature = "ffi")]
define_curve_ffi!(vesta, Vesta, Fr, Projective, 32, 33);
define_curve_tests!(Vesta);

0 comments on commit a2abae9

Please sign in to comment.