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

Use const generics where feasible #18

Closed
tgeoghegan opened this issue Mar 30, 2021 · 7 comments
Closed

Use const generics where feasible #18

tgeoghegan opened this issue Mar 30, 2021 · 7 comments

Comments

@tgeoghegan
Copy link
Contributor

tgeoghegan commented Mar 30, 2021

Once const generics (1 2) are out of beta, we might consider them to implement FieldElement as a generic over values of FieldParameter.

@tgeoghegan
Copy link
Contributor Author

It turns out I misunderstood this post: const generics are stable and generally available in Rust 1.51, so we could investigate adopting this now.

@cjpatton
Copy link
Collaborator

cjpatton commented Apr 5, 2021

I think we should keep FieldElement as a trait, as it would be useful to have around in case someone comes along with an alternative implementation of a particular field. What we could do is replace the make_field!() macro with an implementation of FieldElement that uses a const generic FieldParameter. Call it GenericField.Then we would implement the concrete fields as

type Field32 = GenericField<FP32>;
type Field64 = GenericField<FP64>;
type Field80 = GenericField<FP80>;
type Field126 = GenericField<FP126>;

@cjpatton
Copy link
Collaborator

cjpatton commented May 4, 2021

Const generics supports only primitive types, like integers. As such, we can't use them for FieldElement in this way.

An area where const generics would be very helpful is in the gadget implementations (prio::pcp::gadgets). In particular, the PolyEval<F> struct has a Vec<F> as a parameter:

pub struct PolyEval<F: FieldElement> {
     poly: Vec<F>,
     /// Size of buffer for FFT operations.
     n: usize,
     /// Inverse of `n` in `F`.
     n_inv: F,
}    

Usually poly.len() will be known at compile time, so we could write this as

pub struct PolyEval<F: FieldElement, const L: usize> {
     poly: [F; L],
     /// Size of buffer for FFT operations.
     n: usize,
     /// Inverse of `n` in `F`.
     n_inv: F,
}    

@tgeoghegan
Copy link
Contributor Author

We've tried a couple times now to use const generics in this library (#85) and it hasn't worked out. @cjpatton and I agree we can't be taken seriously as a Rust cryptography project until we manage to use this language feature, so we will figure out a way to to adopt it! One promising candidate would be to make vdaf::dist_input and vdaf::dist_init (or whatever they get renamed to) return constant sized arrays.

@cjpatton
Copy link
Collaborator

One promising candidate would be to make vdaf::dist_input and vdaf::dist_init (or whatever they get renamed to) return constant sized arrays.

I'm now less certain that this is a good idea. The downside, of course, is the number of shares needs to be known at compile time. If there is no obvious benefit other than aesthetics, then I don't think it's worth doing.

My main takeaway from this side quest so far is that const generics aren't mature enough to use the way we imagine they're intended to be used.

@cjpatton cjpatton changed the title Use const generics in FieldElement Use const generics where feasible Sep 30, 2021
@cjpatton
Copy link
Collaborator

cjpatton commented Mar 4, 2022

We now use const L: usize for the side of PRG seeds.

@tgeoghegan
Copy link
Contributor Author

As const generics continue to evolve in the language, we'll keep trying to use them as appropriate, but this issue doesn't track any specific work or problem so I'm closing it.

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

No branches or pull requests

2 participants