-
Notifications
You must be signed in to change notification settings - Fork 3
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
Using generics inputs #139
base: main
Are you sure you want to change the base?
Conversation
4dfcff7
to
198ed61
Compare
src/centralized_telescope/proof.rs
Outdated
}; | ||
use blake2::{Blake2s256, Digest}; | ||
|
||
/// Centralized Telescope proof | ||
#[derive(Debug, Clone)] | ||
pub struct Proof { | ||
pub struct Proof<E: ToBytes + Clone + Sized + Ord> { |
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.
I think it's customary to not impose restrictions on generic types where unnecessary. For example, HashMap doesn't require the key type to implement Eq
and Hash
traits in the struct definition or other functions that don't require them. https://doc.rust-lang.org/src/std/collections/hash/map.rs.html#211-213
The convention is to require as few trait bounds as possible.
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.
All these traits are needed (Except for ord if we compare on the bytes as you mentionned in the other comment).
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.
I could remove Sized when using AsRef :)
src/utils/misc.rs
Outdated
pub(crate) fn check_distinct<E: ToBytes + Clone + Sized + Ord>(elements: &[E]) -> bool { | ||
let mut elements = elements.to_vec(); | ||
elements.sort_unstable(); | ||
elements.is_sorted_by(|a, b| a < b) |
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.
We should probably check that the byte representations are distinct since that is what the proving algorithms really require.
pub(crate) fn check_distinct<E: ToBytes + Clone + Sized + Ord>(elements: &[E]) -> bool { | |
let mut elements = elements.to_vec(); | |
elements.sort_unstable(); | |
elements.is_sorted_by(|a, b| a < b) | |
pub(crate) fn check_distinct<E: ToBytes + Clone>(elements: &[E]) -> bool { | |
let mut elements = elements.to_vec(); | |
elements.sort_unstable_by(|a, b| a.to_be_bytes().as_ref().cmp(b.to_be_bytes().as_ref())); | |
elements.is_sorted_by(|a, b| a.to_be_bytes().as_ref() < b.to_be_bytes().as_ref()) |
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.
I was not sure if comparing on bytes is the best approach as the conversion to byte might become expensive (say we have a big structure and not simply a signature). What do you reckon?
Perhaps we should, when truncating the dataset or filling the bins, convert everything to byte there and use references to retrieve the elements at the end?
d2fefe7
to
3fa62ae
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.
LGTM
Content
So far, we could only use Element as input type. This was fixed to a byte array of a specific size for the whole repository.
This PR aims at supporting mroe input types, and allowing the use of different input types across examples and tests.
To do so, I have created a
ToBytes
trait and updating the relevant struct and functions to use a generic type implementing it. The hash functions then only have to callto_be_bytes()
that is the one function defined in the trait.I have also written some macros to implement
ToBytes
, and so support, some standard types.Pre-submit checklist
Comments
We should be able to support both BLS and KES (with current Cardano params) signatures.
Issue(s)
Relates to #30