Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- match definition of parity function to sgn0 of ietf draft in swu map #33

Merged
merged 3 commits into from
Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions ec/src/hashing/curve_maps/swu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ pub struct SWUMap<P: SWUParams> {
curve_params: PhantomData<fn() -> P>,
}

/// Trait defining a parity method on the Field elements.
/// Trait defining a parity method on the Field elements based on [\[1\]] Section 4.1
///
/// - [\[1\]] <https://datatracker.ietf.org/doc/draft-irtf-cfrg-hash-to-curve/>
trait ElementParity<F: Field> {
fn parity(element: &F) -> bool {
element
.to_base_prime_field_elements()
.next()
.unwrap()
.into_repr()
.is_odd()
.find(|&x| !x.is_zero())
.map_or(false, |x| x.into_repr().is_odd())
}
}

Expand Down Expand Up @@ -188,34 +188,45 @@ mod tests {
impl ElementParity<Fq6> for Fq6 {}

#[test]
fn test_parity() {
fn test_parity_of_prime_field_elements() {
let a1 = Fq::from(0);
let a2 = Fq::from(1);
let a3 = Fq::from(10);
assert_eq!(Fq::parity(&a1), false);
assert_eq!(Fq::parity(&a2), true);
assert_eq!(Fq::parity(&a3), false);
}

#[test]
fn test_parity_of_quadratic_extension_elements() {
let element_test1 = Fq2::new(Fq::from(0), Fq::from(1));
let element_test2 = Fq2::new(Fq::from(1), Fq::from(0));
let element_test3 = Fq2::new(Fq::from(10), Fq::from(5));
let element_test4 = Fq2::new(Fq::from(5), Fq::from(10));
assert_eq!(Fq2::parity(&element_test1), true, "parity is the oddness of first non-zero coefficient of element represented over the prime field" );
assert_eq!(Fq2::parity(&element_test2), true);
assert_eq!(Fq2::parity(&element_test3), false);
assert_eq!(Fq2::parity(&element_test4), true);
}

#[test]
fn test_parity_of_cubic_extension_elements() {
let a1 = Fq2::new(Fq::from(0), Fq::from(0));
let a2 = Fq2::new(Fq::from(0), Fq::from(1));
let a3 = Fq2::new(Fq::from(1), Fq::from(0));
let a4 = Fq2::new(Fq::from(1), Fq::from(1));
let a5 = Fq2::new(Fq::from(0), Fq::from(2));

let element_test1 = Fq6::new(a1, a2, a3);
let element_test2 = Fq6::new(a2, a3, a4);
let element_test3 = Fq6::new(a3, a4, a1);
let element_test4 = Fq6::new(a4, a1, a2);
assert_eq!(Fq6::parity(&element_test1), false);
assert_eq!(Fq6::parity(&element_test2), false);
let element_test5 = Fq6::new(a1, a5, a2);

assert_eq!(Fq6::parity(&element_test1), true, "parity is the oddness of first non-zero coefficient of element represented over the prime field");
assert_eq!(Fq6::parity(&element_test2), true, "parity is the oddness of first non-zero coefficient of element represented over the prime field");
assert_eq!(Fq6::parity(&element_test3), true);
assert_eq!(Fq6::parity(&element_test4), true);

let element_test1 = Fq2::new(Fq::from(0), Fq::from(1));
let element_test2 = Fq2::new(Fq::from(1), Fq::from(0));
let element_test3 = Fq2::new(Fq::from(10), Fq::from(5));
let element_test4 = Fq2::new(Fq::from(5), Fq::from(10));
assert_eq!(Fq2::parity(&element_test1), false);
assert_eq!(Fq2::parity(&element_test2), true);
assert_eq!(Fq2::parity(&element_test3), false);
assert_eq!(Fq2::parity(&element_test4), true);

let a1 = Fq::from(0);
let a2 = Fq::from(1);
let a3 = Fq::from(10);
assert_eq!(Fq::parity(&a1), false);
assert_eq!(Fq::parity(&a2), true);
assert_eq!(Fq::parity(&a3), false);
assert_eq!(Fq6::parity(&element_test5), false);
}
}