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

Add return length assert #19

Closed
olehmisar opened this issue Oct 24, 2024 · 11 comments
Closed

Add return length assert #19

olehmisar opened this issue Oct 24, 2024 · 11 comments

Comments

@olehmisar
Copy link

To prevent fools (like me) from shooting themselves in the foot.
Add this assert:

assert(InputElements == div_ceil(OutputBytes, 3) * 4, "base64 length mismatch");

Add this check for encode and decode. You can grab dev_ceil implementation here.

https://github.com/grjte/noir_base64/blob/eb933f20b8175132f8e965e519b98c41ad805af1/src/lib.nr#L100

@grjte
Copy link
Contributor

grjte commented Oct 24, 2024

@olehmisar good point! I think that this exact fix won't work for the non-padding case, only for padding (which is the standard setting), but I will update it in the code to make padding optional that I mentioned in #15 , since it adds support for padding and addresses a similar error, and we can put in a slightly different assert for the non-padding case. (I'm just waiting for #18 to be merged to be merged so I can open a PR for the padding fix and then the follow-up for the base64url)

@grjte
Copy link
Contributor

grjte commented Oct 24, 2024

the ideal will be to compute OutputBytes from InputElements, which I get the impression from the inline comments will be possible at some point in the future (once arithmetic ops are supported on generics), but it would be cool to hear if the Noir team has a timeline for this

@olehmisar
Copy link
Author

olehmisar commented Oct 24, 2024

@grjte arithmetic ops are already supported on generics. But only them. You need div_ceil to calculate base64 encoding length: out = ceil(input / 4) * 3. It requires comptime fns in generics. Is it something that is on the roadmap @TomAFrench?

@TomAFrench
Copy link
Member

Having support for arbitrary comptime functions in generics is definitely not on the roadmap. ceil(input/4) can just be done as (input + 3) / 4.

@olehmisar
Copy link
Author

No, you have to ceil it

@olehmisar
Copy link
Author

@TomAFrench if conditions were supported in generics, it would be written as:

fn decode<let N: u32>(input: N) -> [(N / 3 + if N % 3 == 0 { 0 } else { 1 }) * 4)] { ... }

@grjte
Copy link
Contributor

grjte commented Oct 25, 2024

helpful clarifications, thanks. Sounds like that inline comment should go away

@grjte
Copy link
Contributor

grjte commented Oct 25, 2024

In this case @olehmisar I'm just going to push the change to add padding and then alphabet support as they're already written to avoid making the PRs more complex & I'll leave this bug open

@olehmisar
Copy link
Author

@TomAFrench oh yeah, you are absolutely right, ceil(a / b) can be rewritten as (a + b - 1) / b

@saleel
Copy link
Contributor

saleel commented Oct 25, 2024

Just saw this - should be fixed for both pad/non-pad once this is merged

@saleel
Copy link
Contributor

saleel commented Oct 28, 2024

Fixed in #21. Will be released in #20

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

4 participants