From a58b3a133cd745ab8d0098aaa3b0b13a45885361 Mon Sep 17 00:00:00 2001 From: "J. Burfeind" Date: Fri, 12 Aug 2022 22:05:48 +0200 Subject: [PATCH] Add implementation for `is_odd()` (#79) in dalek-ff-group Co-authored-by: Luke Parker --- crypto/dalek-ff-group/src/field.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crypto/dalek-ff-group/src/field.rs b/crypto/dalek-ff-group/src/field.rs index a5baf5d2..2831f69e 100644 --- a/crypto/dalek-ff-group/src/field.rs +++ b/crypto/dalek-ff-group/src/field.rs @@ -117,7 +117,7 @@ impl PrimeField for FieldElement { const S: u32 = 2; fn is_odd(&self) -> Choice { - unimplemented!() + (self.to_repr()[0] & 1).into() } fn multiplicative_generator() -> Self { 2u64.into() @@ -159,6 +159,17 @@ impl FieldElement { } #[test] +fn test_is_odd() { + assert_eq!(0, FieldElement::zero().is_odd().unwrap_u8()); + assert_eq!(1, FieldElement::one().is_odd().unwrap_u8()); + assert_eq!(0, FieldElement::one().double().is_odd().unwrap_u8()); + + // 0 is even, yet the modulus is odd + // -1 moves to the even value before the modulus + assert_eq!(0, (-FieldElement::one()).is_odd().unwrap_u8()); + assert_eq!(1, (-FieldElement::one().double()).is_odd().unwrap_u8()); +} + fn test_edwards_d() { let a = -FieldElement(U256::from_u32(121665)); let b = FieldElement(U256::from_u32(121666));