Skip to content

Commit dc64fb4

Browse files
authored
Reverse Concat(Mixed)/Split(Mixed) argument ordering (#526)
Changes to a "little endian" `lo, hi` convention for the ordering of arguments to concatenation methods and the ordering of the returned 2-tuple from split methods. This is more consistent with the rest of the crate, and the `Uint { limbs }` array which uses a little endian ordering. Closes #519
1 parent 49b54aa commit dc64fb4

File tree

8 files changed

+49
-53
lines changed

8 files changed

+49
-53
lines changed

src/modular.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ mod tests {
121121
#[test]
122122
fn test_reducing_r2_wide() {
123123
// Divide the value ONE^2 by R, which should equal ONE
124-
let (hi, lo) = Modulus2::ONE.square().split();
124+
let (lo, hi) = Modulus2::ONE.square().split();
125125
assert_eq!(
126126
montgomery_reduction::<{ Modulus2::LIMBS }>(
127127
&(lo, hi),
@@ -157,9 +157,9 @@ mod tests {
157157

158158
// Computing xR mod modulus without Montgomery reduction
159159
let (lo, hi) = x.split_mul(&Modulus2::ONE);
160-
let c = hi.concat(&lo);
161-
let red = c.rem_vartime(&NonZero::new(U256::ZERO.concat(&Modulus2::MODULUS)).unwrap());
162-
let (hi, lo) = red.split();
160+
let c = lo.concat(&hi);
161+
let red = c.rem_vartime(&NonZero::new(Modulus2::MODULUS.0.concat(&U256::ZERO)).unwrap());
162+
let (lo, hi) = red.split();
163163
assert_eq!(hi, Uint::ZERO);
164164

165165
assert_eq!(

src/modular/monty_form.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ impl<const LIMBS: usize> MontyParams<LIMBS> {
4646
// `R^2 mod modulus`, used to convert integers to Montgomery form.
4747
let r2 = one
4848
.square()
49-
.rem(&NonZero(Uint::<LIMBS>::ZERO.concat(&modulus.0)))
49+
.rem(&NonZero(modulus.0.concat(&Uint::ZERO)))
5050
.split()
51-
.1;
51+
.0;
5252

5353
// The modular inverse should always exist, because it was ensured odd above, which also ensures it's non-zero
5454
let inv_mod = modulus

src/traits.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -467,49 +467,46 @@ pub trait CheckedSub<Rhs = Self>: Sized {
467467
fn checked_sub(&self, rhs: &Rhs) -> CtOption<Self>;
468468
}
469469

470-
/// Concatenate two numbers into a "wide" double-width value, using the `lo`
471-
/// value as the least significant value.
470+
/// Concatenate two numbers into a "wide" double-width value, using the `hi` value as the most
471+
/// significant portion of the resulting value.
472472
pub trait Concat: ConcatMixed<Self, MixedOutput = Self::Output> {
473473
/// Concatenated output: twice the width of `Self`.
474474
type Output: Integer;
475475

476-
/// Concatenate the two halves, with `self` as most significant and `lo`
477-
/// as the least significant.
478-
fn concat(&self, lo: &Self) -> Self::Output {
479-
self.concat_mixed(lo)
476+
/// Concatenate the two halves, with `self` as least significant and `hi` as the least
477+
/// significant.
478+
fn concat(&self, hi: &Self) -> Self::Output {
479+
self.concat_mixed(hi)
480480
}
481481
}
482482

483-
/// Concatenate two numbers into a "wide" combined-width value, using the `lo`
484-
/// value as the least significant value.
485-
pub trait ConcatMixed<Lo: ?Sized = Self> {
486-
/// Concatenated output: combination of `Lo` and `Self`.
483+
/// Concatenate two numbers into a "wide" combined-width value, using the `hi` value as the most
484+
/// significant value.
485+
pub trait ConcatMixed<Hi: ?Sized = Self> {
486+
/// Concatenated output: combination of `Self` and `Hi`.
487487
type MixedOutput: Integer;
488488

489-
/// Concatenate the two values, with `self` as most significant and `lo`
490-
/// as the least significant.
491-
fn concat_mixed(&self, lo: &Lo) -> Self::MixedOutput;
489+
/// Concatenate the two values, with `self` as least significant and `hi` as the most
490+
/// significant.
491+
fn concat_mixed(&self, hi: &Hi) -> Self::MixedOutput;
492492
}
493493

494-
/// Split a number in half, returning the most significant half followed by
495-
/// the least significant.
494+
/// Split a number in half, returning the least significant half followed by the most significant.
496495
pub trait Split: SplitMixed<Self::Output, Self::Output> {
497-
/// Split output: high/low components of the value.
496+
/// Split output: low/high components of the value.
498497
type Output;
499498

500-
/// Split this number in half, returning its high and low components
501-
/// respectively.
499+
/// Split this number in half, returning its low and high components respectively.
502500
fn split(&self) -> (Self::Output, Self::Output) {
503501
self.split_mixed()
504502
}
505503
}
506504

507-
/// Split a number into parts, returning the most significant part followed by
508-
/// the least significant.
509-
pub trait SplitMixed<Hi, Lo> {
510-
/// Split this number into parts, returning its high and low components
511-
/// respectively.
512-
fn split_mixed(&self) -> (Hi, Lo);
505+
/// Split a number into parts, returning the least significant part followed by the most
506+
/// significant.
507+
pub trait SplitMixed<Lo, Hi> {
508+
/// Split this number into parts, returning its low and high components respectively.
509+
fn split_mixed(&self) -> (Lo, Hi);
513510
}
514511

515512
/// Encoding support.

src/uint/concat.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ mod tests {
4040
let hi = U64::from_u64(0x0011223344556677);
4141
let lo = U64::from_u64(0x8899aabbccddeeff);
4242
assert_eq!(
43-
hi.concat(&lo),
43+
lo.concat(&hi),
4444
U128::from_be_hex("00112233445566778899aabbccddeeff")
4545
);
4646
}
4747

4848
#[test]
4949
fn concat_mixed() {
50-
let a = U64::from_u64(0x0011223344556677);
51-
let b = U128::from_u128(0x8899aabbccddeeff_8899aabbccddeeff);
50+
let hi = U64::from_u64(0x0011223344556677);
51+
let lo = U128::from_u128(0x8899aabbccddeeff_8899aabbccddeeff);
5252
assert_eq!(
53-
a.concat_mixed(&b),
53+
lo.concat_mixed(&hi),
5454
U192::from_be_hex("00112233445566778899aabbccddeeff8899aabbccddeeff")
5555
);
5656
assert_eq!(
57-
b.concat_mixed(&a),
57+
hi.concat_mixed(&lo),
5858
U192::from_be_hex("8899aabbccddeeff8899aabbccddeeff0011223344556677")
5959
);
6060
}

src/uint/from.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,19 @@ impl<const LIMBS: usize> From<Limb> for Uint<LIMBS> {
198198

199199
impl<const L: usize, const H: usize, const LIMBS: usize> From<(Uint<L>, Uint<H>)> for Uint<LIMBS>
200200
where
201-
Uint<H>: ConcatMixed<Uint<L>, MixedOutput = Uint<LIMBS>>,
201+
Uint<L>: ConcatMixed<Uint<H>, MixedOutput = Uint<LIMBS>>,
202202
{
203203
fn from(nums: (Uint<L>, Uint<H>)) -> Uint<LIMBS> {
204-
nums.1.concat_mixed(&nums.0)
204+
nums.0.concat_mixed(&nums.1)
205205
}
206206
}
207207

208208
impl<const L: usize, const H: usize, const LIMBS: usize> From<&(Uint<L>, Uint<H>)> for Uint<LIMBS>
209209
where
210-
Uint<H>: ConcatMixed<Uint<L>, MixedOutput = Uint<LIMBS>>,
210+
Uint<L>: ConcatMixed<Uint<H>, MixedOutput = Uint<LIMBS>>,
211211
{
212212
fn from(nums: &(Uint<L>, Uint<H>)) -> Uint<LIMBS> {
213-
nums.1.concat_mixed(&nums.0)
213+
nums.0.concat_mixed(&nums.1)
214214
}
215215
}
216216

src/uint/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ macro_rules! impl_uint_concat_split_mixed {
7777
{
7878
type MixedOutput = $name;
7979

80-
fn concat_mixed(&self, lo: &Uint<{ U64::LIMBS * $size }>) -> Self::MixedOutput {
81-
$crate::uint::concat::concat_mixed(lo, self)
80+
fn concat_mixed(&self, hi: &Uint<{ U64::LIMBS * $size }>) -> Self::MixedOutput {
81+
$crate::uint::concat::concat_mixed(self, hi)
8282
}
8383
}
8484

@@ -107,16 +107,16 @@ macro_rules! impl_uint_concat_split_even {
107107
{
108108
type MixedOutput = $name;
109109

110-
fn concat_mixed(&self, lo: &Uint<{ <$name>::LIMBS / 2 }>) -> Self::MixedOutput {
111-
$crate::uint::concat::concat_mixed(lo, self)
110+
fn concat_mixed(&self, hi: &Uint<{ <$name>::LIMBS / 2 }>) -> Self::MixedOutput {
111+
$crate::uint::concat::concat_mixed(self, hi)
112112
}
113113
}
114114

115115
impl Uint<{ <$name>::LIMBS / 2 }> {
116116
/// Concatenate the two values, with `self` as most significant and `rhs`
117117
/// as the least significant.
118-
pub const fn concat(&self, lo: &Uint<{ <$name>::LIMBS / 2 }>) -> $name {
119-
$crate::uint::concat::concat_mixed(lo, self)
118+
pub const fn concat(&self, hi: &Uint<{ <$name>::LIMBS / 2 }>) -> $name {
119+
$crate::uint::concat::concat_mixed(self, hi)
120120
}
121121
}
122122

src/uint/mul.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,15 @@ mod tests {
358358
#[test]
359359
fn square() {
360360
let n = U64::from_u64(0xffff_ffff_ffff_ffff);
361-
let (hi, lo) = n.square().split();
361+
let (lo, hi) = n.square().split();
362362
assert_eq!(lo, U64::from_u64(1));
363363
assert_eq!(hi, U64::from_u64(0xffff_ffff_ffff_fffe));
364364
}
365365

366366
#[test]
367367
fn square_larger() {
368368
let n = U256::MAX;
369-
let (hi, lo) = n.square().split();
369+
let (lo, hi) = n.square().split();
370370
assert_eq!(lo, U256::ONE);
371371
assert_eq!(hi, U256::MAX.wrapping_sub(&U256::ONE));
372372
}

src/uint/split.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::{Limb, Uint};
22

3-
/// Split this number in half, returning its high and low components
4-
/// respectively.
3+
/// Split this number in half, returning its low and high components respectively.
54
#[inline]
65
pub(crate) const fn split_mixed<const L: usize, const H: usize, const O: usize>(
76
n: &Uint<O>,
8-
) -> (Uint<H>, Uint<L>) {
7+
) -> (Uint<L>, Uint<H>) {
98
let top = L + H;
109
let top = if top < O { top } else { O };
1110
let mut lo = [Limb::ZERO; L];
@@ -21,7 +20,7 @@ pub(crate) const fn split_mixed<const L: usize, const H: usize, const O: usize>(
2120
i += 1;
2221
}
2322

24-
(Uint { limbs: hi }, Uint { limbs: lo })
23+
(Uint { limbs: lo }, Uint { limbs: hi })
2524
}
2625

2726
#[cfg(test)]
@@ -30,8 +29,8 @@ mod tests {
3029

3130
#[test]
3231
fn split() {
33-
let (hi, lo) = U128::from_be_hex("00112233445566778899aabbccddeeff").split();
34-
assert_eq!(hi, U64::from_u64(0x0011223344556677));
32+
let (lo, hi) = U128::from_be_hex("00112233445566778899aabbccddeeff").split();
3533
assert_eq!(lo, U64::from_u64(0x8899aabbccddeeff));
34+
assert_eq!(hi, U64::from_u64(0x0011223344556677));
3635
}
3736
}

0 commit comments

Comments
 (0)