mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-05 23:03:06 +00:00
64 lines
1.8 KiB
Rust
64 lines
1.8 KiB
Rust
use log::info;
|
|
use nssa_core::{
|
|
NullifierPublicKey, SharedSecretKey,
|
|
encryption::{EphemeralPublicKey, EphemeralSecretKey, IncomingViewingPublicKey},
|
|
};
|
|
use rand::{rngs::OsRng, RngCore};
|
|
use sha2::Digest;
|
|
|
|
use crate::key_management::secret_holders::OutgoingViewingSecretKey;
|
|
|
|
#[derive(Debug)]
|
|
///Ephemeral secret key holder. Non-clonable as intended for one-time use. Produces ephemeral public keys. Can produce shared secret for sender.
|
|
pub struct EphemeralKeyHolder {
|
|
ephemeral_secret_key: EphemeralSecretKey,
|
|
}
|
|
|
|
pub fn produce_one_sided_shared_secret_receiver(
|
|
ipk: &IncomingViewingPublicKey,
|
|
) -> (SharedSecretKey, EphemeralPublicKey) {
|
|
let mut esk = [0; 32];
|
|
OsRng.fill_bytes(&mut esk);
|
|
(
|
|
SharedSecretKey::new(&esk, ipk),
|
|
EphemeralPublicKey::from_scalar(esk),
|
|
)
|
|
}
|
|
|
|
impl EphemeralKeyHolder {
|
|
pub fn new(
|
|
receiver_nullifier_public_key: &NullifierPublicKey,
|
|
) -> Self {
|
|
let mut nonce_bytes = [0; 16];
|
|
OsRng.fill_bytes(&mut nonce_bytes);
|
|
let mut hasher = sha2::Sha256::new();
|
|
hasher.update(receiver_nullifier_public_key);
|
|
hasher.update(nonce_bytes);
|
|
|
|
Self {
|
|
ephemeral_secret_key: hasher.finalize().into(),
|
|
}
|
|
}
|
|
|
|
pub fn generate_ephemeral_public_key(&self) -> EphemeralPublicKey {
|
|
EphemeralPublicKey::from_scalar(self.ephemeral_secret_key)
|
|
}
|
|
|
|
pub fn calculate_shared_secret_sender(
|
|
&self,
|
|
receiver_incoming_viewing_public_key: &IncomingViewingPublicKey,
|
|
) -> SharedSecretKey {
|
|
SharedSecretKey::new(
|
|
&self.ephemeral_secret_key,
|
|
receiver_incoming_viewing_public_key,
|
|
)
|
|
}
|
|
|
|
pub fn log(&self) {
|
|
info!(
|
|
"Ephemeral private key is {:?}",
|
|
hex::encode(serde_json::to_vec(&self.ephemeral_secret_key).unwrap())
|
|
);
|
|
}
|
|
}
|