From 3ad896764fc9512d8e2472c93ab5ec82cb0711ee Mon Sep 17 00:00:00 2001 From: grjte Date: Wed, 27 Nov 2024 15:35:14 +0000 Subject: [PATCH] refactor: move shared constants to globals --- src/decoder.nr | 12 ++---------- src/encoder.nr | 13 +++---------- src/lib.nr | 4 ++++ 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/decoder.nr b/src/decoder.nr index 5715c7b..9b250ad 100644 --- a/src/decoder.nr +++ b/src/decoder.nr @@ -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); @@ -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; @@ -704,10 +699,7 @@ impl Base64DecodeBE { let decoded_length = unpadded_input_len * 3 / 4; let mut result: BoundedVec = 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) diff --git a/src/encoder.nr b/src/encoder.nr index e4013cf..03053bc 100644 --- a/src/encoder.nr +++ b/src/encoder.nr @@ -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); @@ -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 { @@ -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; @@ -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; @@ -196,10 +192,7 @@ impl Base64EncodeBE { ); let mut result: BoundedVec = 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 = diff --git a/src/lib.nr b/src/lib.nr index aaf1c9e..cb75b3f 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -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]