Skip to content

Commit

Permalink
WASM: add methods to create randomized keys
Browse files Browse the repository at this point in the history
  • Loading branch information
andiflabs committed Dec 4, 2024
1 parent 1a313d2 commit de6b0cc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
49 changes: 48 additions & 1 deletion ironfish-rust-wasm/src/keys/view_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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()
}
}
11 changes: 11 additions & 0 deletions ironfish-rust-wasm/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<u8> {
self.0.to_bytes_be().to_vec()
Expand All @@ -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<u8> {
self.0.to_bytes().to_vec()
Expand Down

0 comments on commit de6b0cc

Please sign in to comment.