Skip to content

Commit b639b2f

Browse files
authored
ctutils: remove (Partial)Eq impls for Choice (#1315)
These were added in #1266 to simplify a migration from `subtle::Choice` with a TODO to eventually remove them. They're used in tests, including it seems, the ones for `ctutils`. They're problematic because the goal of `Choice` is to be an opaque boolean-alternative for use in constant-time code, but especially a derived `Partial(Eq)` can peek inside them and bypass the encapsulation they're trying to provide in a way that's easy to branch on. Now that `crypto-bigint` has actually been migrated to `ctutils`, we can followup on removing these as part of some final breaking changes.
1 parent d1603ab commit b639b2f

File tree

2 files changed

+86
-87
lines changed

2 files changed

+86
-87
lines changed

ctutils/src/choice.rs

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ macro_rules! bitnz {
3232
/// applied which depend on a value.
3333
///
3434
/// This is used as a "belt-and-suspenders" defense in addition to mechanisms like
35-
/// constant-time predication intrinsics provided by the `cmov` crate, and is never expected to be
35+
/// constant-time predication intrinsics provided by the [`cmov`] crate, and is never expected to be
3636
/// the only line of defense.
37-
// TODO(tarcieri): remove `Eq`/`PartialEq` when `crypto-bigint` is updated
38-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
37+
// NOTE: we deliberately do NOT impl `Eq`, `Hash`, `PartialEq`, etc. See #1315
38+
#[derive(Copy, Clone, Debug)]
3939
pub struct Choice(pub(crate) u8);
4040

4141
impl Choice {
@@ -597,189 +597,189 @@ mod tests {
597597

598598
#[test]
599599
fn from_i64_eq() {
600-
assert_eq!(Choice::from_i64_eq(0, 1), Choice::FALSE);
601-
assert_eq!(Choice::from_i64_eq(1, 1), Choice::TRUE);
600+
assert!(Choice::from_i64_eq(0, 1).eq(Choice::FALSE).to_bool());
601+
assert!(Choice::from_i64_eq(1, 1).eq(Choice::TRUE).to_bool());
602602
}
603603

604604
#[test]
605605
fn from_u8_eq() {
606-
assert_eq!(Choice::from_u8_eq(0, 1), Choice::FALSE);
607-
assert_eq!(Choice::from_u8_eq(1, 1), Choice::TRUE);
606+
assert!(Choice::from_u8_eq(0, 1).eq(Choice::FALSE).to_bool());
607+
assert!(Choice::from_u8_eq(1, 1).eq(Choice::TRUE).to_bool());
608608
}
609609

610610
#[test]
611611
fn from_u8_le() {
612-
assert_eq!(Choice::from_u8_le(0, 0), Choice::TRUE);
613-
assert_eq!(Choice::from_u8_le(1, 0), Choice::FALSE);
614-
assert_eq!(Choice::from_u8_le(1, 1), Choice::TRUE);
615-
assert_eq!(Choice::from_u8_le(1, 2), Choice::TRUE);
612+
assert!(Choice::from_u8_le(0, 0).eq(Choice::TRUE).to_bool());
613+
assert!(Choice::from_u8_le(1, 0).eq(Choice::FALSE).to_bool());
614+
assert!(Choice::from_u8_le(1, 1).eq(Choice::TRUE).to_bool());
615+
assert!(Choice::from_u8_le(1, 2).eq(Choice::TRUE).to_bool());
616616
}
617617

618618
#[test]
619619
fn from_u8_lsb() {
620-
assert_eq!(Choice::from_u8_lsb(0), Choice::FALSE);
621-
assert_eq!(Choice::from_u8_lsb(1), Choice::TRUE);
622-
assert_eq!(Choice::from_u8_lsb(2), Choice::FALSE);
623-
assert_eq!(Choice::from_u8_lsb(3), Choice::TRUE);
620+
assert!(Choice::from_u8_lsb(0).eq(Choice::FALSE).to_bool());
621+
assert!(Choice::from_u8_lsb(1).eq(Choice::TRUE).to_bool());
622+
assert!(Choice::from_u8_lsb(2).eq(Choice::FALSE).to_bool());
623+
assert!(Choice::from_u8_lsb(3).eq(Choice::TRUE).to_bool());
624624
}
625625

626626
#[test]
627627
fn from_u8_lt() {
628-
assert_eq!(Choice::from_u8_lt(0, 0), Choice::FALSE);
629-
assert_eq!(Choice::from_u8_lt(1, 0), Choice::FALSE);
630-
assert_eq!(Choice::from_u8_lt(1, 1), Choice::FALSE);
631-
assert_eq!(Choice::from_u8_lt(1, 2), Choice::TRUE);
628+
assert!(Choice::from_u8_lt(0, 0).eq(Choice::FALSE).to_bool());
629+
assert!(Choice::from_u8_lt(1, 0).eq(Choice::FALSE).to_bool());
630+
assert!(Choice::from_u8_lt(1, 1).eq(Choice::FALSE).to_bool());
631+
assert!(Choice::from_u8_lt(1, 2).eq(Choice::TRUE).to_bool());
632632
}
633633

634634
#[test]
635635
fn from_u8_nz() {
636-
assert_eq!(Choice::from_u8_nz(0), Choice::FALSE);
637-
assert_eq!(Choice::from_u8_nz(1), Choice::TRUE);
638-
assert_eq!(Choice::from_u8_nz(2), Choice::TRUE);
636+
assert!(Choice::from_u8_nz(0).eq(Choice::FALSE).to_bool());
637+
assert!(Choice::from_u8_nz(1).eq(Choice::TRUE).to_bool());
638+
assert!(Choice::from_u8_nz(2).eq(Choice::TRUE).to_bool());
639639
}
640640

641641
#[test]
642642
fn from_u16_eq() {
643-
assert_eq!(Choice::from_u16_eq(0, 1), Choice::FALSE);
644-
assert_eq!(Choice::from_u16_eq(1, 1), Choice::TRUE);
643+
assert!(Choice::from_u16_eq(0, 1).eq(Choice::FALSE).to_bool());
644+
assert!(Choice::from_u16_eq(1, 1).eq(Choice::TRUE).to_bool());
645645
}
646646

647647
#[test]
648648
fn from_u16_le() {
649-
assert_eq!(Choice::from_u16_le(0, 0), Choice::TRUE);
650-
assert_eq!(Choice::from_u16_le(1, 0), Choice::FALSE);
651-
assert_eq!(Choice::from_u16_le(1, 1), Choice::TRUE);
652-
assert_eq!(Choice::from_u16_le(1, 2), Choice::TRUE);
649+
assert!(Choice::from_u16_le(0, 0).eq(Choice::TRUE).to_bool());
650+
assert!(Choice::from_u16_le(1, 0).eq(Choice::FALSE).to_bool());
651+
assert!(Choice::from_u16_le(1, 1).eq(Choice::TRUE).to_bool());
652+
assert!(Choice::from_u16_le(1, 2).eq(Choice::TRUE).to_bool());
653653
}
654654

655655
#[test]
656656
fn from_u16_lsb() {
657-
assert_eq!(Choice::from_u16_lsb(0), Choice::FALSE);
658-
assert_eq!(Choice::from_u16_lsb(1), Choice::TRUE);
659-
assert_eq!(Choice::from_u16_lsb(2), Choice::FALSE);
660-
assert_eq!(Choice::from_u16_lsb(3), Choice::TRUE);
657+
assert!(Choice::from_u16_lsb(0).eq(Choice::FALSE).to_bool());
658+
assert!(Choice::from_u16_lsb(1).eq(Choice::TRUE).to_bool());
659+
assert!(Choice::from_u16_lsb(2).eq(Choice::FALSE).to_bool());
660+
assert!(Choice::from_u16_lsb(3).eq(Choice::TRUE).to_bool());
661661
}
662662

663663
#[test]
664664
fn from_u16_lt() {
665-
assert_eq!(Choice::from_u16_lt(0, 0), Choice::FALSE);
666-
assert_eq!(Choice::from_u16_lt(1, 0), Choice::FALSE);
667-
assert_eq!(Choice::from_u16_lt(1, 1), Choice::FALSE);
668-
assert_eq!(Choice::from_u16_lt(1, 2), Choice::TRUE);
665+
assert!(Choice::from_u16_lt(0, 0).eq(Choice::FALSE).to_bool());
666+
assert!(Choice::from_u16_lt(1, 0).eq(Choice::FALSE).to_bool());
667+
assert!(Choice::from_u16_lt(1, 1).eq(Choice::FALSE).to_bool());
668+
assert!(Choice::from_u16_lt(1, 2).eq(Choice::TRUE).to_bool());
669669
}
670670

671671
#[test]
672672
fn from_u16_nz() {
673-
assert_eq!(Choice::from_u16_nz(0), Choice::FALSE);
674-
assert_eq!(Choice::from_u16_nz(1), Choice::TRUE);
675-
assert_eq!(Choice::from_u16_nz(2), Choice::TRUE);
673+
assert!(Choice::from_u16_nz(0).eq(Choice::FALSE).to_bool());
674+
assert!(Choice::from_u16_nz(1).eq(Choice::TRUE).to_bool());
675+
assert!(Choice::from_u16_nz(2).eq(Choice::TRUE).to_bool());
676676
}
677677

678678
#[test]
679679
fn from_u32_eq() {
680-
assert_eq!(Choice::from_u32_eq(0, 1), Choice::FALSE);
681-
assert_eq!(Choice::from_u32_eq(1, 1), Choice::TRUE);
680+
assert!(Choice::from_u32_eq(0, 1).eq(Choice::FALSE).to_bool());
681+
assert!(Choice::from_u32_eq(1, 1).eq(Choice::TRUE).to_bool());
682682
}
683683

684684
#[test]
685685
fn from_u32_le() {
686-
assert_eq!(Choice::from_u32_le(0, 0), Choice::TRUE);
687-
assert_eq!(Choice::from_u32_le(1, 0), Choice::FALSE);
688-
assert_eq!(Choice::from_u32_le(1, 1), Choice::TRUE);
689-
assert_eq!(Choice::from_u32_le(1, 2), Choice::TRUE);
686+
assert!(Choice::from_u32_le(0, 0).eq(Choice::TRUE).to_bool());
687+
assert!(Choice::from_u32_le(1, 0).eq(Choice::FALSE).to_bool());
688+
assert!(Choice::from_u32_le(1, 1).eq(Choice::TRUE).to_bool());
689+
assert!(Choice::from_u32_le(1, 2).eq(Choice::TRUE).to_bool());
690690
}
691691

692692
#[test]
693693
fn from_u32_lsb() {
694-
assert_eq!(Choice::from_u32_lsb(0), Choice::FALSE);
695-
assert_eq!(Choice::from_u32_lsb(1), Choice::TRUE);
696-
assert_eq!(Choice::from_u32_lsb(2), Choice::FALSE);
697-
assert_eq!(Choice::from_u32_lsb(3), Choice::TRUE);
694+
assert!(Choice::from_u32_lsb(0).eq(Choice::FALSE).to_bool());
695+
assert!(Choice::from_u32_lsb(1).eq(Choice::TRUE).to_bool());
696+
assert!(Choice::from_u32_lsb(2).eq(Choice::FALSE).to_bool());
697+
assert!(Choice::from_u32_lsb(3).eq(Choice::TRUE).to_bool());
698698
}
699699

700700
#[test]
701701
fn from_u32_lt() {
702-
assert_eq!(Choice::from_u32_lt(0, 0), Choice::FALSE);
703-
assert_eq!(Choice::from_u32_lt(1, 0), Choice::FALSE);
704-
assert_eq!(Choice::from_u32_lt(1, 1), Choice::FALSE);
705-
assert_eq!(Choice::from_u32_lt(1, 2), Choice::TRUE);
702+
assert!(Choice::from_u32_lt(0, 0).eq(Choice::FALSE).to_bool());
703+
assert!(Choice::from_u32_lt(1, 0).eq(Choice::FALSE).to_bool());
704+
assert!(Choice::from_u32_lt(1, 1).eq(Choice::FALSE).to_bool());
705+
assert!(Choice::from_u32_lt(1, 2).eq(Choice::TRUE).to_bool());
706706
}
707707

708708
#[test]
709709
fn from_u32_nz() {
710-
assert_eq!(Choice::from_u32_nz(0), Choice::FALSE);
711-
assert_eq!(Choice::from_u32_nz(1), Choice::TRUE);
712-
assert_eq!(Choice::from_u32_nz(2), Choice::TRUE);
710+
assert!(Choice::from_u32_nz(0).eq(Choice::FALSE).to_bool());
711+
assert!(Choice::from_u32_nz(1).eq(Choice::TRUE).to_bool());
712+
assert!(Choice::from_u32_nz(2).eq(Choice::TRUE).to_bool());
713713
}
714714

715715
#[test]
716716
fn from_u64_eq() {
717-
assert_eq!(Choice::from_u64_eq(0, 1), Choice::FALSE);
718-
assert_eq!(Choice::from_u64_eq(1, 1), Choice::TRUE);
717+
assert!(Choice::from_u64_eq(0, 1).eq(Choice::FALSE).to_bool());
718+
assert!(Choice::from_u64_eq(1, 1).eq(Choice::TRUE).to_bool());
719719
}
720720

721721
#[test]
722722
fn from_u64_le() {
723-
assert_eq!(Choice::from_u64_le(0, 0), Choice::TRUE);
724-
assert_eq!(Choice::from_u64_le(1, 0), Choice::FALSE);
725-
assert_eq!(Choice::from_u64_le(1, 1), Choice::TRUE);
726-
assert_eq!(Choice::from_u64_le(1, 2), Choice::TRUE);
723+
assert!(Choice::from_u64_le(0, 0).eq(Choice::TRUE).to_bool());
724+
assert!(Choice::from_u64_le(1, 0).eq(Choice::FALSE).to_bool());
725+
assert!(Choice::from_u64_le(1, 1).eq(Choice::TRUE).to_bool());
726+
assert!(Choice::from_u64_le(1, 2).eq(Choice::TRUE).to_bool());
727727
}
728728

729729
#[test]
730730
fn from_u64_lsb() {
731-
assert_eq!(Choice::from_u64_lsb(0), Choice::FALSE);
732-
assert_eq!(Choice::from_u64_lsb(1), Choice::TRUE);
731+
assert!(Choice::from_u64_lsb(0).eq(Choice::FALSE).to_bool());
732+
assert!(Choice::from_u64_lsb(1).eq(Choice::TRUE).to_bool());
733733
}
734734

735735
#[test]
736736
fn from_u64_lt() {
737-
assert_eq!(Choice::from_u64_lt(0, 0), Choice::FALSE);
738-
assert_eq!(Choice::from_u64_lt(1, 0), Choice::FALSE);
739-
assert_eq!(Choice::from_u64_lt(1, 1), Choice::FALSE);
740-
assert_eq!(Choice::from_u64_lt(1, 2), Choice::TRUE);
737+
assert!(Choice::from_u64_lt(0, 0).eq(Choice::FALSE).to_bool());
738+
assert!(Choice::from_u64_lt(1, 0).eq(Choice::FALSE).to_bool());
739+
assert!(Choice::from_u64_lt(1, 1).eq(Choice::FALSE).to_bool());
740+
assert!(Choice::from_u64_lt(1, 2).eq(Choice::TRUE).to_bool());
741741
}
742742

743743
#[test]
744744
fn from_u64_nz() {
745-
assert_eq!(Choice::from_u64_nz(0), Choice::FALSE);
746-
assert_eq!(Choice::from_u64_nz(1), Choice::TRUE);
747-
assert_eq!(Choice::from_u64_nz(2), Choice::TRUE);
745+
assert!(Choice::from_u64_nz(0).eq(Choice::FALSE).to_bool());
746+
assert!(Choice::from_u64_nz(1).eq(Choice::TRUE).to_bool());
747+
assert!(Choice::from_u64_nz(2).eq(Choice::TRUE).to_bool());
748748
}
749749

750750
#[test]
751751
fn from_u128_eq() {
752-
assert_eq!(Choice::from_u128_eq(0, 1), Choice::FALSE);
753-
assert_eq!(Choice::from_u128_eq(1, 1), Choice::TRUE);
752+
assert!(Choice::from_u128_eq(0, 1).eq(Choice::FALSE).to_bool());
753+
assert!(Choice::from_u128_eq(1, 1).eq(Choice::TRUE).to_bool());
754754
}
755755

756756
#[test]
757757
fn from_u128_le() {
758-
assert_eq!(Choice::from_u128_le(0, 0), Choice::TRUE);
759-
assert_eq!(Choice::from_u128_le(1, 0), Choice::FALSE);
760-
assert_eq!(Choice::from_u128_le(1, 1), Choice::TRUE);
761-
assert_eq!(Choice::from_u128_le(1, 2), Choice::TRUE);
758+
assert!(Choice::from_u128_le(0, 0).eq(Choice::TRUE).to_bool());
759+
assert!(Choice::from_u128_le(1, 0).eq(Choice::FALSE).to_bool());
760+
assert!(Choice::from_u128_le(1, 1).eq(Choice::TRUE).to_bool());
761+
assert!(Choice::from_u128_le(1, 2).eq(Choice::TRUE).to_bool());
762762
}
763763

764764
#[test]
765765
fn from_u128_lsb() {
766-
assert_eq!(Choice::from_u128_lsb(0), Choice::FALSE);
767-
assert_eq!(Choice::from_u128_lsb(1), Choice::TRUE);
766+
assert!(Choice::from_u128_lsb(0).eq(Choice::FALSE).to_bool());
767+
assert!(Choice::from_u128_lsb(1).eq(Choice::TRUE).to_bool());
768768
}
769769

770770
#[test]
771771
fn from_u128_lt() {
772-
assert_eq!(Choice::from_u128_lt(0, 0), Choice::FALSE);
773-
assert_eq!(Choice::from_u128_lt(1, 0), Choice::FALSE);
774-
assert_eq!(Choice::from_u128_lt(1, 1), Choice::FALSE);
775-
assert_eq!(Choice::from_u128_lt(1, 2), Choice::TRUE);
772+
assert!(Choice::from_u128_lt(0, 0).eq(Choice::FALSE).to_bool());
773+
assert!(Choice::from_u128_lt(1, 0).eq(Choice::FALSE).to_bool());
774+
assert!(Choice::from_u128_lt(1, 1).eq(Choice::FALSE).to_bool());
775+
assert!(Choice::from_u128_lt(1, 2).eq(Choice::TRUE).to_bool());
776776
}
777777

778778
#[test]
779779
fn from_u128_nz() {
780-
assert_eq!(Choice::from_u128_nz(0), Choice::FALSE);
781-
assert_eq!(Choice::from_u128_nz(1), Choice::TRUE);
782-
assert_eq!(Choice::from_u128_nz(2), Choice::TRUE);
780+
assert!(Choice::from_u128_nz(0).eq(Choice::FALSE).to_bool());
781+
assert!(Choice::from_u128_nz(1).eq(Choice::TRUE).to_bool());
782+
assert!(Choice::from_u128_nz(2).eq(Choice::TRUE).to_bool());
783783
}
784784

785785
#[test]

ctutils/src/traits/ct_neg.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ macro_rules! impl_unsigned_ct_neg {
5858
impl_signed_ct_neg!(i8, i16, i32, i64, i128);
5959
impl_unsigned_ct_neg!(u8, u16, u32, u64, u128);
6060

61-
// TODO(tarcieri): test all signed/unsigned integer types
6261
#[cfg(test)]
6362
mod tests {
6463
/// Test `CtNeg` impl on `i*`

0 commit comments

Comments
 (0)