Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PrimeFieldBits support to Scalar #579

Merged
merged 11 commits into from
Sep 20, 2023
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.0

* Add arbitrary integer multiplication with `MontgomeryPoint::mul_bits_be`
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"]
kayabaNerve marked this conversation as resolved.
Show resolved Hide resolved

[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.to_bytes().into()
rozbb marked this conversation as resolved.
Show resolved Hide resolved
}
}

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