Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM: add bindings for EphemeralKeyPair #5727

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions ironfish-rust-wasm/src/keys/ephemeral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::{
errors::IronfishError,
primitives::{Fr, SubgroupPoint},
wasm_bindgen_wrapper,
};
use wasm_bindgen::prelude::*;

wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EphemeralKeyPair(ironfish::keys::EphemeralKeyPair);
}

#[wasm_bindgen]
impl EphemeralKeyPair {
#[wasm_bindgen(constructor)]
pub fn deserialize(bytes: &[u8]) -> Result<Self, IronfishError> {
Ok(Self(ironfish::keys::EphemeralKeyPair::read(bytes)?))
}

#[wasm_bindgen]
pub fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.0
.write(&mut buf)
.expect("failed to serialize ephemeral key pair");
buf
}

#[wasm_bindgen]
pub fn random() -> Self {
Self(ironfish::keys::EphemeralKeyPair::new())
}

#[wasm_bindgen(getter)]
pub fn secret(&self) -> Fr {
self.0.secret().to_owned().into()
}

#[wasm_bindgen(getter)]
pub fn public(&self) -> SubgroupPoint {
self.0.public().to_owned().into()
}
}
2 changes: 2 additions & 0 deletions ironfish-rust-wasm/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

mod ephemeral;
mod mnemonics;
mod proof_generation_key;
mod public_address;
mod sapling_key;
mod view_keys;

pub use ephemeral::EphemeralKeyPair;
pub use mnemonics::Language;
pub use proof_generation_key::ProofGenerationKey;
pub use public_address::PublicAddress;
Expand Down
35 changes: 31 additions & 4 deletions ironfish-rust/src/keys/ephemeral.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::errors::{IronfishError, IronfishErrorKind};
use ff::Field;
use ironfish_zkp::constants::PUBLIC_KEY_GENERATOR;
use rand::thread_rng;
use std::io;

/// Diffie Hellman key exchange pair as used in note encryption.
///
/// This can be used according to the protocol described in
/// [`crate::keys::shared_secret`]
#[derive(Default)]
#[derive(Default, Clone, PartialEq, Eq, Debug)]
pub struct EphemeralKeyPair {
secret: ironfish_jubjub::Fr,
public: ironfish_jubjub::SubgroupPoint,
}

impl EphemeralKeyPair {
pub fn new() -> Self {
let secret = ironfish_jubjub::Fr::random(thread_rng());
loop {
let secret = ironfish_jubjub::Fr::random(thread_rng());
if let Ok(key_pair) = Self::from_secret(secret) {
break key_pair;
}
}
}

Self {
pub fn from_secret(secret: ironfish_jubjub::Fr) -> Result<Self, IronfishError> {
if secret == ironfish_jubjub::Fr::zero() || secret == ironfish_jubjub::Fr::one() {
return Err(IronfishError::new(IronfishErrorKind::InvalidSecret));
}
Ok(Self {
secret,
public: *PUBLIC_KEY_GENERATOR * secret,
}
})
}

pub fn secret(&self) -> &ironfish_jubjub::Fr {
Expand All @@ -32,6 +45,20 @@ impl EphemeralKeyPair {
pub fn public(&self) -> &ironfish_jubjub::SubgroupPoint {
&self.public
}

pub fn read<R: io::Read>(mut reader: R) -> Result<Self, IronfishError> {
let mut secret_bytes = [0u8; 32];
reader.read_exact(&mut secret_bytes)?;
let secret = Option::from(ironfish_jubjub::Fr::from_bytes(&secret_bytes))
.ok_or_else(|| IronfishError::new(IronfishErrorKind::InvalidData))?;
Self::from_secret(secret)
}

pub fn write<W: io::Write>(&self, mut writer: W) -> Result<(), IronfishError> {
let secret_bytes = self.secret.to_bytes();
writer.write_all(&secret_bytes)?;
Ok(())
}
}

#[cfg(test)]
Expand Down