Skip to content

Commit e458a92

Browse files
authored
BREAKING: seal the Integer trait (#1227)
Note: this is a breaking change in a stable release, and hopefully the last such change we make. As a result, the v0.7.1 release will be yanked. We suspect this will not have an impact on users as it's unlikely any downstream crates have impl'd the `Integer` trait. Sealing the `Integer` trait allows us to make additive changes to its bounds, e.g. #1226
1 parent 2484f32 commit e458a92

5 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/int.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
//! Stack-allocated big signed integers.
22
3-
use crate::{
4-
Bounded, Choice, ConstOne, ConstZero, Constants, CtEq, CtOption, FixedInteger, Integer, Limb,
5-
NonZero, Odd, One, Signed, Uint, Word, Zero,
6-
};
7-
use core::fmt;
8-
9-
#[cfg(feature = "serde")]
10-
use crate::Encoding;
11-
#[cfg(feature = "serde")]
12-
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};
13-
143
mod add;
154
mod bit_and;
165
mod bit_not;
@@ -39,6 +28,17 @@ pub(crate) mod types;
3928
#[cfg(feature = "rand_core")]
4029
mod rand;
4130

31+
use crate::{
32+
Bounded, Choice, ConstOne, ConstZero, Constants, CtEq, CtOption, FixedInteger, Integer, Limb,
33+
NonZero, Odd, One, Signed, Uint, Word, Zero, sealed::Sealed,
34+
};
35+
use core::fmt;
36+
37+
#[cfg(feature = "serde")]
38+
use crate::Encoding;
39+
#[cfg(feature = "serde")]
40+
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};
41+
4242
/// Stack-allocated big _signed_ integer.
4343
/// See [`Uint`] for _unsigned_ integers.
4444
///
@@ -264,6 +264,8 @@ impl<const LIMBS: usize> Integer for Int<LIMBS> {
264264
}
265265
}
266266

267+
impl<const LIMBS: usize> Sealed for Int<LIMBS> {}
268+
267269
impl<const LIMBS: usize> Signed for Int<LIMBS> {
268270
type Unsigned = Uint<LIMBS>;
269271

src/limb.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod rand;
2424

2525
use crate::{
2626
Bounded, Choice, ConstOne, ConstZero, Constants, CtEq, CtOption, Integer, NonZero, One,
27-
UintRef, Unsigned, WideWord, Word, Zero, primitives::u32_bits, word,
27+
UintRef, Unsigned, WideWord, Word, Zero, primitives::u32_bits, traits::sealed::Sealed, word,
2828
};
2929
use core::{fmt, ptr, slice};
3030

@@ -260,6 +260,8 @@ impl Integer for Limb {
260260
}
261261
}
262262

263+
impl Sealed for Limb {}
264+
263265
impl Unsigned for Limb {
264266
#[inline]
265267
fn as_uint_ref(&self) -> &UintRef {

src/traits.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub trait Bounded {
3232
/// Integer trait: represents common functionality of integer types provided by this crate.
3333
pub trait Integer:
3434
'static
35+
+ sealed::Sealed
3536
+ Add<Output = Self>
3637
+ for<'a> Add<&'a Self, Output = Self>
3738
+ AddAssign<Self>
@@ -1286,6 +1287,12 @@ pub(crate) trait AmmMultiplier<'a>: MontyMultiplier<'a> {
12861287
fn square_amm_assign(&mut self, a: &mut <Self::Monty as MontyForm>::Integer);
12871288
}
12881289

1290+
/// Support for sealing traits defined in this module.
1291+
pub(crate) mod sealed {
1292+
/// Sealed trait.
1293+
pub trait Sealed {}
1294+
}
1295+
12891296
#[cfg(test)]
12901297
pub(crate) mod tests {
12911298
use super::{Integer, Signed, ToUnsigned, Unsigned};

src/uint.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) use ref_type::UintRef;
88
use crate::{
99
Bounded, Choice, ConstOne, ConstZero, Constants, CtEq, CtOption, EncodedUint, FixedInteger,
1010
Int, Integer, Limb, NonZero, Odd, One, Unsigned, UnsignedWithMontyForm, Word, Zero,
11-
limb::nlimbs, modular::FixedMontyForm, primitives::u32_bits,
11+
limb::nlimbs, modular::FixedMontyForm, primitives::u32_bits, traits::sealed::Sealed,
1212
};
1313
use core::fmt;
1414

@@ -344,6 +344,8 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
344344
}
345345
}
346346

347+
impl<const LIMBS: usize> Sealed for Uint<LIMBS> {}
348+
347349
impl<const LIMBS: usize> Unsigned for Uint<LIMBS> {
348350
fn as_uint_ref(&self) -> &UintRef {
349351
self.as_uint_ref()

src/uint/boxed.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod rand;
3131

3232
use crate::{
3333
Choice, CtAssign, CtEq, CtOption, Integer, Limb, NonZero, Odd, One, Resize, UintRef, Unsigned,
34-
UnsignedWithMontyForm, Word, Zero, modular::BoxedMontyForm,
34+
UnsignedWithMontyForm, Word, Zero, modular::BoxedMontyForm, traits::sealed::Sealed,
3535
};
3636
use alloc::{boxed::Box, vec, vec::Vec};
3737
use core::{
@@ -458,6 +458,8 @@ impl Integer for BoxedUint {
458458
}
459459
}
460460

461+
impl Sealed for BoxedUint {}
462+
461463
impl Unsigned for BoxedUint {
462464
fn as_uint_ref(&self) -> &UintRef {
463465
self.as_uint_ref()

0 commit comments

Comments
 (0)