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

Arithmetic on numeric generics #4784

Open
nventuro opened this issue Apr 11, 2024 · 1 comment · May be fixed by #4958
Open

Arithmetic on numeric generics #4784

nventuro opened this issue Apr 11, 2024 · 1 comment · May be fixed by #4958
Assignees
Labels
aztec.nr Helpful for development of Aztec.nr the smart contract framework enhancement New feature or request

Comments

@nventuro
Copy link
Contributor

nventuro commented Apr 11, 2024

Problem

There's multiple design patterns that result in the need to declare generic array lenghts that are calculated on some other generic value.

Examples include:

impl<T, const N: usize> [T; N] {
    fn split_first(self) -> (T, [T; N - 1]) {
        ...
    }
}

or

trait Serialize<N> {
    fn serialize(self) -> [Field; N];
}

struct Foo<T> {
    a: T,
    b: T,
}

impl<T, N> Serialize<N * 2> for Foo<T> {
    fn serialize(self) -> [Field; N * 2] where T: Serialize<N> {
      ...
    }
}

Happy Case

I'd want to be able to both have numeric generics without having to resort to the workaround from #4633, and to be able to perform basic arithmetic on them. My current use case is to do addition to both other generics and constants (e.g. N + N + 1), but it seems multiplication and subtraction are common patterns as well.

Project Impact

Blocker

Impact Context

Lack of this feature blocks multiple things, such as flatten as requested by @LogvinovLeon here, or AztecProtocol/aztec-packages#5492 which is required for non-trivial aztec-nr data structures.

This is also related to #4633, since many of the above fall into scenarios where the current workaround to force a generic value to be numeric (i.e. make it an array length) don't work.

Workaround

None

@nventuro nventuro added the enhancement New feature or request label Apr 11, 2024
@Savio-Sou Savio-Sou added the aztec.nr Helpful for development of Aztec.nr the smart contract framework label Apr 12, 2024
@michaeljklein michaeljklein linked a pull request May 2, 2024 that will close this issue
5 tasks
@Thunkar
Copy link
Contributor

Thunkar commented May 8, 2024

Would be awesome to also have the modulo operation (mainly for AES)

pub fn compute_ciphertext<N>(
       self,
       secret: GrumpkinPrivateKey,
       point: GrumpkinPoint
   ) -> [u8; N + 16 - N % 16] where Note: NoteInterface<N> {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aztec.nr Helpful for development of Aztec.nr the smart contract framework enhancement New feature or request
Projects
Status: 🏗 In progress
Development

Successfully merging a pull request may close this issue.

4 participants