Skip to content

Commit

Permalink
Make digest an optional feature
Browse files Browse the repository at this point in the history
As proposed in dalek-cryptography#442 this makes `digest` an
optional feature that is not covered by the
SemVer public API stability guarantees.
  • Loading branch information
pinkforest committed Nov 30, 2022
1 parent 03b8668 commit 4d230f5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ major series.

## 4.x series

* Make `digest` an optional feature
* Migrate documentation to docs.rs hosted
* Fix backend documentation generation
* Deprecate `EdwardsPoint::hash_from_bytes` and rename it `EdwardsPoint::nonspect_map_to_curve`
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ harness = false
[dependencies]
cfg-if = "1"
rand_core = { version = "0.6", default-features = false }
digest = { version = "0.10", default-features = false }
digest = { version = "0.10", default-features = false, optional = true }
subtle = { version = "^2.2.1", default-features = false }
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
# The original packed_simd package was orphaned, see
Expand All @@ -58,6 +58,7 @@ nightly = ["subtle/nightly"]
default = ["std"]
std = ["alloc", "subtle/std", "rand_core/std"]
alloc = ["zeroize/alloc"]
digest = ["dep:digest"]

# fiat-crypto backend with formally-verified field arithmetic
fiat_backend = ["fiat-crypto"]
Expand Down
7 changes: 5 additions & 2 deletions src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ use core::ops::{Add, Neg, Sub};
use core::ops::{AddAssign, SubAssign};
use core::ops::{Mul, MulAssign};

#[cfg(feature = "digest")]
use digest::{generic_array::typenum::U64, Digest};

use subtle::Choice;
use subtle::ConditionallyNegatable;
use subtle::ConditionallySelectable;
Expand Down Expand Up @@ -534,6 +536,7 @@ impl EdwardsPoint {
CompressedEdwardsY(s)
}

#[cfg(feature = "digest")]
/// Maps the digest of the input bytes to the curve. This is NOT a hash-to-curve function, as
/// it produces points with a non-uniform distribution. Rather, it performs something that
/// resembles (but is not) half of the
Expand Down Expand Up @@ -1683,7 +1686,7 @@ mod test {
// https://github.com/signalapp/libsignal-protocol-c/ //
////////////////////////////////////////////////////////////

#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", feature = "digest"))]
fn test_vectors() -> Vec<Vec<&'static str>> {
vec![
vec![
Expand Down Expand Up @@ -1731,7 +1734,7 @@ mod test {

#[test]
#[allow(deprecated)]
#[cfg(feature = "alloc")]
#[cfg(all(feature = "alloc", feature = "digest"))]
fn elligator_signal_test_vectors() {
for vector in test_vectors().iter() {
let input = hex::decode(vector[0]).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern crate alloc;
#[macro_use]
extern crate std;

#[cfg(feature = "digest")]
pub use digest;

// Internal macros. Must come first!
Expand Down
4 changes: 4 additions & 0 deletions src/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ use core::ops::{Mul, MulAssign};

use rand_core::{CryptoRng, RngCore};

#[cfg(feature = "digest")]
use digest::generic_array::typenum::U64;
#[cfg(feature = "digest")]
use digest::Digest;

use crate::constants;
Expand Down Expand Up @@ -685,6 +687,7 @@ impl RistrettoPoint {
RistrettoPoint::from_uniform_bytes(&uniform_bytes)
}

#[cfg(feature = "digest")]
/// Hash a slice of bytes into a `RistrettoPoint`.
///
/// Takes a type parameter `D`, which is any `Digest` producing 64
Expand Down Expand Up @@ -722,6 +725,7 @@ impl RistrettoPoint {
RistrettoPoint::from_hash(hash)
}

#[cfg(feature = "digest")]
/// Construct a `RistrettoPoint` from an existing `Digest` instance.
///
/// Use this instead of `hash_from_bytes` if it is more convenient
Expand Down
33 changes: 7 additions & 26 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,9 @@
//! assert!(a == two);
//! ```
//!
//! There is also a constructor that reduces a \\(512\\)-bit integer,
//! [`Scalar::from_bytes_mod_order_wide`](struct.Scalar.html#method.from_bytes_mod_order_wide).
//!
//! To construct a `Scalar` as the hash of some input data, use
//! [`Scalar::hash_from_bytes`](struct.Scalar.html#method.hash_from_bytes),
//! which takes a buffer, or
//! [`Scalar::from_hash`](struct.Scalar.html#method.from_hash),
//! which allows an IUF API.
//!
//! ```
//! # fn main() {
//! use sha2::{Digest, Sha512};
//! use curve25519_dalek::scalar::Scalar;
//!
//! // Hashing a single byte slice
//! let a = Scalar::hash_from_bytes::<Sha512>(b"Abolish ICE");
//!
//! // Streaming data into a hash object
//! let mut hasher = Sha512::default();
//! hasher.update(b"Abolish ");
//! hasher.update(b"ICE");
//! let a2 = Scalar::from_hash(hasher);
//!
//! assert_eq!(a, a2);
//! # }
//! ```
//! See also `Scalar::hash_from_bytes` and `Scalar::from_hash` that
//! reduces a \\(512\\)-bit integer, if the optional `digest` feature
//! has been enabled.
//!
//! Finally, to create a `Scalar` with a specific bit-pattern
//! (e.g., for compatibility with X/Ed25519
Expand Down Expand Up @@ -154,7 +131,9 @@ use cfg_if::cfg_if;

use rand_core::{CryptoRng, RngCore};

#[cfg(feature = "digest")]
use digest::generic_array::typenum::U64;
#[cfg(feature = "digest")]
use digest::Digest;

use subtle::Choice;
Expand Down Expand Up @@ -591,6 +570,7 @@ impl Scalar {
Scalar::from_bytes_mod_order_wide(&scalar_bytes)
}

#[cfg(feature = "digest")]
/// Hash a slice of bytes into a scalar.
///
/// Takes a type parameter `D`, which is any `Digest` producing 64
Expand Down Expand Up @@ -620,6 +600,7 @@ impl Scalar {
Scalar::from_hash(hash)
}

#[cfg(feature = "digest")]
/// Construct a scalar from an existing `Digest` instance.
///
/// Use this instead of `hash_from_bytes` if it is more convenient
Expand Down

0 comments on commit 4d230f5

Please sign in to comment.