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
Replace many impls of (Add|Mul|AddAssign|MulAssign)<Self>
with derive macros
#62
Conversation
It would actually be great to add macros for Sub, Neg and Div as well, so that we can have impls for those too =) |
456d5ed
to
adae5e6
Compare
@Pratyush Done! You might want to read the end of the last commit message before review. |
Hmm it would be nice to avoid adding the bound and removing const fn. Is it possible to rework the macros with the following changes:
If you give me commit access to your branch I can also make these changes, since I'm interested in playing with proc macros anyway =) |
ping @huitseeker if you give me access to your branch then I can make these changes |
@Pratyush AFAICT this is already on: |
Oh hmm I think I had tried that in the past and it hadn't worked; let me try again. Sorry for the noise! |
Note that while the attribute modification is straightforward, the trigger on additivity may be a tad more complicated, as there are many instances where you have |
Hmm that's a bug; there should be nothing in algebra that is missing one of the impls |
@Pratyush No issue in ## Add :
&'b DensePolynomial<F> 1
Evaluations<F> 1
DensePolynomial<F> 2
LinearCombination<F> 7
ConstraintVar<F> 5
&ConstraintVar<F> 2
&'b Evaluations<F> 1
&LinearCombination<F> 4
## Sub :
&'b DensePolynomial<F> 1
Evaluations<F> 1
DensePolynomial<F> 1
LinearCombination<F> 6
ConstraintVar<F> 4
&ConstraintVar<F> 2
&'b Evaluations<F> 1
&LinearCombination<F> 4
## Div :
&'b DensePolynomial<F> 1
Evaluations<F> 1
## Mul :
&'b DensePolynomial<F> 1
Evaluations<F> 1
LinearCombination<F> 2
ConstraintVar<F> 1
&'b Evaluations<F> 1 Of those:
## Add:
ff-fft/src/polynomial/dense.rs
156:impl<'a, 'b, F: Field> Add<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
182:impl<'a, 'b, F: Field> AddAssign<&'a DensePolynomial<F>> for DensePolynomial<F> {
203:impl<'a, 'b, F: Field> AddAssign<(F, &'a DensePolynomial<F>)> for DensePolynomial<F> {
## Sub:
ff-fft/src/polynomial/dense.rs
252:impl<'a, 'b, F: Field> Sub<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
284:impl<'a, 'b, F: Field> SubAssign<&'a DensePolynomial<F>> for DensePolynomial<F> {
## Mul:
ff-fft/src/polynomial/dense.rs
321:impl<'a, 'b, F: PrimeField> Mul<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
## Div:
ff-fft/src/polynomial/dense.rs
309:impl<'a, 'b, F: Field> Div<&'a DensePolynomial<F>> for &'b DensePolynomial<F> {
## Add:
r1cs-core/src/impl_constraint_var.rs
69:impl<F: Field> Add<LinearCombination<F>> for ConstraintVar<F> {
94:impl<F: Field> Add<LinearCombination<F>> for &ConstraintVar<F> {
119:impl<F: Field> Add<(F, Variable)> for ConstraintVar<F> {
132:impl<F: Field> AddAssign<(F, Variable)> for ConstraintVar<F> {
183:impl<F: Field> Add<Variable> for ConstraintVar<F> {
200:impl<'a, F: Field> Add<&'a Self> for ConstraintVar<F> {
234:impl<F: Field> Add<&ConstraintVar<F>> for &ConstraintVar<F> {
252:impl<'a, F: Field> Add<(F, &'a Self)> for ConstraintVar<F> {
## Sub:
r1cs-core/src/impl_constraint_var.rs
81:impl<F: Field> Sub<LinearCombination<F>> for ConstraintVar<F> {
106:impl<F: Field> Sub<LinearCombination<F>> for &ConstraintVar<F> {
174:impl<F: Field> Sub<(F, Variable)> for ConstraintVar<F> {
191:impl<F: Field> Sub<Variable> for ConstraintVar<F> {
217:impl<'a, F: Field> Sub<&'a Self> for ConstraintVar<F> {
243:impl<F: Field> Sub<&ConstraintVar<F>> for &ConstraintVar<F> {
270:impl<'a, F: Field> Sub<(F, &'a Self)> for ConstraintVar<F> {
## Mul:
r1cs-core/src/impl_constraint_var.rs
152:impl<F: Field> Mul<F> for ConstraintVar<F> {
164:impl<F: Field> MulAssign<F> for ConstraintVar<F> {
## Div:
## Add:
r1cs-core/src/impl_lc.rs
77:impl<F: Field> Add<(F, Variable)> for LinearCombination<F> {
87:impl<F: Field> AddAssign<(F, Variable)> for LinearCombination<F> {
133:impl<F: Field> Add<Variable> for LinearCombination<F> {
187:impl<F: Field> Add<&LinearCombination<F>> for &LinearCombination<F> {
205:impl<F: Field> Add<LinearCombination<F>> for &LinearCombination<F> {
223:impl<'a, F: Field> Add<&'a LinearCombination<F>> for LinearCombination<F> {
241:impl<F: Field> Add<LinearCombination<F>> for LinearCombination<F> {
340:impl<F: Field> Add<(F, &LinearCombination<F>)> for &LinearCombination<F> {
360:impl<'a, F: Field> Add<(F, &'a LinearCombination<F>)> for LinearCombination<F> {
380:impl<F: Field> Add<(F, LinearCombination<F>)> for &LinearCombination<F> {
399:impl<F: Field> Add<(F, Self)> for LinearCombination<F> {
## Sub:
r1cs-core/src/impl_lc.rs
97:impl<F: Field> Sub<(F, Variable)> for LinearCombination<F> {
142:impl<F: Field> Sub<Variable> for LinearCombination<F> {
259:impl<F: Field> Sub<&LinearCombination<F>> for &LinearCombination<F> {
281:impl<'a, F: Field> Sub<&'a LinearCombination<F>> for LinearCombination<F> {
301:impl<F: Field> Sub<LinearCombination<F>> for &LinearCombination<F> {
321:impl<F: Field> Sub<LinearCombination<F>> for LinearCombination<F> {
419:impl<F: Field> Sub<(F, &LinearCombination<F>)> for &LinearCombination<F> {
427:impl<'a, F: Field> Sub<(F, &'a LinearCombination<F>)> for LinearCombination<F> {
435:impl<F: Field> Sub<(F, LinearCombination<F>)> for &LinearCombination<F> {
443:impl<'a, F: Field> Sub<(F, LinearCombination<F>)> for LinearCombination<F> {
## Mul:
r1cs-core/src/impl_lc.rs
116:impl<F: Field> Mul<F> for LinearCombination<F> {
126:impl<F: Field> MulAssign<F> for LinearCombination<F> {
## Div: In sum, the unbundled macro seems more versatile unless we want to rewrite it for use outside the algebra crate. I also like the idea that with an unbundled macro scheme, the developer can deduce what traits are implemented from the derive annotation, without too much implicit knowledge. |
adae5e6
to
e2cf170
Compare
@Pratyush Done with the custom attribute (at the cost of some complexity in the macros, please have them reviewed by your local/favorite proc macro expert). |
e2cf170
to
1934cc9
Compare
@Pratyush rebased on the sccache master state. |
1934cc9
to
a413cfa
Compare
…ve macros The derive macros defer to the versions implemented on `Self`-type references.
…FromRef This required removing the const annotation on the constructor for fp_256->fp_832. This can be re-added with `#![feature(const_fn)]`. This was necessary because otherwise the derivation of generic parameters would have to rely on name mangling.
… a trait bound on the struct
a413cfa
to
6343100
Compare
Hmm, I did actually intend to provide these impls only for However, I think the machinery in this PR can be repurposed for two uses:
|
@Pratyush At the end of the day, this PR is pretty much exactly Would you mind opening (two) issues fleshing out your thinking? |
Superseded by #98 |
The derive macros defer to the versions implemented on
Self
-type references.This eliminates a lot of the impl boilerplate (& might go away when Rust figures out how to to autoref/deref on operators).
Hopefully this also serves as inspiration for extension of the approach to Sub, Neg ...