Repository navigation
zk-sdk: Do not require passing owned vectors to RangeProof - #194
Merged
joncinque merged 3 commits intoSep 22, 2026
Merged
Conversation
vadorovsky
marked this pull request as ready for review
January 14, 2026 12:53
samkim-crypto
self-requested a review
January 14, 2026 13:28
samkim-crypto
force-pushed
the
zk-sdk-avoid-heap-alloc
branch
from
September 16, 2026 13:23
06f7d8d to
2ba9891
Compare
Contributor
|
@vadorovsky I rebased the changes onto main, cleaned up the lint issues, and added a few compatibility and validation tests for sanity. Let me know if I made any mistakes! |
samkim-crypto
previously approved these changes
Sep 17, 2026
vadorovsky
commented
Sep 21, 2026
vadorovsky
left a comment
Member
Author
There was a problem hiding this comment.
@samkim-crypto Your changes look good. Feel free to merge it.
Contributor
|
@joncinque since I was the last committer, I can't seem to approve and merge this PR. Can you give an approval for this PR 🙏 ? |
joncinque
previously approved these changes
Sep 22, 2026
joncinque
left a comment
Contributor
There was a problem hiding this comment.
Looks good to me! But needs a merge conflict resolution
The current implementation of `RangeProof::new` and `RangeProof::verify`
requires the caller to pass inputs as owned vectors. That requires heap
allocations even if the number of inputs is small (even as small as 1).
Remove that requirement by accepting `AsRef<[T]>` trait as inputs, which
still makes it possible to pass `Vec<T>`, so we don't break the old
users relying on the old API. But for callers who want performance, it
accepts also the following types that can be allocated on the stack:
- `[T]`
- `[T; N]`
- `&[T]`
Furthermore, single elements can be passed using `slice::from_ref`[0],
e.g.
```rust
let proof = RangeProof::new(
slice::from_ref(&55),
slice::from_ref(&64),
slice::from_ref(&open),
&mut transcript_create,
)
```
which does not perform any allocation at all. Instead, it performs raw
pointer casting to coerce a reference to a single-element slice[1][2].
Additionally, `AsRef<T>` makes it possible to pass the following
iterable types:
- `std::slice::Iter<'_, T>`
- `IterMut<'_, T>`
- `IntoIter<T, A>`
[0] https://doc.rust-lang.org/std/slice/fn.from_ref.html
[1] https://github.com/rust-lang/rust/blob/1.92.0/library/core/src/slice/raw.rs#L203-L205
[2] https://github.com/rust-lang/rust/blob/1.92.0/library/core/src/array/mod.rs#L164-L167
samkim-crypto
dismissed stale reviews from joncinque and themself
via
September 22, 2026 13:44
4222a1a
samkim-crypto
force-pushed
the
zk-sdk-avoid-heap-alloc
branch
from
September 22, 2026 13:44
2ba9891 to
4222a1a
Compare
vadorovsky
commented
Sep 22, 2026
vadorovsky
left a comment
Member
Author
There was a problem hiding this comment.
I don't have a right to approve, but ship it ![]()
joncinque
approved these changes
Sep 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current implementation of
RangeProof::newandRangeProof::verifyrequires the caller to pass inputs as owned vectors. That requires heap allocations even if the number of inputs is small (even as small as 1).Remove that requirement by accepting
AsRef<[T]>trait as inputs, which still makes it possible to passVec<T>, so we don't break the old users relying on the old API. But for callers who want performance, it accepts also the following types that can be allocated on the stack:[T][T; N]&[T]Furthermore, single elements can be passed using
slice::from_ref[0], e.g.which does not perform any allocation at all. Instead, it performs raw pointer casting to coerce a reference to a single-element slice[1][2].
Additionally,
AsRef<T>makes it possible to pass the following iterable types:std::slice::Iter<'_, T>IterMut<'_, T>IntoIter<T, A>[0] https://doc.rust-lang.org/std/slice/fn.from_ref.html
[1] https://github.com/rust-lang/rust/blob/1.92.0/library/core/src/slice/raw.rs#L203-L205
[2] https://github.com/rust-lang/rust/blob/1.92.0/library/core/src/array/mod.rs#L164-L167