Skip to content

Commit 57ae2a2

Browse files
committed
Improved Boolean type conversion
1 parent f995791 commit 57ae2a2

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/ring/crypto_bigint_int.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use crate::{crypto_bigint_uint::Uint, impl_pow_via_repeated_squaring};
2+
use crate::{boolean::Boolean, crypto_bigint_uint::Uint, impl_pow_via_repeated_squaring};
33
use core::{
44
cmp::Ordering,
55
fmt::{Debug, Display, Formatter, LowerHex, Result as FmtResult, UpperHex},
@@ -457,6 +457,13 @@ impl<const LIMBS: usize> From<bool> for Int<LIMBS> {
457457
}
458458
}
459459

460+
impl<const LIMBS: usize> From<Boolean> for Int<LIMBS> {
461+
#[inline(always)]
462+
fn from(value: Boolean) -> Self {
463+
Self::from(value.into_inner())
464+
}
465+
}
466+
460467
macro_rules! impl_from_signed_primitive {
461468
($($t:ty),+) => {
462469
$(
@@ -789,6 +796,9 @@ mod tests {
789796
let wrapped = Int4::new(value);
790797
assert_eq!(wrapped.inner(), &value);
791798
assert_eq!(wrapped.into_inner(), value);
799+
800+
assert_eq!(Int4::from(true), Int4::ONE);
801+
assert_eq!(Int4::from(Boolean::TRUE), Int4::ONE);
792802
}
793803

794804
#[test]

src/semiring/boolean.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,21 @@ impl From<bool> for Boolean {
147147
}
148148
}
149149

150-
impl From<Boolean> for bool {
151-
#[inline(always)]
152-
fn from(value: Boolean) -> Self {
153-
value.0
154-
}
150+
macro_rules! impl_from_boolean_for {
151+
($($to:ty),*) => {$(
152+
impl From<Boolean> for $to {
153+
#[inline(always)]
154+
fn from(value: Boolean) -> Self {
155+
value.0 as $to
156+
}
157+
}
158+
)*};
155159
}
156160

161+
impl_from_boolean_for!(bool);
162+
impl_from_boolean_for!(i8, i16, i32, i64, i128, isize);
163+
impl_from_boolean_for!(u8, u16, u32, u64, u128, usize);
164+
157165
//
158166
// Basic arithmetic operations
159167
//
@@ -535,6 +543,15 @@ mod tests {
535543
assert_eq!(Boolean::from_u8(2), Boolean::TRUE);
536544
}
537545

546+
#[test]
547+
fn reverse_conversions() {
548+
assert_eq!(bool::from(Boolean::TRUE), true);
549+
assert_eq!(i8::from(Boolean::TRUE), 1);
550+
assert_eq!(i128::from(Boolean::TRUE), 1);
551+
assert_eq!(u8::from(Boolean::TRUE), 1);
552+
assert_eq!(u128::from(Boolean::TRUE), 1);
553+
}
554+
538555
#[test]
539556
fn from_str() {
540557
assert_eq!("true".parse::<Boolean>(), Ok(Boolean::TRUE));

src/semiring/crypto_bigint_uint.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use crate::{crypto_bigint_int::Int, impl_pow_via_repeated_squaring};
2+
use crate::{boolean::Boolean, crypto_bigint_int::Int, impl_pow_via_repeated_squaring};
33
use core::{
44
cmp::Ordering,
55
fmt::{Debug, Display, Formatter, LowerHex, Result as FmtResult, UpperHex},
@@ -486,6 +486,13 @@ impl<const LIMBS: usize> From<bool> for Uint<LIMBS> {
486486
}
487487
}
488488

489+
impl<const LIMBS: usize> From<Boolean> for Uint<LIMBS> {
490+
#[inline(always)]
491+
fn from(value: Boolean) -> Self {
492+
Self::from(value.into_inner())
493+
}
494+
}
495+
489496
macro_rules! impl_from_primitive {
490497
($($t:ty),+) => {
491498
$(
@@ -746,6 +753,9 @@ mod tests {
746753
let wrapped = Uint4::new(value);
747754
assert_eq!(wrapped.inner(), &value);
748755
assert_eq!(wrapped.into_inner(), value);
756+
757+
assert_eq!(Uint4::from(true), Uint4::ONE);
758+
assert_eq!(Uint4::from(Boolean::TRUE), Uint4::ONE);
749759
}
750760

751761
#[test]

0 commit comments

Comments
 (0)