### Pad Array Start in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Illustrates the `pad_start` method from `ArrayExtensions`, which prepends a specified value to an array until it reaches a target length. ```rust use nodash::ArrayExtensions; assert([1, 2, 3].pad_start::<5>(0) == [0, 0, 1, 2, 3]); ``` -------------------------------- ### Install Nodash in Nargo.toml Source: https://github.com/olehmisar/nodash/blob/main/README.md This TOML snippet shows how to add Nodash as a dependency to your Noir project by specifying the Git repository and a specific tag. ```toml nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.43.0" } ``` -------------------------------- ### Array Padding at Start with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Pads the beginning of an array with a specified value to reach a target length. This function is useful for creating arrays of a fixed size or aligning data structures. ```rust use nodash::ArrayExtensions; fn main() { // Pad to length 5 with zeros let arr = [1, 2, 3]; let padded: [i32; 5] = arr.pad_start::<5>(0); assert(padded == [0, 0, 1, 2, 3]); // Pad with different values assert([7, 8, 9].pad_start::<6>(99) == [99, 99, 99, 7, 8, 9]); } ``` -------------------------------- ### Runtime Input Validation for Main Functions in Rust Source: https://context7.com/olehmisar/nodash/llms.txt Illustrates how to use the `#[validate_inputs]` attribute and `ValidateInput` trait from the `nodash` library to enforce runtime validation of circuit inputs. This prevents invalid values from JavaScript from bypassing type constraints, enhancing security. The example defines a custom bounded type `U120` and applies validation to a main function. ```rust use nodash::{validate_inputs, ValidateInput}; // Custom bounded type struct U120 { inner: Field, } impl U120 { fn new(inner: Field) -> Self { inner.assert_max_bit_size::<120>(); Self { inner } } } // Implement validation impl ValidateInput for U120 { fn validate(self) { U120::new(self.inner); // Checks bounds } } // Apply validation to main function #[validate_inputs] fn main(a: U120, b: U120) { // a and b are guaranteed to be valid U120 values // Even if caller passes invalid data from JavaScript, // it will fail at the start of main let sum = a.inner + b.inner; } // Without #[validate_inputs], this JavaScript call would succeed: // await noir.execute({ a: { inner: 2n ** 120n + 1n } }) // // With #[validate_inputs], it fails with: // "Assertion failed: call to assert_max_bit_size" ``` -------------------------------- ### Slice Array with Fixed Length in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Shows the `slice` method from `ArrayExtensions`, which returns a sub-array of a specified length `L` starting from a given index. Panics if the slice is out of bounds. ```rust use nodash::ArrayExtensions; assert([1, 2, 3, 4, 5].slice::<3>(1) == [2, 3, 4]); ``` -------------------------------- ### Get ASCII Character Code with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Returns the ASCII (Unicode code point) value of a single character. This utility is useful for character manipulation and comparisons. ```rust use nodash::ord; fn main() { assert(ord("a") == 97); assert(ord("A") == 65); assert(ord("0") == 48); assert(ord(" ") == 32); } ``` -------------------------------- ### Get ASCII Code of Character in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates the `ord` function, which returns the ASCII (Unicode code point) value of a single character string. ```rust use nodash::ord; assert(ord("a") == 97); ``` -------------------------------- ### SHA256 Hashing in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates the `sha256` hash function. It's noted as expensive in Noir, recommending Poseidon2 where possible. Supports fixed-size arrays and BoundedVec. ```rust use nodash::sha256; let hash = sha256([10, 20]); // or let hash = sha256(BoundedVec::from_parts([10, 20, 0], 2)); ``` -------------------------------- ### Pack Bytes for Efficient Hashing in Rust Source: https://context7.com/olehmisar/nodash/llms.txt Demonstrates packing byte arrays into Field elements for more efficient hashing within ZK circuits using the `nodash` library. It shows how to use `pack_bytes` to prepare data for hashing with `poseidon2`, significantly reducing computational cost compared to hashing raw bytes. ```rust use nodash::{pack_bytes, poseidon2}; fn main() { // Pack 32 bytes into 2 Field elements let bytes: [u8; 32] = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 ]; let packed: [Field; 2] = pack_bytes(bytes); // Hash packed representation - MUCH cheaper than hashing bytes directly let efficient_hash = poseidon2(packed); // Compare: this would be very expensive // let expensive_hash = poseidon2(bytes); } ``` -------------------------------- ### Poseidon2 Hashing in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates the `poseidon2` hash function, which can accept a fixed-size array or a BoundedVec. This is a recommended hash for Noir. ```rust use nodash::poseidon2; // hashes the whole array let hash = poseidon2([10, 20]); // hashes elements up to the length (in this case, 2) let hash = poseidon2(BoundedVec::from_parts([10, 20, 0], 2)); ``` -------------------------------- ### Calculate Integer Square Root in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates the `sqrt` function from Nodash, which calculates the integer square root of a u64. Note that it floors the result. ```rust use nodash::sqrt; assert(sqrt(4 as u64) == 2); // it floors the result assert(sqrt(8 as u64) == 2); ``` -------------------------------- ### Add Nodash Dependency to Nargo.toml Source: https://context7.com/olehmisar/nodash/llms.txt Specifies how to add the Nodash library as a dependency in your project's Nargo.toml file. This ensures that the library's functions and types are available for use in your Noir code. It references a specific Git repository and tag for version control. ```toml # Add to Nargo.toml [dependencies] nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.43.0" } ``` -------------------------------- ### Keccak256 Hashing in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Illustrates the `keccak256` hash function. Similar to SHA256, it's noted as expensive in Noir. Supports fixed-size arrays and BoundedVec. ```rust use nodash::keccak256; let hash = keccak256([10, 20]); // or let hash = keccak256(BoundedVec::from_parts([10, 20, 0], 2)); ``` -------------------------------- ### Safe Type Conversion in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Illustrates the `try_from` trait for fallible conversions between numeric types, such as converting a number to u8 or u128. Conversion errors will cause a runtime panic. ```rust use nodash::TryFrom; assert(u8::try_from(123) == 123); u8::try_from(256); // runtime error assert(u128::try_from(123) == 123); ``` -------------------------------- ### Compute Poseidon2 Hash with Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Implements the Poseidon2 hash function, an efficient zero-knowledge-friendly hash suitable for ZK circuits. It accepts fixed-size arrays or BoundedVecs. Poseidon2 is preferred over SHA256 and Keccak256 in ZK circuits due to its lower computational cost. ```rust use nodash::poseidon2; fn main() { // Hash a fixed array let hash = poseidon2([10, 20]); // Hash a BoundedVec - only hashes up to length let vec = BoundedVec::from_parts([10, 20, 0], 2); let hash = poseidon2(vec); // Both produce the same result for the same data let arr: [Field; 2] = [1, 2]; let vec: BoundedVec = BoundedVec::from(arr); assert(poseidon2(arr) == poseidon2(vec)); } ``` -------------------------------- ### JavaScript: Executing Noir with Validated U120 Input (Runtime Error) Source: https://github.com/olehmisar/nodash/blob/main/README.md Illustrates how executing a Noir circuit with the `#[validate_inputs]` attribute enabled on `fn main` will now result in a runtime error when an invalid `U120` input is provided from JavaScript. This confirms that the input validation mechanism is working correctly. ```javascript // runtime error: "Assertion failed: call to assert_max_bit_size" await noir.execute({ a: { inner: 2n ** 120n + 1n, }, }); ``` -------------------------------- ### Pedersen Hashing in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Shows the `pedersen` hash function, which computes a Pedersen hash. It accepts a fixed-size array of fields. ```rust use nodash::pedersen; let hash = pedersen([10, 20]); ``` -------------------------------- ### JavaScript: Executing Noir with Invalid U120 Input (Fails) Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates how executing a Noir circuit with a `fn main` function accepting a `U120` struct can succeed with an invalid input from JavaScript, despite the `U120::new` assertion. This highlights the need for explicit input validation at the `main` function level. ```javascript // this succeeds but it shouldn't! await noir.execute({ a: { inner: 2n ** 120n + 1n, }, }); ``` -------------------------------- ### Clamp a Value within a Range in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Illustrates the `clamp` function, which restricts a given u64 value to be within a specified minimum and maximum range. ```rust use nodash::clamp; // if too small, return min assert(clamp(1 as u64, 2 as u64, 3 as u64) == 2 as u64); // if too big, return max assert(clamp(4 as u64, 1 as u64, 3 as u64) == 3 as u64); // if in range, return value assert(clamp(2 as u64, 1 as u64, 3 as u64) == 2 as u64); ``` -------------------------------- ### Ceiling Division in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Shows the `div_ceil` function, which performs integer division of two u64 numbers and rounds the result up to the nearest integer. ```rust use nodash::div_ceil; assert(div_ceil(10 as u64, 3) == 4); ``` -------------------------------- ### Pack Bytes into Fields in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Explains the `pack_bytes` function, which converts a `[u8; N]` array into `[Field; N / 31 + 1]`. This is useful for hashing byte arrays more efficiently using functions like `poseidon2`. ```rust use nodash::pack_bytes; let bytes: [u8; 32] = [0; 32]; let packed = pack_bytes(bytes); ``` -------------------------------- ### Compute Keccak256 Hash with Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Provides the Keccak256 hash function, compatible with Ethereum standards. It accepts byte arrays and BoundedVecs. Similar to SHA256, Keccak256 is computationally intensive within ZK circuits, making Poseidon2 a more efficient alternative for these environments. ```rust use nodash::keccak256; fn main() { // Hash byte array let hash: [u8; 32] = keccak256([10, 20]); // Hash BoundedVec let vec = BoundedVec::from_parts([10, 20, 0], 2); let hash: [u8; 32] = keccak256(vec); // Verify equivalence let arr: [u8; 2] = [1, 2]; let vec: BoundedVec = BoundedVec::from(arr); assert(keccak256(arr) == keccak256(vec)); } ``` -------------------------------- ### Solidity ABI Encoding with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Encodes function selectors and arguments in Solidity ABI format, similar to `abi.encodeWithSelector` in Solidity. It takes a selector and an array of arguments to produce the ABI-encoded byte array. ```rust use nodash::solidity::encode_with_selector; fn main() { // Encode transfer(address,uint256) call let selector: u32 = 0xa9059cbb; let args: [Field; 2] = [ 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045, // recipient address 123 // amount ]; let encoded: [u8; 68] = encode_with_selector(selector, args); // More complex example with multiple arguments let selector: u32 = 0x86d09dae; // deposit_private(bytes32,address[2],uint256[2]) let args: [Field; 5] = [ 0x1a7097a0f09457b0b0496684b2ed6723a262da3a1b061b598ee92ce3376cb302, 0x610178dA211FEF7D417bC0e6FeD39F05609AD788, 0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e, 333, 64 ]; let encoded: [u8; 164] = encode_with_selector(selector, args); } ``` -------------------------------- ### Compute Pedersen Hash with Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Provides the Pedersen hash function, a cryptographic hash function commonly used in zero-knowledge proofs, particularly with Pedersen commitments. It accepts fixed-size arrays of Field elements. ```rust use nodash::pedersen; fn main() { // Hash array of Field elements let hash = pedersen([10, 20]); // Larger arrays let data: [Field; 5] = [1, 2, 3, 4, 5]; let hash = pedersen(data); } ``` -------------------------------- ### Compute SHA256 Hash with Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Implements the standard SHA256 hash function. It supports hashing byte arrays and BoundedVecs. While functional, SHA256 is computationally expensive in ZK circuits, and Poseidon2 is generally recommended for such applications. ```rust use nodash::sha256; fn main() { // Hash byte array let hash: [u8; 32] = sha256([10, 20]); // Hash BoundedVec - only hashes up to length let vec = BoundedVec::from_parts([10, 20, 0], 2); let hash: [u8; 32] = sha256(vec); // Both produce same result for same data let arr: [u8; 2] = [1, 2]; let vec: BoundedVec = BoundedVec::from(arr); assert(sha256(arr) == sha256(vec)); } ``` -------------------------------- ### Compute Integer Square Root with Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Computes the integer square root of a value, flooring the result to the nearest integer. This function works with any numeric type supported by Noir, including Field elements. It's useful for calculations requiring a floored square root within zero-knowledge circuits. ```rust use nodash::sqrt; fn main() { // Perfect square assert(sqrt(4 as u64) == 2); // Non-perfect square - result is floored assert(sqrt(8 as u64) == 2); // Works with multiple integer types assert(sqrt(18 as u8) == 4); assert(sqrt(2482737472 as u32) == 49827); assert(sqrt(14446244073709551616 as u64) == 3800821499); assert(sqrt(1444624284781234073709551616 as u128) == 38008213385809); } ``` -------------------------------- ### String to u64 Conversion with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Converts a numeric string, up to 20 characters long, to a u64 integer. It supports zero-padding, allowing for consistent input formats even if the numeric value is shorter than 20 digits. ```rust use nodash::str_to_u64; fn main() { // With zero padding assert(str_to_u64("02345678912345678912") == 2345678912345678912); // Partial string with padding let s = "13378584420".as_bytes(); let padded = s.concat([0; 9]); // Pad to 20 chars assert(str_to_u64(padded) == 13378584420); } ``` -------------------------------- ### Enumerate Array Elements in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Shows the `enumerate` method from `ArrayExtensions`, which transforms an array into an array of tuples, where each tuple contains the index and the original element. ```rust use nodash::ArrayExtensions; assert(["a", "b", "c"].enumerate() == [(0, "a"), (1, "b"), (2, "c")]); ``` -------------------------------- ### Encode with Selector in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Provides a function `encode_with_selector` equivalent to Solidity's `abi.encodeWithSelector`. It takes a selector and an array of fields as arguments. ```rust use nodash::solidity::encode_with_selector; let selector: u32 = 0xa9059cbb; // transfer(address,uint256) let args: [Field; 2] = [ 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045, // address 123 // uint256 ]; let encoded = encode_with_selector(selector, args); // typeof encoded: [u8; 68] ``` -------------------------------- ### Pad Array End in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates the `pad_end` method from `ArrayExtensions`, which appends a specified value to an array until it reaches a target length. ```rust use nodash::ArrayExtensions; assert([1, 2, 3].pad_end::<5>(0) == [1, 2, 3, 0, 0]); ``` -------------------------------- ### Rust: Implement Input Validation for U120 in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Shows how to implement input validation for a `U120` type in Noir using the `#[validate_inputs]` attribute and the `ValidateInput` trait. This ensures that when a `U120` is passed to `fn main`, its `inner` field is validated using the `U120::new` function's assertion. ```rust use nodash::{validate_inputs, ValidateInput}; // this attribute checks that `U120` is within the range via `ValidateInput` trait #[validate_inputs] fn main(a: U120) { // do something with a } impl ValidateInput for U120 { fn validate(self) { // call the `new` function that asserts the max bit size U120::new(self.inner); } } ``` -------------------------------- ### Convert String to u64 in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Shows the `str_to_u64` function, which converts a string literal representing a number into a u64 integer type. ```rust use nodash::str_to_u64; assert(str_to_u64("02345678912345678912") == 02345678912345678912); ``` -------------------------------- ### Safe Field to Integer Type Conversion with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Provides a fail-safe way to convert Field elements to various integer types (u8, u16, u32, u64, etc.) with automatic runtime bounds checking. If the value exceeds the target integer type's range, the conversion will panic. ```rust use nodash::TryFrom; fn main() { // Successful conversions assert(u8::try_from(123) == 123); assert(u128::try_from(123) == 123); assert(u64::try_from(1000) == 1000); // This will cause runtime error: value exceeds u8 range // u8::try_from(256); // Panics with "call to assert_max_bit_size" // Works with all integer types let value: Field = 42; let as_u8: u8 = u8::try_from(value); let as_u16: u16 = u16::try_from(value); let as_u32: u32 = u32::try_from(value); let as_u64: u64 = u64::try_from(value); } ``` -------------------------------- ### Perform Ceiling Division with Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Calculates division rounded up to the nearest integer, equivalent to `ceil(a / b)`. This function is essential for scenarios where fractional results must be rounded upwards, ensuring that no quantity is lost due to truncation. It supports various numeric types. ```rust use nodash::div_ceil; fn main() { // Exact division assert(div_ceil(2 as u64, 2) == 1); assert(div_ceil(4 as u64, 2) == 2); // Division with remainder - rounds up assert(div_ceil(1 as u64, 2) == 1); assert(div_ceil(3 as u64, 2) == 2); assert(div_ceil(10 as u64, 3) == 4); assert(div_ceil(1337 as u64, 19) == 71); } ``` -------------------------------- ### Array Enumeration with Index and Value in nodash Source: https://context7.com/olehmisar/nodash/llms.txt Creates an array of (index, value) tuples from an existing array. This is a common pattern for iterating over elements while keeping track of their original positions. ```rust use nodash::ArrayExtensions; fn main() { let arr = ["a", "b", "c"]; let enumerated = arr.enumerate(); assert(enumerated == [(0, "a"), (1, "b"), (2, "c")]); // Use with numeric arrays let numbers = [10, 20, 30]; let indexed = numbers.enumerate(); assert(indexed == [(0, 10), (1, 20), (2, 30)]); } ``` -------------------------------- ### Convert Field to Hex String in Noir Source: https://github.com/olehmisar/nodash/blob/main/README.md Demonstrates the `field_to_hex` function, which converts a Noir `Field` type into its hexadecimal string representation without the '0x' prefix. ```rust let my_hash = 0x0d67824fead966192029093a3aa5c719f2b80262c4f14a5c97c5d70e4b27f2bf; let expected = "0d67824fead966192029093a3aa5c719f2b80262c4f14a5c97c5d70e4b27f2bf"; assert_eq(field_to_hex(my_hash), expected); ``` -------------------------------- ### Array Padding at End with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Pads the end of an array with a specified value to reach a target length. This is useful for ensuring arrays have a consistent size, often required in data processing or serialization. ```rust use nodash::ArrayExtensions; fn main() { // Pad to length 5 with zeros let arr = [1, 2, 3]; let padded: [i32; 5] = arr.pad_end::<5>(0); assert(padded == [1, 2, 3, 0, 0]); // Pad with different values assert([7, 8, 9].pad_end::<6>(99) == [7, 8, 9, 99, 99, 99]); } ``` -------------------------------- ### Convert Field Element to Hex String with nodash Source: https://context7.com/olehmisar/nodash/llms.txt Converts a Field element to a 64-character hexadecimal string without the `0x` prefix. This function is useful for representing large numbers or hashes in a hexadecimal format for display or further processing. ```rust use nodash::field_to_hex; fn main() { let my_hash = 0x0d67824fead966192029093a3aa5c719f2b80262c4f14a5c97c5d70e4b27f2bf; let hex: str<64> = field_to_hex(my_hash); assert(hex == "0d67824fead966192029093a3aa5c719f2b80262c4f14a5c97c5d70e4b27f2bf"); // Use in string concatenation let header_prefix: [u8; 26] = "subject:Re: Tx request: 0x".as_bytes(); let hex_bytes = hex.as_bytes(); let full_header: [u8; 90] = header_prefix.concat(hex_bytes); } ``` -------------------------------- ### Array Slicing with Compile-Time Checks in nodash Source: https://context7.com/olehmisar/nodash/llms.txt Extracts a contiguous subsequence from an array. This function provides compile-time length checking, ensuring that the slice operation is valid and does not exceed the array bounds at compile time. ```rust use nodash::ArrayExtensions; fn main() { // Extract 3 elements starting at index 1 let arr = [1, 2, 3, 4, 5]; let sliced: [i32; 3] = arr.slice::<3>(1); assert(sliced == [2, 3, 4]); // Extract from different positions assert([10, 20, 30, 40, 50].slice::<2>(0) == [10, 20]); assert([10, 20, 30, 40, 50].slice::<2>(3) == [40, 50]); } ``` -------------------------------- ### Clamp Value within Range using Nodash Source: https://context7.com/olehmisar/nodash/llms.txt Constrains a value to a specified range. If the value is less than the minimum, it returns the minimum; if it's greater than the maximum, it returns the maximum. Otherwise, it returns the value itself. This is useful for ensuring values stay within acceptable bounds in circuits. ```rust use nodash::clamp; fn main() { // Value too small - returns min assert(clamp(1 as u64, 2 as u64, 3 as u64) == 2 as u64); // Value too large - returns max assert(clamp(4 as u64, 1 as u64, 3 as u64) == 3 as u64); // Value in range - returns value assert(clamp(2 as u64, 1 as u64, 3 as u64) == 2 as u64); } ``` -------------------------------- ### Rust: Define U120 Struct with Bit Size Assertion Source: https://github.com/olehmisar/nodash/blob/main/README.md Defines a `U120` struct in Rust for Noir, which includes an `inner` field of type `Field`. The `new` function asserts that the provided `inner` value does not exceed 120 bits, preventing runtime errors for values larger than 2^120. ```rust struct U120 { inner: Field, } impl U120 { fn new(inner: Field) -> Self { inner.assert_max_bit_size::<120>(); Self { inner } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.