From 4d37fb0e7a59b543f1c54feb6fbd02a4a29d164b Mon Sep 17 00:00:00 2001 From: Michael Connor Date: Wed, 29 Jan 2025 19:45:32 +0000 Subject: [PATCH] chore: clarify to_radix docs examples (#7230) --- noir_stdlib/src/field/mod.nr | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/noir_stdlib/src/field/mod.nr b/noir_stdlib/src/field/mod.nr index ce4bfca4085..03c958a7274 100644 --- a/noir_stdlib/src/field/mod.nr +++ b/noir_stdlib/src/field/mod.nr @@ -384,9 +384,15 @@ mod tests { #[test] // docs:start:to_be_radix_example fn test_to_be_radix() { - let field = 2; + // 259, in base 256, big endian, is [1, 3]. + // i.e. 3 * 256^0 + 1 * 256^1 + let field = 259; + + // The radix (in this example, 256) must be a power of 2. + // The length of the returned byte array can be specified to be + // >= the amount of space needed. let bytes: [u8; 8] = field.to_be_radix(256); - assert_eq(bytes, [0, 0, 0, 0, 0, 0, 0, 2]); + assert_eq(bytes, [0, 0, 0, 0, 0, 0, 1, 3]); assert_eq(Field::from_be_bytes::<8>(bytes), field); } // docs:end:to_be_radix_example @@ -394,9 +400,15 @@ mod tests { #[test] // docs:start:to_le_radix_example fn test_to_le_radix() { - let field = 2; + // 259, in base 256, little endian, is [3, 1]. + // i.e. 3 * 256^0 + 1 * 256^1 + let field = 259; + + // The radix (in this example, 256) must be a power of 2. + // The length of the returned byte array can be specified to be + // >= the amount of space needed. let bytes: [u8; 8] = field.to_le_radix(256); - assert_eq(bytes, [2, 0, 0, 0, 0, 0, 0, 0]); + assert_eq(bytes, [3, 1, 0, 0, 0, 0, 0, 0]); assert_eq(Field::from_le_bytes::<8>(bytes), field); } // docs:end:to_le_radix_example