diff --git a/zokrates_stdlib/stdlib/utils/casts/u16_to_bits.zok b/zokrates_stdlib/stdlib/utils/casts/u16_to_bits.zok new file mode 100644 index 00000000..1e3f8bb5 --- /dev/null +++ b/zokrates_stdlib/stdlib/utils/casts/u16_to_bits.zok @@ -0,0 +1,4 @@ +import "EMBED/u16_to_bits" as to_bits + +def main(u16 a) -> bool[16]: + return to_bits(a) \ No newline at end of file diff --git a/zokrates_stdlib/stdlib/utils/casts/u32_to_bits.zok b/zokrates_stdlib/stdlib/utils/casts/u32_to_bits.zok new file mode 100644 index 00000000..204a0f40 --- /dev/null +++ b/zokrates_stdlib/stdlib/utils/casts/u32_to_bits.zok @@ -0,0 +1,4 @@ +import "EMBED/u32_to_bits" as to_bits + +def main(u32 a) -> bool[32]: + return to_bits(a) \ No newline at end of file diff --git a/zokrates_stdlib/stdlib/utils/casts/u8_to_bits.zok b/zokrates_stdlib/stdlib/utils/casts/u8_to_bits.zok new file mode 100644 index 00000000..9bb8b592 --- /dev/null +++ b/zokrates_stdlib/stdlib/utils/casts/u8_to_bits.zok @@ -0,0 +1,4 @@ +import "EMBED/u8_to_bits" as to_bits + +def main(u8 a) -> bool[8]: + return to_bits(a) \ No newline at end of file diff --git a/zokrates_stdlib/tests/tests/utils/casts/to_bits.json b/zokrates_stdlib/tests/tests/utils/casts/to_bits.json new file mode 100644 index 00000000..ca4c9840 --- /dev/null +++ b/zokrates_stdlib/tests/tests/utils/casts/to_bits.json @@ -0,0 +1,29 @@ +{ + "entry_point": "./tests/tests/utils/casts/to_bits.zok", + "curves": ["Bn128"], + "tests": [ + { + "input": { + "values": ["0", "1", "4294967295", "42", "0", "1", "65535", "42", "0", "1", "255", "42"] + }, + "output": { + "Ok": { + "values": [ + "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", + "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", + "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", + "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", + "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", + "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", + "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", + "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", + "0", "0", "0", "0", "0", "0", "0", "0", + "0", "0", "0", "0", "0", "0", "0", "1", + "1", "1", "1", "1", "1", "1", "1", "1", + "0", "0", "1", "0", "1", "0", "1", "0" + ] + } + } + } + ] +} \ No newline at end of file diff --git a/zokrates_stdlib/tests/tests/utils/casts/to_bits.zok b/zokrates_stdlib/tests/tests/utils/casts/to_bits.zok new file mode 100644 index 00000000..9785fcb0 --- /dev/null +++ b/zokrates_stdlib/tests/tests/utils/casts/to_bits.zok @@ -0,0 +1,19 @@ +import "utils/casts/u32_to_bits" +import "utils/casts/u16_to_bits" +import "utils/casts/u8_to_bits" + +def main(u32[4] a, u16[4] b, u8[4] c) -> (bool[4][32], bool[4][16], bool[4][8]): + bool[4][32] d = [[false; 32]; 4] + bool[4][16] e = [[false; 16]; 4] + bool[4][8] f = [[false; 8]; 4] + + for field i in 0..4 do + bool[32] g = u32_to_bits(a[i]) + d[i] = g + bool[16] h = u16_to_bits(b[i]) + e[i] = h + bool[8] j = u8_to_bits(c[i]) + f[i] = j + endfor + + return d, e, f \ No newline at end of file