-
Notifications
You must be signed in to change notification settings - Fork 178
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
impl (From<u32>, Hash, Ord, Product) for Scalar #52
base: main
Are you sure you want to change the base?
Conversation
232d2c9
to
3aa4d87
Compare
3aa4d87
to
3e24b43
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
impl From<u32> for Scalar { | ||
fn from(val: u32) -> Scalar { | ||
Scalar([val as u64, 0, 0, 0]) * R2 | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As is visible from other changes in the PR, this decreases usability by requiring the caller to specify the source type (e.g. 5u64.into()
instead of 5.into()
). I suspect this would be more usable if impl From<u64> for Scalar
was replaced with impl From<T: Into<u64>> for Scalar
(and it would then also work for any u*
type).
impl Ord for Scalar { | ||
fn cmp(&self, other: &Self) -> core::cmp::Ordering { | ||
let mut self_bytes = self.to_bytes(); | ||
let mut other_bytes = other.to_bytes(); | ||
&self_bytes.reverse(); | ||
&other_bytes.reverse(); | ||
self_bytes.cmp(&other_bytes) | ||
} | ||
} | ||
|
||
impl PartialOrd for Scalar { | ||
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cryptographic structures, in particular finite fields, do not have a total ordering (in the "well acktually" mathematical sense) so I disagree with exposing Ord
/PartialOrd
when they are in the form of Scalar
since they're expected to behave like finite fields. However, obviously we can totally order them when they're represented in serialized form (as this implementation leverages) assuming they're serialized canonically.
If you need Ord
for things like a BTreeMap
(for example) then the end user should convert it into its serialized form and depend on the total ordering of that instead.
No description provided.