Skip to content

Commit

Permalink
Add PrimeFieldBits support to Scalar (dalek-cryptography#579)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Rosenberg <[email protected]>
Co-authored-by: pinkforest(she/her) <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2023
1 parent 533b53a commit 76a8b2a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions curve25519-dalek/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ major series.

## 4.x series

### Unreleased

* Add implementation for `PrimeFieldBits`, behind the `group-bits` feature flag.

### 4.1.1

* Mark `constants::BASEPOINT_ORDER` deprecated from pub API
Expand Down
4 changes: 3 additions & 1 deletion curve25519-dalek/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rustdoc-args = [
"--html-in-header", "docs/assets/rustdoc-include-katex-header.html",
"--cfg", "docsrs",
]
features = ["serde", "rand_core", "digest", "legacy_compatibility", "group"]
features = ["serde", "rand_core", "digest", "legacy_compatibility", "group-bits"]

[dev-dependencies]
sha2 = { version = "0.10", default-features = false }
Expand All @@ -48,6 +48,7 @@ required-features = ["alloc", "rand_core"]

[dependencies]
cfg-if = "1"
ff = { version = "0.13", default-features = false, optional = true }
group = { version = "0.13", default-features = false, optional = true }
rand_core = { version = "0.6.4", default-features = false, optional = true }
digest = { version = "0.10", default-features = false, optional = true }
Expand All @@ -67,6 +68,7 @@ alloc = ["zeroize?/alloc"]
precomputed-tables = []
legacy_compatibility = []
group = ["dep:group", "rand_core"]
group-bits = ["group", "ff/bits"]

[target.'cfg(all(not(curve25519_dalek_backend = "fiat"), not(curve25519_dalek_backend = "serial"), target_arch = "x86_64"))'.dependencies]
curve25519-dalek-derive = { version = "0.1", path = "../curve25519-dalek-derive" }
15 changes: 15 additions & 0 deletions curve25519-dalek/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ use core::ops::{Sub, SubAssign};

use cfg_if::cfg_if;

#[cfg(feature = "group-bits")]
use group::ff::{FieldBits, PrimeFieldBits};
#[cfg(feature = "group")]
use {
group::ff::{Field, FromUniformBytes, PrimeField},
Expand Down Expand Up @@ -1321,6 +1323,19 @@ impl PrimeField for Scalar {
};
}

#[cfg(feature = "group-bits")]
impl PrimeFieldBits for Scalar {
type ReprBits = [u8; 32];

fn to_le_bits(&self) -> FieldBits<Self::ReprBits> {
self.to_repr().into()
}

fn char_le_bits() -> FieldBits<Self::ReprBits> {
constants::BASEPOINT_ORDER_PRIVATE.to_bytes().into()
}
}

#[cfg(feature = "group")]
impl FromUniformBytes<64> for Scalar {
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
Expand Down

0 comments on commit 76a8b2a

Please sign in to comment.