Skip to content

Commit b7cf702

Browse files
committed
[WIP] curve: enable the unreachable_pub lint
In #850 I noticed there are a lot of places that internal types are currently marked `pub` which is a bit confusing when trying to reason about whether APIs allow for violating invariants. This enables the `unreachable_pub` lint which can automatically check for such situations. Most of them were corrected automatically using `cargo fix`
1 parent 81c642d commit b7cf702

File tree

17 files changed

+80
-76
lines changed

17 files changed

+80
-76
lines changed

curve25519-dalek/src/backend.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
use crate::EdwardsPoint;
3838
use crate::Scalar;
3939

40-
pub mod serial;
40+
pub(crate) mod serial;
4141

4242
#[cfg(curve25519_dalek_backend = "simd")]
4343
pub mod vector;
@@ -76,7 +76,10 @@ fn get_selected_backend() -> BackendKind {
7676

7777
#[allow(missing_docs)]
7878
#[cfg(feature = "alloc")]
79-
pub fn pippenger_optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
79+
pub(crate) fn pippenger_optional_multiscalar_mul<I, J>(
80+
scalars: I,
81+
points: J,
82+
) -> Option<EdwardsPoint>
8083
where
8184
I: IntoIterator,
8285
I::Item: core::borrow::Borrow<Scalar>,
@@ -109,7 +112,7 @@ pub(crate) enum VartimePrecomputedStraus {
109112

110113
#[cfg(feature = "alloc")]
111114
impl VartimePrecomputedStraus {
112-
pub fn new<I>(static_points: I) -> Self
115+
pub(crate) fn new<I>(static_points: I) -> Self
113116
where
114117
I: IntoIterator,
115118
I::Item: core::borrow::Borrow<EdwardsPoint>,
@@ -129,7 +132,7 @@ impl VartimePrecomputedStraus {
129132
}
130133

131134
/// Return the number of static points in the precomputation.
132-
pub fn len(&self) -> usize {
135+
pub(crate) fn len(&self) -> usize {
133136
use crate::traits::VartimePrecomputedMultiscalarMul;
134137

135138
match self {
@@ -142,7 +145,7 @@ impl VartimePrecomputedStraus {
142145
}
143146

144147
/// Determine if the precomputation is empty.
145-
pub fn is_empty(&self) -> bool {
148+
pub(crate) fn is_empty(&self) -> bool {
146149
use crate::traits::VartimePrecomputedMultiscalarMul;
147150

148151
match self {
@@ -154,7 +157,7 @@ impl VartimePrecomputedStraus {
154157
}
155158
}
156159

157-
pub fn optional_mixed_multiscalar_mul<I, J, K>(
160+
pub(crate) fn optional_mixed_multiscalar_mul<I, J, K>(
158161
&self,
159162
static_scalars: I,
160163
dynamic_scalars: J,
@@ -193,7 +196,7 @@ impl VartimePrecomputedStraus {
193196

194197
#[allow(missing_docs)]
195198
#[cfg(feature = "alloc")]
196-
pub fn straus_multiscalar_mul<I, J>(scalars: I, points: J) -> EdwardsPoint
199+
pub(crate) fn straus_multiscalar_mul<I, J>(scalars: I, points: J) -> EdwardsPoint
197200
where
198201
I: IntoIterator,
199202
I::Item: core::borrow::Borrow<Scalar>,
@@ -221,7 +224,7 @@ where
221224

222225
#[allow(missing_docs)]
223226
#[cfg(feature = "alloc")]
224-
pub fn straus_optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
227+
pub(crate) fn straus_optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
225228
where
226229
I: IntoIterator,
227230
I::Item: core::borrow::Borrow<Scalar>,
@@ -250,7 +253,7 @@ where
250253
}
251254

252255
/// Perform constant-time, variable-base scalar multiplication.
253-
pub fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
256+
pub(crate) fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
254257
match get_selected_backend() {
255258
#[cfg(curve25519_dalek_backend = "simd")]
256259
BackendKind::Avx2 => vector::scalar_mul::variable_base::spec_avx2::mul(point, scalar),
@@ -264,7 +267,7 @@ pub fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint
264267

265268
/// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint.
266269
#[allow(non_snake_case)]
267-
pub fn vartime_double_base_mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
270+
pub(crate) fn vartime_double_base_mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
268271
match get_selected_backend() {
269272
#[cfg(curve25519_dalek_backend = "simd")]
270273
BackendKind::Avx2 => vector::scalar_mul::vartime_double_base::spec_avx2::mul(a, A, b),

curve25519-dalek/src/backend/serial.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ cfg_if! {
3939

4040
#[cfg(curve25519_dalek_bits = "64")]
4141
#[doc(hidden)]
42-
pub mod u64;
42+
pub(crate) mod u64;
4343

4444
}
4545
}
4646

47-
pub mod curve_models;
47+
pub(crate) mod curve_models;
4848

49-
pub mod scalar_mul;
49+
pub(crate) mod scalar_mul;

curve25519-dalek/src/backend/serial/scalar_mul.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
//! curve model.
1919
2020
#[allow(missing_docs)]
21-
pub mod variable_base;
21+
pub(crate) mod variable_base;
2222

2323
#[allow(missing_docs)]
24-
pub mod vartime_double_base;
24+
pub(crate) mod vartime_double_base;
2525

2626
#[cfg(feature = "alloc")]
27-
pub mod straus;
27+
pub(crate) mod straus;
2828

2929
#[cfg(feature = "alloc")]
30-
pub mod precomputed_straus;
30+
pub(crate) mod precomputed_straus;
3131

3232
#[cfg(feature = "alloc")]
33-
pub mod pippenger;
33+
pub(crate) mod pippenger;

curve25519-dalek/src/backend/serial/scalar_mul/pippenger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use crate::traits::VartimeMultiscalarMul;
5959
/// Therefore, the optimal choice of `w` grows slowly as `n` grows.
6060
///
6161
/// This algorithm is adapted from section 4 of <https://eprint.iacr.org/2012/549.pdf>.
62-
pub struct Pippenger;
62+
pub(crate) struct Pippenger;
6363

6464
impl VartimeMultiscalarMul for Pippenger {
6565
type Point = EdwardsPoint;

curve25519-dalek/src/backend/serial/scalar_mul/precomputed_straus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::traits::VartimePrecomputedMultiscalarMul;
2626
use crate::window::{NafLookupTable5, NafLookupTable8};
2727

2828
#[allow(missing_docs)]
29-
pub struct VartimePrecomputedStraus {
29+
pub(crate) struct VartimePrecomputedStraus {
3030
static_lookup_tables: Vec<NafLookupTable8<AffineNielsPoint>>,
3131
}
3232

curve25519-dalek/src/backend/serial/scalar_mul/straus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::traits::VartimeMultiscalarMul;
4444
///
4545
/// [solution]: https://www.jstor.org/stable/2310929
4646
/// [problem]: https://www.jstor.org/stable/2312273
47-
pub struct Straus {}
47+
pub(crate) struct Straus {}
4848

4949
impl MultiscalarMul for Straus {
5050
type Point = EdwardsPoint;

curve25519-dalek/src/backend/serial/scalar_mul/vartime_double_base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::traits::Identity;
2020
use crate::window::NafLookupTable5;
2121

2222
/// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint.
23-
pub fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
23+
pub(crate) fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
2424
let a_naf = a.non_adjacent_form(5);
2525

2626
#[cfg(feature = "precomputed-tables")]

curve25519-dalek/src/backend/serial/u64.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
//! (allowing the CPU to compute two carry chains in parallel). These
2121
//! will be used if available.
2222
23-
pub mod field;
23+
pub(crate) mod field;
2424

25-
pub mod scalar;
25+
pub(crate) mod scalar;
2626

27-
pub mod constants;
27+
pub(crate) mod constants;

curve25519-dalek/src/backend/serial/u64/scalar.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::constants;
2323
/// The `Scalar52` struct represents an element in
2424
/// \\(\mathbb Z / \ell \mathbb Z\\) as 5 \\(52\\)-bit limbs.
2525
#[derive(Copy, Clone)]
26-
pub struct Scalar52(pub [u64; 5]);
26+
pub(crate) struct Scalar52(pub [u64; 5]);
2727

2828
impl Debug for Scalar52 {
2929
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -59,11 +59,11 @@ fn m(x: u64, y: u64) -> u128 {
5959

6060
impl Scalar52 {
6161
/// The scalar \\( 0 \\).
62-
pub const ZERO: Scalar52 = Scalar52([0, 0, 0, 0, 0]);
62+
pub(crate) const ZERO: Scalar52 = Scalar52([0, 0, 0, 0, 0]);
6363

6464
/// Unpack a 32 byte / 256 bit scalar into 5 52-bit limbs.
6565
#[rustfmt::skip] // keep alignment of s[*] calculations
66-
pub fn from_bytes(bytes: &[u8; 32]) -> Scalar52 {
66+
pub(crate) fn from_bytes(bytes: &[u8; 32]) -> Scalar52 {
6767
let mut words = [0u64; 4];
6868
for i in 0..4 {
6969
for j in 0..8 {
@@ -86,7 +86,7 @@ impl Scalar52 {
8686

8787
/// Reduce a 64 byte / 512 bit scalar mod l
8888
#[rustfmt::skip] // keep alignment of lo[*] and hi[*] calculations
89-
pub fn from_bytes_wide(bytes: &[u8; 64]) -> Scalar52 {
89+
pub(crate) fn from_bytes_wide(bytes: &[u8; 64]) -> Scalar52 {
9090
let mut words = [0u64; 8];
9191
for i in 0..8 {
9292
for j in 0..8 {
@@ -118,7 +118,7 @@ impl Scalar52 {
118118
/// Pack the limbs of this `Scalar52` into 32 bytes
119119
#[rustfmt::skip] // keep alignment of s[*] calculations
120120
#[allow(clippy::identity_op)]
121-
pub fn to_bytes(self) -> [u8; 32] {
121+
pub(crate) fn to_bytes(self) -> [u8; 32] {
122122
let mut s = [0u8; 32];
123123

124124
s[ 0] = (self.0[ 0] >> 0) as u8;
@@ -158,7 +158,7 @@ impl Scalar52 {
158158
}
159159

160160
/// Compute `a + b` (mod l)
161-
pub fn add(a: &Scalar52, b: &Scalar52) -> Scalar52 {
161+
pub(crate) fn add(a: &Scalar52, b: &Scalar52) -> Scalar52 {
162162
let mut sum = Scalar52::ZERO;
163163
let mask = (1u64 << 52) - 1;
164164

@@ -174,7 +174,7 @@ impl Scalar52 {
174174
}
175175

176176
/// Compute `a - b` (mod l)
177-
pub fn sub(a: &Scalar52, b: &Scalar52) -> Scalar52 {
177+
pub(crate) fn sub(a: &Scalar52, b: &Scalar52) -> Scalar52 {
178178
let mut difference = Scalar52::ZERO;
179179
let mask = (1u64 << 52) - 1;
180180

@@ -299,41 +299,41 @@ impl Scalar52 {
299299

300300
/// Compute `a * b` (mod l)
301301
#[inline(never)]
302-
pub fn mul(a: &Scalar52, b: &Scalar52) -> Scalar52 {
302+
pub(crate) fn mul(a: &Scalar52, b: &Scalar52) -> Scalar52 {
303303
let ab = Scalar52::montgomery_reduce(&Scalar52::mul_internal(a, b));
304304
Scalar52::montgomery_reduce(&Scalar52::mul_internal(&ab, &constants::RR))
305305
}
306306

307307
/// Compute `a^2` (mod l)
308308
#[inline(never)]
309309
#[allow(dead_code)] // XXX we don't expose square() via the Scalar API
310-
pub fn square(&self) -> Scalar52 {
310+
pub(crate) fn square(&self) -> Scalar52 {
311311
let aa = Scalar52::montgomery_reduce(&Scalar52::square_internal(self));
312312
Scalar52::montgomery_reduce(&Scalar52::mul_internal(&aa, &constants::RR))
313313
}
314314

315315
/// Compute `(a * b) / R` (mod l), where R is the Montgomery modulus 2^260
316316
#[inline(never)]
317-
pub fn montgomery_mul(a: &Scalar52, b: &Scalar52) -> Scalar52 {
317+
pub(crate) fn montgomery_mul(a: &Scalar52, b: &Scalar52) -> Scalar52 {
318318
Scalar52::montgomery_reduce(&Scalar52::mul_internal(a, b))
319319
}
320320

321321
/// Compute `(a^2) / R` (mod l) in Montgomery form, where R is the Montgomery modulus 2^260
322322
#[inline(never)]
323-
pub fn montgomery_square(&self) -> Scalar52 {
323+
pub(crate) fn montgomery_square(&self) -> Scalar52 {
324324
Scalar52::montgomery_reduce(&Scalar52::square_internal(self))
325325
}
326326

327327
/// Puts a Scalar52 in to Montgomery form, i.e. computes `a*R (mod l)`
328328
#[inline(never)]
329-
pub fn as_montgomery(&self) -> Scalar52 {
329+
pub(crate) fn as_montgomery(&self) -> Scalar52 {
330330
Scalar52::montgomery_mul(self, &constants::RR)
331331
}
332332

333333
/// Takes a Scalar52 out of Montgomery form, i.e. computes `a/R (mod l)`
334334
#[allow(clippy::wrong_self_convention)]
335335
#[inline(never)]
336-
pub fn from_montgomery(&self) -> Scalar52 {
336+
pub(crate) fn from_montgomery(&self) -> Scalar52 {
337337
let mut limbs = [0u128; 9];
338338
for i in 0..5 {
339339
limbs[i] = self[i] as u128;
@@ -352,7 +352,7 @@ mod test {
352352
/// x = 14474011154664524427946373126085988481658748083205070504932198000989141204991
353353
/// x = 7237005577332262213973186563042994240801631723825162898930247062703686954002 mod l
354354
/// x = 3057150787695215392275360544382990118917283750546154083604586903220563173085*R mod l in Montgomery form
355-
pub static X: Scalar52 = Scalar52([
355+
pub(super) static X: Scalar52 = Scalar52([
356356
0x000fffffffffffff,
357357
0x000fffffffffffff,
358358
0x000fffffffffffff,
@@ -361,7 +361,7 @@ mod test {
361361
]);
362362

363363
/// x^2 = 3078544782642840487852506753550082162405942681916160040940637093560259278169 mod l
364-
pub static XX: Scalar52 = Scalar52([
364+
pub(super) static XX: Scalar52 = Scalar52([
365365
0x0001668020217559,
366366
0x000531640ffd0ec0,
367367
0x00085fd6f9f38a31,
@@ -370,7 +370,7 @@ mod test {
370370
]);
371371

372372
/// x^2 = 4413052134910308800482070043710297189082115023966588301924965890668401540959*R mod l in Montgomery form
373-
pub static XX_MONT: Scalar52 = Scalar52([
373+
pub(super) static XX_MONT: Scalar52 = Scalar52([
374374
0x000c754eea569a5c,
375375
0x00063b6ed36cb215,
376376
0x0008ffa36bf25886,
@@ -379,7 +379,7 @@ mod test {
379379
]);
380380

381381
/// y = 6145104759870991071742105800796537629880401874866217824609283457819451087098
382-
pub static Y: Scalar52 = Scalar52([
382+
pub(super) static Y: Scalar52 = Scalar52([
383383
0x000b75071e1458fa,
384384
0x000bf9d75e1ecdac,
385385
0x000433d2baf0672b,
@@ -388,7 +388,7 @@ mod test {
388388
]);
389389

390390
/// x*y = 36752150652102274958925982391442301741 mod l
391-
pub static XY: Scalar52 = Scalar52([
391+
pub(super) static XY: Scalar52 = Scalar52([
392392
0x000ee6d76ba7632d,
393393
0x000ed50d71d84e02,
394394
0x00000000001ba634,
@@ -397,7 +397,7 @@ mod test {
397397
]);
398398

399399
/// x*y = 658448296334113745583381664921721413881518248721417041768778176391714104386*R mod l in Montgomery form
400-
pub static XY_MONT: Scalar52 = Scalar52([
400+
pub(super) static XY_MONT: Scalar52 = Scalar52([
401401
0x0006d52bf200cfd5,
402402
0x00033fb1d7021570,
403403
0x000f201bc07139d8,
@@ -406,7 +406,7 @@ mod test {
406406
]);
407407

408408
/// a = 2351415481556538453565687241199399922945659411799870114962672658845158063753
409-
pub static A: Scalar52 = Scalar52([
409+
pub(super) static A: Scalar52 = Scalar52([
410410
0x0005236c07b3be89,
411411
0x0001bc3d2a67c0c4,
412412
0x000a4aa782aae3ee,
@@ -415,7 +415,7 @@ mod test {
415415
]);
416416

417417
/// b = 4885590095775723760407499321843594317911456947580037491039278279440296187236
418-
pub static B: Scalar52 = Scalar52([
418+
pub(super) static B: Scalar52 = Scalar52([
419419
0x000d3fae55421564,
420420
0x000c2df24f65a4bc,
421421
0x0005b5587d69fb0b,
@@ -425,7 +425,7 @@ mod test {
425425

426426
/// a+b = 0
427427
/// a-b = 4702830963113076907131374482398799845891318823599740229925345317690316127506
428-
pub static AB: Scalar52 = Scalar52([
428+
pub(super) static AB: Scalar52 = Scalar52([
429429
0x000a46d80f677d12,
430430
0x0003787a54cf8188,
431431
0x0004954f0555c7dc,
@@ -434,7 +434,7 @@ mod test {
434434
]);
435435

436436
// c = (2^512 - 1) % l = 1627715501170711445284395025044413883736156588369414752970002579683115011840
437-
pub static C: Scalar52 = Scalar52([
437+
pub(super) static C: Scalar52 = Scalar52([
438438
0x000611e3449c0f00,
439439
0x000a768859347a40,
440440
0x0007f5be65d00e1b,

0 commit comments

Comments
 (0)