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

feat: Added BoundedVec type conversion #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod utils;

pub use utils::{conditional_select, lt_f, DebugRandomEngine};
pub use utils::{conditional_select, DebugRandomEngine, lt_f};

use std::collections::bounded_vec::BoundedVec;

/**
* @brief represents a byte-array of up to MaxBytes, that is used as a "haystack" array,
Expand Down Expand Up @@ -204,6 +206,12 @@ impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32>
}
}

impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32> From<BoundedVec<u8, MaxBytes>> for SubString<MaxPaddedBytes, PaddedChunksMinusOne, MaxBytes> {
fn from(input: BoundedVec<u8, MaxBytes>) -> Self {
Self::new(input.storage(), input.len() as u32)
}
}

// ######################################################
// S T R I N G B O D Y
// ######################################################
Expand Down Expand Up @@ -409,6 +417,12 @@ impl<let MaxPaddedBytes: u32, let PaddedChunks: u32, let MaxBytes: u32> StringBo
}
}

impl<let MaxPaddedBytes: u32, let PaddedChunksMinusOne: u32, let MaxBytes: u32> From<BoundedVec<u8, MaxBytes>> for StringBody<MaxPaddedBytes, PaddedChunksMinusOne, MaxBytes> {
fn from(input: BoundedVec<u8, MaxBytes>) -> Self {
Self::new(input.storage(), input.len() as u32)
}
}

/// Given an input byte array, convert into 31-byte chunks
///
/// Cost: ~0.5 gates per byte
Expand Down Expand Up @@ -544,3 +558,29 @@ unconstrained fn test_partial_match() {

assert(position == 123);
}

#[test]
fn test_substring_from_bounded_vec() {
let haystack_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
.as_bytes();
let needle_text = " dolor in reprehenderit in voluptate velit esse".as_bytes();

let mut haystack: StringBody512 = BoundedVec::from(haystack_text).into();
let mut needle: SubString64 = BoundedVec::from(needle_text).into();

let result = haystack.substring_match(needle);
assert(result.0 == true);
}

#[test]
fn test_string_body_from_bounded_vec() {
let haystack_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
.as_bytes();

let mut haystack: StringBody512 = BoundedVec::from(haystack_text).into();
let needle_text = " dolor in reprehenderit in voluptate velit esse".as_bytes();
let mut needle: SubString64 = BoundedVec::from(needle_text).into();

let result = haystack.substring_match(needle);
assert(result.0 == true);
}