Skip to content

impl (From<u32>, Hash, Ord, Product) for Scalar#52

Open
A-Manning wants to merge 3 commits intozkcrypto:mainfrom
A-Manning:ash/scalar-traits
Open

impl (From<u32>, Hash, Ord, Product) for Scalar#52
A-Manning wants to merge 3 commits intozkcrypto:mainfrom
A-Manning:ash/scalar-traits

Conversation

@A-Manning
Copy link
Contributor

No description provided.

@A-Manning A-Manning changed the title impl (From<u32>, Hash, Ord) for Scalar impl (From<u32>, Hash, Ord, Product) for Scalar Jan 7, 2021
Copy link
Member

@str4d str4d left a 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!

Comment on lines +40 to +45
impl From<u32> for Scalar {
fn from(val: u32) -> Scalar {
Scalar([val as u64, 0, 0, 0]) * R2
}
}

Copy link
Member

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).

Comment on lines +68 to +82
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))
}
}
Copy link
Contributor

@ebfull ebfull Sep 1, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants