From e22ffba069ff265d93abb862dbeb6b3c0b230280 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Wed, 21 Jun 2023 06:07:40 +0900 Subject: [PATCH] [zk-token-sdk] Make inner scalar and ristretto point of `PedersenOpening` and `PedersenCommitment` private (#32187) * make `PedersenOpening` inner scalar private * make `PedersenCommitment` inner point private (cherry picked from commit 1616123b84c2b910ee08ebf01eb19c866312a10c) --- zk-token-sdk/src/encryption/elgamal.rs | 10 ++++++---- zk-token-sdk/src/encryption/pedersen.rs | 12 ++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/zk-token-sdk/src/encryption/elgamal.rs b/zk-token-sdk/src/encryption/elgamal.rs index 1900eb20e95896..0fd54805447a62 100644 --- a/zk-token-sdk/src/encryption/elgamal.rs +++ b/zk-token-sdk/src/encryption/elgamal.rs @@ -133,7 +133,7 @@ impl ElGamal { fn decrypt(secret: &ElGamalSecretKey, ciphertext: &ElGamalCiphertext) -> DiscreteLog { DiscreteLog::new( *G, - &ciphertext.commitment.0 - &(&secret.0 * &ciphertext.handle.0), + ciphertext.commitment.get_point() - &(&secret.0 * &ciphertext.handle.0), ) } @@ -520,7 +520,8 @@ pub struct ElGamalCiphertext { } impl ElGamalCiphertext { pub fn add_amount>(&self, amount: T) -> Self { - let commitment_to_add = PedersenCommitment(amount.into() * &(*G)); + let point = amount.into() * &(*G); + let commitment_to_add = PedersenCommitment::new(point); ElGamalCiphertext { commitment: &self.commitment + &commitment_to_add, handle: self.handle, @@ -528,7 +529,8 @@ impl ElGamalCiphertext { } pub fn subtract_amount>(&self, amount: T) -> Self { - let commitment_to_subtract = PedersenCommitment(amount.into() * &(*G)); + let point = amount.into() * &(*G); + let commitment_to_subtract = PedersenCommitment::new(point); ElGamalCiphertext { commitment: &self.commitment - &commitment_to_subtract, handle: self.handle, @@ -650,7 +652,7 @@ define_mul_variants!( pub struct DecryptHandle(RistrettoPoint); impl DecryptHandle { pub fn new(public: &ElGamalPubkey, opening: &PedersenOpening) -> Self { - Self(&public.0 * &opening.0) + Self(&public.0 * opening.get_scalar()) } pub fn get_point(&self) -> &RistrettoPoint { diff --git a/zk-token-sdk/src/encryption/pedersen.rs b/zk-token-sdk/src/encryption/pedersen.rs index e4aab0b17348e7..3a6db74a4eb0e1 100644 --- a/zk-token-sdk/src/encryption/pedersen.rs +++ b/zk-token-sdk/src/encryption/pedersen.rs @@ -67,8 +67,12 @@ impl Pedersen { /// Instances of Pedersen openings are zeroized on drop. #[derive(Clone, Debug, Default, Serialize, Deserialize, Zeroize)] #[zeroize(drop)] -pub struct PedersenOpening(pub(crate) Scalar); +pub struct PedersenOpening(Scalar); impl PedersenOpening { + pub fn new(scalar: Scalar) -> Self { + Self(scalar) + } + pub fn get_scalar(&self) -> &Scalar { &self.0 } @@ -163,8 +167,12 @@ define_mul_variants!( /// Pedersen commitment type. #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] -pub struct PedersenCommitment(pub(crate) RistrettoPoint); +pub struct PedersenCommitment(RistrettoPoint); impl PedersenCommitment { + pub fn new(point: RistrettoPoint) -> Self { + Self(point) + } + pub fn get_point(&self) -> &RistrettoPoint { &self.0 }