add ElGamal key derivation from Ed25519 signing key

This commit is contained in:
Sam Kim 2021-10-12 10:21:07 -04:00 committed by Michael Vines
parent 7aef523a41
commit 43e368faf6
1 changed files with 14 additions and 0 deletions

View File

@ -11,6 +11,7 @@ use {
ristretto::{CompressedRistretto, RistrettoPoint}, ristretto::{CompressedRistretto, RistrettoPoint},
scalar::Scalar, scalar::Scalar,
}, },
ed25519_dalek::SecretKey as SigningKey,
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::collections::HashMap, std::collections::HashMap,
std::convert::TryInto, std::convert::TryInto,
@ -20,6 +21,7 @@ use {
#[cfg(not(target_arch = "bpf"))] #[cfg(not(target_arch = "bpf"))]
use { use {
rand::{rngs::OsRng, CryptoRng, RngCore}, rand::{rngs::OsRng, CryptoRng, RngCore},
sha3::Sha3_512,
std::{ std::{
fmt, fmt,
fs::{self, File, OpenOptions}, fs::{self, File, OpenOptions},
@ -217,6 +219,13 @@ impl ElGamalKeypair {
#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, Eq, PartialEq)] #[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, Eq, PartialEq)]
pub struct ElGamalPubkey(RistrettoPoint); pub struct ElGamalPubkey(RistrettoPoint);
impl ElGamalPubkey { impl ElGamalPubkey {
/// Derive the `ElGamalPubkey` that uniquely corresponds to an `ElGamalSecretKey`
#[allow(non_snake_case)]
pub fn new(sk: ElGamalSecretKey) -> Self {
let H = PedersenBase::default().H;
ElGamalPubkey(sk.0 * H)
}
pub fn get_point(&self) -> RistrettoPoint { pub fn get_point(&self) -> RistrettoPoint {
self.0 self.0
} }
@ -271,6 +280,11 @@ impl fmt::Display for ElGamalPubkey {
#[zeroize(drop)] #[zeroize(drop)]
pub struct ElGamalSecretKey(Scalar); pub struct ElGamalSecretKey(Scalar);
impl ElGamalSecretKey { impl ElGamalSecretKey {
pub fn new(signing_key: SigningKey, label: &'static [u8]) -> Self {
let hashable = [&signing_key.to_bytes(), label].concat();
ElGamalSecretKey(Scalar::hash_from_bytes::<Sha3_512>(&hashable))
}
pub fn get_scalar(&self) -> Scalar { pub fn get_scalar(&self) -> Scalar {
self.0 self.0
} }