Skip to content

Commit 6b911bc

Browse files
committed
Rename CtChoice to ConstChoice
1 parent 525a81e commit 6b911bc

File tree

21 files changed

+178
-178
lines changed

21 files changed

+178
-178
lines changed

src/ct_choice.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::Word;
66
// TODO: should be replaced by `subtle::Choice` or `CtOption`
77
// when `subtle` starts supporting const fns.
88
#[derive(Debug, Copy, Clone)]
9-
pub struct CtChoice(Word);
9+
pub struct ConstChoice(Word);
1010

11-
impl CtChoice {
11+
impl ConstChoice {
1212
/// The falsy value.
1313
pub const FALSE: Self = Self(0);
1414

@@ -137,7 +137,7 @@ impl CtChoice {
137137

138138
#[inline]
139139
pub(crate) const fn is_true_vartime(&self) -> bool {
140-
self.0 == CtChoice::TRUE.0
140+
self.0 == ConstChoice::TRUE.0
141141
}
142142

143143
#[inline]
@@ -146,34 +146,34 @@ impl CtChoice {
146146
}
147147
}
148148

149-
impl From<CtChoice> for Choice {
150-
fn from(choice: CtChoice) -> Self {
149+
impl From<ConstChoice> for Choice {
150+
fn from(choice: ConstChoice) -> Self {
151151
Choice::from(choice.to_u8())
152152
}
153153
}
154154

155-
impl From<CtChoice> for bool {
156-
fn from(choice: CtChoice) -> Self {
155+
impl From<ConstChoice> for bool {
156+
fn from(choice: ConstChoice) -> Self {
157157
choice.is_true_vartime()
158158
}
159159
}
160160

161-
impl PartialEq for CtChoice {
161+
impl PartialEq for ConstChoice {
162162
fn eq(&self, other: &Self) -> bool {
163163
self.0 == other.0
164164
}
165165
}
166166

167167
#[cfg(test)]
168168
mod tests {
169-
use super::CtChoice;
169+
use super::ConstChoice;
170170
use crate::Word;
171171

172172
#[test]
173173
fn select() {
174174
let a: Word = 1;
175175
let b: Word = 2;
176-
assert_eq!(CtChoice::TRUE.select_word(a, b), b);
177-
assert_eq!(CtChoice::FALSE.select_word(a, b), a);
176+
assert_eq!(ConstChoice::TRUE.select_word(a, b), b);
177+
assert_eq!(ConstChoice::FALSE.select_word(a, b), a);
178178
}
179179
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ mod wrapping;
176176

177177
pub use crate::{
178178
checked::Checked,
179-
ct_choice::CtChoice,
179+
ct_choice::ConstChoice,
180180
limb::{Limb, WideWord, Word},
181181
non_zero::NonZero,
182182
traits::*,

src/limb/cmp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Limb comparisons
22
3-
use crate::{CtChoice, Limb};
3+
use crate::{ConstChoice, Limb};
44
use core::cmp::Ordering;
55
use subtle::{
66
Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess,
@@ -28,14 +28,14 @@ impl Limb {
2828

2929
/// Return `b` if `c` is truthy, otherwise return `a`.
3030
#[inline]
31-
pub(crate) const fn select(a: Self, b: Self, c: CtChoice) -> Self {
31+
pub(crate) const fn select(a: Self, b: Self, c: ConstChoice) -> Self {
3232
Self(c.select_word(a.0, b.0))
3333
}
3434

3535
/// Returns the truthy value if `self != 0` and the falsy value otherwise.
3636
#[inline]
37-
pub(crate) const fn is_nonzero(&self) -> CtChoice {
38-
CtChoice::from_word_nonzero(self.0)
37+
pub(crate) const fn is_nonzero(&self) -> ConstChoice {
38+
ConstChoice::from_word_nonzero(self.0)
3939
}
4040
}
4141

src/modular/dyn_residue/inv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Multiplicative inverses of residues with a modulus set at runtime.
22
33
use super::DynResidue;
4-
use crate::{modular::inv::inv_montgomery_form, traits::Invert, CtChoice};
4+
use crate::{modular::inv::inv_montgomery_form, traits::Invert, ConstChoice};
55
use subtle::CtOption;
66

77
impl<const LIMBS: usize> DynResidue<LIMBS> {
88
/// Computes the residue `self^-1` representing the multiplicative inverse of `self`.
99
/// I.e. `self * self^-1 = 1`.
1010
/// If the number was invertible, the second element of the tuple is the truthy value,
1111
/// otherwise it is the falsy value (in which case the first element's value is unspecified).
12-
pub const fn invert(&self) -> (Self, CtChoice) {
12+
pub const fn invert(&self) -> (Self, ConstChoice) {
1313
let (montgomery_form, is_some) = inv_montgomery_form(
1414
&self.montgomery_form,
1515
&self.residue_params.modulus,

src/modular/inv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::{modular::reduction::montgomery_reduction, CtChoice, Limb, Uint};
1+
use crate::{modular::reduction::montgomery_reduction, ConstChoice, Limb, Uint};
22

33
pub const fn inv_montgomery_form<const LIMBS: usize>(
44
x: &Uint<LIMBS>,
55
modulus: &Uint<LIMBS>,
66
r3: &Uint<LIMBS>,
77
mod_neg_inv: Limb,
8-
) -> (Uint<LIMBS>, CtChoice) {
8+
) -> (Uint<LIMBS>, ConstChoice) {
99
let (inverse, is_some) = x.inv_odd_mod(modulus);
1010
(
1111
montgomery_reduction(&inverse.mul_wide(r3), modulus, mod_neg_inv),

src/modular/pow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CtChoice, Limb, Uint, Word};
1+
use crate::{ConstChoice, Limb, Uint, Word};
22

33
use super::mul::{mul_montgomery_form, square_montgomery_form};
44

@@ -163,7 +163,7 @@ const fn multi_exponentiate_montgomery_form_internal<const LIMBS: usize, const R
163163
let mut power = powers[0];
164164
let mut j = 1;
165165
while j < 1 << WINDOW {
166-
let choice = CtChoice::from_word_eq(j, idx);
166+
let choice = ConstChoice::from_word_eq(j, idx);
167167
power = Uint::<LIMBS>::select(&power, &powers[j as usize], choice);
168168
j += 1;
169169
}

src/modular/residue/inv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Multiplicative inverses of residues with a constant modulus.
22
33
use super::{Residue, ResidueParams};
4-
use crate::{modular::inv::inv_montgomery_form, traits::Invert, CtChoice, NonZero};
4+
use crate::{modular::inv::inv_montgomery_form, traits::Invert, ConstChoice, NonZero};
55
use core::marker::PhantomData;
66
use subtle::CtOption;
77

@@ -10,7 +10,7 @@ impl<MOD: ResidueParams<LIMBS>, const LIMBS: usize> Residue<MOD, LIMBS> {
1010
/// I.e. `self * self^-1 = 1`.
1111
/// If the number was invertible, the second element of the tuple is the truthy value,
1212
/// otherwise it is the falsy value (in which case the first element's value is unspecified).
13-
pub const fn invert(&self) -> (Self, CtChoice) {
13+
pub const fn invert(&self) -> (Self, ConstChoice) {
1414
let (montgomery_form, is_some) = inv_montgomery_form(
1515
&self.montgomery_form,
1616
&MOD::MODULUS.0,

src/non_zero.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Wrapper type for non-zero integers.
22
3-
use crate::{Bounded, Constants, CtChoice, Encoding, Limb, Uint, Zero};
3+
use crate::{Bounded, ConstChoice, Constants, Encoding, Limb, Uint, Zero};
44
use core::{
55
fmt,
66
num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8},
@@ -27,15 +27,15 @@ pub struct NonZero<T: Zero>(pub(crate) T);
2727
impl NonZero<Limb> {
2828
/// Creates a new non-zero limb in a const context.
2929
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
30-
pub const fn const_new(n: Limb) -> (Self, CtChoice) {
30+
pub const fn const_new(n: Limb) -> (Self, ConstChoice) {
3131
(Self(n), n.is_nonzero())
3232
}
3333
}
3434

3535
impl<const LIMBS: usize> NonZero<Uint<LIMBS>> {
3636
/// Creates a new non-zero integer in a const context.
3737
/// The second return value is `FALSE` if `n` is zero, `TRUE` otherwise.
38-
pub const fn const_new(n: Uint<LIMBS>) -> (Self, CtChoice) {
38+
pub const fn const_new(n: Uint<LIMBS>) -> (Self, ConstChoice) {
3939
(Self(n), n.is_nonzero())
4040
}
4141
}

src/uint/add.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! [`Uint`] addition operations.
22
3-
use crate::{Checked, CheckedAdd, CtChoice, Limb, Uint, Wrapping, Zero};
3+
use crate::{Checked, CheckedAdd, ConstChoice, Limb, Uint, Wrapping, Zero};
44
use core::ops::{Add, AddAssign};
55
use subtle::CtOption;
66

@@ -24,7 +24,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
2424
/// Perform saturating addition, returning `MAX` on overflow.
2525
pub const fn saturating_add(&self, rhs: &Self) -> Self {
2626
let (res, overflow) = self.adc(rhs, Limb::ZERO);
27-
Self::select(&res, &Self::MAX, CtChoice::from_word_lsb(overflow.0))
27+
Self::select(&res, &Self::MAX, ConstChoice::from_word_lsb(overflow.0))
2828
}
2929

3030
/// Perform wrapping addition, discarding overflow.
@@ -37,11 +37,11 @@ impl<const LIMBS: usize> Uint<LIMBS> {
3737
pub(crate) const fn conditional_wrapping_add(
3838
&self,
3939
rhs: &Self,
40-
choice: CtChoice,
41-
) -> (Self, CtChoice) {
40+
choice: ConstChoice,
41+
) -> (Self, ConstChoice) {
4242
let actual_rhs = Uint::select(&Uint::ZERO, rhs, choice);
4343
let (sum, carry) = self.adc(&actual_rhs, Limb::ZERO);
44-
(sum, CtChoice::from_word_lsb(carry.0))
44+
(sum, ConstChoice::from_word_lsb(carry.0))
4545
}
4646
}
4747

src/uint/bits.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::{CtChoice, Limb, Uint};
1+
use crate::{ConstChoice, Limb, Uint};
22

33
impl<const LIMBS: usize> Uint<LIMBS> {
4-
/// Get the value of the bit at position `index`, as a truthy or falsy `CtChoice`.
4+
/// Get the value of the bit at position `index`, as a truthy or falsy `ConstChoice`.
55
/// Returns the falsy value for indices out of range.
6-
pub const fn bit(&self, index: u32) -> CtChoice {
6+
pub const fn bit(&self, index: u32) -> ConstChoice {
77
let limb_num = index / Limb::BITS;
88
let index_in_limb = index % Limb::BITS;
99
let index_mask = 1 << index_in_limb;
@@ -14,12 +14,12 @@ impl<const LIMBS: usize> Uint<LIMBS> {
1414
let mut i = 0;
1515
while i < LIMBS {
1616
let bit = limbs[i] & index_mask;
17-
let is_right_limb = CtChoice::from_u32_eq(i as u32, limb_num);
17+
let is_right_limb = ConstChoice::from_u32_eq(i as u32, limb_num);
1818
result |= is_right_limb.if_true_word(bit);
1919
i += 1;
2020
}
2121

22-
CtChoice::from_word_lsb(result >> index_in_limb)
22+
ConstChoice::from_word_lsb(result >> index_in_limb)
2323
}
2424

2525
/// Returns `true` if the bit at position `index` is set, `false` otherwise.
@@ -59,14 +59,14 @@ impl<const LIMBS: usize> Uint<LIMBS> {
5959

6060
let mut count = 0;
6161
let mut i = LIMBS;
62-
let mut nonzero_limb_not_encountered = CtChoice::TRUE;
62+
let mut nonzero_limb_not_encountered = ConstChoice::TRUE;
6363
while i > 0 {
6464
i -= 1;
6565
let l = limbs[i];
6666
let z = l.leading_zeros();
6767
count += nonzero_limb_not_encountered.if_true_u32(z);
6868
nonzero_limb_not_encountered =
69-
nonzero_limb_not_encountered.and(CtChoice::from_word_nonzero(l.0).not());
69+
nonzero_limb_not_encountered.and(ConstChoice::from_word_nonzero(l.0).not());
7070
}
7171

7272
count
@@ -98,13 +98,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {
9898

9999
let mut count = 0;
100100
let mut i = 0;
101-
let mut nonzero_limb_not_encountered = CtChoice::TRUE;
101+
let mut nonzero_limb_not_encountered = ConstChoice::TRUE;
102102
while i < LIMBS {
103103
let l = limbs[i];
104104
let z = l.trailing_zeros();
105105
count += nonzero_limb_not_encountered.if_true_u32(z);
106106
nonzero_limb_not_encountered =
107-
nonzero_limb_not_encountered.and(CtChoice::from_word_nonzero(l.0).not());
107+
nonzero_limb_not_encountered.and(ConstChoice::from_word_nonzero(l.0).not());
108108
i += 1;
109109
}
110110

@@ -137,13 +137,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {
137137

138138
let mut count = 0;
139139
let mut i = 0;
140-
let mut nonmax_limb_not_encountered = CtChoice::TRUE;
140+
let mut nonmax_limb_not_encountered = ConstChoice::TRUE;
141141
while i < LIMBS {
142142
let l = limbs[i];
143143
let z = l.trailing_ones();
144144
count += nonmax_limb_not_encountered.if_true_u32(z);
145145
nonmax_limb_not_encountered =
146-
nonmax_limb_not_encountered.and(CtChoice::from_word_eq(l.0, Limb::MAX.0));
146+
nonmax_limb_not_encountered.and(ConstChoice::from_word_eq(l.0, Limb::MAX.0));
147147
i += 1;
148148
}
149149

@@ -171,15 +171,15 @@ impl<const LIMBS: usize> Uint<LIMBS> {
171171
}
172172

173173
/// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`.
174-
pub(crate) const fn set_bit(self, index: u32, bit_value: CtChoice) -> Self {
174+
pub(crate) const fn set_bit(self, index: u32, bit_value: ConstChoice) -> Self {
175175
let mut result = self;
176176
let limb_num = index / Limb::BITS;
177177
let index_in_limb = index % Limb::BITS;
178178
let index_mask = 1 << index_in_limb;
179179

180180
let mut i = 0;
181181
while i < LIMBS {
182-
let is_right_limb = CtChoice::from_u32_eq(i as u32, limb_num);
182+
let is_right_limb = ConstChoice::from_u32_eq(i as u32, limb_num);
183183
let old_limb = result.limbs[i].0;
184184
let new_limb = bit_value.select_word(old_limb & !index_mask, old_limb | index_mask);
185185
result.limbs[i] = Limb(is_right_limb.select_word(old_limb, new_limb));
@@ -191,7 +191,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
191191

192192
#[cfg(test)]
193193
mod tests {
194-
use crate::{CtChoice, U256};
194+
use crate::{ConstChoice, U256};
195195

196196
fn uint_with_bits_at(positions: &[u32]) -> U256 {
197197
let mut result = U256::ZERO;
@@ -337,25 +337,25 @@ mod tests {
337337
fn set_bit() {
338338
let u = uint_with_bits_at(&[16, 79, 150]);
339339
assert_eq!(
340-
u.set_bit(127, CtChoice::TRUE),
340+
u.set_bit(127, ConstChoice::TRUE),
341341
uint_with_bits_at(&[16, 79, 127, 150])
342342
);
343343

344344
let u = uint_with_bits_at(&[16, 79, 150]);
345345
assert_eq!(
346-
u.set_bit(150, CtChoice::TRUE),
346+
u.set_bit(150, ConstChoice::TRUE),
347347
uint_with_bits_at(&[16, 79, 150])
348348
);
349349

350350
let u = uint_with_bits_at(&[16, 79, 150]);
351351
assert_eq!(
352-
u.set_bit(127, CtChoice::FALSE),
352+
u.set_bit(127, ConstChoice::FALSE),
353353
uint_with_bits_at(&[16, 79, 150])
354354
);
355355

356356
let u = uint_with_bits_at(&[16, 79, 150]);
357357
assert_eq!(
358-
u.set_bit(150, CtChoice::FALSE),
358+
u.set_bit(150, ConstChoice::FALSE),
359359
uint_with_bits_at(&[16, 79])
360360
);
361361
}

0 commit comments

Comments
 (0)