mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-04 20:03:39 +00:00
Implement Ord and PartialOrd for Field
This commit is contained in:
parent
07e2d390a9
commit
1e21c08acd
3 changed files with 46 additions and 0 deletions
|
|
@ -36,6 +36,8 @@ pub trait Field:
|
||||||
+ for<'a> SubAssign<&'a Self>
|
+ for<'a> SubAssign<&'a Self>
|
||||||
+ PartialEq
|
+ PartialEq
|
||||||
+ Eq
|
+ Eq
|
||||||
|
+ PartialOrd
|
||||||
|
+ Ord
|
||||||
+ ConditionallySelectable
|
+ ConditionallySelectable
|
||||||
+ ConstantTimeEq
|
+ ConstantTimeEq
|
||||||
+ Group<Scalar = Self>
|
+ Group<Scalar = Self>
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
impl ConditionallySelectable for Fp {
|
||||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||||
Fp([
|
Fp([
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
impl ConditionallySelectable for Fq {
|
||||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||||
Fq([
|
Fq([
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue