From a2d48a27761820810ae4c29a08abca9e84fbcc8a Mon Sep 17 00:00:00 2001 From: Joshua Trujillo Date: Mon, 25 Nov 2024 16:15:28 +0000 Subject: [PATCH 1/2] add "from" trait --- src/lib.nr | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/lib.nr b/src/lib.nr index 332ba5b..8f2da50 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -2,6 +2,8 @@ mod utils; pub use utils::{conditional_select, lt_f, DebugRandomEngine}; +use std::collections::bounded_vec::BoundedVec; + /** * @brief represents a byte-array of up to MaxBytes, that is used as a "haystack" array, * where we want to validate a substring "needle" is present in the "haystack" @@ -204,6 +206,15 @@ impl } } +impl From> for SubString { + fn from(input: BoundedVec) -> Self { + Self::new( + input.storage(), + input.len() as u32 + ) + } +} + // ###################################################### // S T R I N G B O D Y // ###################################################### @@ -409,6 +420,15 @@ impl StringBo } } +impl From> for StringBody { + fn from(input: BoundedVec) -> 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 @@ -544,3 +564,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); +} From f6b15f8999a8f7ac48a0530156daecf6d2e7a1cd Mon Sep 17 00:00:00 2001 From: Joshua Trujillo Date: Mon, 25 Nov 2024 16:16:11 +0000 Subject: [PATCH 2/2] fmt --- src/lib.nr | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/lib.nr b/src/lib.nr index 8f2da50..c0c2cf2 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -1,6 +1,6 @@ mod utils; -pub use utils::{conditional_select, lt_f, DebugRandomEngine}; +pub use utils::{conditional_select, DebugRandomEngine, lt_f}; use std::collections::bounded_vec::BoundedVec; @@ -206,13 +206,10 @@ impl } } -impl From> for SubString { +impl From> for SubString { fn from(input: BoundedVec) -> Self { - Self::new( - input.storage(), - input.len() as u32 - ) - } + Self::new(input.storage(), input.len() as u32) + } } // ###################################################### @@ -420,13 +417,10 @@ impl StringBo } } -impl From> for StringBody { +impl From> for StringBody { fn from(input: BoundedVec) -> Self { - Self::new( - input.storage(), - input.len() as u32 - ) - } + Self::new(input.storage(), input.len() as u32) + } } /// Given an input byte array, convert into 31-byte chunks