Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Using generics inputs #139

wants to merge 7 commits into from

Conversation

rrtoledo
Copy link
Collaborator

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 call to_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

  • Branch
    • Tests are provided (if possible)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
  • PR
    • No clippy warnings in the CI
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested
  • Documentation
    • Update README file (if relevant)
    • Update documentation website (if relevant)

Comments

We should be able to support both BLS and KES (with current Cardano params) signatures.

Issue(s)

Relates to #30

@rrtoledo rrtoledo added the enhancement New feature or request label Jan 29, 2025
@rrtoledo rrtoledo added this to the 1.0 milestone Jan 29, 2025
@rrtoledo rrtoledo self-assigned this Jan 29, 2025
@rrtoledo rrtoledo force-pushed the raph@bytes-generics branch 3 times, most recently from 4dfcff7 to 198ed61 Compare January 29, 2025 13:13
@rrtoledo rrtoledo linked an issue Jan 29, 2025 that may be closed by this pull request
};
use blake2::{Blake2s256, Digest};

/// Centralized Telescope proof
#[derive(Debug, Clone)]
pub struct Proof {
pub struct Proof<E: ToBytes + Clone + Sized + Ord> {
Copy link
Member

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.

https://stackoverflow.com/questions/49229332/should-trait-bounds-be-duplicated-in-struct-and-impl/66369912#66369912

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

Comment on lines 4 to 5
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)
Copy link
Member

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.

Suggested change
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())

Copy link
Collaborator Author

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?

src/utils/types.rs Outdated Show resolved Hide resolved
@rrtoledo rrtoledo force-pushed the raph@bytes-generics branch from d2fefe7 to 3fa62ae Compare February 10, 2025 12:11
Copy link
Collaborator

@curiecrypt curiecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use generics
3 participants