Skip to content

Commit

Permalink
feat: Added BoundedVec type conversion (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: kashbrti <[email protected]>
  • Loading branch information
jtriley2p and kashbrti authored Feb 25, 2025
1 parent fba4ab0 commit 0758014
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod utils;

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 +205,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 @@ -410,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 @@ -546,6 +559,32 @@ 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);
}

#[test]
fn regression_20() {
let haystack: [u8; 128] = [
Expand Down

0 comments on commit 0758014

Please sign in to comment.