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

Implement std::ops::Add for vectors of field elements #301

Open
divergentdave opened this issue Sep 8, 2022 · 3 comments · May be fixed by #1205
Open

Implement std::ops::Add for vectors of field elements #301

divergentdave opened this issue Sep 8, 2022 · 3 comments · May be fixed by #1205
Assignees

Comments

@divergentdave
Copy link
Collaborator

There are many places in the codebase where we zip or index through two vectors, adding one to the other. We could write blanket implementations of Add and AddAssign on vectors of field elements to get rid of some boilerplate. These could also check that the lengths of the addend vectors are equal, and panic otherwise to be defensive.

See: #300 (comment)

@cjpatton cjpatton self-assigned this Jan 9, 2025
@cjpatton
Copy link
Collaborator

What's y our idea here @divergentdave? You can't just implement Add on Vec<F> because Vec is a foreign type, i.e., this doesn't compile:

impl<F: FieldElement> Add for Vec<F> {
    type Output = Vec<F>;

    fn add(self, rhs: Vec<F>) -> Vec<F> {
        todo!()
    }
}

We could do this wherever we have a wrapper type, like OutputShare or AggregateShare. Would that suffice to close this?

@divergentdave
Copy link
Collaborator Author

Good point. Implementing it on OutputShare and AggregateShare sounds good. We could also go through and replace for loops adding field vectors with calls to appropriate private helper functions.

@cjpatton cjpatton linked a pull request Jan 18, 2025 that will close this issue
@cjpatton
Copy link
Collaborator

Alrighty, here's my proposal: #1205

A couple of caveats:

  • I decided to not implement Add and AddAssign for OutputShare and AggregateShare, since the Aggregator API already facilitates arithmetic on these. I suppose we could simplify the implementations of our concrete VDAFs a little, but I didn't bother.
  • vec_assign_vector(a, b) panics if a is longer than b, but not if b is longer than a. The reason is that I wanted to be able to let b be an iterator, and there are cases where the iterator has infinite about. For example, in Prio3, b is a Prng. As far as I can tell, enforcing the length of b would require an allocation here, but I could be wrong.

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 a pull request may close this issue.

2 participants