zk-sdk: Do not require passing owned vectors to RangeProof#194
Open
vadorovsky wants to merge 1 commit into
Open
zk-sdk: Do not require passing owned vectors to RangeProof#194vadorovsky wants to merge 1 commit into
RangeProof#194vadorovsky wants to merge 1 commit into
Conversation
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
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