Skip to content

Commit

Permalink
max input length info
Browse files Browse the repository at this point in the history
  • Loading branch information
akonior committed Nov 28, 2023
1 parent f35a34b commit 69d8c48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn encode_str<N, M>(input: str<N>, mut result: [u8; M]) -> [u8; M]
pub fn encode<N, M>(input: [u8; N], mut result: [u8; M], url_safe: bool) -> [u8; M]
```

- `input` - input string or array of bytes to encode
- `input` - input string or array of bytes to encode. Max input length is 600.
- `result` - array of bytes to store result in. it should have a proper size otherwise function will fail on assert.
Due to Noir language limitations result size need to be known at compile time and should be calculated by user.
Value of this parameter is irrelevant, only size matters.
Expand All @@ -43,4 +43,4 @@ Directory `examples/base64_example/` contains example Noir project with `base64`

## License

This project is licensed under the MIT License. See the [LICENSE](https://github.com/colinnielsen/noir-array-helpers/blob/main/LICENSE) file for details.
This project is licensed under the MIT License. See the [LICENSE](https://github.com/zkworks-xyz/noir-base64/blob/main/LICENSE) file for details.
47 changes: 28 additions & 19 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,39 @@ mod utils;
use dep::std;
use crate::utils::{as_byte, as_bytes};

global MAX_LEN = 255;
global MAX_INPUT_LEN = 600;
global MAX_RESULT_LEN = 800;

pub fn encode_str<N, M>(input: str<N>, mut result: [u8; M]) -> [u8; M] {
encode(as_bytes(input), result, false)
}

pub fn encode<N, M>(input: [u8; N], mut result: [u8; M], url_safe: bool) -> [u8; M] {
assert(N as u32 < MAX_INPUT_LEN, "input is too long. Max input length is 600");
assert(base64_calculate_length(input) == M);

let mut max_result = [0; MAX_LEN];
let mut max_input = [0; MAX_LEN];
for i in 0..input.len() {
max_input[i] = input[i];
}
for i in 0..((N as u32) / 3 + 1) {
let chunk = if i * 3 + 2 < N as u32 {
convert_chunk([max_input[i * 3], max_input[i * 3 + 1], max_input[i * 3 + 2]], url_safe)
} else if i * 3 + 1 < N as u32 {
convert_chunk([max_input[i * 3], max_input[i * 3 + 1]], url_safe)
} else {
convert_chunk([max_input[i * 3]], url_safe)
};
for j in 0..4 {
max_result[i * 4 + j] = chunk[j];
let mut max_result = [0; MAX_INPUT_LEN];
let mut max_input = [0; MAX_RESULT_LEN];
if (N as u32 <= MAX_INPUT_LEN) {
for i in 0..input.len() {
max_input[i] = input[i];
}
for i in 0..((N as u32) / 3 + 1) {
let chunk = if i * 3 + 2 < N as u32 {
convert_chunk([max_input[i * 3], max_input[i * 3 + 1], max_input[i * 3 + 2]], url_safe)
} else if i * 3 + 1 < N as u32 {
convert_chunk([max_input[i * 3], max_input[i * 3 + 1]], url_safe)
} else {
convert_chunk([max_input[i * 3]], url_safe)
};
for j in 0..4 {
max_result[i * 4 + j] = chunk[j];
}
}
}

for i in 0..result.len() {
result[i] = max_result[i];
for i in 0..result.len() {
result[i] = max_result[i];
}
}
result
}
Expand Down Expand Up @@ -158,3 +162,8 @@ fn test_convert_empty_chunk() -> [u8; 4] {
fn test_convert_too_long_chunk() -> [u8; 4] {
convert_chunk([1, 2, 3, 4], true)
}

#[test(should_fail)]
fn test_input_is_too_long() -> [u8; 1200] {
encode([0; 900], [0; 1200], false)
}

0 comments on commit 69d8c48

Please sign in to comment.