1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use core::fmt::{Debug, Formatter};
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_TABLE;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Deserializer, Serialize, de::Visitor};
use crate::{Ciphertext, EncryptionKey};
use crate::util::random_scalar;
#[derive(Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize))]
pub struct DecryptionKey {
pub(crate) secret: Scalar,
#[cfg_attr(feature = "enable-serde", serde(skip_serializing))]
pub(crate) ek: EncryptionKey,
}
impl DecryptionKey {
pub fn new<R: RngCore + CryptoRng>(mut rng: R) -> Self {
let secret = random_scalar(&mut rng);
let ek = EncryptionKey(&secret * &RISTRETTO_BASEPOINT_TABLE);
Self { secret, ek }
}
pub fn decrypt(&self, ct: Ciphertext) -> RistrettoPoint {
ct.1 - ct.0 * &self.secret
}
pub fn encryption_key(&self) -> &EncryptionKey {
&self.ek
}
}
impl Debug for DecryptionKey {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "DecryptionKey({:?})", self.secret)
}
}
impl From<Scalar> for DecryptionKey {
fn from(secret: Scalar) -> Self {
let ek = EncryptionKey(&secret * &RISTRETTO_BASEPOINT_TABLE);
Self { secret, ek }
}
}
impl AsRef<Scalar> for DecryptionKey {
fn as_ref(&self) -> &Scalar {
&self.secret
}
}
#[cfg(feature = "enable-serde")]
impl<'de> Deserialize<'de> for DecryptionKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
struct DecryptionKeyVisitor;
impl<'de> Visitor<'de> for DecryptionKeyVisitor {
type Value = DecryptionKey;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("a valid ElGamal decryption key")
}
fn visit_seq<A>(self, mut seq: A) -> Result<DecryptionKey, A::Error>
where A: serde::de::SeqAccess<'de>
{
let secret = seq.next_element()?
.ok_or(serde::de::Error::invalid_length(0, &"expected decryption key (32 bytes)"))?;
let ek = EncryptionKey(&secret * &RISTRETTO_BASEPOINT_TABLE);
Ok(DecryptionKey { secret, ek })
}
}
deserializer.deserialize_tuple(32, DecryptionKeyVisitor)
}
}
#[cfg(feature = "enable-serde")]
#[cfg(test)]
mod tests {
use rand::prelude::StdRng;
use rand_core::SeedableRng;
use crate::DecryptionKey;
#[test]
fn serde_decryption_key() {
const N: usize = 100;
let mut rng = StdRng::from_entropy();
for _ in 0..N {
let dk = DecryptionKey::new(&mut rng);
let encoded = bincode::serialize(&dk).unwrap();
assert_eq!(encoded.len(), 32);
let decoded = bincode::deserialize(&encoded).unwrap();
assert_eq!(dk, decoded);
}
}
}