Commit graph

77 commits

Author SHA1 Message Date
daxpedda
44433757a0
Add fixed-array Scalar batch inversion (#789) 2025-08-26 20:54:09 +02:00
Iñigo Querejeta Azurmendi
015707ab4e
Add proper hash_to_curve. (#786)
* Rename hash_to_curve as encode_to_curve

* Implement the inline description of the standard.

* Generalise map_to_field to return an arbitrary number of field elements.

* Implement hash_to_curve as defined in the standard.

* Put elligator behind the "digest" feature.

* Add warning on non-uniformity of `encode_to_curve`.

* Remove the need of Vec for hash_to_field.

* Apply suggestions from code review

* Refactor expand_message_xmd out of hash_to_field

* Add hash-to-curve to benches

* Constraint COUNT to 1 or 2; add note on secure hash function usage

* Correct hash function usage in encode- and hash-to-curve

---------

Co-authored-by: Armando Faz <armfazh@users.noreply.github.com>
Co-authored-by: Michael Rosenberg <mrosenberg@cloudflare.com>
2025-08-26 20:51:39 +02:00
Tony Arcieri
fc8815721c
Scalar::div_by_2 (#805)
* [WIP] Scalar::div_by_2

* debug_assert that carry is 0

* revise tests

* Test multiply by half scalar, double and compress (#804)

* Test `div_by_2` with `proptest` (#806)

---------

Co-authored-by: daxpedda <daxpedda@gmail.com>
2025-08-22 20:45:51 +02:00
yumeiyin
1ad4603e0a
chore: fix some minor issues in sage comments (#788) 2025-08-08 16:13:28 -04:00
Arthur Gautier
246723eefe
workspace: use a global patch.crates-io to avoid duplicates (#793)
When pulling crates via git (to grab non-released yet fixes or
otherwise), the `path = "../"` in each crate crates duplicates unless
you pull the whole tree in your local `[patch.crates-io]`.

This creates issues in downstream packaging (nix, ...) where a crate
version can only appear once.
Those are somewhat difficult to diagnose.

Using a `[patch.crates-io]` in the workspace serves the same purpose but
does not create the duplication in consumers' tree.
2025-07-18 08:26:13 -06:00
Tony Arcieri
c6e32a2c7a
curve: add yanking notes for v4.2.0 (#794)
Yanked due to #785
2025-07-17 21:52:40 -04:00
Michael Rosenberg
fc8a8a5276
Cut pre.0 prereleases (#784) 2025-07-09 19:04:17 +02:00
Michael Rosenberg
6a515e60a8
Update all READMEs and CHANGELOGs (#783)
* Update changelogs and readmes

* Fix missing/wrong features in readmes

* ed: Remove std entirely

* ed: Fix deprecated warnings in bench

* Document removing std from ed
2025-07-08 21:46:55 -04:00
Aaron Feickert
55e3e00d97
curve: Remove unnecessary CofactorGroup documentation (#683) 2025-07-07 21:42:31 -04:00
Michael Rosenberg
86832640ed
curve: Undeprecate Scalar::from_bits (#780) 2025-07-07 17:15:06 -04:00
Aaron Feickert
9e4ec01fa2
curve: Use constant-time compressed edwards equality testing (#684) 2025-07-07 23:02:51 +02:00
Aaron Feickert
d54b196d18
curve: Use constant-time compressed equality testing (#669) 2025-07-07 23:01:55 +02:00
Michael Rosenberg
445fd7d59d
curve,x: Clear deprecated functions (#778) 2025-07-07 22:33:05 +02:00
Arthur Gautier
a99efe2304
curve,ed,x: Bump rand_core to 0.9 (#777)
---------

Co-authored-by: pinkforest <36498018+pinkforest@users.noreply.github.com>
2025-07-07 15:36:11 -04:00
Tony Arcieri
a9aa94736b
Bump digest, ed25519, signature, and sha2 (#676)
Bumps the aforementioned dependencies to their latest (pre)releases.
2025-07-07 11:34:26 -06:00
Tony Arcieri
53df025bb3
Bump edition to 2024 and crate versions to prereleases (#775)
This represents the first breaking change in a new release series,
bumping all crates to the 2024 edition of Rust.

As such, the version numbers of all crates have been incremented to
represent a new prerelease series:

- `curve25519-dalek`: v5.0.0-pre
- `ed25519-dalek`: v3.0.0-pre
- `x25519-dalek`: v3.0.0-pre

Note that this commit isn't intended to cut associated crate releases of
these on crates.io, but is merely bumping the version numbers to denote
there are pending breaking changes.

This commit also includes rustfmt changes which were made as part of the
2024 edition.

Also includes clippy fixes.
2025-07-07 11:52:25 -04:00
Iñigo Querejeta Azurmendi
25a9dbb811
curve: Hash to curve and field as defined in the standard (#377)
* Implementation of `hash_to_field` as defined in the standard
* Implementation of `hash_to_curve` as defined in the standard, by changing the mechanism over which we chose the sign.
* For the point above, had to change the `elligator_encode` to return whether `eps` is a square or not (required for `hash_to_curve`).
* Included test vectors of the draft.
* Included `FieldElement::from_bytes_wide(bytes: &u8; 64])` to reduce integers encoded in 64 bytes.
2025-07-05 22:01:26 +02:00
Tony Arcieri
655992f3c2
curve: fix nightly build (#773)
The latest nightlies have stabilized `stdarch_x86_avx512` so having it
present is now an error
2025-06-15 12:56:10 -04:00
Tony Arcieri
8c53a8f10b
curve: extract AffinePoint type (#769)
* curve: extract `AffinePoint` type

Based on discussions about `elliptic-curve` trait impls in #746, and
observing a similar type in `ed448-goldilocks` which inspired this one
(not to mention in all of the @RustCrypto elliptic curve crates), adds
an `AffinePoint` type with `x` and `y` coordinates.

For now, the type is kept out of the public API, and used as an
implementation detail for point compression. However, it's been written
with the intent of eventually stabilizing and exposing it. It's been
marked `pub` so unused functionality doesn't automatically trigger dead
code lints.

Further work could include refactoring point decompression to first
produce an `AffinePoint` and then convert to extended twisted Edwards
coordinates (i.e. `EdwardsPoint`), which is more or less what the
existing `step_1` and `step_2` functions do (`step_1` technically
produces projective coordinates, but `Z` is always set to `ONE`).

* Update curve25519-dalek/src/edwards.rs
2025-06-08 01:23:59 -04:00
Tony Arcieri
cf7b099585
curve: rename FieldElement*::as_bytes => ::to_bytes (#767)
* curve: rename `FieldElement*::as_bytes` => `::to_bytes`

Methods named `as_*` should perform a zero-cost  borrowing conversion:

https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv

Methods named `to_*` can perform an expensive owned conversion.

Since the `FieldElement*` types are technically part of the public API
(but feature gated), this also preserves the old names with a
deprecation. We can remove them in the next breaking release.

The same change was also made to the backend `Scalar*` types, however
these types are not a part of the public API.
2025-06-06 15:10:29 -06:00
Tony Arcieri
ad4a37df53
CI: bump clippy to Rust 1.87 (#768)
* CI: bump `clippy` to Rust 1.87

Performs a `cargo clippy --fix`

* ed25519: fix warning

* Rename solitary `'b` lifetimes to `'a`
2025-06-06 18:30:32 +02:00
Tony Arcieri
5e0b429b05
Add typos action and correct typos (#766)
* Add `typos` action and correct typos

https://github.com/crate-ci/typos

* Add `name` to `typos` job
2025-06-04 17:40:10 +02:00
root
83ddc4b34b
fix typos (#740) 2025-06-04 10:43:16 -04:00
Andrew Poelstra
dcd39743ea
curve25519-dalek: add batch montgomery conversion (#722) 2025-06-02 16:02:43 -06:00
Tony Arcieri
dd5bd108d6
curve: add EdwardsPoint::compress_batch and inherent ::random (#759)
* curve: add `EdwardsPoint::compress_batch` and `::random`

We've had various requests to implement batch point compression for
`EdwardsPoint`, e.g. #705.

We can leverage `FieldElement::batch_invert` to implement it, which
results in a fairly significant speedup.

The name `EdwardsPoint::compress_batch` has been chosen to match
`RistrettoPoint::double_and_compress_batch`.

For benchmarking, randomized `EdwardsPoint`s have been used. To obtain
these, an inherent `EdwardsPoint::random` has been extracted from the
existing `Group::random` implementation, which uses rejection sampling.
`Group::random` has been updated to call the inherent
`EdwardsPoint::random`. This avoids a `group` dependency just to run the
batch compression benchmarks.

The following benchmark results have been obtained:

edwards benches/EdwardsPoint compression
                        time:   [3.5029 µs 3.5098 µs 3.5171 µs]

edwards benches/Batch EdwardsPoint compression/1
                        time:   [3.6698 µs 3.6758 µs 3.6817 µs]
edwards benches/Batch EdwardsPoint compression/2
                        time:   [3.8410 µs 3.8461 µs 3.8516 µs]
edwards benches/Batch EdwardsPoint compression/4
                        time:   [4.1534 µs 4.1961 µs 4.2558 µs]
edwards benches/Batch EdwardsPoint compression/8
                        time:   [4.8466 µs 4.8533 µs 4.8600 µs]
edwards benches/Batch EdwardsPoint compression/16
                        time:   [6.1216 µs 6.1315 µs 6.1410 µs]

As you can see, it affords a fairly significant speedup, batch
compressing 16 points in less time than the standard point compression
algorithm would take to compress 2 in a row.
2025-05-28 00:09:49 -04:00
Tony Arcieri
67625763c1
curve: remove feature(avx512_target_feature) (#757)
The build is currently broken because it's been stabilized:

    error: the feature `avx512_target_feature` has been stable since 1.89.0-nightly and no longer requires an attribute to enable
2025-05-25 20:21:55 -04:00
Arthur Gautier
8c0cf3a64f
chore(deps): bump cpufeatures from 0.2.12 to 0.2.17 (#750)
This fixes warnings like:
```
warning: unexpected `cfg` condition value: ``
  --> curve25519-dalek/src/backend/mod.rs:58:9
   |
58 |         cpufeatures::new!(cpuid_avx512, "avx512ifma", "avx512vl");
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
2025-05-08 14:06:28 -06:00
Arthur Gautier
773a0646ce
fixup ci: unsafe usage on nightly and bump ed25519's MSRV (#753)
* ed25519-dalek: bump MSRV to 1.81

base64ct now requires rustc 1.81

* nightly and stable disagree on usage of unsafe

```
error: unnecessary `unsafe` block
492
   --> curve25519-dalek/src/backend/vector/avx2/field.rs:479:28
493
    |
494
479 |         let c9_19: u32x8 = unsafe {
495
    |                            ^^^^^^ unnecessary `unsafe` block
```

This would only happen on nightly.
2025-05-08 12:37:08 -06:00
Aaron Feickert
868d614020
Support zeroizing for SubgroupPoint (#682) 2025-01-19 11:54:30 -05:00
camcui
6a0caa1b98
chore: fix spelling issues (#727)
Signed-off-by: camcui <cuishua@sina.cn>
2025-01-18 16:34:18 -07:00
Aaron Feickert
485ffab769
docs: update VartimeRistrettoPrecomputation documentation (#667) 2025-01-11 23:07:09 -05:00
Aaron Feickert
ed83542d7e
curve: add precomputation length to MSM structs (#685) 2025-01-11 22:57:50 -05:00
Arthur Gautier
cbf794d883
{curve,ed}25519-dalek: clippy fixes (#710)
Clippy 1.81 brings new lints, this fixes those warnings
2024-09-30 15:09:28 -06:00
pinkforest(she/her)
b636fb8ee4
Make AVX512IFMA opt-in backend (#695)
* Make AVX512IFMA opt-in backend

* Updated README to have backend info

* Added entry to changelog

---------

Co-authored-by: Michael Rosenberg <michael@mrosenberg.pub>
2024-09-08 01:13:36 -04:00
Aaron Feickert
0964f800ab
curve: Support MSM #static scalars <= #static points (#668) 2024-07-30 09:43:13 -04:00
Aaron Feickert
83a57e591f
curve: Impl Default ConstantTImeEq and ConditionallySelectable for SubgroupPoint (#672) 2024-07-30 02:05:52 -04:00
Elichai Turkel
79ab6c29bd
curve: Implement ConditionallySelectable for MontgomeryPoint (#677) 2024-07-30 01:51:44 -04:00
Aaron Feickert
a7a9fffdc9
Minor documentation fixes (#671) 2024-07-30 01:11:26 -04:00
Simon Wülker
35e78b21ef
Enable unexpected cfgs lint (#656)
Previously, the only way to configure this lint was using a build.rs
file, but now it can be done using Cargo.toml as well.
2024-07-25 20:24:39 -06:00
Tony Arcieri
921bd7ced0
curve: use subtle::Choice for constant-time fixes (#665)
Alternative to #659/#661 and #662 which leverages `subtle::Choice` and
`subtle::ConditionallySelectable` as the optimization barriers.

Really the previous masking was there to conditionally add the scalar
field modulus on underflow, so instead of that, we can conditionally
select zero or the modulus using a `Choice` constructed from the
underflow bit.
2024-07-17 12:05:46 -06:00
Tony Arcieri
5b7082bbc8
curve: use subtle::BlackBox optimization barrier (#662)
Replaces the security mitigation added in #659 and #661 for
masking-related timing variability which used an inline `black_box`
using the recently added `subtle::BlackBox` newtype (see
dalek-cryptography/subtle#123)

Internally `BlackBox` uses a volatile read by default (i.e. same
strategy which was used before) or when the `core_hint_black_box`
feature of `subtle` is enabled, it uses `core::hint::black_box`
(whose documentation was recently updated to reflect the nuances of
potential cryptographic use, see rust-lang/rust#126703)

This PR goes ahead and uses `BlackBox` for both `mask` and
`underflow_mask` where previously it was only used on `underflow_mask`.
The general pattern of bitwise masking inside a loop seems worrisome for
the optimizer potentially inserting branches in the future.

Below are godbolt inspections of the generated assembly, which are free
of the `jns` instructions originally spotted in #659/#661:

- 32-bit (read_volatile): https://godbolt.org/z/TKo9fqza4
- 32-bit (hint::black_box): https://godbolt.org/z/caoMxYbET
- 64-bit (read_volatile): https://godbolt.org/z/PM6zKjj1f
- 64-bit (hint::black_box): https://godbolt.org/z/nseaPvdWv
2024-06-24 20:13:54 +02:00
Michael Rosenberg
5312a0311e
curve: Bump version to 4.1.3 (#660)
* Bumped to v4.1.3

* Added recent PRs to changelog
2024-06-18 21:18:51 +02:00
Tony Arcieri
b4f9e4df92
SECURITY: fix timing variability in backend/serial/u32/scalar.rs (#661)
Similar security fix to #659, but for the 32-bit backend. See that PR
for more information about the problem. Relevant compiler outputs (thanks to @tarcieri):

Without fix
https://godbolt.org/z/zvaWxzvqv
Notice the `jns` ("jump if not sign") instruction on line 106.

With fix
https://godbolt.org/z/jc9j7eb8E
2024-06-18 21:02:37 +02:00
Michael Rosenberg
415892acf1
SECURITY: fix timing variability in backend/serial/u64/scalar.rs (#659)
Timing variability of any kind is problematic when working with
potentially secret values such as elliptic curve scalars, and such
issues can potentially leak private keys and other secrets. Such a
problem was recently discovered in `curve25519-dalek`.

The `Scalar52::sub` function contained usage of a mask value inside of a
loop where LLVM saw an opportunity to insert a branch instruction
(`jns` on x86) to conditionally bypass this code section when the mask
value is set to zero, as can be seen in godbolt:

https://godbolt.org/z/PczYj7Pda

A similar problem was recently discovered in the Kyber reference
implementation:

https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/cnE3pbueBgAJ

As discussed on that thread, one portable solution, which is also used
in this PR, is to introduce a volatile read as an optimization barrier,
which prevents the compiler from optimizing it away.

The fix can be validated in godbolt here:

https://godbolt.org/z/x8d46Yfah

The problem was discovered and the solution independently verified by
Alexander Wagner <alexander.wagner@aisec.fraunhofer.de> and
Lea Themint <lea.thiemt@tum.de> using their DATA tool:

https://github.com/Fraunhofer-AISEC/DATA

Co-authored-by: Tony Arcieri <bascule@gmail.com>
2024-06-18 19:49:31 +02:00
pinkforest(she/her)
9252fa5c0d
Mitigate check-cfg until MSRV 1.77 (#652) 2024-05-09 07:24:16 -06:00
pinkforest(she/her)
858c4ca8ae
Address new nightly clippy unnecessary qualifications (#639) 2024-03-07 16:58:20 -07:00
pinkforest(she/her)
31ccb67050
Remove platforms in favor using CARGO_CFG_TARGET_POINTER_WIDTH (#636) 2024-03-01 07:35:23 -07:00
pinkforest(she/her)
19c7f4a5d5
Fix new nightly redundant import lint warns (#638) 2024-02-29 18:56:52 -07:00
Tony Arcieri
50401ab430
curve: mark ValidityCheck trait as allow(dead_code) (#625)
Recent nightlies have started emitting a dead code lint
2024-02-12 11:56:06 -05:00
Michael Rosenberg
4ac84dd066
curve,ed,x: Bump patch version to reflect fix to nightly SIMD build (#621) 2024-02-06 20:09:18 -05:00