Skip to content

Commit 65853b4

Browse files
committed
Remove *SafeGcdInverter types from public API
Previously they needed to be public to make trait bounds work, but now that those bounds are no longer required (#894) these types no longer need to be public, as they're otherwise an implementation detail.
1 parent 7e90ef2 commit 65853b4

5 files changed

Lines changed: 10 additions & 50 deletions

File tree

src/modular.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ pub use self::{
3636
const_monty_form::{ConstMontyForm, ConstMontyParams, invert::ConstMontyFormInverter},
3737
monty_form::{MontyForm, MontyParams},
3838
reduction::montgomery_reduction,
39-
safegcd::SafeGcdInverter,
4039
};
4140

41+
pub(crate) use self::safegcd::SafeGcdInverter;
42+
4243
#[cfg(feature = "alloc")]
43-
pub use self::{
44-
boxed_monty_form::{BoxedMontyForm, BoxedMontyParams},
45-
safegcd::boxed::BoxedSafeGcdInverter,
46-
};
44+
pub use self::boxed_monty_form::{BoxedMontyForm, BoxedMontyParams};
4745

4846
/// A generalization for numbers kept in optimized representations (e.g. Montgomery)
4947
/// that can be converted back to the original form.

src/modular/boxed_monty_form/invert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Multiplicative inverses of boxed integers in Montgomery form.
22
33
use super::{BoxedMontyForm, BoxedMontyParams};
4-
use crate::{Invert, modular::BoxedSafeGcdInverter};
4+
use crate::{Invert, modular::safegcd::boxed::BoxedSafeGcdInverter};
55
use subtle::CtOption;
66

77
impl BoxedMontyForm {

src/modular/safegcd.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const GCD_BATCH_SIZE: u32 = 62;
4646
/// - P. Wuille, "The safegcd implementation in libsecp256k1 explained",
4747
/// <https://github.com/bitcoin-core/secp256k1/blob/master/doc/safegcd_implementation.md>
4848
#[derive(Clone, Debug)]
49-
pub struct SafeGcdInverter<const LIMBS: usize> {
49+
pub(crate) struct SafeGcdInverter<const LIMBS: usize> {
5050
/// Modulus
5151
pub(super) modulus: Odd<Uint<LIMBS>>,
5252

@@ -62,9 +62,8 @@ type Matrix = [[i64; 2]; 2];
6262

6363
impl<const LIMBS: usize> SafeGcdInverter<LIMBS> {
6464
/// Creates the inverter for specified modulus and adjusting parameter.
65-
///
66-
/// Modulus must be odd. Returns `None` if it is not.
67-
pub const fn new(modulus: &Odd<Uint<LIMBS>>, adjuster: &Uint<LIMBS>) -> Self {
65+
#[cfg(test)]
66+
pub(crate) const fn new(modulus: &Odd<Uint<LIMBS>>, adjuster: &Uint<LIMBS>) -> Self {
6867
Self::new_with_inverse(
6968
modulus,
7069
U64::from_u64(invert_mod_u64(modulus.as_ref().as_words())),
@@ -85,28 +84,12 @@ impl<const LIMBS: usize> SafeGcdInverter<LIMBS> {
8584
}
8685
}
8786

88-
/// Returns either the adjusted modular multiplicative inverse for the argument or `None`
89-
/// depending on invertibility of the argument, i.e. its coprimality with the modulus.
90-
#[deprecated(since = "0.7.0", note = "please use `invert` instead")]
91-
pub const fn inv(&self, value: &Uint<LIMBS>) -> ConstCtOption<Uint<LIMBS>> {
92-
self.invert(value)
93-
}
94-
9587
/// Returns either the adjusted modular multiplicative inverse for the argument or `None`
9688
/// depending on invertibility of the argument, i.e. its coprimality with the modulus.
9789
pub const fn invert(&self, value: &Uint<LIMBS>) -> ConstCtOption<Uint<LIMBS>> {
9890
invert_odd_mod_precomp::<LIMBS, false>(value, &self.modulus, self.inverse, &self.adjuster)
9991
}
10092

101-
/// Returns either the adjusted modular multiplicative inverse for the argument or `None`
102-
/// depending on invertibility of the argument, i.e. its coprimality with the modulus.
103-
///
104-
/// This version is variable-time with respect to `value`.
105-
#[deprecated(since = "0.7.0", note = "please use `invert_vartime` instead")]
106-
pub const fn inv_vartime(&self, value: &Uint<LIMBS>) -> ConstCtOption<Uint<LIMBS>> {
107-
self.invert_vartime(value)
108-
}
109-
11093
/// Returns either the adjusted modular multiplicative inverse for the argument or `None`
11194
/// depending on invertibility of the argument, i.e. its coprimality with the modulus.
11295
///

src/modular/safegcd/boxed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use subtle::{Choice, CtOption};
1616
///
1717
/// See [`super::SafeGcdInverter`] for more information.
1818
#[derive(Clone, Debug)]
19-
pub struct BoxedSafeGcdInverter {
19+
pub(crate) struct BoxedSafeGcdInverter {
2020
/// Modulus
2121
pub(crate) modulus: Odd<BoxedUint>,
2222

@@ -31,6 +31,7 @@ impl BoxedSafeGcdInverter {
3131
/// Creates the inverter for specified modulus and adjusting parameter.
3232
///
3333
/// Modulus must be odd. Returns `None` if it is not.
34+
#[cfg(test)]
3435
pub fn new(modulus: Odd<BoxedUint>, adjuster: BoxedUint) -> Self {
3536
let inverse = U64::from_u64(invert_mod_u64(modulus.as_ref().as_words()));
3637
Self::new_with_inverse(modulus, inverse, adjuster)

tests/safegcd.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod common;
44

55
use common::to_biguint;
6-
use crypto_bigint::{Odd, U256, Uint, modular::SafeGcdInverter};
6+
use crypto_bigint::{Odd, U256};
77
use num_bigint::BigUint;
88
use num_integer::Integer;
99
use num_traits::One;
@@ -64,28 +64,6 @@ proptest! {
6464
}
6565
}
6666

67-
#[test]
68-
fn invert_precomputed(x in uint()) {
69-
let x_bi = to_biguint(&x);
70-
let p_bi = to_biguint(&P);
71-
72-
let expected_is_some = x_bi.gcd(&p_bi) == BigUint::one();
73-
let inverter = SafeGcdInverter::new(&P, &Uint::ONE);
74-
let actual = inverter.invert(&x);
75-
76-
prop_assert_eq!(expected_is_some, bool::from(actual.is_some()));
77-
78-
if let Some(actual) = Option::<U256>::from(actual) {
79-
let inv_bi = to_biguint(&actual);
80-
let res = (inv_bi * x_bi) % p_bi;
81-
prop_assert_eq!(res, BigUint::one());
82-
83-
// check vartime implementation equivalence
84-
let actual_vartime = inverter.invert_vartime(&x).unwrap();
85-
prop_assert_eq!(actual, actual_vartime);
86-
}
87-
}
88-
8967
#[cfg(feature = "alloc")]
9068
#[test]
9169
fn boxed_invert_mod(x in boxed_uint()) {

0 commit comments

Comments
 (0)