Skip to content

Commit

Permalink
fix(repo): complete rename of stealth commitments to addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed May 27, 2024
1 parent 7d99437 commit 3abd6ce
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bn254 = []
pallas = []
vesta = []
bw6_761 = []
default = ["ffi", "secp256k1"]
default = ["all"]
all = ["ffi", "secp256k1", "bls12_381", "bls12_377", "bn254", "secp256r1", "pallas", "vesta", "bw6_761"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ fn main() {
// generate ephemeral keypair
let (ephemeral_private_key, ephemeral_public_key) = Bn254::random_keypair();

let (stealth_commitment, view_tag) = Bn254::generate_stealth_commitment(viewing_public_key, spending_public_key, ephemeral_private_key);
let (stealth_address, view_tag) = Bn254::generate_stealth_address(viewing_public_key, spending_public_key, ephemeral_private_key);

let stealth_private_key_opt = Bn254::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");
}

let derived_commitment = Bn254::derive_public_key(stealth_private_key_opt.unwrap());
assert_eq!(derived_commitment, stealth_commitment);
let derived_stealth_address = Bn254::derive_public_key(stealth_private_key_opt.unwrap());
assert_eq!(derived_stealth_address, stealth_address);
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/bls12_377_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stealth_commitments::StealthAddressOnCurve;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};

use ark_bls12_377::{Bls12_377, Fr, G1Projective};
Expand Down
2 changes: 1 addition & 1 deletion src/bls12_381_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stealth_commitments::StealthAddressOnCurve;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};

use ark_bls12_381::{Bls12_381, Fr, G1Projective};
Expand Down
21 changes: 10 additions & 11 deletions src/bn254_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stealth_commitments::StealthAddressOnCurve;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_bn254::{Bn254, Fr, G1Projective};
#[allow(unused_imports)]
Expand Down Expand Up @@ -45,21 +45,20 @@ mod rln_tests {

// 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 commitment for alice
// the application generates a stealth address for alice
let (ephemeral_private_key, ephemeral_public_key) = Curve::random_keypair();
let (alice_stealth_commitment, view_tag) = Curve::generate_stealth_commitment(
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_commitment.x, alice_stealth_commitment.y];
let parts = [alice_stealth_address.x, alice_stealth_address.y];
let fr_parts = parts.map(|x| Fr::from(x.0));
let alice_stealth_commitment_buffer =
Cursor::new(fr_to_bytes_le(&poseidon_hash(&fr_parts)));
rln_app_tree.set_leaf(0, alice_stealth_commitment_buffer)?;
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 commitment has been inserted into the tree, but alice has not
// 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,
Expand All @@ -74,11 +73,11 @@ mod rln_tests {

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

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

use ark_bw6_761::{Fr, G1Projective, BW6_761};
Expand Down
70 changes: 35 additions & 35 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ macro_rules! define_curve_ffi {

#[repr(C)]
#[derive(Debug)]
pub struct [<$curve_name _StealthCommitment>] {
stealth_commitment: [<$curve_name _G1Projective>],
pub struct [<$curve_name _StealthAddress>] {
stealth_address: [<$curve_name _G1Projective>],
view_tag: u64,
}

Expand Down Expand Up @@ -158,30 +158,30 @@ macro_rules! define_curve_ffi {
}
}

impl [<$curve_name _StealthCommitment>] {
impl [<$curve_name _StealthAddress>] {
pub fn zero() -> Self {
[<$curve_name _StealthCommitment>] {
stealth_commitment: [<$curve_name _G1Projective>]::zero(),
[<$curve_name _StealthAddress>] {
stealth_address: [<$curve_name _G1Projective>]::zero(),
view_tag: 0,
}
}
}

impl TryFrom<($G1Projective, u64)> for [<$curve_name _StealthCommitment>] {
impl TryFrom<($G1Projective, u64)> for [<$curve_name _StealthAddress>] {
type Error = SerializationError;

fn try_from(value: ($G1Projective, u64)) -> Result<Self, Self::Error> {
Ok([<$curve_name _StealthCommitment>] {
stealth_commitment: <[<$curve_name _G1Projective>]>::try_from(value.0)?,
Ok([<$curve_name _StealthAddress>] {
stealth_address: <[<$curve_name _G1Projective>]>::try_from(value.0)?,
view_tag: value.1,
})
}
}

impl TryInto<($G1Projective, u64)> for [<$curve_name _StealthCommitment>] {
impl TryInto<($G1Projective, u64)> for [<$curve_name _StealthAddress>] {
type Error = SerializationError;
fn try_into(self) -> Result<($G1Projective, u64), Self::Error> {
Ok((self.stealth_commitment.try_into()?, self.view_tag))
Ok((self.stealth_address.try_into()?, self.view_tag))
}
}
#[no_mangle]
Expand Down Expand Up @@ -295,15 +295,15 @@ macro_rules! define_curve_ffi {
}

#[no_mangle]
pub extern "C" fn [<$curve_name _ffi_generate_stealth_commitment>](
pub extern "C" fn [<$curve_name _ffi_generate_stealth_address>](
viewing_public_key: *mut [<$curve_name _G1Projective>],
spending_public_key: *mut [<$curve_name _G1Projective>],
ephemeral_private_key: *mut [<$curve_name _Fr>],
) -> *mut CReturn<[<$curve_name _StealthCommitment>]> {
) -> *mut CReturn<[<$curve_name _StealthAddress>]> {
let viewing_public_key = unsafe {
if viewing_public_key.is_null() {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: CErrorCode::InvalidKeys,
}));
}
Expand All @@ -312,7 +312,7 @@ macro_rules! define_curve_ffi {
let spending_public_key = unsafe {
if spending_public_key.is_null() {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: CErrorCode::InvalidKeys,
}));
}
Expand All @@ -321,7 +321,7 @@ macro_rules! define_curve_ffi {
let ephemeral_private_key = unsafe {
if ephemeral_private_key.is_null() {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: CErrorCode::InvalidKeys,
}));
}
Expand All @@ -332,7 +332,7 @@ macro_rules! define_curve_ffi {
Ok(v) => v,
Err(_) => {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: CErrorCode::InvalidKeys,
}))
}
Expand All @@ -341,7 +341,7 @@ macro_rules! define_curve_ffi {
Ok(v) => v,
Err(_) => {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: CErrorCode::InvalidKeys,
}))
}
Expand All @@ -350,12 +350,12 @@ macro_rules! define_curve_ffi {
Ok(v) => v,
Err(_) => {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: CErrorCode::InvalidKeys,
}))
}
};
let res = match [<$curve_name _StealthCommitment>]::try_from(<$Curve>::generate_stealth_commitment(
let res = match [<$curve_name _StealthAddress>]::try_from(<$Curve>::generate_stealth_address(
viewing_public_key,
spending_public_key,
ephemeral_private_key,
Expand All @@ -366,7 +366,7 @@ macro_rules! define_curve_ffi {
},
Err(err) => {
return Box::into_raw(Box::new(CReturn {
value: [<$curve_name _StealthCommitment>]::zero(),
value: [<$curve_name _StealthAddress>]::zero(),
err_code: err.into(),
}))
}
Expand All @@ -375,7 +375,7 @@ macro_rules! define_curve_ffi {
}

#[no_mangle]
pub extern "C" fn [<drop_ $curve_name _ffi_generate_stealth_commitment>](ptr: *mut CReturn<[<$curve_name _StealthCommitment>]>) {
pub extern "C" fn [<drop_ $curve_name _ffi_generate_stealth_address>](ptr: *mut CReturn<[<$curve_name _StealthAddress>]>) {
if ptr.is_null() {
return;
}
Expand Down Expand Up @@ -491,12 +491,12 @@ macro_rules! define_curve_ffi {
use ark_ec::CurveGroup;

#[test]
fn [<test_ $curve_name _ffi_generate_random_fr>]() {
fn generate_random_fr_happy_path() {
let _ = [<$curve_name _ffi_generate_random_fr>]();
}

#[test]
fn [<test_ $curve_name _ffi_random_keypair>]() {
fn random_keypair_happy_path() {
// Generate a random keypair
let keypair_raw = [<$curve_name _ffi_random_keypair>]();
let keypair = unsafe { &*keypair_raw };
Expand All @@ -516,7 +516,7 @@ macro_rules! define_curve_ffi {
}

#[test]
fn test_ffi_generate_stealth_commitment() {
fn generate_stealth_address_happy_path() {
// Generate random keypairs
let spending_key_raw = [<$curve_name _ffi_random_keypair>]();
let spending_key = unsafe { &mut *spending_key_raw };
Expand All @@ -533,14 +533,14 @@ macro_rules! define_curve_ffi {
let ephemeral_pub_key_ptr = &mut ephemeral_key.value.public_key;
let ephemeral_priv_key_ptr = &mut ephemeral_key.value.private_key;

// Generate stealth commitment payload
let stealth_commitment_payload_raw = [<$curve_name _ffi_generate_stealth_commitment>](
// Generate stealth address payload
let stealth_address_payload_raw = [<$curve_name _ffi_generate_stealth_address>](
viewing_pub_key_ptr,
spending_pub_key_ptr,
ephemeral_priv_key_ptr,
);
let stealth_commitment_payload = unsafe { &mut *stealth_commitment_payload_raw };
let view_tag_ptr = &mut stealth_commitment_payload.value.view_tag;
let stealth_address_payload = unsafe { &mut *stealth_address_payload_raw };
let view_tag_ptr = &mut stealth_address_payload.value.view_tag;

// Generate stealth private key
let stealth_private_key_raw = [<$curve_name _ffi_generate_stealth_private_key>](
Expand All @@ -560,19 +560,19 @@ macro_rules! define_curve_ffi {
panic!("View tags did not match");
}

// Derive commitment
let derived_commitment_raw = [<$curve_name _ffi_derive_public_key>](&mut stealth_private_key.value);
// Derive address
let derived_address_raw = [<$curve_name _ffi_derive_public_key>](&mut stealth_private_key.value);
[<drop_ $curve_name _ffi_generate_stealth_private_key>](stealth_private_key_raw);

let derived_commitment = unsafe { &*derived_commitment_raw };
let derived_address = unsafe { &*derived_address_raw };

assert_eq!(
derived_commitment.value,
stealth_commitment_payload.value.stealth_commitment
derived_address.value,
stealth_address_payload.value.stealth_address
);
// Drop all allocated memory to avoid memory leaks
[<drop_ $curve_name _ffi_generate_stealth_commitment>](stealth_commitment_payload_raw);
[<drop_ $curve_name _ffi_derive_public_key>](derived_commitment_raw);
[<drop_ $curve_name _ffi_generate_stealth_address>](stealth_address_payload_raw);
[<drop_ $curve_name _ffi_derive_public_key>](derived_address_raw);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod macros;
mod stealth_commitments;
mod stealth_addresses;

#[cfg(feature = "bls12_377")]
mod bls12_377_impl;
Expand Down
14 changes: 7 additions & 7 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ macro_rules! define_curve_tests {
use ark_ec::CurveGroup;

#[test]
fn test_random_keypair() {
fn random_keypair_happy_path() {
let (key, pub_key) = <$Curve>::random_keypair();
// Check the derived key matches the one generated from original key
assert_eq!(<$Curve>::derive_public_key(&key), pub_key);
}

#[test]
fn test_hash_to_fr() {
fn hash_to_fr_happy_path() {
// Test that hash_to_fr(input_1) != hash_to_fr(input_2) when input_1 != input_2
let input_1 = b"input_1";
let input_2 = b"input_2";
assert_ne!(<$Curve>::hash_to_fr(input_1), <$Curve>::hash_to_fr(input_2));
}

#[test]
fn test_compute_shared_point() {
fn compute_shared_point_happy_path() {
// In a multiple participant scenario, any participant's public key
// combined with any other participant's private key should arrive at the same shared key
let (key1, pub_key1) = <$Curve>::random_keypair();
Expand All @@ -40,14 +40,14 @@ macro_rules! define_curve_tests {
}

#[test]
fn test_stealth_commitment_generation() {
fn generate_stealth_address_happy_path() {
let (spending_key, spending_public_key) = <$Curve>::random_keypair();
let (viewing_key, viewing_public_key) = <$Curve>::random_keypair();

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

let (stealth_commitment, view_tag) = <$Curve>::generate_stealth_commitment(
let (stealth_address, view_tag) = <$Curve>::generate_stealth_address(
viewing_public_key,
spending_public_key,
ephemeral_private_key,
Expand All @@ -64,9 +64,9 @@ macro_rules! define_curve_tests {
panic!("View tags did not match");
}

let derived_commitment =
let derived_address =
<$Curve>::derive_public_key(&stealth_private_key_opt.unwrap());
assert_eq!(derived_commitment, stealth_commitment);
assert_eq!(derived_address, stealth_address);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/pallas_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stealth_commitments::StealthAddressOnCurve;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_pallas::{Fr, Projective};

Expand Down
2 changes: 1 addition & 1 deletion src/secp256k1_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stealth_commitments::StealthAddressOnCurve;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_secp256k1::{Fr, Projective};

Expand Down
2 changes: 1 addition & 1 deletion src/secp256r1_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stealth_commitments::StealthAddressOnCurve;
use crate::stealth_addresses::StealthAddressOnCurve;
use crate::{define_curve_ffi, define_curve_tests};
use ark_secp256r1::{Fr, Projective};

Expand Down
Loading

0 comments on commit 3abd6ce

Please sign in to comment.