Implement Ord and PartialOrd for Field

This commit is contained in:
therealyingtong 2020-10-07 21:54:00 +08:00
parent 07e2d390a9
commit 1e21c08acd
3 changed files with 46 additions and 0 deletions

View File

@ -36,6 +36,8 @@ pub trait Field:
+ for<'a> SubAssign<&'a Self>
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ ConditionallySelectable
+ ConstantTimeEq
+ Group<Scalar = Self>

View File

@ -62,6 +62,28 @@ impl PartialEq for Fp {
}
}
impl std::cmp::Ord for Fp {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let left = self.to_bytes();
let right = other.to_bytes();
for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() {
let tmp = left_byte.cmp(right_byte);
if let std::cmp::Ordering::Equal = tmp {
continue;
} else {
return tmp;
}
}
std::cmp::Ordering::Equal
}
}
impl std::cmp::PartialOrd for Fp {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ConditionallySelectable for Fp {
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
Fp([

View File

@ -62,6 +62,28 @@ impl PartialEq for Fq {
}
}
impl std::cmp::Ord for Fq {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let left = self.to_bytes();
let right = other.to_bytes();
for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() {
let tmp = left_byte.cmp(right_byte);
if let std::cmp::Ordering::Equal = tmp {
continue;
} else {
return tmp;
}
}
std::cmp::Ordering::Equal
}
}
impl std::cmp::PartialOrd for Fq {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ConditionallySelectable for Fq {
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
Fq([