Add implementation for is_odd() (#79)

in dalek-ff-group

Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
J. Burfeind 2022-08-12 22:05:48 +02:00 committed by GitHub
parent 169d5e26ca
commit a58b3a133c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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));