Rename ElGamalSK to ElGamalSecretKey

This commit is contained in:
Michael Vines
2021-10-01 09:48:45 -07:00
parent 5445e13828
commit 8bb6f0dc6f
5 changed files with 35 additions and 35 deletions

View File

@ -23,7 +23,7 @@ pub struct ElGamal;
impl ElGamal { impl ElGamal {
/// Generates the public and secret keys for ElGamal encryption. /// Generates the public and secret keys for ElGamal encryption.
#[cfg(not(target_arch = "bpf"))] #[cfg(not(target_arch = "bpf"))]
pub fn keygen() -> (ElGamalPubkey, ElGamalSK) { pub fn keygen() -> (ElGamalPubkey, ElGamalSecretKey) {
ElGamal::keygen_with(&mut OsRng) // using OsRng for now ElGamal::keygen_with(&mut OsRng) // using OsRng for now
} }
@ -31,7 +31,7 @@ impl ElGamal {
/// secret keys for ElGamal encryption. /// secret keys for ElGamal encryption.
#[cfg(not(target_arch = "bpf"))] #[cfg(not(target_arch = "bpf"))]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn keygen_with<T: RngCore + CryptoRng>(rng: &mut T) -> (ElGamalPubkey, ElGamalSK) { pub fn keygen_with<T: RngCore + CryptoRng>(rng: &mut T) -> (ElGamalPubkey, ElGamalSecretKey) {
// sample a non-zero scalar // sample a non-zero scalar
let mut s: Scalar; let mut s: Scalar;
loop { loop {
@ -45,7 +45,7 @@ impl ElGamal {
let H = PedersenBase::default().H; let H = PedersenBase::default().H;
let P = s.invert() * H; let P = s.invert() * H;
(ElGamalPubkey(P), ElGamalSK(s)) (ElGamalPubkey(P), ElGamalSecretKey(s))
} }
/// On input a public key and a message to be encrypted, the function /// On input a public key and a message to be encrypted, the function
@ -82,8 +82,8 @@ impl ElGamal {
/// ///
/// The output of the function is of type `DiscreteLog`. The exact message /// The output of the function is of type `DiscreteLog`. The exact message
/// can be recovered via the DiscreteLog's decode method. /// can be recovered via the DiscreteLog's decode method.
pub fn decrypt(sk: &ElGamalSK, ct: &ElGamalCiphertext) -> DiscreteLog { pub fn decrypt(sk: &ElGamalSecretKey, ct: &ElGamalCiphertext) -> DiscreteLog {
let ElGamalSK(s) = sk; let ElGamalSecretKey(s) = sk;
let ElGamalCiphertext { let ElGamalCiphertext {
message_comm, message_comm,
decrypt_handle, decrypt_handle,
@ -97,7 +97,7 @@ impl ElGamal {
/// On input a secret key and a ciphertext, the function decrypts the /// On input a secret key and a ciphertext, the function decrypts the
/// ciphertext for a u32 value. /// ciphertext for a u32 value.
pub fn decrypt_u32(sk: &ElGamalSK, ct: &ElGamalCiphertext) -> Option<u32> { pub fn decrypt_u32(sk: &ElGamalSecretKey, ct: &ElGamalCiphertext) -> Option<u32> {
let discrete_log_instance = ElGamal::decrypt(sk, ct); let discrete_log_instance = ElGamal::decrypt(sk, ct);
discrete_log_instance.decode_u32() discrete_log_instance.decode_u32()
} }
@ -105,7 +105,7 @@ impl ElGamal {
/// On input a secret key, ciphertext, and hashmap, the function decrypts the /// On input a secret key, ciphertext, and hashmap, the function decrypts the
/// ciphertext for a u32 value. /// ciphertext for a u32 value.
pub fn decrypt_u32_online( pub fn decrypt_u32_online(
sk: &ElGamalSK, sk: &ElGamalSecretKey,
ct: &ElGamalCiphertext, ct: &ElGamalCiphertext,
hashmap: &HashMap<[u8; 32], u32>, hashmap: &HashMap<[u8; 32], u32>,
) -> Option<u32> { ) -> Option<u32> {
@ -160,8 +160,8 @@ impl From<RistrettoPoint> for ElGamalPubkey {
/// Secret key for the ElGamal encryption scheme. /// Secret key for the ElGamal encryption scheme.
#[derive(Serialize, Deserialize, Debug, Zeroize)] #[derive(Serialize, Deserialize, Debug, Zeroize)]
#[zeroize(drop)] #[zeroize(drop)]
pub struct ElGamalSK(Scalar); pub struct ElGamalSecretKey(Scalar);
impl ElGamalSK { impl ElGamalSecretKey {
pub fn get_scalar(&self) -> Scalar { pub fn get_scalar(&self) -> Scalar {
self.0 self.0
} }
@ -189,27 +189,27 @@ impl ElGamalSK {
self.0.to_bytes() self.0.to_bytes()
} }
pub fn from_bytes(bytes: &[u8]) -> Option<ElGamalSK> { pub fn from_bytes(bytes: &[u8]) -> Option<ElGamalSecretKey> {
match bytes.try_into() { match bytes.try_into() {
Ok(bytes) => Scalar::from_canonical_bytes(bytes).map(ElGamalSK), Ok(bytes) => Scalar::from_canonical_bytes(bytes).map(ElGamalSecretKey),
_ => None, _ => None,
} }
} }
} }
impl From<Scalar> for ElGamalSK { impl From<Scalar> for ElGamalSecretKey {
fn from(scalar: Scalar) -> ElGamalSK { fn from(scalar: Scalar) -> ElGamalSecretKey {
ElGamalSK(scalar) ElGamalSecretKey(scalar)
} }
} }
impl Eq for ElGamalSK {} impl Eq for ElGamalSecretKey {}
impl PartialEq for ElGamalSK { impl PartialEq for ElGamalSecretKey {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).unwrap_u8() == 1u8 self.ct_eq(other).unwrap_u8() == 1u8
} }
} }
impl ConstantTimeEq for ElGamalSK { impl ConstantTimeEq for ElGamalSecretKey {
fn ct_eq(&self, other: &Self) -> Choice { fn ct_eq(&self, other: &Self) -> Choice {
self.0.ct_eq(&other.0) self.0.ct_eq(&other.0)
} }
@ -262,19 +262,19 @@ impl ElGamalCiphertext {
} }
/// Utility method for code ergonomics. /// Utility method for code ergonomics.
pub fn decrypt(&self, sk: &ElGamalSK) -> DiscreteLog { pub fn decrypt(&self, sk: &ElGamalSecretKey) -> DiscreteLog {
ElGamal::decrypt(sk, self) ElGamal::decrypt(sk, self)
} }
/// Utility method for code ergonomics. /// Utility method for code ergonomics.
pub fn decrypt_u32(&self, sk: &ElGamalSK) -> Option<u32> { pub fn decrypt_u32(&self, sk: &ElGamalSecretKey) -> Option<u32> {
ElGamal::decrypt_u32(sk, self) ElGamal::decrypt_u32(sk, self)
} }
/// Utility method for code ergonomics. /// Utility method for code ergonomics.
pub fn decrypt_u32_online( pub fn decrypt_u32_online(
&self, &self,
sk: &ElGamalSK, sk: &ElGamalSecretKey,
hashmap: &HashMap<[u8; 32], u32>, hashmap: &HashMap<[u8; 32], u32>,
) -> Option<u32> { ) -> Option<u32> {
ElGamal::decrypt_u32_online(sk, self, hashmap) ElGamal::decrypt_u32_online(sk, self, hashmap)
@ -507,7 +507,7 @@ mod tests {
let (_, sk) = ElGamal::keygen(); let (_, sk) = ElGamal::keygen();
let encoded = bincode::serialize(&sk).unwrap(); let encoded = bincode::serialize(&sk).unwrap();
let decoded: ElGamalSK = bincode::deserialize(&encoded).unwrap(); let decoded: ElGamalSecretKey = bincode::deserialize(&encoded).unwrap();
assert_eq!(sk, decoded); assert_eq!(sk, decoded);
} }

View File

@ -5,7 +5,7 @@ use {
#[cfg(not(target_arch = "bpf"))] #[cfg(not(target_arch = "bpf"))]
use { use {
crate::{ crate::{
encryption::elgamal::{ElGamalCiphertext, ElGamalSK}, encryption::elgamal::{ElGamalCiphertext, ElGamalSecretKey},
errors::ProofError, errors::ProofError,
instruction::Verifiable, instruction::Verifiable,
transcript::TranscriptProtocol, transcript::TranscriptProtocol,
@ -39,7 +39,7 @@ pub struct CloseAccountData {
#[cfg(not(target_arch = "bpf"))] #[cfg(not(target_arch = "bpf"))]
impl CloseAccountData { impl CloseAccountData {
pub fn new(source_sk: &ElGamalSK, balance: ElGamalCiphertext) -> Self { pub fn new(source_sk: &ElGamalSecretKey, balance: ElGamalCiphertext) -> Self {
let proof = CloseAccountProof::new(source_sk, &balance); let proof = CloseAccountProof::new(source_sk, &balance);
CloseAccountData { CloseAccountData {
@ -74,7 +74,7 @@ impl CloseAccountProof {
Transcript::new(b"CloseAccountProof") Transcript::new(b"CloseAccountProof")
} }
pub fn new(source_sk: &ElGamalSK, balance: &ElGamalCiphertext) -> Self { pub fn new(source_sk: &ElGamalSecretKey, balance: &ElGamalCiphertext) -> Self {
let mut transcript = Self::transcript_new(); let mut transcript = Self::transcript_new();
// add a domain separator to record the start of the protocol // add a domain separator to record the start of the protocol

View File

@ -6,7 +6,7 @@ use {
use { use {
crate::{ crate::{
encryption::{ encryption::{
elgamal::{ElGamalCiphertext, ElGamalPubkey, ElGamalSK}, elgamal::{ElGamalCiphertext, ElGamalPubkey, ElGamalSecretKey},
pedersen::{Pedersen, PedersenBase, PedersenComm, PedersenDecHandle, PedersenOpen}, pedersen::{Pedersen, PedersenBase, PedersenComm, PedersenDecHandle, PedersenOpen},
}, },
errors::ProofError, errors::ProofError,
@ -38,7 +38,7 @@ impl TransferData {
spendable_balance: u64, spendable_balance: u64,
spendable_ct: ElGamalCiphertext, spendable_ct: ElGamalCiphertext,
source_pk: ElGamalPubkey, source_pk: ElGamalPubkey,
source_sk: &ElGamalSK, source_sk: &ElGamalSecretKey,
dest_pk: ElGamalPubkey, dest_pk: ElGamalPubkey,
auditor_pk: ElGamalPubkey, auditor_pk: ElGamalPubkey,
) -> Self { ) -> Self {
@ -234,7 +234,7 @@ impl TransferProofs {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
#[allow(clippy::many_single_char_names)] #[allow(clippy::many_single_char_names)]
pub fn new( pub fn new(
source_sk: &ElGamalSK, source_sk: &ElGamalSecretKey,
source_pk: &ElGamalPubkey, source_pk: &ElGamalPubkey,
dest_pk: &ElGamalPubkey, dest_pk: &ElGamalPubkey,
auditor_pk: &ElGamalPubkey, auditor_pk: &ElGamalPubkey,

View File

@ -6,7 +6,7 @@ use {
use { use {
crate::{ crate::{
encryption::{ encryption::{
elgamal::{ElGamalCiphertext, ElGamalPubkey, ElGamalSK}, elgamal::{ElGamalCiphertext, ElGamalPubkey, ElGamalSecretKey},
pedersen::PedersenBase, pedersen::PedersenBase,
}, },
errors::ProofError, errors::ProofError,
@ -55,9 +55,9 @@ impl UpdateAccountPkData {
current_balance: u64, current_balance: u64,
current_ct: ElGamalCiphertext, current_ct: ElGamalCiphertext,
current_pk: ElGamalPubkey, current_pk: ElGamalPubkey,
current_sk: &ElGamalSK, current_sk: &ElGamalSecretKey,
new_pk: ElGamalPubkey, new_pk: ElGamalPubkey,
new_sk: &ElGamalSK, new_sk: &ElGamalSecretKey,
) -> Self { ) -> Self {
let new_ct = new_pk.encrypt(current_balance); let new_ct = new_pk.encrypt(current_balance);
@ -105,8 +105,8 @@ impl UpdateAccountPkProof {
fn new( fn new(
current_balance: u64, current_balance: u64,
current_sk: &ElGamalSK, current_sk: &ElGamalSecretKey,
new_sk: &ElGamalSK, new_sk: &ElGamalSecretKey,
current_ct: &ElGamalCiphertext, current_ct: &ElGamalCiphertext,
new_ct: &ElGamalCiphertext, new_ct: &ElGamalCiphertext,
) -> Self { ) -> Self {

View File

@ -6,7 +6,7 @@ use {
use { use {
crate::{ crate::{
encryption::{ encryption::{
elgamal::{ElGamalCiphertext, ElGamalPubkey, ElGamalSK}, elgamal::{ElGamalCiphertext, ElGamalPubkey, ElGamalSecretKey},
pedersen::{PedersenBase, PedersenOpen}, pedersen::{PedersenBase, PedersenOpen},
}, },
errors::ProofError, errors::ProofError,
@ -43,7 +43,7 @@ impl WithdrawData {
pub fn new( pub fn new(
amount: u64, amount: u64,
source_pk: ElGamalPubkey, source_pk: ElGamalPubkey,
source_sk: &ElGamalSK, source_sk: &ElGamalSecretKey,
current_balance: u64, current_balance: u64,
current_balance_ct: ElGamalCiphertext, current_balance_ct: ElGamalCiphertext,
) -> Self { ) -> Self {
@ -96,7 +96,7 @@ impl WithdrawProof {
} }
pub fn new( pub fn new(
source_sk: &ElGamalSK, source_sk: &ElGamalSecretKey,
final_balance: u64, final_balance: u64,
final_balance_ct: &ElGamalCiphertext, final_balance_ct: &ElGamalCiphertext,
) -> Self { ) -> Self {