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

wallet-core: add generate_profile function #2262

Merged
merged 1 commit into from
Sep 4, 2024
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
33 changes: 33 additions & 0 deletions wallet-core/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::keys::{derive_bls_pk, derive_phoenix_pk};
use crate::RNG_SEED;
use core::ptr;
use dusk_bytes::Serializable;
use execution_core::{
signatures::bls::PublicKey as BlsPublicKey,
transfer::phoenix::PublicKey as PhoenixPublicKey,
};

use alloc::alloc::{alloc, dealloc, Layout};

/// The alignment of the memory allocated by the FFI.
Expand Down Expand Up @@ -31,6 +40,30 @@ pub fn free(ptr: u32, len: u32) {
}
}

#[no_mangle]
pub unsafe extern "C" fn generate_profile(
seed: &[u8; RNG_SEED],
index: u8,
profile: *mut [u8; PhoenixPublicKey::SIZE + BlsPublicKey::SIZE],
) -> u8 {
let ppk = derive_phoenix_pk(seed, index).to_bytes();
let bpk = derive_bls_pk(seed, index).to_bytes();

ptr::copy_nonoverlapping(
&ppk[0],
&mut (*profile)[0],
PhoenixPublicKey::SIZE,
);

ptr::copy_nonoverlapping(
&bpk[0],
&mut (*profile)[PhoenixPublicKey::SIZE],
BlsPublicKey::SIZE,
);

0
}

// Currently we're not handling panic message in the WASM module; in the future
// we might want to enable it for `debug` releases.
mod panic_handling {
Expand Down
14 changes: 13 additions & 1 deletion wallet-core/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use sha2::{Digest, Sha256};
use zeroize::Zeroize;

use execution_core::{
signatures::bls::SecretKey as BlsSecretKey,
signatures::bls::{PublicKey as BlsPublicKey, SecretKey as BlsSecretKey},
transfer::phoenix::{
PublicKey as PhoenixPublicKey, SecretKey as PhoenixSecretKey,
ViewKey as PhoenixViewKey,
Expand All @@ -34,6 +34,18 @@ pub fn derive_bls_sk(seed: &[u8; RNG_SEED], index: u8) -> BlsSecretKey {
BlsSecretKey::random(&mut rng_with_index(seed, index, b"SK"))
}

/// Generates a [`BlsPublicKey`] from a seed and index.
///
/// The randomness is generated using [`rng_with_index`].
#[must_use]
pub fn derive_bls_pk(seed: &[u8; RNG_SEED], index: u8) -> BlsPublicKey {
let mut sk = derive_bls_sk(seed, index);
let pk = BlsPublicKey::from(&sk);
sk.zeroize();

pk
}

/// Generates a [`PhoenixSecretKey`] from a seed and index.
///
/// The randomness is generated using [`rng_with_index`].
Expand Down
Loading