From 18ae176fa569479c72b7bb1429125ce910128a4f Mon Sep 17 00:00:00 2001 From: Colin Nielsen Date: Wed, 12 Jul 2023 13:29:43 -0700 Subject: [PATCH] feat: :sparkles: add u72-u120 --- Nargo.toml | 2 +- README.md | 4 +- src/main.nr | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 368 insertions(+), 17 deletions(-) diff --git a/Nargo.toml b/Nargo.toml index 846a0f6..9f2f015 100644 --- a/Nargo.toml +++ b/Nargo.toml @@ -1,7 +1,7 @@ [package] name = "noir-u2b" authors = ["@colinnielsen"] -compiler_version = "0.7.1" +compiler_version = "0.8.0" notes = "AMDG" [dependencies] diff --git a/README.md b/README.md index 91649d8..c5c5229 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ In your `Nargo.toml` file, add the following dependency: ```toml [dependencies] -u2b = { tag = "v0.2.0", git = "https://github.com/colinnielsen/noir-u2b" } +u2b = { tag = "v0.3.0", git = "https://github.com/colinnielsen/noir-u2b" } ``` -Conversion functions are available for all unsigned integer types `u(8-64)`. They can be used as follows: +Conversion functions are available for all unsigned integer types `u(8-128)`. They can be used as follows: ```rust use dep::u2b; diff --git a/src/main.nr b/src/main.nr index 3ddbdc9..1d80455 100644 --- a/src/main.nr +++ b/src/main.nr @@ -1,7 +1,7 @@ fn u16_to_u8(num: u16) -> [u8; 2] { let mut out: [u8; 2] = [0; 2]; for i in 0..2 { - out[i] = (num >> (8 - (i * 8)) as u16 & 0xff) as u8; + out[i] = (num >> (8 - (i * 8))) as u8; } out @@ -10,6 +10,7 @@ fn u16_to_u8(num: u16) -> [u8; 2] { #[test] fn test_u16() { let small = u16_to_u8(10); + assert(small.len() == 2); assert(small[0] == 0); assert(small[1] == 10); @@ -25,7 +26,7 @@ fn test_u16() { fn u24_to_u8(num: u24) -> [u8; 3] { let mut out: [u8; 3] = [0; 3]; for i in 0..3 { - out[i] = (num >> (16 - (i * 8)) as u24 & 0xff) as u8; + out[i] = (num >> (16 - (i * 8))) as u8; } out @@ -34,6 +35,7 @@ fn u24_to_u8(num: u24) -> [u8; 3] { #[test] fn test_u24() { let small = u24_to_u8(10); + assert(small.len() == 3); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 10); @@ -52,7 +54,7 @@ fn test_u24() { fn u32_to_u8(num: u32) -> [u8; 4] { let mut out: [u8; 4] = [0; 4]; for i in 0..4 { - out[i] = (num >> (24 - (i * 8)) as u32 & 0xff) as u8; + out[i] = (num >> (24 - (i * 8))) as u8; } out @@ -61,6 +63,7 @@ fn u32_to_u8(num: u32) -> [u8; 4] { #[test] fn test_u32() { let small = u32_to_u8(10); + assert(small.len() == 4); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 0); @@ -82,7 +85,7 @@ fn test_u32() { fn u40_to_u8(num: u40) -> [u8; 5] { let mut out: [u8; 5] = [0; 5]; for i in 0..5 { - out[i] = (num >> (32 - (i * 8)) as u40 & 0xff) as u8; + out[i] = (num >> (32 - (i * 8))) as u8; } out @@ -91,6 +94,7 @@ fn u40_to_u8(num: u40) -> [u8; 5] { #[test] fn test_u40() { let small = u40_to_u8(10); + assert(small.len() == 5); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 0); @@ -115,7 +119,7 @@ fn test_u40() { fn u48_to_u8(num: u48) -> [u8; 6] { let mut out: [u8; 6] = [0; 6]; for i in 0..6 { - out[i] = (num >> (40 - (i * 8)) as u48 & 0xff) as u8; + out[i] = (num >> (40 - (i * 8))) as u8; } out @@ -124,6 +128,7 @@ fn u48_to_u8(num: u48) -> [u8; 6] { #[test] fn test_u48() { let small = u48_to_u8(10); + assert(small.len() == 6); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 0); @@ -151,7 +156,7 @@ fn test_u48() { fn u56_to_u8(num: u56) -> [u8; 7] { let mut out: [u8; 7] = [0; 7]; for i in 0..7 { - out[i] = (num >> (48 - (i * 8)) as u56 & 0xff) as u8; + out[i] = (num >> (48 - (i * 8))) as u8; } out @@ -160,6 +165,7 @@ fn u56_to_u8(num: u56) -> [u8; 7] { #[test] fn test_u56() { let small = u56_to_u8(10); + assert(small.len() == 7); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 0); @@ -190,7 +196,7 @@ fn test_u56() { fn u64_to_u8(num: u64) -> [u8; 8] { let mut out: [u8; 8] = [0; 8]; for i in 0..8 { - out[i] = (num >> (56 - (i * 8)) as u64 & 0xff) as u8; + out[i] = (num >> (56 - (i * 8))) as u8; } out @@ -199,6 +205,7 @@ fn u64_to_u8(num: u64) -> [u8; 8] { #[test] fn test_u64() { let small = u64_to_u8(10); + assert(small.len() == 8); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 0); @@ -239,10 +246,10 @@ fn test_u64() { assert(ceil[7] == 0xff); } -fn u72_to_u8(num: u72) -> [u8; 8] { - let mut out: [u8; 8] = [0; 8]; - for i in 0..8 { - out[i] = (num >> (56 - (i * 8)) as u72 & 0xff) as u8; +fn u72_to_u8(num: u72) -> [u8; 9] { + let mut out: [u8; 9] = [0; 9]; + for i in 0..9 { + out[i] = (num >> (64 - (i * 8))) as u8; } out @@ -251,6 +258,7 @@ fn u72_to_u8(num: u72) -> [u8; 8] { #[test] fn test_u72() { let small = u72_to_u8(10); + assert(small.len() == 9); assert(small[0] == 0); assert(small[1] == 0); assert(small[2] == 0); @@ -258,7 +266,8 @@ fn test_u72() { assert(small[4] == 0); assert(small[5] == 0); assert(small[6] == 0); - assert(small[7] == 10); + assert(small[7] == 0); + assert(small[8] == 10); let two_byte = u72_to_u8(256); assert(two_byte[0] == 0x00); @@ -267,10 +276,344 @@ fn test_u72() { assert(two_byte[3] == 0x00); assert(two_byte[4] == 0x00); assert(two_byte[5] == 0x00); - assert(two_byte[6] == 0x01); + assert(two_byte[6] == 0x00); + assert(two_byte[7] == 0x01); + assert(two_byte[8] == 0x00); + + let ceil = u72_to_u8(4722366482869645213695); + assert(ceil[0] == 0xff); + assert(ceil[1] == 0xff); + assert(ceil[2] == 0xff); + assert(ceil[3] == 0xff); + assert(ceil[4] == 0xff); + assert(ceil[5] == 0xff); + assert(ceil[6] == 0xff); + assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); +} + +fn u80_to_u8(num: u80) -> [u8; 10] { + let mut out: [u8; 10] = [0; 10]; + for i in 0..10 { + out[i] = (num >> (72 - (i * 8))) as u8; + } + + out +} + +#[test] +fn test_u80() { + let small = u80_to_u8(10); + assert(small.len() == 10); + assert(small[0] == 0); + assert(small[1] == 0); + assert(small[2] == 0); + assert(small[3] == 0); + assert(small[4] == 0); + assert(small[5] == 0); + assert(small[6] == 0); + assert(small[7] == 0); + assert(small[8] == 0); + assert(small[9] == 10); + + let two_byte = u80_to_u8(256); + assert(two_byte[0] == 0x00); + assert(two_byte[1] == 0x00); + assert(two_byte[2] == 0x00); + assert(two_byte[3] == 0x00); + assert(two_byte[4] == 0x00); + assert(two_byte[5] == 0x00); + assert(two_byte[6] == 0x00); assert(two_byte[7] == 0x00); + assert(two_byte[8] == 0x01); + assert(two_byte[9] == 0x00); - let ceil = u72_to_u8(18446744073709551615); + let ceil = u80_to_u8(1208925819614629174706175); + assert(ceil[0] == 0xff); + assert(ceil[1] == 0xff); + assert(ceil[2] == 0xff); + assert(ceil[3] == 0xff); + assert(ceil[4] == 0xff); + assert(ceil[5] == 0xff); + assert(ceil[6] == 0xff); + assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); + assert(ceil[9] == 0xff); +} + +fn u88_to_u8(num: u88) -> [u8; 11] { + let mut out: [u8; 11] = [0; 11]; + for i in 0..11 { + out[i] = (num >> (80 - (i * 8))) as u8; + } + + out +} + +#[test] +fn test_u88() { + let small = u88_to_u8(10); + assert(small.len() == 11); + assert(small[0] == 0); + assert(small[1] == 0); + assert(small[2] == 0); + assert(small[3] == 0); + assert(small[4] == 0); + assert(small[5] == 0); + assert(small[6] == 0); + assert(small[7] == 0); + assert(small[8] == 0); + assert(small[9] == 0); + assert(small[10] == 10); + + let two_byte = u88_to_u8(256); + assert(two_byte[0] == 0x00); + assert(two_byte[1] == 0x00); + assert(two_byte[2] == 0x00); + assert(two_byte[3] == 0x00); + assert(two_byte[4] == 0x00); + assert(two_byte[5] == 0x00); + assert(two_byte[6] == 0x00); + assert(two_byte[7] == 0x00); + assert(two_byte[8] == 0x00); + assert(two_byte[9] == 0x01); + assert(two_byte[10] == 0x00); + + let ceil = u88_to_u8(309485009821345068724781055); + assert(ceil[0] == 0xff); + assert(ceil[1] == 0xff); + assert(ceil[2] == 0xff); + assert(ceil[3] == 0xff); + assert(ceil[4] == 0xff); + assert(ceil[5] == 0xff); + assert(ceil[6] == 0xff); + assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); + assert(ceil[9] == 0xff); + assert(ceil[10] == 0xff); +} + +fn u96_to_u8(num: u96) -> [u8; 12] { + let mut out: [u8; 12] = [0; 12]; + for i in 0..12 { + out[i] = (num >> (88 - (i * 8))) as u8; + } + + out +} + +#[test] +fn test_u96() { + let small = u96_to_u8(10); + assert(small.len() == 12); + assert(small[0] == 0); + assert(small[1] == 0); + assert(small[2] == 0); + assert(small[3] == 0); + assert(small[4] == 0); + assert(small[5] == 0); + assert(small[6] == 0); + assert(small[7] == 0); + assert(small[8] == 0); + assert(small[9] == 0); + assert(small[10] == 0); + assert(small[11] == 10); + + let two_byte = u96_to_u8(256); + assert(two_byte[0] == 0x00); + assert(two_byte[1] == 0x00); + assert(two_byte[2] == 0x00); + assert(two_byte[3] == 0x00); + assert(two_byte[4] == 0x00); + assert(two_byte[5] == 0x00); + assert(two_byte[6] == 0x00); + assert(two_byte[7] == 0x00); + assert(two_byte[8] == 0x00); + assert(two_byte[9] == 0x00); + assert(two_byte[10] == 0x01); + assert(two_byte[11] == 0x00); + + let ceil = u96_to_u8(79228162514264337593543950335); + assert(ceil[0] == 0xff); + assert(ceil[1] == 0xff); + assert(ceil[2] == 0xff); + assert(ceil[3] == 0xff); + assert(ceil[4] == 0xff); + assert(ceil[5] == 0xff); + assert(ceil[6] == 0xff); + assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); + assert(ceil[9] == 0xff); + assert(ceil[10] == 0xff); + assert(ceil[11] == 0xff); +} + +fn u104_to_u8(num: u104) -> [u8; 13] { + let mut out: [u8; 13] = [0; 13]; + for i in 0..13 { + out[i] = (num >> (96 - (i * 8))) as u8; + } + + out +} + +#[test] +fn test_u104() { + let small = u104_to_u8(10); + assert(small.len() == 13); + assert(small[0] == 0); + assert(small[1] == 0); + assert(small[2] == 0); + assert(small[3] == 0); + assert(small[4] == 0); + assert(small[5] == 0); + assert(small[6] == 0); + assert(small[7] == 0); + assert(small[8] == 0); + assert(small[9] == 0); + assert(small[10] == 0); + assert(small[11] == 0); + assert(small[12] == 10); + + let two_byte = u104_to_u8(256); + assert(two_byte[0] == 0x00); + assert(two_byte[1] == 0x00); + assert(two_byte[2] == 0x00); + assert(two_byte[3] == 0x00); + assert(two_byte[4] == 0x00); + assert(two_byte[5] == 0x00); + assert(two_byte[6] == 0x00); + assert(two_byte[7] == 0x00); + assert(two_byte[8] == 0x00); + assert(two_byte[9] == 0x00); + assert(two_byte[10] == 0x00); + assert(two_byte[11] == 0x01); + assert(two_byte[12] == 0x00); + + let ceil = u104_to_u8(20282409603651670423947251286015); + assert(ceil[0] == 0xff); + assert(ceil[1] == 0xff); + assert(ceil[2] == 0xff); + assert(ceil[3] == 0xff); + assert(ceil[4] == 0xff); + assert(ceil[5] == 0xff); + assert(ceil[6] == 0xff); + assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); + assert(ceil[9] == 0xff); + assert(ceil[10] == 0xff); + assert(ceil[11] == 0xff); + assert(ceil[12] == 0xff); +} + +fn u112_to_u8(num: u112) -> [u8; 14] { + let mut out: [u8; 14] = [0; 14]; + for i in 0..14 { + out[i] = (num >> (104 - (i * 8))) as u8; + } + + out +} + +#[test] +fn test_u112() { + let small = u112_to_u8(10); + assert(small.len() == 14); + assert(small[0] == 0); + assert(small[1] == 0); + assert(small[2] == 0); + assert(small[3] == 0); + assert(small[4] == 0); + assert(small[5] == 0); + assert(small[6] == 0); + assert(small[7] == 0); + assert(small[8] == 0); + assert(small[9] == 0); + assert(small[10] == 0); + assert(small[11] == 0); + assert(small[12] == 0); + assert(small[13] == 10); + + let two_byte = u112_to_u8(256); + assert(two_byte[0] == 0x00); + assert(two_byte[1] == 0x00); + assert(two_byte[2] == 0x00); + assert(two_byte[3] == 0x00); + assert(two_byte[4] == 0x00); + assert(two_byte[5] == 0x00); + assert(two_byte[6] == 0x00); + assert(two_byte[7] == 0x00); + assert(two_byte[8] == 0x00); + assert(two_byte[9] == 0x00); + assert(two_byte[10] == 0x00); + assert(two_byte[11] == 0x00); + assert(two_byte[12] == 0x01); + assert(two_byte[13] == 0x00); + + let ceil = u112_to_u8(5192296858534827628530496329220095); + assert(ceil[0] == 0xff); + assert(ceil[1] == 0xff); + assert(ceil[2] == 0xff); + assert(ceil[3] == 0xff); + assert(ceil[4] == 0xff); + assert(ceil[5] == 0xff); + assert(ceil[6] == 0xff); + assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); + assert(ceil[9] == 0xff); + assert(ceil[10] == 0xff); + assert(ceil[11] == 0xff); + assert(ceil[12] == 0xff); + assert(ceil[13] == 0xff); +} + +fn u120_to_u8(num: u120) -> [u8; 15] { + let mut out: [u8; 15] = [0; 15]; + for i in 0..15 { + out[i] = (num >> (112 - (i * 8))) as u8; + } + + out +} + +#[test] +fn test_u120() { + let small = u120_to_u8(10); + assert(small.len() == 15); + assert(small[0] == 0); + assert(small[1] == 0); + assert(small[2] == 0); + assert(small[3] == 0); + assert(small[4] == 0); + assert(small[5] == 0); + assert(small[6] == 0); + assert(small[7] == 0); + assert(small[8] == 0); + assert(small[9] == 0); + assert(small[10] == 0); + assert(small[11] == 0); + assert(small[12] == 0); + assert(small[13] == 0); + assert(small[14] == 10); + + let two_byte = u120_to_u8(256); + assert(two_byte[0] == 0x00); + assert(two_byte[1] == 0x00); + assert(two_byte[2] == 0x00); + assert(two_byte[3] == 0x00); + assert(two_byte[4] == 0x00); + assert(two_byte[5] == 0x00); + assert(two_byte[6] == 0x00); + assert(two_byte[7] == 0x00); + assert(two_byte[8] == 0x00); + assert(two_byte[9] == 0x00); + assert(two_byte[10] == 0x00); + assert(two_byte[11] == 0x00); + assert(two_byte[12] == 0x00); + assert(two_byte[13] == 0x01); + assert(two_byte[14] == 0x00); + + let ceil = u120_to_u8(1329227995784915872903807060280344575); assert(ceil[0] == 0xff); assert(ceil[1] == 0xff); assert(ceil[2] == 0xff); @@ -279,6 +622,13 @@ fn test_u72() { assert(ceil[5] == 0xff); assert(ceil[6] == 0xff); assert(ceil[7] == 0xff); + assert(ceil[8] == 0xff); + assert(ceil[9] == 0xff); + assert(ceil[10] == 0xff); + assert(ceil[11] == 0xff); + assert(ceil[12] == 0xff); + assert(ceil[13] == 0xff); + assert(ceil[14] == 0xff); } fn u8_to_u8(num: u8) -> [u8; 1] { @@ -290,6 +640,7 @@ fn u8_to_u8(num: u8) -> [u8; 1] { #[test] fn test_u8() { let small = u8_to_u8(10); + assert(small.len() == 1); assert(small[0] == 10); let ceil = u8_to_u8(255);