Skip to content

Commit 0ac2d01

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 0ac2d01

File tree

17 files changed

+77
-76
lines changed

17 files changed

+77
-76
lines changed

curve25519-dalek/src/backend.rs

Lines changed: 10 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,7 @@ 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>(scalars: I, points: J) -> Option<EdwardsPoint>
8080
where
8181
I: IntoIterator,
8282
I::Item: core::borrow::Borrow<Scalar>,
@@ -109,7 +109,7 @@ pub(crate) enum VartimePrecomputedStraus {
109109

110110
#[cfg(feature = "alloc")]
111111
impl VartimePrecomputedStraus {
112-
pub fn new<I>(static_points: I) -> Self
112+
pub(crate) fn new<I>(static_points: I) -> Self
113113
where
114114
I: IntoIterator,
115115
I::Item: core::borrow::Borrow<EdwardsPoint>,
@@ -129,7 +129,7 @@ impl VartimePrecomputedStraus {
129129
}
130130

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

135135
match self {
@@ -142,7 +142,7 @@ impl VartimePrecomputedStraus {
142142
}
143143

144144
/// Determine if the precomputation is empty.
145-
pub fn is_empty(&self) -> bool {
145+
pub(crate) fn is_empty(&self) -> bool {
146146
use crate::traits::VartimePrecomputedMultiscalarMul;
147147

148148
match self {
@@ -154,7 +154,7 @@ impl VartimePrecomputedStraus {
154154
}
155155
}
156156

157-
pub fn optional_mixed_multiscalar_mul<I, J, K>(
157+
pub(crate) fn optional_mixed_multiscalar_mul<I, J, K>(
158158
&self,
159159
static_scalars: I,
160160
dynamic_scalars: J,
@@ -193,7 +193,7 @@ impl VartimePrecomputedStraus {
193193

194194
#[allow(missing_docs)]
195195
#[cfg(feature = "alloc")]
196-
pub fn straus_multiscalar_mul<I, J>(scalars: I, points: J) -> EdwardsPoint
196+
pub(crate) fn straus_multiscalar_mul<I, J>(scalars: I, points: J) -> EdwardsPoint
197197
where
198198
I: IntoIterator,
199199
I::Item: core::borrow::Borrow<Scalar>,
@@ -221,7 +221,7 @@ where
221221

222222
#[allow(missing_docs)]
223223
#[cfg(feature = "alloc")]
224-
pub fn straus_optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
224+
pub(crate) fn straus_optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
225225
where
226226
I: IntoIterator,
227227
I::Item: core::borrow::Borrow<Scalar>,
@@ -250,7 +250,7 @@ where
250250
}
251251

252252
/// Perform constant-time, variable-base scalar multiplication.
253-
pub fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
253+
pub(crate) fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
254254
match get_selected_backend() {
255255
#[cfg(curve25519_dalek_backend = "simd")]
256256
BackendKind::Avx2 => vector::scalar_mul::variable_base::spec_avx2::mul(point, scalar),
@@ -264,7 +264,7 @@ pub fn variable_base_mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint
264264

265265
/// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint.
266266
#[allow(non_snake_case)]
267-
pub fn vartime_double_base_mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
267+
pub(crate) fn vartime_double_base_mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
268268
match get_selected_backend() {
269269
#[cfg(curve25519_dalek_backend = "simd")]
270270
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,

curve25519-dalek/src/edwards.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ mod test {
18101810
]);
18111811

18121812
/// 4493907448824000747700850167940867464579944529806937181821189941592931634714
1813-
pub static A_SCALAR: Scalar = Scalar {
1813+
pub(super) static A_SCALAR: Scalar = Scalar {
18141814
bytes: [
18151815
0x1a, 0x0e, 0x97, 0x8a, 0x90, 0xf6, 0x62, 0x2d, 0x37, 0x47, 0x02, 0x3f, 0x8a, 0xd8,
18161816
0x26, 0x4d, 0xa7, 0x58, 0xaa, 0x1b, 0x88, 0xe0, 0x40, 0xd1, 0x58, 0x9e, 0x7b, 0x7f,
@@ -1819,7 +1819,7 @@ mod test {
18191819
};
18201820

18211821
/// 2506056684125797857694181776241676200180934651973138769173342316833279714961
1822-
pub static B_SCALAR: Scalar = Scalar {
1822+
pub(super) static B_SCALAR: Scalar = Scalar {
18231823
bytes: [
18241824
0x91, 0x26, 0x7a, 0xcf, 0x25, 0xc2, 0x09, 0x1b, 0xa2, 0x17, 0x74, 0x7b, 0x66, 0xf0,
18251825
0xb3, 0x2e, 0x9d, 0xf2, 0xa5, 0x67, 0x41, 0xcf, 0xda, 0xc4, 0x56, 0xa7, 0xd4, 0xaa,
@@ -1828,7 +1828,7 @@ mod test {
18281828
};
18291829

18301830
/// A_SCALAR * basepoint, computed with ed25519.py
1831-
pub static A_TIMES_BASEPOINT: CompressedEdwardsY = CompressedEdwardsY([
1831+
pub(super) static A_TIMES_BASEPOINT: CompressedEdwardsY = CompressedEdwardsY([
18321832
0xea, 0x27, 0xe2, 0x60, 0x53, 0xdf, 0x1b, 0x59, 0x56, 0xf1, 0x4d, 0x5d, 0xec, 0x3c, 0x34,
18331833
0xc3, 0x84, 0xa2, 0x69, 0xb7, 0x4c, 0xc3, 0x80, 0x3e, 0xa8, 0xe2, 0xe7, 0xc9, 0x42, 0x5e,
18341834
0x40, 0xa5,

0 commit comments

Comments
 (0)