Skip to content

Commit df24fb3

Browse files
committed
ctutils: remove (Partial)Eq impls for Choice
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 df24fb3

File tree

2 files changed

+84
-86
lines changed

2 files changed

+84
-86
lines changed

ctutils/src/choice.rs

Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ macro_rules! bitnz {
3434
/// This is used as a "belt-and-suspenders" defense in addition to mechanisms like
3535
/// 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+
#[derive(Copy, Clone, Debug)]
3938
pub struct Choice(pub(crate) u8);
4039

4140
impl Choice {
@@ -597,189 +596,189 @@ mod tests {
597596

598597
#[test]
599598
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);
599+
assert!(Choice::from_i64_eq(0, 1).eq(Choice::FALSE).to_bool());
600+
assert!(Choice::from_i64_eq(1, 1).eq(Choice::TRUE).to_bool());
602601
}
603602

604603
#[test]
605604
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);
605+
assert!(Choice::from_u8_eq(0, 1).eq(Choice::FALSE).to_bool());
606+
assert!(Choice::from_u8_eq(1, 1).eq(Choice::TRUE).to_bool());
608607
}
609608

610609
#[test]
611610
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);
611+
assert!(Choice::from_u8_le(0, 0).eq(Choice::TRUE).to_bool());
612+
assert!(Choice::from_u8_le(1, 0).eq(Choice::FALSE).to_bool());
613+
assert!(Choice::from_u8_le(1, 1).eq(Choice::TRUE).to_bool());
614+
assert!(Choice::from_u8_le(1, 2).eq(Choice::TRUE).to_bool());
616615
}
617616

618617
#[test]
619618
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);
619+
assert!(Choice::from_u8_lsb(0).eq(Choice::FALSE).to_bool());
620+
assert!(Choice::from_u8_lsb(1).eq(Choice::TRUE).to_bool());
621+
assert!(Choice::from_u8_lsb(2).eq(Choice::FALSE).to_bool());
622+
assert!(Choice::from_u8_lsb(3).eq(Choice::TRUE).to_bool());
624623
}
625624

626625
#[test]
627626
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);
627+
assert!(Choice::from_u8_lt(0, 0).eq(Choice::FALSE).to_bool());
628+
assert!(Choice::from_u8_lt(1, 0).eq(Choice::FALSE).to_bool());
629+
assert!(Choice::from_u8_lt(1, 1).eq(Choice::FALSE).to_bool());
630+
assert!(Choice::from_u8_lt(1, 2).eq(Choice::TRUE).to_bool());
632631
}
633632

634633
#[test]
635634
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);
635+
assert!(Choice::from_u8_nz(0).eq(Choice::FALSE).to_bool());
636+
assert!(Choice::from_u8_nz(1).eq(Choice::TRUE).to_bool());
637+
assert!(Choice::from_u8_nz(2).eq(Choice::TRUE).to_bool());
639638
}
640639

641640
#[test]
642641
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);
642+
assert!(Choice::from_u16_eq(0, 1).eq(Choice::FALSE).to_bool());
643+
assert!(Choice::from_u16_eq(1, 1).eq(Choice::TRUE).to_bool());
645644
}
646645

647646
#[test]
648647
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);
648+
assert!(Choice::from_u16_le(0, 0).eq(Choice::TRUE).to_bool());
649+
assert!(Choice::from_u16_le(1, 0).eq(Choice::FALSE).to_bool());
650+
assert!(Choice::from_u16_le(1, 1).eq(Choice::TRUE).to_bool());
651+
assert!(Choice::from_u16_le(1, 2).eq(Choice::TRUE).to_bool());
653652
}
654653

655654
#[test]
656655
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);
656+
assert!(Choice::from_u16_lsb(0).eq(Choice::FALSE).to_bool());
657+
assert!(Choice::from_u16_lsb(1).eq(Choice::TRUE).to_bool());
658+
assert!(Choice::from_u16_lsb(2).eq(Choice::FALSE).to_bool());
659+
assert!(Choice::from_u16_lsb(3).eq(Choice::TRUE).to_bool());
661660
}
662661

663662
#[test]
664663
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);
664+
assert!(Choice::from_u16_lt(0, 0).eq(Choice::FALSE).to_bool());
665+
assert!(Choice::from_u16_lt(1, 0).eq(Choice::FALSE).to_bool());
666+
assert!(Choice::from_u16_lt(1, 1).eq(Choice::FALSE).to_bool());
667+
assert!(Choice::from_u16_lt(1, 2).eq(Choice::TRUE).to_bool());
669668
}
670669

671670
#[test]
672671
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);
672+
assert!(Choice::from_u16_nz(0).eq(Choice::FALSE).to_bool());
673+
assert!(Choice::from_u16_nz(1).eq(Choice::TRUE).to_bool());
674+
assert!(Choice::from_u16_nz(2).eq(Choice::TRUE).to_bool());
676675
}
677676

678677
#[test]
679678
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);
679+
assert!(Choice::from_u32_eq(0, 1).eq(Choice::FALSE).to_bool());
680+
assert!(Choice::from_u32_eq(1, 1).eq(Choice::TRUE).to_bool());
682681
}
683682

684683
#[test]
685684
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);
685+
assert!(Choice::from_u32_le(0, 0).eq(Choice::TRUE).to_bool());
686+
assert!(Choice::from_u32_le(1, 0).eq(Choice::FALSE).to_bool());
687+
assert!(Choice::from_u32_le(1, 1).eq(Choice::TRUE).to_bool());
688+
assert!(Choice::from_u32_le(1, 2).eq(Choice::TRUE).to_bool());
690689
}
691690

692691
#[test]
693692
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);
693+
assert!(Choice::from_u32_lsb(0).eq(Choice::FALSE).to_bool());
694+
assert!(Choice::from_u32_lsb(1).eq(Choice::TRUE).to_bool());
695+
assert!(Choice::from_u32_lsb(2).eq(Choice::FALSE).to_bool());
696+
assert!(Choice::from_u32_lsb(3).eq(Choice::TRUE).to_bool());
698697
}
699698

700699
#[test]
701700
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);
701+
assert!(Choice::from_u32_lt(0, 0).eq(Choice::FALSE).to_bool());
702+
assert!(Choice::from_u32_lt(1, 0).eq(Choice::FALSE).to_bool());
703+
assert!(Choice::from_u32_lt(1, 1).eq(Choice::FALSE).to_bool());
704+
assert!(Choice::from_u32_lt(1, 2).eq(Choice::TRUE).to_bool());
706705
}
707706

708707
#[test]
709708
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);
709+
assert!(Choice::from_u32_nz(0).eq(Choice::FALSE).to_bool());
710+
assert!(Choice::from_u32_nz(1).eq(Choice::TRUE).to_bool());
711+
assert!(Choice::from_u32_nz(2).eq(Choice::TRUE).to_bool());
713712
}
714713

715714
#[test]
716715
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);
716+
assert!(Choice::from_u64_eq(0, 1).eq(Choice::FALSE).to_bool());
717+
assert!(Choice::from_u64_eq(1, 1).eq(Choice::TRUE).to_bool());
719718
}
720719

721720
#[test]
722721
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);
722+
assert!(Choice::from_u64_le(0, 0).eq(Choice::TRUE).to_bool());
723+
assert!(Choice::from_u64_le(1, 0).eq(Choice::FALSE).to_bool());
724+
assert!(Choice::from_u64_le(1, 1).eq(Choice::TRUE).to_bool());
725+
assert!(Choice::from_u64_le(1, 2).eq(Choice::TRUE).to_bool());
727726
}
728727

729728
#[test]
730729
fn from_u64_lsb() {
731-
assert_eq!(Choice::from_u64_lsb(0), Choice::FALSE);
732-
assert_eq!(Choice::from_u64_lsb(1), Choice::TRUE);
730+
assert!(Choice::from_u64_lsb(0).eq(Choice::FALSE).to_bool());
731+
assert!(Choice::from_u64_lsb(1).eq(Choice::TRUE).to_bool());
733732
}
734733

735734
#[test]
736735
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);
736+
assert!(Choice::from_u64_lt(0, 0).eq(Choice::FALSE).to_bool());
737+
assert!(Choice::from_u64_lt(1, 0).eq(Choice::FALSE).to_bool());
738+
assert!(Choice::from_u64_lt(1, 1).eq(Choice::FALSE).to_bool());
739+
assert!(Choice::from_u64_lt(1, 2).eq(Choice::TRUE).to_bool());
741740
}
742741

743742
#[test]
744743
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);
744+
assert!(Choice::from_u64_nz(0).eq(Choice::FALSE).to_bool());
745+
assert!(Choice::from_u64_nz(1).eq(Choice::TRUE).to_bool());
746+
assert!(Choice::from_u64_nz(2).eq(Choice::TRUE).to_bool());
748747
}
749748

750749
#[test]
751750
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);
751+
assert!(Choice::from_u128_eq(0, 1).eq(Choice::FALSE).to_bool());
752+
assert!(Choice::from_u128_eq(1, 1).eq(Choice::TRUE).to_bool());
754753
}
755754

756755
#[test]
757756
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);
757+
assert!(Choice::from_u128_le(0, 0).eq(Choice::TRUE).to_bool());
758+
assert!(Choice::from_u128_le(1, 0).eq(Choice::FALSE).to_bool());
759+
assert!(Choice::from_u128_le(1, 1).eq(Choice::TRUE).to_bool());
760+
assert!(Choice::from_u128_le(1, 2).eq(Choice::TRUE).to_bool());
762761
}
763762

764763
#[test]
765764
fn from_u128_lsb() {
766-
assert_eq!(Choice::from_u128_lsb(0), Choice::FALSE);
767-
assert_eq!(Choice::from_u128_lsb(1), Choice::TRUE);
765+
assert!(Choice::from_u128_lsb(0).eq(Choice::FALSE).to_bool());
766+
assert!(Choice::from_u128_lsb(1).eq(Choice::TRUE).to_bool());
768767
}
769768

770769
#[test]
771770
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);
771+
assert!(Choice::from_u128_lt(0, 0).eq(Choice::FALSE).to_bool());
772+
assert!(Choice::from_u128_lt(1, 0).eq(Choice::FALSE).to_bool());
773+
assert!(Choice::from_u128_lt(1, 1).eq(Choice::FALSE).to_bool());
774+
assert!(Choice::from_u128_lt(1, 2).eq(Choice::TRUE).to_bool());
776775
}
777776

778777
#[test]
779778
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);
779+
assert!(Choice::from_u128_nz(0).eq(Choice::FALSE).to_bool());
780+
assert!(Choice::from_u128_nz(1).eq(Choice::TRUE).to_bool());
781+
assert!(Choice::from_u128_nz(2).eq(Choice::TRUE).to_bool());
783782
}
784783

785784
#[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)