-
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
5 changed files
with
167 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use crate::stealth_commitments::{AffineWrapper, RawFr, StealthAddressOnCurve}; | ||
use ark_ff::PrimeField; | ||
use ark_secp256k1::{Affine as G1Affine, Fq, Fr, Projective as G1Projective}; | ||
use ark_secp256k1::{G_GENERATOR_X, G_GENERATOR_Y}; | ||
use tiny_keccak::{Hasher, Keccak}; | ||
|
||
#[allow(non_camel_case_types)] | ||
pub struct Secp256k1_G1Affine(G1Affine); | ||
impl AffineWrapper for Secp256k1_G1Affine { | ||
type Fq = Fq; | ||
fn new(x: Self::Fq, y: Self::Fq) -> Self { | ||
Secp256k1_G1Affine(G1Affine::new(x, y)) | ||
} | ||
} | ||
|
||
impl From<Secp256k1_G1Affine> for G1Projective { | ||
fn from(value: Secp256k1_G1Affine) -> Self { | ||
G1Projective::from(value.0) | ||
} | ||
} | ||
|
||
impl RawFr for Fr { | ||
type Fr = Fr; | ||
fn as_u64(&self) -> u64 { | ||
self.0 .0[0] | ||
} | ||
} | ||
|
||
pub struct Secp256k1; | ||
|
||
impl StealthAddressOnCurve for Secp256k1 { | ||
type Projective = G1Projective; | ||
type Affine = Secp256k1_G1Affine; | ||
type Fr = Fr; | ||
|
||
fn derive_public_key(private_key: &Self::Fr) -> Self::Projective { | ||
let g1_generator_affine = Self::Affine::new(G_GENERATOR_X, G_GENERATOR_Y); | ||
(Self::Projective::from(g1_generator_affine)) * *private_key | ||
} | ||
|
||
fn hash_to_fr(input: &[u8]) -> Self::Fr { | ||
let mut hash = [0; 32]; | ||
let mut hasher = Keccak::v256(); | ||
hasher.update(input); | ||
hasher.finalize(&mut hash); | ||
|
||
// We export the hash as a field element | ||
Self::Fr::from_le_bytes_mod_order(hash.as_slice()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use ark_ec::CurveGroup; | ||
|
||
type Curve = Secp256k1; | ||
|
||
#[test] | ||
fn test_random_keypair() { | ||
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() { | ||
// 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() { | ||
// 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(); | ||
let (key2, pub_key2) = Curve::random_keypair(); | ||
|
||
let shared1 = Curve::compute_shared_point(key1, pub_key2); | ||
let shared2 = Curve::compute_shared_point(key2, pub_key1); | ||
|
||
// Convert Projective to Affine for equality comparison | ||
let shared1_affine = shared1.into_affine(); | ||
let shared2_affine = shared2.into_affine(); | ||
|
||
assert_eq!(shared1_affine.x, shared2_affine.x); | ||
assert_eq!(shared1_affine.y, shared2_affine.y); | ||
} | ||
|
||
#[test] | ||
fn test_stealth_commitment_generation() { | ||
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( | ||
viewing_public_key, | ||
spending_public_key, | ||
ephemeral_private_key, | ||
); | ||
|
||
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"); | ||
} | ||
|
||
let derived_commitment = Curve::derive_public_key(&stealth_private_key_opt.unwrap()); | ||
assert_eq!(derived_commitment, stealth_commitment); | ||
} | ||
} |