Skip to content

Commit 4a30ac7

Browse files
committed
Key types
1 parent 1b5fc1e commit 4a30ac7

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

fastcrypto/src/twisted_elgamal.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ lazy_static! {
1818
static ref H: RistrettoPoint = RistrettoPoint(PedersenGens::default().B_blinding);
1919
}
2020

21-
pub fn generate_keypair(rng: &mut impl AllowedRng) -> (RistrettoPoint, RistrettoScalar) {
22-
let sk = RistrettoScalar::rand(rng);
21+
pub struct PublicKey(RistrettoPoint);
22+
pub struct PrivateKey(RistrettoScalar);
23+
24+
pub fn generate_keypair(rng: &mut impl AllowedRng) -> (PublicKey, PrivateKey) {
25+
let sk = PrivateKey(RistrettoScalar::rand(rng));
2326
(pk_from_sk(&sk), sk)
2427
}
2528

26-
pub fn pk_from_sk(sk: &RistrettoScalar) -> RistrettoPoint {
27-
*H * sk.inverse().unwrap()
29+
pub fn pk_from_sk(sk: &PrivateKey) -> PublicKey {
30+
PublicKey(*H * sk.0.inverse().unwrap())
2831
}
2932

3033
// TODO: Encryptions of the same message can reuse commitments
@@ -36,14 +39,14 @@ pub struct Ciphertext {
3639

3740
impl Ciphertext {
3841
pub fn encrypt(
39-
public_key: &RistrettoPoint,
42+
public_key: &PublicKey,
4043
message: u32,
4144
rng: &mut impl AllowedRng,
4245
) -> (Self, Blinding) {
4346
let blinding = Blinding::rand(rng);
4447
(
4548
Self {
46-
decryption_handle: public_key * blinding.0,
49+
decryption_handle: public_key.0 * blinding.0,
4750
commitment: PedersenCommitment::from_blinding(
4851
&RistrettoScalar::from(message as u64),
4952
&blinding,
@@ -55,10 +58,10 @@ impl Ciphertext {
5558

5659
pub fn decrypt(
5760
&self,
58-
private_key: &RistrettoScalar,
61+
private_key: &PrivateKey,
5962
table: &HashMap<[u8; RISTRETTO_POINT_BYTE_LENGTH], u32>,
6063
) -> FastCryptoResult<u32> {
61-
let mut c = self.commitment.0 - self.decryption_handle * private_key;
64+
let mut c = self.commitment.0 - self.decryption_handle * private_key.0;
6265
for x_low in 0..1u32 << 16 {
6366
if let Some(x_high) = table.get(&c.to_byte_array()) {
6467
return Ok(x_low + (x_high << 16));
@@ -77,14 +80,14 @@ pub struct ZeroProof {
7780
}
7881

7982
impl ZeroProof {
80-
pub fn prove(ciphertext: &Ciphertext, sk: &RistrettoScalar, rng: &mut impl AllowedRng) -> Self {
83+
pub fn prove(ciphertext: &Ciphertext, sk: &PrivateKey, rng: &mut impl AllowedRng) -> Self {
8184
let y = RistrettoScalar::rand(rng);
8285
let pk = pk_from_sk(sk);
8386

84-
let y_p = pk * y;
87+
let y_p = pk.0 * y;
8588
let y_d = ciphertext.decryption_handle * y;
86-
let challenge = Self::challenge(ciphertext, &pk, &y_p, &y_d);
87-
let z = sk * challenge + y;
89+
let challenge = Self::challenge(ciphertext, &pk.0, &y_p, &y_d);
90+
let z = sk.0 * challenge + y;
8891
Self { y_p, y_d, z }
8992
}
9093

@@ -99,9 +102,9 @@ impl ZeroProof {
99102
)
100103
}
101104

102-
pub fn verify(&self, ciphertext: &Ciphertext, pk: &RistrettoPoint) -> FastCryptoResult<()> {
103-
let challenge = -Self::challenge(ciphertext, pk, &self.y_p, &self.y_d);
104-
if RistrettoPoint::multi_scalar_mul(&[self.z, challenge], &[*pk, *H]).unwrap() == self.y_p
105+
pub fn verify(&self, ciphertext: &Ciphertext, pk: &PublicKey) -> FastCryptoResult<()> {
106+
let challenge = -Self::challenge(ciphertext, &pk.0, &self.y_p, &self.y_d);
107+
if RistrettoPoint::multi_scalar_mul(&[self.z, challenge], &[pk.0, *H]).unwrap() == self.y_p
105108
&& RistrettoPoint::multi_scalar_mul(
106109
&[self.z, challenge],
107110
&[ciphertext.decryption_handle, ciphertext.commitment.0],
@@ -128,7 +131,7 @@ impl EqualityProof {
128131
ciphertext: &Ciphertext,
129132
other_commitment: &PedersenCommitment,
130133
other_blinding: &Blinding,
131-
sk: &RistrettoScalar,
134+
sk: &PrivateKey,
132135
rng: &mut impl AllowedRng,
133136
) -> Self {
134137
let pk = pk_from_sk(sk);
@@ -139,16 +142,16 @@ impl EqualityProof {
139142
);
140143

141144
let y = (
142-
pk * r.0,
145+
&pk.0 * r.0,
143146
RistrettoPoint::multi_scalar_mul(&[r.1, r.0], &[*G, ciphertext.decryption_handle])
144147
.unwrap(),
145148
RistrettoPoint::multi_scalar_mul(&[r.1, r.2], &[*G, *H]).unwrap(),
146149
);
147150

148-
let challenge = Self::challenge(ciphertext, other_commitment, &pk, &y);
151+
let challenge = Self::challenge(ciphertext, other_commitment, &pk.0, &y);
149152

150153
let z = (
151-
challenge * sk + r.0,
154+
challenge * sk.0 + r.0,
152155
challenge * value + r.1,
153156
challenge * other_blinding.0 + r.2,
154157
);
@@ -171,12 +174,12 @@ impl EqualityProof {
171174
&self,
172175
ciphertext: &Ciphertext,
173176
other_commitment: &PedersenCommitment,
174-
pk: &RistrettoPoint,
177+
pk: &PublicKey,
175178
) -> FastCryptoResult<()> {
176-
let challenge = -Self::challenge(ciphertext, other_commitment, pk, &self.y);
179+
let challenge = -Self::challenge(ciphertext, other_commitment, &pk.0, &self.y);
177180
if self.y
178181
== (
179-
RistrettoPoint::multi_scalar_mul(&[self.z.0, challenge], &[*pk, *H]).unwrap(),
182+
RistrettoPoint::multi_scalar_mul(&[self.z.0, challenge], &[pk.0, *H]).unwrap(),
180183
RistrettoPoint::multi_scalar_mul(
181184
&[self.z.1, self.z.0, challenge],
182185
&[*G, ciphertext.decryption_handle, ciphertext.commitment.0],

0 commit comments

Comments
 (0)