From de6b0cc11266027002d67529a0863dc526905837 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 4 Dec 2024 10:45:53 -0800 Subject: [PATCH] WASM: add methods to create randomized keys --- ironfish-rust-wasm/src/keys/view_keys.rs | 49 +++++++++++++++++++++++- ironfish-rust-wasm/src/primitives.rs | 11 ++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/ironfish-rust-wasm/src/keys/view_keys.rs b/ironfish-rust-wasm/src/keys/view_keys.rs index 6166bfd85f..f4a88d5111 100644 --- a/ironfish-rust-wasm/src/keys/view_keys.rs +++ b/ironfish-rust-wasm/src/keys/view_keys.rs @@ -3,8 +3,13 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use crate::{ - errors::IronfishError, keys::PublicAddress, primitives::PublicKey, wasm_bindgen_wrapper, + errors::IronfishError, + keys::PublicAddress, + primitives::{Fr, PublicKey}, + wasm_bindgen_wrapper, }; +use ironfish_zkp::constants::SPENDING_KEY_GENERATOR; +use rand::thread_rng; use wasm_bindgen::prelude::*; wasm_bindgen_wrapper! { @@ -116,4 +121,46 @@ impl ViewKey { pub fn nullifier_deriving_key(&self) -> PublicKey { self.0.nullifier_deriving_key.into() } + + #[wasm_bindgen(js_name = randomizedPublicKey)] + pub fn randomized_public_key_pair(&self) -> RandomizedPublicKeyPair { + let (r, s) = self.0.randomized_public_key(thread_rng()); + RandomizedPublicKeyPair::new(r.into(), s.into()) + } +} + +#[wasm_bindgen] +#[derive(Clone, Debug)] +pub struct RandomizedPublicKeyPair(Fr, PublicKey); + +#[wasm_bindgen] +impl RandomizedPublicKeyPair { + #[wasm_bindgen(constructor)] + pub fn new(public_key_randomness: Fr, randomized_public_key: PublicKey) -> Self { + Self(public_key_randomness, randomized_public_key) + } + + #[wasm_bindgen(js_name = fromViewKey)] + pub fn from_view_key(view_key: &ViewKey) -> Self { + let public_key_randomness = Fr::random(); + Self::from_view_key_and_randomness(view_key, public_key_randomness) + } + + #[wasm_bindgen(js_name = fromViewKeyAndRandomness)] + pub fn from_view_key_and_randomness(view_key: &ViewKey, public_key_randomness: Fr) -> Self { + let randomized_public_key = view_key + .authorizing_key() + .randomize(&public_key_randomness, &(*SPENDING_KEY_GENERATOR).into()); + Self(public_key_randomness, randomized_public_key) + } + + #[wasm_bindgen(js_name = publicKeyRandomness)] + pub fn public_key_randomness(&self) -> Fr { + self.0 + } + + #[wasm_bindgen(js_name = randomizedPublicKey)] + pub fn randomized_public_key(&self) -> PublicKey { + self.1.clone() + } } diff --git a/ironfish-rust-wasm/src/primitives.rs b/ironfish-rust-wasm/src/primitives.rs index ba1c1c5088..adea5f3995 100644 --- a/ironfish-rust-wasm/src/primitives.rs +++ b/ironfish-rust-wasm/src/primitives.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use crate::{errors::IronfishError, wasm_bindgen_wrapper}; +use group::ff::Field; use group::GroupEncoding; use ironfish::errors::IronfishErrorKind; use ironfish_zkp::redjubjub; @@ -16,6 +17,11 @@ wasm_bindgen_wrapper! { #[wasm_bindgen] impl Scalar { + #[wasm_bindgen] + pub fn random() -> Self { + Self(blstrs::Scalar::random(thread_rng())) + } + #[wasm_bindgen(js_name = toBytesBe)] pub fn to_bytes_be(&self) -> Vec { self.0.to_bytes_be().to_vec() @@ -34,6 +40,11 @@ wasm_bindgen_wrapper! { #[wasm_bindgen] impl Fr { + #[wasm_bindgen] + pub fn random() -> Self { + Self(ironfish_jubjub::Fr::random(thread_rng())) + } + #[wasm_bindgen(js_name = toBytes)] pub fn to_bytes(&self) -> Vec { self.0.to_bytes().to_vec()