Skip to content

Commit

Permalink
p521: serde feature (#962)
Browse files Browse the repository at this point in the history
Adds `serde::{Serialize, Deserialize}` impls for `AffinePoint` and
`Scalar`.
  • Loading branch information
tarcieri authored Nov 10, 2023
1 parent c3deb5a commit d128d2d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion p521/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ecdsa-core = { version = "0.16.5", package = "ecdsa", optional = true, default-f
hex-literal = { version = "0.4", optional = true }
primeorder = { version = "0.13.3", optional = true, path = "../primeorder" }
rand_core = { version = "0.6", optional = true, default-features = false }
serdect = { version = "0.2", optional = true, default-features = false }
sha2 = { version = "0.10", optional = true, default-features = false }

[dev-dependencies]
Expand All @@ -45,7 +46,8 @@ ecdsa = ["arithmetic", "ecdsa-core/signing", "ecdsa-core/verifying", "sha512"]
getrandom = ["rand_core/getrandom"]
jwk = ["elliptic-curve/jwk"]
pem = ["elliptic-curve/pem", "pkcs8"]
pkcs8 = ["elliptic-curve/pkcs8"]
pkcs8 = ["ecdsa-core?/pkcs8", "elliptic-curve/pkcs8"]
serde = ["ecdsa-core?/serde", "elliptic-curve/serde", "primeorder?/serde", "serdect"]
sha512 = ["digest", "dep:sha2"]
test-vectors = ["dep:hex-literal"]
voprf = ["elliptic-curve/voprf", "dep:sha2"]
Expand Down
20 changes: 20 additions & 0 deletions p521/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,26 @@ impl TryFrom<U576> for Scalar {
}
}

#[cfg(feature = "serde")]
impl Serialize for Scalar {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
ScalarPrimitive::from(self).serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Scalar {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
Ok(ScalarPrimitive::deserialize(deserializer)?.into())
}
}

#[cfg(test)]
mod tests {
use super::Scalar;
Expand Down
10 changes: 10 additions & 0 deletions p521/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
unused_qualifications
)]

//! ## `serde` support
//!
//! When the `serde` feature of this crate is enabled, `Serialize` and
//! `Deserialize` are impl'd for the following types:
//!
//! - [`AffinePoint`]
//! - [`Scalar`]
//!
//! Please see type-specific documentation for more information.

#[cfg(feature = "arithmetic")]
mod arithmetic;

Expand Down

0 comments on commit d128d2d

Please sign in to comment.