Skip to content

Commit 7529328

Browse files
committed
Remove custom Serde implementation
1 parent 6b74a6c commit 7529328

File tree

3 files changed

+59
-101
lines changed

3 files changed

+59
-101
lines changed

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ p256_ = { package = "p256", version = "0.9", default-features = false, features
4646
"zeroize",
4747
], optional = true }
4848
rand_core = { version = "0.6", default-features = false }
49-
serde = { version = "1", default-features = false, optional = true }
49+
serde = { version = "1", default-features = false, features = [
50+
"derive",
51+
], optional = true }
5052
subtle = { version = "2.3", default-features = false }
5153
zeroize = { version = "1", default-features = false }
5254

src/serialization.rs

-69
Original file line numberDiff line numberDiff line change
@@ -179,72 +179,3 @@ impl<G: Group, H: BlockInput + Digest> EvaluationElement<G, H> {
179179
})
180180
}
181181
}
182-
183-
/////////////////////////////////////////////
184-
// Serde implementation for High-Level API //
185-
// ======================================= //
186-
/////////////////////////////////////////////
187-
188-
/// Macro used for deriving `serde`'s `Serialize` and `Deserialize` traits.
189-
macro_rules! impl_serialize_and_deserialize_for {
190-
($item:ident$( where $($path:ty: $bound1:path $(| $bound2:path)*),+$(,)?)?) => {
191-
#[cfg(feature = "serde")]
192-
impl<G: Group, H: BlockInput + Digest> serde::Serialize for $item<G, H>
193-
$(where
194-
$($path: $bound1 $(+ $bound2)*),+
195-
)?
196-
{
197-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
198-
where
199-
S: serde::Serializer,
200-
{
201-
serializer.serialize_bytes(&self.serialize())
202-
}
203-
}
204-
205-
#[cfg(feature = "serde")]
206-
impl<'de, G: Group, H: BlockInput + Digest> serde::Deserialize<'de> for $item<G, H> {
207-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
208-
where
209-
D: serde::Deserializer<'de>,
210-
{
211-
use serde::de::Error;
212-
213-
struct ByteVisitor<G: Group, H: BlockInput + Digest>(core::marker::PhantomData<(G, H)>);
214-
215-
impl<'de, G: Group, H: BlockInput + Digest> serde::de::Visitor<'de> for ByteVisitor<G, H> {
216-
type Value = $item<G, H>;
217-
218-
fn expecting(
219-
&self,
220-
formatter: &mut core::fmt::Formatter,
221-
) -> core::fmt::Result {
222-
formatter.write_str(core::concat!(
223-
"the byte representation of a ",
224-
core::stringify!($item)
225-
))
226-
}
227-
228-
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
229-
where
230-
E: Error,
231-
{
232-
$item::<G, H>::deserialize(value).map_err(|_| {
233-
Error::invalid_value(
234-
serde::de::Unexpected::Bytes(value),
235-
&core::concat!(
236-
"invalid byte sequence for ",
237-
core::stringify!($item)
238-
),
239-
)
240-
})
241-
}
242-
}
243-
244-
deserializer
245-
.deserialize_bytes(ByteVisitor::<G, H>(core::marker::PhantomData))
246-
.map_err(Error::custom)
247-
}
248-
}
249-
};
250-
}

src/voprf.rs

+56-31
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use generic_array::typenum::{U1, U11, U2, U20};
2020
use generic_array::GenericArray;
2121
use rand_core::{CryptoRng, RngCore};
2222
use subtle::ConstantTimeEq;
23-
#[cfg(feature = "serde")]
24-
use ::{core::ops::Add, generic_array::typenum::Sum, generic_array::ArrayLength};
2523

2624
use crate::errors::InternalError;
2725
use crate::group::Group;
@@ -58,110 +56,137 @@ enum Mode {
5856
#[derive(DeriveWhere)]
5957
#[derive_where(Clone, Zeroize(drop))]
6058
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G::Scalar)]
59+
#[cfg_attr(
60+
feature = "serde",
61+
derive(serde::Deserialize, serde::Serialize),
62+
serde(bound(
63+
deserialize = "G::Scalar: serde::Deserialize<'de>",
64+
serialize = "G::Scalar: serde::Serialize"
65+
))
66+
)]
6167
pub struct NonVerifiableClient<G: Group, H: BlockInput + Digest> {
6268
pub(crate) blind: G::Scalar,
6369
#[derive_where(skip(Zeroize))]
6470
pub(crate) hash: PhantomData<H>,
6571
}
6672

67-
impl_serialize_and_deserialize_for!(NonVerifiableClient);
68-
6973
/// A client which engages with a [VerifiableServer] in verifiable mode, meaning
7074
/// that the OPRF outputs can be checked against a server public key.
7175
#[derive(DeriveWhere)]
7276
#[derive_where(Clone, Zeroize(drop))]
7377
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G, G::Scalar)]
78+
#[cfg_attr(
79+
feature = "serde",
80+
derive(serde::Deserialize, serde::Serialize),
81+
serde(bound(
82+
deserialize = "G::Scalar: serde::Deserialize<'de>, G: serde::Deserialize<'de>",
83+
serialize = "G::Scalar: serde::Serialize, G: serde::Serialize"
84+
))
85+
)]
7486
pub struct VerifiableClient<G: Group, H: BlockInput + Digest> {
7587
pub(crate) blind: G::Scalar,
7688
pub(crate) blinded_element: G,
7789
#[derive_where(skip(Zeroize))]
7890
pub(crate) hash: PhantomData<H>,
7991
}
8092

81-
impl_serialize_and_deserialize_for!(
82-
VerifiableClient
83-
where
84-
G::ScalarLen: Add<G::ElemLen>,
85-
Sum<G::ScalarLen, G::ElemLen>: ArrayLength<u8>,
86-
);
87-
8893
/// A server which engages with a [NonVerifiableClient] in base mode, meaning
8994
/// that the OPRF outputs are not verifiable.
9095
#[derive(DeriveWhere)]
9196
#[derive_where(Clone, Zeroize(drop))]
9297
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G::Scalar)]
98+
#[cfg_attr(
99+
feature = "serde",
100+
derive(serde::Deserialize, serde::Serialize),
101+
serde(bound(
102+
deserialize = "G::Scalar: serde::Deserialize<'de>",
103+
serialize = "G::Scalar: serde::Serialize"
104+
))
105+
)]
93106
pub struct NonVerifiableServer<G: Group, H: BlockInput + Digest> {
94107
pub(crate) sk: G::Scalar,
95108
#[derive_where(skip(Zeroize))]
96109
pub(crate) hash: PhantomData<H>,
97110
}
98111

99-
impl_serialize_and_deserialize_for!(NonVerifiableServer);
100-
101112
/// A server which engages with a [VerifiableClient] in verifiable mode, meaning
102113
/// that the OPRF outputs can be checked against a server public key.
103114
#[derive(DeriveWhere)]
104115
#[derive_where(Clone, Zeroize(drop))]
105116
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G, G::Scalar)]
117+
#[cfg_attr(
118+
feature = "serde",
119+
derive(serde::Deserialize, serde::Serialize),
120+
serde(bound(
121+
deserialize = "G::Scalar: serde::Deserialize<'de>, G: serde::Deserialize<'de>",
122+
serialize = "G::Scalar: serde::Serialize, G: serde::Serialize"
123+
))
124+
)]
106125
pub struct VerifiableServer<G: Group, H: BlockInput + Digest> {
107126
pub(crate) sk: G::Scalar,
108127
pub(crate) pk: G,
109128
#[derive_where(skip(Zeroize))]
110129
pub(crate) hash: PhantomData<H>,
111130
}
112131

113-
impl_serialize_and_deserialize_for!(
114-
VerifiableServer
115-
where
116-
G::ScalarLen: Add<G::ElemLen>,
117-
Sum<G::ScalarLen, G::ElemLen>: ArrayLength<u8>,
118-
);
119-
120132
/// A proof produced by a [VerifiableServer] that the OPRF output matches
121133
/// against a server public key.
122134
#[derive(DeriveWhere)]
123135
#[derive_where(Clone, Zeroize(drop))]
124136
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G::Scalar)]
137+
#[cfg_attr(
138+
feature = "serde",
139+
derive(serde::Deserialize, serde::Serialize),
140+
serde(bound(
141+
deserialize = "G::Scalar: serde::Deserialize<'de>",
142+
serialize = "G::Scalar: serde::Serialize"
143+
))
144+
)]
125145
pub struct Proof<G: Group, H: BlockInput + Digest> {
126146
pub(crate) c_scalar: G::Scalar,
127147
pub(crate) s_scalar: G::Scalar,
128148
#[derive_where(skip(Zeroize))]
129149
pub(crate) hash: PhantomData<H>,
130150
}
131151

132-
impl_serialize_and_deserialize_for!(
133-
Proof
134-
where
135-
G::ScalarLen: Add<G::ScalarLen>,
136-
Sum<G::ScalarLen, G::ScalarLen>: ArrayLength<u8>,
137-
);
138-
139152
/// The first client message sent from a client (either verifiable or not) to a
140153
/// server (either verifiable or not).
141154
#[derive(DeriveWhere)]
142155
#[derive_where(Clone, Zeroize(drop))]
143156
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G)]
157+
#[cfg_attr(
158+
feature = "serde",
159+
derive(serde::Deserialize, serde::Serialize),
160+
serde(bound(
161+
deserialize = "G: serde::Deserialize<'de>",
162+
serialize = "G: serde::Serialize"
163+
))
164+
)]
144165
pub struct BlindedElement<G: Group, H: BlockInput + Digest> {
145166
pub(crate) value: G,
146167
#[derive_where(skip(Zeroize))]
147168
pub(crate) hash: PhantomData<H>,
148169
}
149170

150-
impl_serialize_and_deserialize_for!(BlindedElement);
151-
152171
/// The server's response to the [BlindedElement] message from a client (either
153172
/// verifiable or not) to a server (either verifiable or not).
154173
#[derive(DeriveWhere)]
155174
#[derive_where(Clone, Zeroize(drop))]
156175
#[derive_where(Debug, Eq, Hash, Ord, PartialEq, PartialOrd; G)]
176+
#[cfg_attr(
177+
feature = "serde",
178+
derive(serde::Deserialize, serde::Serialize),
179+
serde(bound(
180+
deserialize = "G: serde::Deserialize<'de>",
181+
serialize = "G: serde::Serialize"
182+
))
183+
)]
157184
pub struct EvaluationElement<G: Group, H: BlockInput + Digest> {
158185
pub(crate) value: G,
159186
#[derive_where(skip(Zeroize))]
160187
pub(crate) hash: PhantomData<H>,
161188
}
162189

163-
impl_serialize_and_deserialize_for!(EvaluationElement);
164-
165190
/////////////////////////
166191
// API Implementations //
167192
// =================== //

0 commit comments

Comments
 (0)