Skip to content

Commit

Permalink
refactor: move shared constants to globals
Browse files Browse the repository at this point in the history
  • Loading branch information
grjte committed Nov 27, 2024
1 parent 2df9180 commit 9ea0dcb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
12 changes: 2 additions & 10 deletions src/decoder.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::defaults::BASE64_PADDING_CHAR;
use super::defaults::{BASE64_ELEMENTS_PER_CHUNK, BASE64_PADDING_CHAR, BYTES_PER_CHUNK};

pub global STANDARD: Base64DecodeBE = Base64DecodeBE::new(true);
pub global STANDARD_NO_PAD: Base64DecodeBE = Base64DecodeBE::new(false);
Expand Down Expand Up @@ -606,12 +606,7 @@ impl Base64DecodeBE {
);
}

// 240 bits fits 40 6-bit chunks and 30 8-bit chunks
// we pack 40 base64 values into a field element and convert into 30 bytes
// TODO: once we support arithmetic ops on generics, derive OutputBytes from InputBytes
let mut result: [u8; OutputBytes] = [0; OutputBytes];
let BASE64_ELEMENTS_PER_CHUNK: u32 = 40;
let BYTES_PER_CHUNK: u32 = 30;
let num_chunks = (InputElements / BASE64_ELEMENTS_PER_CHUNK)
+ (InputElements % BASE64_ELEMENTS_PER_CHUNK != 0) as u32;

Expand Down Expand Up @@ -704,10 +699,7 @@ impl Base64DecodeBE {
let decoded_length = unpadded_input_len * 3 / 4;

let mut result: BoundedVec<u8, MAX_OUTPUT_LEN> = BoundedVec::new();
// 240 bits fits 40 6-bit chunks and 30 8-bit chunks
// we pack 40 base64 values into a field element and convert into 30 bytes
let BASE64_ELEMENTS_PER_CHUNK: u32 = 40;
let BYTES_PER_CHUNK: u32 = 30;

let max_chunks = MAX_INPUT_LEN / BASE64_ELEMENTS_PER_CHUNK
+ (MAX_INPUT_LEN % BASE64_ELEMENTS_PER_CHUNK != 0) as u32;
let num_chunks = (input.len() / BASE64_ELEMENTS_PER_CHUNK)
Expand Down
13 changes: 3 additions & 10 deletions src/encoder.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::defaults::BASE64_PADDING_CHAR;
use super::defaults::{BASE64_ELEMENTS_PER_CHUNK, BASE64_PADDING_CHAR, BYTES_PER_CHUNK};

pub global STANDARD: Base64EncodeBE = Base64EncodeBE::new(true);
pub global STANDARD_NO_PAD: Base64EncodeBE = Base64EncodeBE::new(false);
Expand Down Expand Up @@ -76,9 +76,6 @@ impl Base64EncodeBE {
self,
input: [u8; InputBytes],
) -> [u8; OutputElements] {
// 240 bits fits 40 6-bit chunks and 30 8-bit chunks
// we pack 40 base64 values into a field element and convert into 30 bytes
// TODO: once we support arithmetic ops on generics, derive OutputBytes from InputBytes
// Calculate the number of padding characters and the length of the output without padding
let rem = InputBytes % 3;
let num_padding_chars = if rem == 1 {
Expand Down Expand Up @@ -107,8 +104,6 @@ impl Base64EncodeBE {

let mut result: [u8; OutputElements] = [0; OutputElements];

let BASE64_ELEMENTS_PER_CHUNK: u32 = 40;
let BYTES_PER_CHUNK: u32 = 30;
let num_chunks =
(InputBytes / BYTES_PER_CHUNK) + (InputBytes % BYTES_PER_CHUNK != 0) as u32;

Expand Down Expand Up @@ -187,6 +182,7 @@ impl Base64EncodeBE {
} else {
0
};

// Every 3 chars will be encoded as 4 base64 chars
let encoded_length = (input.len() + 2) / 3 * 4; // ceil(input * 4 / 3)
let non_padded_output_length = encoded_length - num_padding_chars;
Expand All @@ -196,10 +192,7 @@ impl Base64EncodeBE {
);

let mut result: BoundedVec<u8, MAX_OUTPUT_LEN> = BoundedVec::new();
// 240 bits fits 40 6-bit chunks and 30 8-bit chunks
// we pack 40 base64 values into a field element and convert into 30 bytes
let BASE64_ELEMENTS_PER_CHUNK: u32 = 40;
let BYTES_PER_CHUNK: u32 = 30;

let max_chunks =
MAX_INPUT_LEN / BYTES_PER_CHUNK + (MAX_INPUT_LEN % BYTES_PER_CHUNK != 0) as u32;
let num_chunks =
Expand Down
4 changes: 4 additions & 0 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub use decoder::{

pub(crate) mod defaults {
pub(crate) global BASE64_PADDING_CHAR: u8 = 61;
// 240 bits fits 40 6-bit chunks and 30 8-bit chunks
// we pack 40 base64 values into a field element and convert into 30 bytes
pub(crate) global BASE64_ELEMENTS_PER_CHUNK: u32 = 40;
pub(crate) global BYTES_PER_CHUNK: u32 = 30;
}

#[test]
Expand Down

0 comments on commit 9ea0dcb

Please sign in to comment.