Skip to content

Commit

Permalink
Merge pull request #1 from noir-lang/mv/update-u32-num-gen
Browse files Browse the repository at this point in the history
fix: Switch to u32 num generic
  • Loading branch information
vezenovm authored Jul 31, 2024
2 parents 449170f + 5e32d42 commit e018498
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Base64DecodeBE {
/**
* @brief Take an array of ASCII values and convert into base64 values
**/
pub fn base64_encode_elements<let InputElements: u64>(input: [u8; InputElements]) -> [u8; InputElements] {
pub fn base64_encode_elements<let InputElements: u32>(input: [u8; InputElements]) -> [u8; InputElements] {
// for some reason, if the lookup table is not defined in a struct, access costs are expensive and ROM tables aren't being used :/
let mut Base64Encoder = Base64EncodeBE::new();

Expand All @@ -69,7 +69,7 @@ pub fn base64_encode_elements<let InputElements: u64>(input: [u8; InputElements]
/**
* @brief Take an array of base64 values and convert into ASCII values
**/
pub fn base64_decode_elements<let InputElements: u64>(input: [u8; InputElements]) -> [u8; InputElements] {
pub fn base64_decode_elements<let InputElements: u32>(input: [u8; InputElements]) -> [u8; InputElements] {
// for some reason, if the lookup table is not defined in a struct, access costs are expensive and ROM tables aren't being used :/
let mut Base64Decoder = Base64DecodeBE::new();

Expand All @@ -86,16 +86,16 @@ pub fn base64_decode_elements<let InputElements: u64>(input: [u8; InputElements]
* Each Base64 value is 6 bits. This method will produce a byte array where data is concatenated so that there are no sparse bits
* (e.g. encoding 4 ASCII values produces 24 bits of Base64 data = 3 bytes of output data)
**/
pub fn base64_encode<let InputElements: u64, let OutputBytes: u64>(input: [u8; InputElements]) -> [u8; OutputBytes] {
pub fn base64_encode<let InputElements: u32, let OutputBytes: u32>(input: [u8; InputElements]) -> [u8; OutputBytes] {
let encoded: [u8; InputElements] = base64_encode_elements(input);
// 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: u64 = 40;
let BYTES_PER_CHUNK: u64 = 30;
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 u64;
+ (InputElements % BASE64_ELEMENTS_PER_CHUNK != 0) as u32;

for i in 0..num_chunks - 1 {
let mut slice: Field = 0;
Expand Down Expand Up @@ -131,15 +131,15 @@ pub fn base64_encode<let InputElements: u64, let OutputBytes: u64>(input: [u8; I
/**
* @brief Take an array of packed base64 encoded bytes and convert into ASCII
**/
pub fn base64_decode<let InputBytes: u64, let OutputElements: u64>(input: [u8; InputBytes]) -> [u8; OutputElements] {
pub fn base64_decode<let InputBytes: u32, let OutputElements: u32>(input: [u8; InputBytes]) -> [u8; OutputElements] {
// let decoded: [u8; InputBytes] = base64_decode_elements(input);
// 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; OutputElements] = [0; OutputElements];
let BASE64_ELEMENTS_PER_CHUNK: u64 = 40;
let BYTES_PER_CHUNK: u64 = 30;
let num_chunks = (InputBytes / BYTES_PER_CHUNK) + (InputBytes % BYTES_PER_CHUNK != 0) as u64;
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;

for i in 0..num_chunks - 1 {
let mut slice: Field = 0;
Expand Down Expand Up @@ -175,7 +175,6 @@ pub fn base64_decode<let InputBytes: u64, let OutputElements: u64>(input: [u8; I
base64_decode_elements(result)
}


// ooook so we take ascii and encode as base64, we get an undefined character
#[test]
fn test_base64_decode_elements() {
Expand Down Expand Up @@ -244,7 +243,6 @@ fn test_base64_decode() {
116, 108, 85
];


let input: [u8; 32] = [
27, 19, 37, 131, 2, 226, 202, 153, 213, 172,
77, 130, 209, 39, 248, 203, 56, 92, 89, 57,
Expand Down

0 comments on commit e018498

Please sign in to comment.