-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
351 additions
and
2,174 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters