commit_hash
stringlengths
40
40
author
stringlengths
1
57
date
timestamp[s]date
2010-07-26 04:45:09
2026-04-14 18:21:10
message
stringlengths
8
1.39M
diff
stringlengths
68
51.2k
files_changed
int64
1
136
insertions
int64
0
2.35k
deletions
int64
0
1.9k
656458c0a17bf40160c7d27686d39171485659a8
Tony Arcieri
2023-12-19T22:40:39
Impl `Residue::inv(ert)` using Bernstein-Yang (#481) This results in the following performance improvement: Const Montgomery arithmetic/invert, U256 time: [1.7561 µs 1.7596 µs 1.7636 µs] change: [-62.813% -62.407% -62.076%] (p = 0.00 < 0.05) ...
diff --git a/src/modular.rs b/src/modular.rs index d4cc35b..32e81bd 100644 --- a/src/modular.rs +++ b/src/modular.rs @@ -23,7 +23,6 @@ mod residue; mod add; mod bernstein_yang; mod div_by_2; -mod inv; mod mul; mod pow; mod sub; diff --git a/src/modular/dyn_residue/inv.rs b/src/modular/dyn_residue/inv.rs index f52...
4
31
45
f2350a29c71bc6b174ec00e47e4c48fff8ae7f48
Tony Arcieri
2023-12-19T20:31:39
Residue benchmarks (#480) Adds benchmarks for the `Residue` type, largely copy/pasted from `DynResidue`.
diff --git a/Cargo.toml b/Cargo.toml index ea83e0c..e99c5b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,10 @@ harness = false name = "limb" harness = false +[[bench]] +name = "residue" +harness = false + [[bench]] name = "uint" harness = false diff --git a/benches/boxed_residue.rs b/benches/boxed_resid...
4
126
2
c1753dfb11c91353e27e960741de7955a39052ff
Bogdan Opanchuk
2023-12-19T20:31:21
Move operations on Words to the primitives module (#458)
diff --git a/src/const_choice.rs b/src/const_choice.rs index 6e8e39a..b34a7b6 100644 --- a/src/const_choice.rs +++ b/src/const_choice.rs @@ -16,9 +16,9 @@ impl ConstChoice { pub const TRUE: Self = Self(Word::MAX); #[inline] + #[allow(trivial_numeric_casts)] pub(crate) const fn as_u32_mask(&self) -> ...
12
144
133
7590d5b3cb62857dc97107b03e555a86a25324a0
Tony Arcieri
2023-12-19T19:04:45
Residue: add proptests (#478) Adds an initial set of proptests which cover inversions, similar to the ones recently added to `DynResidue`.
diff --git a/src/modular/residue.rs b/src/modular/residue.rs index f114165..328790c 100644 --- a/src/modular/residue.rs +++ b/src/modular/residue.rs @@ -64,15 +64,12 @@ pub trait ResidueParams<const LIMBS: usize>: } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] /// A residue mod `MOD`, represented using `LIM...
4
87
10
00e1a60e1ec65fd0f8f681956179ccef81caa045
Tony Arcieri
2023-12-19T17:01:11
Impl `DynResidue::inv(ert)` using Bernstein-Yang (#477) This results in a massive performance improvement: Montgomery arithmetic/invert, U256 time: [1.7834 µs 1.7870 µs 1.7913 µs] change: [-99.974% -99.974% -99.974%] (p = 0.00 < 0.05)
diff --git a/benches/dyn_residue.rs b/benches/dyn_residue.rs index 98e840e..9ee6536 100644 --- a/benches/dyn_residue.rs +++ b/benches/dyn_residue.rs @@ -4,7 +4,7 @@ use criterion::{ }; use crypto_bigint::{ modular::{DynResidue, DynResidueParams}, - Inverter, PrecomputeInverter, Random, U256, + Invert, Inve...
3
31
18
2bff2ef0c75a356d55b7d77fabb4fd0d25a4cade
Tony Arcieri
2023-12-19T14:52:49
Uint: make GCD fallible (#476) Since Bernstein-Yang relies on `f` being odd, this makes `Uint::gcd` return a `ConstCtOption` rather than panicking in the event that `f` is even.
diff --git a/src/uint/gcd.rs b/src/uint/gcd.rs index 1b75933..308d663 100644 --- a/src/uint/gcd.rs +++ b/src/uint/gcd.rs @@ -1,7 +1,7 @@ //! Support for computing greatest common divisor of two `Uint`s. use super::Uint; -use crate::{modular::BernsteinYangInverter, PrecomputeInverter}; +use crate::{modular::Bernstei...
2
17
10
45f9a52271ed577d21688d0e234b2e7d256104ad
Tony Arcieri
2023-12-19T14:32:02
Uint: expand GCD tests (#475)
diff --git a/src/uint/gcd.rs b/src/uint/gcd.rs index 99e032a..1b75933 100644 --- a/src/uint/gcd.rs +++ b/src/uint/gcd.rs @@ -21,10 +21,26 @@ mod tests { use crate::U256; #[test] - fn gcd() { + fn gcd_relatively_prime() { + // Two semiprimes with no common factors + let f = U256::from(59u...
1
17
1
574f8b9ec059ab678b308ab7f95bd0771514e343
Tony Arcieri
2023-12-19T03:19:02
Ensure schoolbook mul bounds are checked in production code (#474)
diff --git a/src/uint/mul.rs b/src/uint/mul.rs index 704feb0..2ec0156 100644 --- a/src/uint/mul.rs +++ b/src/uint/mul.rs @@ -19,8 +19,9 @@ use subtle::CtOption; // TODO(tarcieri): change this into a `const fn` when `const_mut_refs` is stable macro_rules! impl_schoolbook_multiplication { ($lhs:expr, $rhs:expr, $l...
1
3
2
fab057cc052edd6d54760fe09e6909a17ca28ff0
Tony Arcieri
2023-12-19T02:43:23
Make `Uint::gcd` a `const fn` (#473) All the prerequisites are there. It was simply an oversight not making it so in #472.
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index 6486d04..117b47a 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -99,7 +99,7 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> /// /// This is defined on this type to piggyback on the def...
2
3
3
b57ea5e0f4c05a09e602f5a6b009bdf93b97c9cc
Tony Arcieri
2023-12-19T01:28:56
Bernstein-Yang: minor refactoring (#471) This should make it easier to implement GCD
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index 0e5623a..add61d2 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -81,8 +81,8 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> while !g.eq(&Int62L::ZERO) { (delta, matrix) =...
1
53
54
a3bc07c9c19bb14c2a87d762604e6185a07be5e7
Tony Arcieri
2023-12-19T00:59:45
Bernstein-Yang: rename `Int62L` (#470) Renames from `Uint62L` since the underlying type is signed.
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index 5eb1e8f..0e5623a 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -43,10 +43,10 @@ use subtle::CtOption; #[derive(Clone, Debug)] pub struct BernsteinYangInverter<const SAT_LIMBS: usize, const UNSAT_LIMBS:...
1
23
25
090bb41dceeb0d9f730e6571e7732e1e496afdcf
Tony Arcieri
2023-12-19T00:52:28
Remove `BoxedBernsteinYangInverter` (#469) Per #468 there are undiagnosed issues with it which aren't occurring with e.g. `DynResidueInverter`, which indicates some kind of implementation defect. Until such time as it can be diagnosed, this commit is removing the implementation to be on the safe side.
diff --git a/benches/boxed_residue.rs b/benches/boxed_residue.rs index 8085e6f..7da5229 100644 --- a/benches/boxed_residue.rs +++ b/benches/boxed_residue.rs @@ -4,7 +4,7 @@ use criterion::{ }; use crypto_bigint::{ modular::{BoxedResidue, BoxedResidueParams}, - BoxedUint, Inverter, NonZero, PrecomputeInverter,...
7
9
578
4249410ac465222592bb2512edf1fefb26c893f5
Tony Arcieri
2023-12-18T22:44:12
Add `DynResidue` proptests (#467) Adds proptests for computing modular inverses using the legacy method and Bernstein-Yang.
diff --git a/src/const_choice.rs b/src/const_choice.rs index 8f07c7a..6e8e39a 100644 --- a/src/const_choice.rs +++ b/src/const_choice.rs @@ -241,6 +241,17 @@ impl<T> From<ConstCtOption<T>> for CtOption<T> { } } +impl<T> From<ConstCtOption<T>> for Option<T> { + #[inline] + fn from(value: ConstCtOption<T>) ...
3
99
1
07539e733ca79cba553342225fb13d84eeafa634
Tony Arcieri
2023-12-18T22:26:42
Uint: rename `rem(_wide)` => `rem(_wide)_vartime` (#466) These functions operate in variable time with respect to the `rhs` parameter. Renames them to `*_vartime` as is the convention of this crate, and in their place adds an actual constant-time remainder implementation (using `div_rem`)
diff --git a/benches/uint.rs b/benches/uint.rs index 93a6ffe..b643b7d 100644 --- a/benches/uint.rs +++ b/benches/uint.rs @@ -31,6 +31,19 @@ fn bench_division(c: &mut Criterion) { ) }); + group.bench_function("rem_vartime, U256/U128, full size", |b| { + b.iter_batched( + || { + ...
7
38
21
d8ca40b86a111eec5bcf8e555812b63aba8b9921
Tony Arcieri
2023-12-18T19:27:38
`BoxedBernsteinYangInverter` proptests (#465) Adds proptests for the P-256 modulus analogous to the ones we have for `BernsteinYangInverter`.
diff --git a/src/modular/bernstein_yang/boxed.rs b/src/modular/bernstein_yang/boxed.rs index 124a4bd..831c2ad 100644 --- a/src/modular/bernstein_yang/boxed.rs +++ b/src/modular/bernstein_yang/boxed.rs @@ -27,9 +27,11 @@ impl BoxedBernsteinYangInverter { /// /// Modulus must be odd. Returns `None` if it is not...
5
62
20
e2d696533b086eb423ca452e21d82f042b559b74
Tony Arcieri
2023-12-18T15:52:39
BoxedUint: move `From` impls to module (#464) More consistent with `Uint`
diff --git a/src/uint/boxed.rs b/src/uint/boxed.rs index 5cb0af7..9b93d2b 100644 --- a/src/uint/boxed.rs +++ b/src/uint/boxed.rs @@ -3,30 +3,31 @@ mod add; mod add_mod; mod bit_and; +mod bit_not; mod bit_or; +mod bit_xor; mod bits; mod cmp; mod ct; mod div; pub(crate) mod encoding; +mod from; mod inv_mod; mo...
2
102
88
f77ea0bcd2c6bab4403d0bbb8d404180c77cfe18
Tony Arcieri
2023-12-18T03:06:14
BoxedBernsteinYangInverter: reduce allocations (#463) Montgomery arithmetic/Bernstein-Yang invert, U256 time: [289.83 µs 290.75 µs 291.70 µs] change: [-17.320% -16.859% -16.379%] (p = 0.00 < 0.05)
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index 5e9876e..8e96fda 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -173,6 +173,7 @@ impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> .wrapping_mul(d.lowest() as i64) .wrapping...
2
121
107
6ba9c3f4c9b03a32d6223acc04d49a886cc78302
Tony Arcieri
2023-12-18T02:22:20
BoxedBernsteinYangInverter: refactor `fg` and `de` (#462) Makes them mutate their arguments "in place" (although internally they still do quite a few allocations).
diff --git a/src/modular/bernstein_yang/boxed.rs b/src/modular/bernstein_yang/boxed.rs index 2243cb3..f3d37e5 100644 --- a/src/modular/bernstein_yang/boxed.rs +++ b/src/modular/bernstein_yang/boxed.rs @@ -79,22 +79,24 @@ impl BoxedBernsteinYangInverter { /// Returns the updated values of the variables f and g fo...
1
13
9
4a4dd7969359a3117e8421f447fba544d38b6f61
Tony Arcieri
2023-12-18T01:56:53
`BoxedResidueInverter` tests and benchmarks (#461) Even without any optimization work, it seems significantly faster: Montgomery arithmetic/invert, U256 time: [6.8324 ms 6.8402 ms 6.8482 ms] Montgomery arithmetic/Bernstein-Yang invert, U256 time: [342.58 µs 3...
diff --git a/benches/boxed_residue.rs b/benches/boxed_residue.rs index ae142ab..8085e6f 100644 --- a/benches/boxed_residue.rs +++ b/benches/boxed_residue.rs @@ -4,7 +4,7 @@ use criterion::{ }; use crypto_bigint::{ modular::{BoxedResidue, BoxedResidueParams}, - BoxedUint, + BoxedUint, Inverter, NonZero, Pre...
5
148
5
48eb73da8bf9888b824477bf8a7fcbfee6038b5d
Tony Arcieri
2023-12-18T00:39:40
Add `BoxedBernsteinYangInverter` (#460) Adds a boxed equivalent of `BernsteinYangInverter` which operates on values whose number of limbs is determined at compile time. It's largely copy-pasted from the stack-allocated equivalent, but the long-term goal will be to rewrite it to use in-place operations rather tha...
diff --git a/src/modular.rs b/src/modular.rs index b82b08a..e4f7681 100644 --- a/src/modular.rs +++ b/src/modular.rs @@ -39,7 +39,10 @@ pub use self::{ }; #[cfg(feature = "alloc")] -pub use self::boxed_residue::{BoxedResidue, BoxedResidueParams}; +pub use self::{ + bernstein_yang::boxed::BoxedBernsteinYangInvert...
5
480
91
fcc0933126eb2692561d1b198479eea15e2717e4
Tony Arcieri
2023-12-17T23:48:26
Bernstein-Yang: use `ConstCtOption` (#459) Replaces previous usages of `(_, ConstChoice)` with `ConstCtOption`.
diff --git a/src/const_choice.rs b/src/const_choice.rs index 8fc7789..8f07c7a 100644 --- a/src/const_choice.rs +++ b/src/const_choice.rs @@ -1,6 +1,6 @@ use subtle::{Choice, CtOption}; -use crate::{NonZero, Uint, Word}; +use crate::{modular::BernsteinYangInverter, NonZero, Uint, Word}; /// A boolean value returne...
4
35
20
e7bcff7ee165b0ec36ad034721d204a57251f501
Tony Arcieri
2023-12-17T23:06:57
Add `ConstantTimeSelect` trait (#454) We can't impl `subtle::ConditionallySelectable` for `Boxed*` types due to a bound on `Copy`. I've proposed various ways to try to fix this upstream, e.g. dalek-cryptography/subtle#118, from which the `ConstantTimeSelect` trait has been extracted. It provides the same API as ...
diff --git a/src/modular/boxed_residue.rs b/src/modular/boxed_residue.rs index 98333e5..c015d6a 100644 --- a/src/modular/boxed_residue.rs +++ b/src/modular/boxed_residue.rs @@ -12,7 +12,7 @@ use super::{ reduction::{montgomery_reduction_boxed, montgomery_reduction_boxed_mut}, Retrieve, }; -use crate::{BoxedU...
9
81
164
3e571c37dbf01b3ef3b0ed662185f5286bb73a37
Tony Arcieri
2023-12-17T19:47:24
Bernstein-Yang: minor refactoring (#457) Extracts `from_uint` and `to_uint` methods onto `Uint62L`, rather than the previous `sat_to_unsat` and `unsat_to_sat` free functions. Also extracts the `impl_limb_convert!` macro into its own module, which will assist impl'ing a `BoxedUint62L`.
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index ab093e6..8b13fc9 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -10,6 +10,9 @@ #![allow(clippy::needless_range_loop)] +#[macro_use] +mod macros; + use crate::{ConstChoice, Inverter, Limb, Uint, Word...
2
91
69
aafb7b1ef7a0b7e16a7afb12ea4bda76237efde8
Tony Arcieri
2023-12-17T19:17:25
Bernstein-Yang: minor cleanups (#456) Use `Self::ZERO` as a default value for constructing results, rather than `[0; LIMBS]`.
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index e47ca98..ab093e6 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -333,25 +333,25 @@ impl<const LIMBS: usize> Uint62L<LIMBS> { /// Representation of 1. pub const ONE: Self = { - let mut d...
1
16
16
8f46be05162eb6e8827f142311bfc60aa1cbb5d2
Tony Arcieri
2023-12-17T05:19:21
Uint: const fn encoders (#453) Adds `const fn` encoders which can serialize to big or little endian byte arrays.
diff --git a/src/uint/encoding.rs b/src/uint/encoding.rs index c155265..b89e980 100644 --- a/src/uint/encoding.rs +++ b/src/uint/encoding.rs @@ -7,7 +7,10 @@ mod der; mod rlp; use super::Uint; -use crate::{Encoding, Limb, Word}; +use crate::{Limb, Word}; + +#[cfg(feature = "generic-array")] +use crate::Encoding; ...
2
68
8
2f70cb73962f51252963e8ccc9b0cddc33d93d09
Tony Arcieri
2023-12-17T04:27:15
Unexport `const_assert_*` macros (#452) They're extremely limited and very difficult to use in their current form, and could use some work
diff --git a/src/macros.rs b/src/macros.rs index f46efb2..e173ba2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,23 @@ //! Macro definitions which are a part of the public API. +/// Calculate the number of limbs required to represent the given number of bits. +// TODO(tarcieri): replace with `generic_cons...
1
21
22
5519a2c80d45cadf69e387d017a96436cf1648c0
Tony Arcieri
2023-12-17T04:16:13
DynResidue: preliminary Bernstein-Yang benchmarks (#451) Montgomery arithmetic/invert, U256 time: [5.1410 µs 5.1513 µs 5.1637 µs] Montgomery arithmetic/Bernstein-Yang invert, U256 time: [1.9483 µs 1.9645 µs 1.9863 µs] A 2.6X improvement
diff --git a/benches/dyn_residue.rs b/benches/dyn_residue.rs index f1a1f4e..98e840e 100644 --- a/benches/dyn_residue.rs +++ b/benches/dyn_residue.rs @@ -4,15 +4,64 @@ use criterion::{ }; use crypto_bigint::{ modular::{DynResidue, DynResidueParams}, - Random, U256, + Inverter, PrecomputeInverter, Random, U2...
1
50
31
43dba9c5904649b9bb74660fb3ba3ea95e67a295
Tony Arcieri
2023-12-17T02:51:08
v0.6.0-pre.3 (#450)
diff --git a/Cargo.lock b/Cargo.lock index 42ecc10..92c389e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ dependencies = [ [[package]] name = "crypto-bigint" -version = "0.6.0-pre.2" +version = "0.6.0-pre.3" dependencies = [ "bincode", "criterion", diff --git a/Cargo.toml b/Cargo.toml index 9bc3...
2
2
2
935b242a7a9119d8aa1c39d03982729630e738f8
Tony Arcieri
2023-12-17T02:40:29
Inverter: doc improvement
diff --git a/src/traits.rs b/src/traits.rs index db67d0a..a06e22c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -167,7 +167,8 @@ pub trait Inverter { /// Output of an inversion. type Output; - /// Compute a modular inversion, returning `None` if the result is undefined (i.e. `value` is zero). + //...
1
2
1
62e19cf0f6860ab8af68f0f00f4cc1f15e319cfc
Tony Arcieri
2023-12-17T02:12:12
Extract sealed `PrecomputeInverterWithAdjuster` trait (#449) This trait is needed for the Montgomery form types to instantiate a Bernstein-Yang inverter which operates in the Montgomery domain, by passing R^2 as the adjuster. It's sealed for now, but could be made public if needed (which might be the case for wr...
diff --git a/src/modular/dyn_residue/inv.rs b/src/modular/dyn_residue/inv.rs index 695d547..0d5d6e4 100644 --- a/src/modular/dyn_residue/inv.rs +++ b/src/modular/dyn_residue/inv.rs @@ -4,7 +4,7 @@ use super::{DynResidue, DynResidueParams}; use crate::{ modular::{inv::inv_montgomery_form, BernsteinYangInverter}, ...
5
21
13
71058cc857c564f4d92e6b164b27251e7781a17b
Tony Arcieri
2023-12-17T00:19:08
Add `DynResidueInverter` (#447) Ala `ResidueInverter` added in #446, adds support for computing modular inverses of `DynResidue`s using Bernstein-Yang.
diff --git a/src/modular.rs b/src/modular.rs index 0af926e..b82b08a 100644 --- a/src/modular.rs +++ b/src/modular.rs @@ -33,7 +33,7 @@ pub(crate) mod boxed_residue; pub use self::{ bernstein_yang::BernsteinYangInverter, - dyn_residue::{DynResidue, DynResidueParams}, + dyn_residue::{inv::DynResidueInverter...
5
113
16
3d640a5a1f404f11cfe1532c92b53599d2f47c01
Tony Arcieri
2023-12-16T21:57:55
Add `ResidueInverter` (#446) Adds support for computing modular inverses of `Residue`s using Bernstein-Yang. The implementation supports fully `const fn` usage(!), as well as a trait-based API using the `Inverter` trait. It's composed in terms of the `Inverter` impl on `Uint`. Includes one test using the sa...
diff --git a/src/modular.rs b/src/modular.rs index 0fc3ff3..0af926e 100644 --- a/src/modular.rs +++ b/src/modular.rs @@ -35,7 +35,7 @@ pub use self::{ bernstein_yang::BernsteinYangInverter, dyn_residue::{DynResidue, DynResidueParams}, reduction::montgomery_reduction, - residue::{Residue, ResidueParams...
5
128
10
c3760149f887b15d382518c51bf10932cf71e636
Tony Arcieri
2023-12-16T20:53:21
Bernstein-Yang: fallible constructors; return `CtOption` (#445) Adds a `ConstChoice` to the `const fn` versions of a Bernstein-Yang constructor as well as computing inversions on non-`NonZero` inputs. Changes the `Inverter` trait to return `CtChoice` on non-`NonZero` inputs (`NonZero` inputs are still infallible)...
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index bff482c..6a37c29 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -10,7 +10,8 @@ #![allow(clippy::needless_range_loop)] -use crate::{Inverter, Limb, Uint, Word}; +use crate::{ConstChoice, Inverter, Lim...
4
23
18
6e1fe9f222f07190f046d9761853fa3e7047ccc0
Tony Arcieri
2023-12-16T20:19:05
Add new `Inverter` trait; rename `PrecomputeInverter` (#444) Adds a trait for abstracting over various sized `BernsteinYangInverter` (and prospectively a `BoxedBernsteinYangInverter`) types, which also opens the door to impl'ing `PrecomputeInverter` for `*ResidueParams`, where the inverter would accept a `*Residue`...
diff --git a/src/macros.rs b/src/macros.rs index c5a82be..f46efb2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -63,6 +63,15 @@ macro_rules! nlimbs { }; } +/// Calculate the number of 62-bit unsaturated limbs required to represent the given number of bits when performing +/// Bernstein-Yang inversions. +// ...
6
63
34
45e6e077d150c059833730b1700f6e3eb5a8f042
Tony Arcieri
2023-12-16T17:37:34
BoxedResidue: ensure modpow results are fully reduced (#443) Modular exponentiation of `BoxedResidue` is performed using "almost Montgomery multiplication" whose output is reduced to the bit size of the modulus, but not the modulus itself. This is fine for the intermediate values in the operation, which consist of ...
diff --git a/src/modular/boxed_residue/pow.rs b/src/modular/boxed_residue/pow.rs index 90d167e..e0e33b3 100644 --- a/src/modular/boxed_residue/pow.rs +++ b/src/modular/boxed_residue/pow.rs @@ -3,7 +3,7 @@ use super::{mul::MontgomeryMultiplier, BoxedResidue}; use crate::{BoxedUint, Limb, PowBoundedExp, Word}; use all...
1
6
1
6004fccfb3438a9b1ff9a842ab2bb6b75d880b64
Tony Arcieri
2023-12-15T23:05:28
v0.6.0-pre.2 (#439)
diff --git a/Cargo.lock b/Cargo.lock index c1513d2..42ecc10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ dependencies = [ [[package]] name = "crypto-bigint" -version = "0.6.0-pre.1" +version = "0.6.0-pre.2" dependencies = [ "bincode", "criterion", diff --git a/Cargo.toml b/Cargo.toml index 5074...
2
2
2
9be64a34d68226a5de402ddd29bbed4c4cbc02be
Tony Arcieri
2023-12-15T22:57:35
Add `Inverter` trait (#438) Adds a trait for obtaining a `BernsteinYangInverter` for the given `Uint` modulus, precomputing the number of unsaturated limbs needed for the 62-bit unsaturated form used by Bernstein-Yang at compile time, and associating the proper `BernsteinYangInverter` for a given `Uint` size as an...
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index 73a1d5b..9de2126 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -53,17 +53,17 @@ type Matrix = [[i64; 2]; 2]; impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> BernsteinYangInverter<SAT_LIMBS, ...
5
59
8
b01e8bb486467ee666e7ba206d3318c416bb3c1f
Tony Arcieri
2023-12-15T18:39:49
Expand `Integer` trait (#435) Adds a number of new traits to the bounds including `Add`, `Sub`, and `Mul`, and expands HRTB coverage so that every trait which doesn't implicitly use `&Rhs` is covered for both owned and reference forms.
diff --git a/src/traits.rs b/src/traits.rs index 12a605e..dee2e3f 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -7,8 +7,8 @@ pub use num_traits::{ use crate::{Limb, NonZero}; use core::fmt::Debug; use core::ops::{ - BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Not, Rem, Shl,...
1
23
14
a4c3596ac63f8b0aa513764f5787becd17c1bf39
Tony Arcieri
2023-12-15T05:25:29
Bring `Checked*` traits in line with `Wrapping*` (#434) This crate defines its own `Checked*` traits in order to use `CtOption<T>` rather than `Option<T>`, however it gets its `Wrapping*` traits from `num-bigint`. This brings the `Checked*` traits more in line with `Wrapping*`, automatically taking a reference t...
diff --git a/src/checked.rs b/src/checked.rs index d1f6160..2ed355c 100644 --- a/src/checked.rs +++ b/src/checked.rs @@ -1,5 +1,7 @@ //! Checked arithmetic. +use crate::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub}; +use core::ops::{Add, Div, Mul, Sub}; use subtle::{Choice, ConditionallySelectable, ConstantTimeE...
13
282
383
95fa6aa3c7d639dadc8353499c580d99027d2ce1
Tony Arcieri
2023-12-15T04:28:48
Expand `num_traits` impls (#433) Impls `num_traits::{Zero, One}` for: - `Limb` - `Uint` - `BoxedUint` - `Wrapping` To impl the traits for `Wrapping`, it required generic impls of `Add` and `Mul`, which are possible thanks to the `WrappingAdd`/`WrappingMul` traits. These subsume the per-type impls that exis...
diff --git a/src/limb.rs b/src/limb.rs index e6488bc..cc2119f 100644 --- a/src/limb.rs +++ b/src/limb.rs @@ -21,7 +21,7 @@ mod rand; use crate::{Bounded, Constants, ZeroConstant}; use core::fmt; -use subtle::{Choice, ConditionallySelectable}; +use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; #[cfg(...
17
451
325
80c9029187bcac3b098e1f6d2503db0baf6bf360
Tony Arcieri
2023-12-15T02:43:26
Uint: rename `HLIMBS` to `RHS_LIMBS` (#432) I'm not even sure what "HLIMBS" was supposed to stand for... high limbs perhaps? It's a constant for the number of limbs in the `rhs` parameter, i.e. the right-hand operand, so this renames it to `RHS_LIMBS`, which is much easier to visually scan versus `LIMBS` than `H...
diff --git a/src/uint/mul.rs b/src/uint/mul.rs index c1c1033..0d7b580 100644 --- a/src/uint/mul.rs +++ b/src/uint/mul.rs @@ -52,12 +52,12 @@ macro_rules! impl_schoolbook_multiplication { impl<const LIMBS: usize> Uint<LIMBS> { /// Multiply `self` by `rhs`, returning a concatenated "wide" result. - pub fn wide...
1
60
51
3819a4df050150db4028be50e7b599806378d4a8
Tony Arcieri
2023-12-15T02:35:24
Impl `Wrapping*` traits from `num-traits` (#425) Until now we have used traits defined in this crate, primarily because many of these traits are predicates that return `bool` and in constant-time code we want those to return `Choice`/`CtOption`. `Wrapping*` is an example of the sort of trait we'd want to incorpor...
diff --git a/Cargo.toml b/Cargo.toml index 3b1a54e..1526e60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ subtle = { version = "2.5", default-features = false } # optional dependencies der = { version = "0.7", optional = true, default-features = false } generic-array = { version = "0.14", optional = tru...
24
500
192
d5e00baa80f0d3fd10edbe3ca7fca080ced93398
Tony Arcieri
2023-12-15T00:37:43
Align with `core`/`std` on `overflowing_sh*` (#430) Changes all shift functions which return an overflow flag (as a `Choice` or `ConstChoice`) to use the `overflowing_sh*` name prefix, which aligns with similar APIs in `core`/`std`. In their place, adds new `Uint::{shl, shr}` functions which provide the trait-li...
diff --git a/benches/boxed_uint.rs b/benches/boxed_uint.rs index b34eaa3..dbdf3dc 100644 --- a/benches/boxed_uint.rs +++ b/benches/boxed_uint.rs @@ -19,7 +19,7 @@ fn bench_shifts(c: &mut Criterion) { group.bench_function("shl", |b| { b.iter_batched( || BoxedUint::random(&mut OsRng, UINT_BITS)...
14
133
93
cc3f984a3627a846c957ecc945141d2b15df58c1
Tony Arcieri
2023-12-14T21:12:04
BoxedUint: optimize `shr_assign` (#429) Allows `shl_assign` to operate in-place on `self` (although it still requires a temporary buffer), ala what #423 did for `shl_assign`
diff --git a/src/uint/boxed/shl.rs b/src/uint/boxed/shl.rs index 09ad153..3c25e68 100644 --- a/src/uint/boxed/shl.rs +++ b/src/uint/boxed/shl.rs @@ -17,9 +17,8 @@ impl BoxedUint { /// Computes `self <<= shift`. /// - /// Returns a zero and a truthy `Choice` if `shift >= self.bits_precision()`, - /// o...
2
22
11
63a15e052c8695b2a3d9eef5363d271f891669d7
Tony Arcieri
2023-12-14T21:00:12
BoxedUint: optimize `shl_assign` (#423) Allows `shl_assign` to operate in-place on `self` (although it still requires a temporary buffer)
diff --git a/src/uint/boxed/shl.rs b/src/uint/boxed/shl.rs index 17d22a2..09ad153 100644 --- a/src/uint/boxed/shl.rs +++ b/src/uint/boxed/shl.rs @@ -10,27 +10,37 @@ impl BoxedUint { /// Returns a zero and a truthy `Choice` if `shift >= self.bits_precision()`, /// or the result and a falsy `Choice` otherwise. ...
1
18
8
d5c8dad50d4be6c24e4a3fe1537e8599fc2c23bf
Tony Arcieri
2023-12-14T19:17:36
BoxedUint: optimized `AddAssign`/`SubAssign` impls (#427) Uses `adc_assign`/`sbb_assign` to perform these operations in-place.
diff --git a/src/uint/boxed/add.rs b/src/uint/boxed/add.rs index 5a00644..cd5cb14 100644 --- a/src/uint/boxed/add.rs +++ b/src/uint/boxed/add.rs @@ -94,13 +94,13 @@ impl Add<&Wrapping<BoxedUint>> for &Wrapping<BoxedUint> { impl AddAssign<Wrapping<BoxedUint>> for Wrapping<BoxedUint> { fn add_assign(&mut self, ot...
2
16
3
3094c661566b149ccac7ad59593da9c22db45dea
Tony Arcieri
2023-12-14T19:03:19
Add `Zero::set_zero` method (#426) Adds a method to the `Zero` trait for setting its value to zero in-place, subsuming the previous `BoxedUint::set_to_zero` function. It's inspired by a similar method on `num_traits::Zero` (which we don't use so `is_zero` can return `Choice` and use `ConstantTimeEq` to compare v...
diff --git a/src/traits.rs b/src/traits.rs index 81edcd1..042cd6e 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -135,9 +135,16 @@ pub trait Zero: ConstantTimeEq + Sized { /// # Returns /// /// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`. + #[inline] fn is_zero(&self) -> Choic...
4
19
12
4bf6932cee08af70d0a05625b540242522cc77f3
Tony Arcieri
2023-12-14T02:56:22
`BoxedUint`: use `Integer::{is_even, is_odd}` (#424) Avoid redefining these methods by using the provided impls on the trait
diff --git a/src/modular/boxed_residue.rs b/src/modular/boxed_residue.rs index 7bc2fd8..627d895 100644 --- a/src/modular/boxed_residue.rs +++ b/src/modular/boxed_residue.rs @@ -12,7 +12,7 @@ use super::{ reduction::{montgomery_reduction_boxed, montgomery_reduction_boxed_mut}, Retrieve, }; -use crate::{BoxedU...
5
4
31
4314261bc13c36357d1818ca16c76dfdd449bc26
Bogdan Opanchuk
2023-12-14T00:25:13
Make division methods take `NonZero`-wrapped divisors (#419) This simplifies the API allowing us to avoid returning `CtChoice` results, and making some methods redundant. Also that's what `BoxedUint` is already doing. - Simplify `Reciprocal` API by taking a `NonZero<Limb>` in `Reciprocal::new()` which is now `const...
diff --git a/benches/uint.rs b/benches/uint.rs index 3688a2d..5fbd5c6 100644 --- a/benches/uint.rs +++ b/benches/uint.rs @@ -60,8 +60,11 @@ fn bench_division(c: &mut Criterion) { b.iter_batched( || { let x = U256::random(&mut OsRng); - let y = Limb::random(&mut OsRn...
14
131
240
7359ebce8e2e421c6217c67708bf56a5c04a5cf5
Tony Arcieri
2023-12-13T18:49:17
v0.6.0-pre.1 (#421)
diff --git a/Cargo.lock b/Cargo.lock index dfe1e7d..c1513d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ dependencies = [ [[package]] name = "crypto-bigint" -version = "0.6.0-pre.0" +version = "0.6.0-pre.1" dependencies = [ "bincode", "criterion", diff --git a/Cargo.toml b/Cargo.toml index 89dd...
2
2
2
c529c866fb36ca5aad1b57ed3a98e9cc147d233d
Artyom Pavlov
2023-12-13T14:49:39
Restore preconditions check in montgomery_mul (#420)
diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index a161bce..57b0758 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -197,10 +197,14 @@ fn montgomery_mul(z: &mut [Limb], x: &[Limb], y: &[Limb], m: &[Limb], k: Limb) { // It also assumes that...
1
8
4
5ee582b2d1b7077f1e0737210506c53193c24939
Bogdan Opanchuk
2023-12-12T19:58:43
Make `inv_mod2k(_vartime)` return a `CtChoice` (#416) * Make `inv_mod2k(_vartime)` return a CtChoice indicating if the inverse exists * Make `inv_odd_mod()` return a falsy CtChoice if the given modulus is even * Make `BoxedUint::inv_mod2k(_vartime)` return a Choice indicating if the inverse exists * Make `BoxedUint...
diff --git a/src/ct_choice.rs b/src/ct_choice.rs index 10c2b22..205564e 100644 --- a/src/ct_choice.rs +++ b/src/ct_choice.rs @@ -158,6 +158,12 @@ impl From<CtChoice> for bool { } } +impl PartialEq for CtChoice { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + #[cfg(test)] mod ...
7
143
67
90e472a78fa4a1843ef229b7a5e10d22304fa270
Tony Arcieri
2023-12-10T20:13:04
BoxedUint: fix a constant-time TODO in `montgomery_mul` (#414) Uses constant-time comparisons instead of variable-time ones. This does not appear to impact performance: Montgomery arithmetic/modpow, BoxedUint^BoxedUint time: [24.423 ms 24.441 ms 24.460 ms] chan...
diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index c403de8..a161bce 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -11,7 +11,7 @@ use core::{ borrow::Borrow, ops::{Mul, MulAssign}, }; -use subtle::ConditionallySelectable; +use subtl...
1
2
4
49664077b9c6d853383541a0bb25fc7824d016ef
Tony Arcieri
2023-12-10T19:46:32
Limb: optimize constant-time comparisons (#413) Optimizes `ConstantTimeGreater`/`ConstantTimeLess` impls by using borrowing subtraction and checking whether a borrow occurred. ops/ct_lt time: [900.95 ps 909.83 ps 922.37 ps] change: [-76.095% -75.752% -75.373%] (p = 0.00 < 0...
diff --git a/src/limb/cmp.rs b/src/limb/cmp.rs index a8d8622..1de0f44 100644 --- a/src/limb/cmp.rs +++ b/src/limb/cmp.rs @@ -44,19 +44,26 @@ impl ConstantTimeEq for Limb { fn ct_eq(&self, other: &Self) -> Choice { self.0.ct_eq(&other.0) } + + #[inline] + fn ct_ne(&self, other: &Self) -> Choice ...
1
9
2
1ab049f6f4376025f9b64952cae8c16e8a21dc11
Tony Arcieri
2023-12-10T19:31:41
Limb: add initial benchmarks (#412) Adds benchmarks for constant-time comparison operations, with the goal of optimizing them better.
diff --git a/Cargo.toml b/Cargo.toml index ece5990..89dd4e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,10 @@ required-features = ["alloc"] name = "dyn_residue" harness = false +[[bench]] +name = "limb" +harness = false + [[bench]] name = "uint" harness = false diff --git a/benches/limb.rs b/benches/li...
2
59
0
1aa32e9a8acfdb34cdd2db00a10a62e4e909f430
Tony Arcieri
2023-12-10T19:17:35
Limb: simplify `Ord` impl (#411) Uses the `ConditionallySelectable` impl on `Ordering` to simplify the implementation.
diff --git a/src/limb/cmp.rs b/src/limb/cmp.rs index bd3fff7..a8d8622 100644 --- a/src/limb/cmp.rs +++ b/src/limb/cmp.rs @@ -2,7 +2,9 @@ use crate::{CtChoice, Limb}; use core::cmp::Ordering; -use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess}; +use subtle::{ + Choice, ConditionallySelect...
1
8
14
8263c76d2c7d8abc35868ee455545a873fc0df05
Tony Arcieri
2023-12-10T19:05:33
BoxedUint: use `Limb`s in `montgomery_mul` (#410) This was previously written using raw `Word`s to ensure maximum performance, however it precludes access to the constant-time helper functions implemented on `Limb`. This carefully rewrites the implementation to using `Limb` instead of `Word` while avoiding any p...
diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index 293f57d..c403de8 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -6,12 +6,12 @@ //! Originally (c) 2014 The Rust Project Developers, dual licensed Apache 2.0+MIT. use super::{BoxedResidue, ...
1
27
26
b02bfc7723074f30fd5b179d4cfd0eb771e872db
Tony Arcieri
2023-12-10T18:24:48
Eliminate some timing variability in `montgomery_mul` (#409) Always performs `sub_vv` on the lower half of the output, and conditionally assigns `upper` to `lower` using `subtle`. It doesn't matter if we unconditionally subtract, since the output will be overwritten in the case where it's "unnecessary" (but actua...
diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index 66e6d77..293f57d 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -11,6 +11,7 @@ use core::{ borrow::Borrow, ops::{Mul, MulAssign}, }; +use subtle::{ConditionallySelectable, ConstantT...
1
17
12
9bf67d5affbf9dc7b9d2d607c6360ea7b40b59fa
Tony Arcieri
2023-12-09T22:25:41
BoxedResidue: refactor asserts (#408) Move asserts around to cover more cases and ensure that the output is fully reduced.
diff --git a/src/modular/boxed_residue.rs b/src/modular/boxed_residue.rs index 58fc2db..f37edd4 100644 --- a/src/modular/boxed_residue.rs +++ b/src/modular/boxed_residue.rs @@ -191,6 +191,7 @@ impl BoxedResidue { #[cfg(feature = "zeroize")] montgomery_form.zeroize(); + debug_assert!(ret < sel...
2
7
5
1a0399d4f1f72aabba720074be6893eebf08c39b
Tony Arcieri
2023-12-08T14:41:56
Add `BoxedResidue::new_with_arc` (#407) When the `std` feature is available, allows passing `BoxedResidueParams` as an `Arc` which avoids unnecessary cloning.
diff --git a/src/modular/boxed_residue.rs b/src/modular/boxed_residue.rs index 10f7f5d..58fc2db 100644 --- a/src/modular/boxed_residue.rs +++ b/src/modular/boxed_residue.rs @@ -153,24 +153,26 @@ impl BoxedResidue { /// Instantiates a new [`BoxedResidue`] that represents an integer modulo the provided params. ...
1
28
11
68e958cd9b0a56ee78f34ea6af0a2589721af8c3
Friedel Ziegelmayer
2023-12-08T14:17:57
perf: avoid allocations in the div_rem_unchecked loop (#404)
diff --git a/src/uint/boxed/div.rs b/src/uint/boxed/div.rs index 39379cc..c8a6621 100644 --- a/src/uint/boxed/div.rs +++ b/src/uint/boxed/div.rs @@ -83,9 +83,9 @@ impl BoxedUint { loop { let (mut r, borrow) = rem.sbb(&c, Limb::ZERO); - rem = Self::conditional_select(&r, &rem, Choice::...
1
3
3
06b608d60fb079bdfc4f4569d48de7e360f11f09
aumetra
2023-12-08T01:06:05
Implement `Zeroize` for `NonZero` wrapper (#406)
diff --git a/src/non_zero.rs b/src/non_zero.rs index 96c98c3..6db0786 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -367,6 +367,13 @@ impl<T: Serialize + Zero> Serialize for NonZero<T> { } } +#[cfg(feature = "zeroize")] +impl<T: zeroize::Zeroize + Zero> zeroize::Zeroize for NonZero<T> { + fn zeroize(...
1
7
0
4d242d27d1250b9eb018c272c6fcf4673df1247b
Tony Arcieri
2023-12-07T15:52:22
Add `BoxedResidueParams::new_vartime` (#401) Adds a variable-time constructor usable when the modulus is non-secret, such as is the case with RSA, providing a ~6X performance improvement. Benchmarks: Montgomery arithmetic/BoxedResidueParams::new time: [8.3193 ms 8.3255 ms 8.3326 ms] ...
diff --git a/benches/boxed_residue.rs b/benches/boxed_residue.rs index 1eafc2f..ae142ab 100644 --- a/benches/boxed_residue.rs +++ b/benches/boxed_residue.rs @@ -73,17 +73,25 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) { ); (x_m, p) }, - ...
3
65
7
e2bb31697a22eac5ae9893fbc274115783b12e25
Tony Arcieri
2023-12-07T13:21:42
`BoxedResidue::pow` using "almost Montgomery multiplication" (#399) Implements `BoxedResidue::pow` using the "almost Montgomery multiplication" method used by `num-bigint` as originally added in rust-num/num-bigint@f523b9c359b0b4e8718176aba693c6e061cd0353 Benchmark: Montgomery arithmetic/modpow, BoxedUint^Boxe...
diff --git a/benches/boxed_residue.rs b/benches/boxed_residue.rs index b6deb29..1eafc2f 100644 --- a/benches/boxed_residue.rs +++ b/benches/boxed_residue.rs @@ -6,11 +6,16 @@ use crypto_bigint::{ modular::{BoxedResidue, BoxedResidueParams}, BoxedUint, }; +use num_bigint::BigUint; use rand_core::OsRng; //...
4
155
21
207040790d48b44293f79b6a68abfbd814ee110b
Tony Arcieri
2023-12-07T13:08:09
Use `black_box` on benchmarks; 4096-bit BoxedResidue (#400) This avoids the compiler potentially optimizing away part or all of an operation inside of a benchmark. Using a larger `BoxedResidue` is both more indicative of real-world usages as well as reduces noise in the benchmark.
diff --git a/benches/boxed_residue.rs b/benches/boxed_residue.rs index 941e04f..b6deb29 100644 --- a/benches/boxed_residue.rs +++ b/benches/boxed_residue.rs @@ -1,5 +1,6 @@ use criterion::{ - criterion_group, criterion_main, measurement::Measurement, BatchSize, BenchmarkGroup, Criterion, + black_box, criterion_g...
3
28
25
f6d0374d79e80eb984f13a3f7d30fd94e4a5c3e4
Tony Arcieri
2023-12-06T16:58:43
BoxedUint: add constant-time division implementation (#398) Adapts the implementation originally from #277 to `BoxedUint`, adding the following methods: - `BoxedUint::div_rem` - `BoxedUint::rem` Additionally, `wrapping_div` and `checked_div` have been changed to use the constant-time versions, rather than `*_...
diff --git a/src/modular/boxed_residue.rs b/src/modular/boxed_residue.rs index 1b0f364..933e155 100644 --- a/src/modular/boxed_residue.rs +++ b/src/modular/boxed_residue.rs @@ -58,20 +58,18 @@ impl BoxedResidueParams { .expect("modulus ensured non-zero"); let r = BoxedUint::max(bits_precision) - ...
4
127
18
41ba936904b7552936303b23ac539aa03a1c2377
Tony Arcieri
2023-12-06T16:12:50
BoxedUint: rename `conditional_*` methods for consistency (#397) Some were previously named `cond_*` instead
diff --git a/src/uint/boxed/ct.rs b/src/uint/boxed/ct.rs index a8d03cd..5b88c6f 100644 --- a/src/uint/boxed/ct.rs +++ b/src/uint/boxed/ct.rs @@ -60,17 +60,17 @@ impl BoxedUint { /// intermediate [`CtOption`] value and therefore isn't fully constant time, but the best we can /// do without upstream changes to ...
1
24
24
22aa6dde18540f5fd89a477fe952678e35ea1f70
Tony Arcieri
2023-12-06T16:03:03
BoxedUint: move `subtle` support into `ct` module (#396) Groups all the `subtle`-based constant time code together under `uint::boxed::ct`. This is mostly the `ConditionallySelectable`-alike methods, but we can't actually impl that trait due to its `Copy` bound.
diff --git a/src/uint/boxed.rs b/src/uint/boxed.rs index b1654d8..25cdce1 100644 --- a/src/uint/boxed.rs +++ b/src/uint/boxed.rs @@ -27,7 +27,7 @@ mod rand; use crate::{Integer, Limb, NonZero, Uint, Word, Zero, U128, U64}; use alloc::{boxed::Box, vec, vec::Vec}; use core::{fmt, mem}; -use subtle::{Choice, Conditiona...
2
61
55
bc53cfee094d66f4fbcc7e44139ef78117550b0b
Tony Arcieri
2023-12-05T03:51:36
BoxedResidue: use conditional assignment in `pow` (#393) Changes `BoxedUint::conditional_assign` to actually be an in-place operation rather than allocating, and uses it to impl `BoxedResidue::pow`. This leads to a fairly significant performance increase. Montgomery arithmetic/modpow, BoxedUint^BoxedUint ...
diff --git a/src/modular/boxed_residue/pow.rs b/src/modular/boxed_residue/pow.rs index 2a36d67..63fc872 100644 --- a/src/modular/boxed_residue/pow.rs +++ b/src/modular/boxed_residue/pow.rs @@ -61,10 +61,9 @@ fn pow_montgomery_form( // powers[i] contains x^i let mut powers = vec![r.clone(); 1 << WINDOW]; ...
2
12
14
6f71ecb60ef9fdcb245bcea069ada0b8baad7cb2
Tony Arcieri
2023-12-05T03:32:47
BoxedResidue: use in-place Montgomery ops for modpow (#392) Avoids allocating the immediate intermediate Montgomery form value used for modpow, instead performing multiply and squarings (partially) in-place. Montgomery arithmetic/modpow, BoxedUint^BoxedUint time: [44.597 µs 44.653 µs 44...
diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index 2f732d8..a8a223c 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -1,6 +1,6 @@ //! Multiplications between boxed residues. -use super::{montgomery_reduction_boxed, BoxedResidue}; +use super::...
2
28
10
23048587bdaf68fa83c3381255a2ca7fd942a7fa
Tony Arcieri
2023-12-05T03:08:44
BoxedUint: implement in-place conditional addition/subtraction (#391) Adds internal `conditional_adc_assign` and `conditional_sbb_assign` methods on `BoxedUint` which conditionally perform in-place addition/subtraction with carry. This eliminates the last allocations in Montgomery reduction as well as a number o...
diff --git a/src/modular/reduction.rs b/src/modular/reduction.rs index cf2d3de..2b8faf1 100644 --- a/src/modular/reduction.rs +++ b/src/modular/reduction.rs @@ -1,7 +1,9 @@ +//! Modular reduction implementation. + use crate::{Limb, Uint, WideWord, Word}; #[cfg(feature = "alloc")] -use crate::BoxedUint; +use {crate:...
4
44
33
2a08643adf6ff3251c50f222668d8ef1018d1899
Tony Arcieri
2023-12-05T02:04:56
BoxedResidue: (almost) in-place Montgomery reductions (#390) Eliminates several allocations by performing most operations of a Montgomery reduction in-place. Only conditionally adding the modulus presently incurs an allocation. Montgomery arithmetic/BoxedResidue creation time: [135.30...
diff --git a/benches/boxed_residue.rs b/benches/boxed_residue.rs index 0d6bcbf..941e04f 100644 --- a/benches/boxed_residue.rs +++ b/benches/boxed_residue.rs @@ -18,10 +18,8 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) { group.bench_function("multiplication, BoxedUint*BoxedUint", |b...
12
122
50
910bea9cd8148944d5730cb064b01aaf0397762d
Tony Arcieri
2023-12-04T23:58:14
BoxedUint: bit shift improvements (#389) Analogous changes to #388 for `BoxedUint`, which eliminates some allocations by using in-place operations instead. Montgomery arithmetic/BoxedResidueParams creation time: [17.577 µs 17.599 µs 17.625 µs] change: [-25.727% -...
diff --git a/src/uint/boxed/div.rs b/src/uint/boxed/div.rs index e8d70fd..c9712cf 100644 --- a/src/uint/boxed/div.rs +++ b/src/uint/boxed/div.rs @@ -34,7 +34,7 @@ impl BoxedUint { break rem; } bd -= 1; - c = c.shr_vartime(1); + c.shr1_assign(); }...
3
33
28
b169d2fafba73359fbd22727ffd969b4a6f60f22
Tony Arcieri
2023-12-04T22:30:23
Uint: bit shift improvements (#388) - Add `shl1` and `shr1` methods - Change function for returning overflow bit to `shr1_with_overflow` - Move `HI_BIT` to an inherent constant of `Limb`
diff --git a/src/lib.rs b/src/lib.rs index f38bcc9..5b1b1fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,7 @@ //! U256::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"); //! //! // Compute `MODULUS` shifted right by 1 at compile time -//! pub const MODULUS_SHR1: U256 = MOD...
9
52
53
124b8cf79cc4532d0e27805cb390a642784d7ac2
Tony Arcieri
2023-12-04T20:52:17
Revert "BoxedUint: improve efficiency of `square` (#382)" (#387) This reverts commit cd9cdcecf9cd27fae48843905581b053c31952e0. Benchmarks reveal this caused a performance regression to modpow. Reverting it yields the following improvement: Montgomery arithmetic/modpow, BoxedUint^BoxedUint ...
diff --git a/src/uint/boxed/mul.rs b/src/uint/boxed/mul.rs index e672319..1a747e5 100644 --- a/src/uint/boxed/mul.rs +++ b/src/uint/boxed/mul.rs @@ -1,9 +1,6 @@ //! [`BoxedUint`] multiplication operations. -use crate::{ - uint::mul::{diagonal_mul_limbs, half_mul_limbs, mul_limbs}, - BoxedUint, CheckedMul, Limb...
3
69
115
a3ab554e82e346f61cb1611466e7b4249cf4bb13
Tony Arcieri
2023-12-04T20:12:15
BoxedResidue: initial benchmarks (#386)
diff --git a/Cargo.toml b/Cargo.toml index 0a0d6d0..ece5990 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,11 @@ serde = ["dep:serdect"] all-features = true rustdoc-args = ["--cfg", "docsrs"] +[[bench]] +name = "boxed_residue" +harness = false +required-features = ["alloc"] + [[bench]] name = "dyn_residue"...
2
97
0
a08fc09d86afbc2e954ac321041f35daa5ae6a14
Tony Arcieri
2023-12-04T19:53:21
Reorganize benchmarks (#385) Splits apart the `Uint` and `DynResidue` benchmarks, in preparation for adding benchmarks for `BoxedUint` and `BoxedResidue`.
diff --git a/Cargo.toml b/Cargo.toml index 6bb5ab6..0a0d6d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,5 +52,9 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [[bench]] -name = "bench" +name = "dyn_residue" +harness = false + +[[bench]] +name = "uint" harness = false diff --git a/benches/dyn_residue...
3
116
108
9faa2c0a23f1f3300009715ea79edef47cc65d46
Tony Arcieri
2023-12-04T16:01:38
Reorganize bit shift operations (#384) - Places `shl`/`shr` ahead of `shl_vartime`/`shr_vartime` - Removes variable-time comments about trait impls that call the constant-time bit shift functions - Renames internal `sh*_1` functions to `sh*1`.
diff --git a/src/modular/div_by_2.rs b/src/modular/div_by_2.rs index 20c0a5d..e78b6ee 100644 --- a/src/modular/div_by_2.rs +++ b/src/modular/div_by_2.rs @@ -18,7 +18,7 @@ pub(crate) fn div_by_2<const LIMBS: usize>(a: &Uint<LIMBS>, modulus: &Uint<LIMBS // ("+1" because both `a` and `modulus` are odd, we lose 0.5 in...
7
146
186
d533e8730d6a1cf2263233399177278bf9c41456
Tony Arcieri
2023-12-04T15:52:31
BoxedResidue: use faster squaring algorithm (#383) Uses the faster `BoxedUint::square` algorithm
diff --git a/src/modular/boxed_residue/mul.rs b/src/modular/boxed_residue/mul.rs index eb3e51f..2f732d8 100644 --- a/src/modular/boxed_residue/mul.rs +++ b/src/modular/boxed_residue/mul.rs @@ -113,6 +113,13 @@ pub(super) fn square_montgomery_form( modulus: &BoxedUint, mod_neg_inv: Limb, ) -> BoxedUint { - ...
1
9
2
cd9cdcecf9cd27fae48843905581b053c31952e0
Tony Arcieri
2023-12-04T15:34:26
BoxedUint: improve efficiency of `square` (#382) Uses a more efficient squaring algorithm with fewer iterations, with an implementation shared with `Uint`. Proptested against `num-bigint`.
diff --git a/src/uint/boxed/mul.rs b/src/uint/boxed/mul.rs index 1a747e5..e672319 100644 --- a/src/uint/boxed/mul.rs +++ b/src/uint/boxed/mul.rs @@ -1,6 +1,9 @@ //! [`BoxedUint`] multiplication operations. -use crate::{uint::mul::mul_limbs, BoxedUint, CheckedMul, Limb, WideningMul, Wrapping, Zero}; +use crate::{ + ...
3
115
69
f53faa05638e81f76c7b1c852688f9551240074d
Tony Arcieri
2023-12-03T21:13:45
Add 32-bit optimization TODO for Bernstein-Yang algorithm (#381) See #380
diff --git a/src/modular/bernstein_yang.rs b/src/modular/bernstein_yang.rs index a55aa7c..0b29945 100644 --- a/src/modular/bernstein_yang.rs +++ b/src/modular/bernstein_yang.rs @@ -6,6 +6,8 @@ //! //! Copyright (c) 2023 Privacy Scaling Explorations Team +// TODO(tarcieri): optimized implementation for 32-bit platfo...
1
2
0
1c57cd177137ff642994d5a5036331320aecf536
Bogdan Opanchuk
2023-12-03T21:12:15
Some `sqrt()` fixes (#379) * Disambiguate Uint::LOG2_BITS * Simplify sqrt() loops and add some comments * Add edge case tests for vartime sqrt(), and comments to the tests * Add a TODO for `Uint::sqrt()`
diff --git a/src/uint.rs b/src/uint.rs index fd116fc..35a285f 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -88,9 +88,9 @@ impl<const LIMBS: usize> Uint<LIMBS> { /// Total size of the represented integer in bits. pub const BITS: u32 = LIMBS as u32 * Limb::BITS; - /// Bit size of `BITS`. + /// `floor(l...
4
64
45
7f9301831af6f226f45ad1000ed09e0296b4a0f7
Tony Arcieri
2023-12-03T20:52:55
Bernstein-Yang modular inversion algorithm (#372) Adapted from: privacy-scaling-explorations/halo2curves#83 Original code is Apache 2.0+MIT. Attribution has been added to the top of the module.
diff --git a/src/modular.rs b/src/modular.rs index add5bb8..eb2854b 100644 --- a/src/modular.rs +++ b/src/modular.rs @@ -21,6 +21,7 @@ mod reduction; mod residue; mod add; +mod bernstein_yang; mod div_by_2; mod inv; mod mul; @@ -31,6 +32,7 @@ mod sub; pub(crate) mod boxed_residue; pub use self::{ + bernst...
3
446
0
64add42c57b188154b85afe7a3291890ed3e9783
Tony Arcieri
2023-12-03T14:58:19
Constant-time square root and division (#376) Based on PR #277. The constant-time square root algorithm is described here: https://github.com/RustCrypto/crypto-bigint/files/12600669/ct_sqrt.pdf Co-authored-by: Daniel Hast <32797673+HastD@users.noreply.github.com>
diff --git a/src/uint/div.rs b/src/uint/div.rs index 2d3883c..568c114 100644 --- a/src/uint/div.rs +++ b/src/uint/div.rs @@ -1,7 +1,7 @@ //! [`Uint`] division operations. use super::div_limb::{div_rem_limb_with_reciprocal, Reciprocal}; -use crate::{CheckedDiv, CtChoice, Limb, NonZero, Uint, Wrapping}; +use crate::{...
3
175
53
7d174f02b80ef22d268c2dbc936680be50a20c17
Bogdan Opanchuk
2023-12-02T19:03:12
Round up `bits_precision` when creating `BoxedUint` (#365)
diff --git a/src/limb/encoding.rs b/src/limb/encoding.rs index f690610..d77ab1f 100644 --- a/src/limb/encoding.rs +++ b/src/limb/encoding.rs @@ -34,16 +34,30 @@ impl Encoding for Limb { impl Limb { /// Decode limb from a big endian byte slice. /// - /// Panics if the slice is not the same size as [`Limb::...
6
151
87
4ea713fedb6321cfe92615ac187e2b4784c003b6
Tony Arcieri
2023-12-02T18:00:33
Add `std` feature (#375) - Impl `std::error::Error` for `DecodeError` - Use `Arc` for storing `BoxedResidueParams` when available
diff --git a/Cargo.toml b/Cargo.toml index 6e09f73..6bb5ab6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,8 @@ rand_chacha = "0.3" [features] default = ["rand"] alloc = ["serdect?/alloc"] +std = ["alloc"] + extra-sizes = [] rand = ["rand_core/std"] serde = ["dep:serdect"] diff --git a/src/lib.rs b/src/lib...
5
39
5
0cd01b7a210adfe0e8da7e0c875cc06360f3f934
Friedel Ziegelmayer
2023-12-02T16:26:32
feat(boxed): implement Add and AddAssign for Wrapping<BoxedUint> (#374)
diff --git a/src/uint/boxed/add.rs b/src/uint/boxed/add.rs index 2a4a546..5c08f13 100644 --- a/src/uint/boxed/add.rs +++ b/src/uint/boxed/add.rs @@ -1,6 +1,8 @@ //! [`BoxedUint`] addition operations. -use crate::{BoxedUint, CheckedAdd, Limb, Zero}; +use core::ops::{Add, AddAssign}; + +use crate::{BoxedUint, CheckedA...
1
47
1
31d9373e632da6dc3dcf4e61e83a8931839ba721
Tony Arcieri
2023-11-30T20:46:37
Add `WideningMul` trait (#371) Adds a trait for performing widening multiplication, with an output whose size is equal to the sum of the inputs.
diff --git a/src/traits.rs b/src/traits.rs index 260b442..e63719f 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -272,7 +272,7 @@ pub trait CheckedSub<Rhs = Self>: Sized { /// value as the least significant value. pub trait Concat: ConcatMixed<Self, MixedOutput = Self::Output> { /// Concatenated output: twice...
3
76
4
33e3eb52380dd28dd6ca442a2e0fb0d944cf25ee
Tony Arcieri
2023-11-30T19:21:44
Integer: expand `*Assign` and `From` bounds (#370) Adds bounds for the following to `Integer`: - `BitAndAssign` - `BitOrAssign` - `BitXorAssign` - `DivAssign` - `ShlAssign` - `ShrAssign` Also adds bounds for: - `From<u8>` - `From<u16>` - `From<u32>`
diff --git a/src/traits.rs b/src/traits.rs index e782564..260b442 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -2,7 +2,10 @@ use crate::{Limb, NonZero}; use core::fmt::Debug; -use core::ops::{BitAnd, BitOr, BitXor, Div, Not, Rem, Shl, Shr}; +use core::ops::{ + BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXo...
1
19
1
a57fcf38a71656370a67e920aed7669334e2178f
Tony Arcieri
2023-11-30T17:34:21
Add `CheckedDiv` trait (#369) Adds a trait for checked division by a `Self` divisor that may be zero. This provides feature parity with the other `Checked*` traits, which previously only covered addition, subtraction, and multiplication.
diff --git a/src/traits.rs b/src/traits.rs index 91fb249..e782564 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -31,6 +31,7 @@ pub trait Integer: + for<'a> CheckedAdd<&'a Self, Output = Self> + for<'a> CheckedSub<&'a Self, Output = Self> + for<'a> CheckedMul<&'a Self, Output = Self> + + for<'a> Ch...
3
47
4
030f600c7a1ee9217b7164a9f9c0c4dc3027548c
Tony Arcieri
2023-11-30T17:15:55
Integer: add `bits(_vartime)` and `leading_zeros` methods (#368) Adds the following methods: - `bits`: count the number of bits required to represent a given number in constant-time - `bits_vartime`: count the number of bits required to represent a given number in variable-time - `leading_zeros`: count the nu...
diff --git a/src/traits.rs b/src/traits.rs index 6d634f7..91fb249 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -57,12 +57,24 @@ pub trait Integer: /// The value `1`. fn one() -> Self; + /// Calculate the number of bits required to represent a given number. + fn bits(&self) -> usize; + + /// Ca...
7
112
56
bcb41ff42b0eae91440d60d67c9173e8f2280de5
Tony Arcieri
2023-11-30T04:31:27
Impl `Integer` for `BoxedUint` (#367)
diff --git a/src/uint/boxed.rs b/src/uint/boxed.rs index a44dfd9..1b74c2b 100644 --- a/src/uint/boxed.rs +++ b/src/uint/boxed.rs @@ -24,7 +24,7 @@ mod neg_mod; #[cfg(feature = "rand_core")] mod rand; -use crate::{Limb, NonZero, Uint, Word, Zero, U128, U64}; +use crate::{Integer, Limb, NonZero, Uint, Word, Zero, U12...
1
23
1
1ba85509babc5abbdd2ed983e51d3f4407c74463
Tony Arcieri
2023-11-30T04:22:35
Impl `Div` for `BoxedUint` (#366)
diff --git a/src/uint/boxed/div.rs b/src/uint/boxed/div.rs index 76bfe7b..68afbca 100644 --- a/src/uint/boxed/div.rs +++ b/src/uint/boxed/div.rs @@ -1,10 +1,17 @@ //! [`BoxedUint`] division operations. -use crate::{BoxedUint, Limb, NonZero}; -use core::ops::{Rem, RemAssign}; -use subtle::ConstantTimeEq; +use crate::...
2
156
3
c0c126984cbd9e4daf23734bedc65b69c605475f
Tony Arcieri
2023-11-30T03:46:42
Move `Copy` bound from `Integer` => `FixedInteger` (#364) Allows `Integer` to be impl'd for non-`Copy` types. The bound on `Integer` is replaced by `Clone` to cover the same use cases.
diff --git a/src/traits.rs b/src/traits.rs index 9565e60..6d634f7 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -31,7 +31,7 @@ pub trait Integer: + for<'a> CheckedAdd<&'a Self, Output = Self> + for<'a> CheckedSub<&'a Self, Output = Self> + for<'a> CheckedMul<&'a Self, Output = Self> - + Copy + ...
1
2
2
680f63346661323c0f7a5d610ef94a325461e679
Tony Arcieri
2023-11-30T03:36:06
Add `FixedInteger` trait (#363) Adds a trait specifically for `Uint`-like fixed-width integers whose size is known at compile time. Also adds a blanket impl of the `Integer` trait for all `FixedInteger` types, and adds a default impl of `is_odd` to `Integer`.
diff --git a/src/traits.rs b/src/traits.rs index 4ea098a..9565e60 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -32,7 +32,7 @@ pub trait Integer: + for<'a> CheckedSub<&'a Self, Output = Self> + for<'a> CheckedMul<&'a Self, Output = Self> + Copy - + ConditionallySelectable + // + ConditionallySe...
2
34
36
80634eafb61659ea690840ef654567372b08ea46
Tony Arcieri
2023-11-30T03:24:04
Impl `NegMod` for `BoxedUint` (#362)
diff --git a/src/uint/boxed.rs b/src/uint/boxed.rs index 801f425..a44dfd9 100644 --- a/src/uint/boxed.rs +++ b/src/uint/boxed.rs @@ -20,6 +20,7 @@ mod sub_mod; mod bit_not; mod bit_xor; +mod neg_mod; #[cfg(feature = "rand_core")] mod rand; diff --git a/src/uint/boxed/neg_mod.rs b/src/uint/boxed/neg_mod.rs new f...
4
105
1
e812318b99bf486abb767fd6b75f4c96c34bdf5c
Tony Arcieri
2023-11-30T03:10:39
BoxedUint: impl `CheckedMul` and `Mul` (#361)
diff --git a/src/uint/boxed/mul.rs b/src/uint/boxed/mul.rs index a404603..b740933 100644 --- a/src/uint/boxed/mul.rs +++ b/src/uint/boxed/mul.rs @@ -1,6 +1,8 @@ //! [`BoxedUint`] multiplication operations. -use crate::{uint::mul::mul_limbs, BoxedUint, Limb}; +use crate::{uint::mul::mul_limbs, BoxedUint, CheckedMul, ...
1
65
2
7f5497dec6f43aec8caa2b62e087abd58c24a321
Tony Arcieri
2023-11-30T02:54:23
BoxedUint: share `Mul` impl with `Uint` (#360) Uses a common implementation of schoolbook multiplication factored into the `uint::mul` module. For now, it's impl'd as a macro to share a common implementation between `const fn` and heap-backed `BoxedUint`, since `const_mut_refs` is not yet stable and needed to be...
diff --git a/src/uint/boxed/mul.rs b/src/uint/boxed/mul.rs index e971e18..a404603 100644 --- a/src/uint/boxed/mul.rs +++ b/src/uint/boxed/mul.rs @@ -1,32 +1,15 @@ //! [`BoxedUint`] multiplication operations. -use crate::{BoxedUint, Limb}; +use crate::{uint::mul::mul_limbs, BoxedUint, Limb}; impl BoxedUint { ...
2
56
53
beed3fbcd4218fd6485cb2bb97f54916a4d70c6a
Tony Arcieri
2023-11-30T02:30:42
BoxedUint: rename `mul_wide` to `mul` (#359) This is more consistent with `Uint`, where `mul` returns an output whose limbs are equal to the sum of the input limb counts. `Uint::mul_wide` returns a tuple of lo and hi components, with `Uint::mul` handling the concatenation.
diff --git a/src/modular/boxed_residue.rs b/src/modular/boxed_residue.rs index b3f8d67..c8558ba 100644 --- a/src/modular/boxed_residue.rs +++ b/src/modular/boxed_residue.rs @@ -105,7 +105,7 @@ impl BoxedResidue { pub fn new(integer: &BoxedUint, residue_params: BoxedResidueParams) -> Self { debug_assert_eq...
6
15
16
d97372057b330fe880529e8d478d48def6f0e948
Tony Arcieri
2023-11-30T02:14:12
Impl `BitNot` + `BitXor` for `BoxedUint` (#358)
diff --git a/src/uint/bit_not.rs b/src/uint/bit_not.rs index 6b18933..a3b897d 100644 --- a/src/uint/bit_not.rs +++ b/src/uint/bit_not.rs @@ -23,9 +23,8 @@ impl<const LIMBS: usize> Uint<LIMBS> { impl<const LIMBS: usize> Not for Uint<LIMBS> { type Output = Self; - #[allow(clippy::needless_borrow)] - fn not(...
4
188
3