Skip to content

Commit dc8ad05

Browse files
chore: remove trait and resolve warnings (#49)
Co-authored-by: Tom French <[email protected]>
1 parent 9344b7e commit dc8ad05

19 files changed

+337
-368
lines changed

Nargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ authors = [""]
55
compiler_version = ">=1.0.0"
66

77
[dependencies]
8-
noir_sort = {tag = "v0.2.2", git = "https://github.com/noir-lang/noir_sort"}
9-
poseidon = { git = "https://github.com/noir-lang/poseidon", tag = "v0.1.0" }
8+
noir_sort = {tag = "v0.3.0", git = "https://github.com/noir-lang/noir_sort"}
9+
poseidon = { git = "https://github.com/noir-lang/poseidon", tag = "v0.1.1" }

src/_comparison_tools/bounds_checker.nr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ in this case, i == M
2525
* @description this method is cheaper than querying `i < boundary` for `u16` and `u32` types
2626
* cost = 3 gates + 2 gates per iteration
2727
**/
28-
pub fn get_validity_flags<let N: u32>(boundary: u32) -> [Field; N] {
28+
pub fn get_validity_flags<let N: u32>(boundary: u32) -> [u32; N] {
2929
// Safety: The constraining is happening inside get_validity_flags_inner
30-
let flags: [Field; N] = unsafe { __get_validity_flags(boundary) };
30+
let flags: [u32; N] = unsafe { __get_validity_flags(boundary) };
3131
get_validity_flags_inner(boundary, flags)
3232
}
3333

34-
unconstrained fn __get_validity_flags<let N: u32>(boundary: u32) -> [Field; N] {
35-
let mut result: [Field; N] = [0; N];
34+
unconstrained fn __get_validity_flags<let N: u32>(boundary: u32) -> [u32; N] {
35+
let mut result: [u32; N] = [0; N];
3636
for i in 0..N {
3737
if i < boundary {
3838
result[i] = 1;
@@ -54,7 +54,7 @@ unconstrained fn __get_validity_flags<let N: u32>(boundary: u32) -> [Field; N] {
5454
* aligns with what is expected from testing `i < boundary`
5555
* N.B. this method will revert if `boundary > N`
5656
**/
57-
fn get_validity_flags_inner<let N: u32>(boundary: u32, flags: [Field; N]) -> [Field; N] {
57+
fn get_validity_flags_inner<let N: u32>(boundary: u32, flags: [u32; N]) -> [u32; N] {
5858
let initial_flag = flags[0];
5959
let final_flag = flags[N - 1];
6060

@@ -70,64 +70,64 @@ fn get_validity_flags_inner<let N: u32>(boundary: u32, flags: [Field; N]) -> [Fi
7070
assert(new_flag == old_flag * new_flag);
7171

7272
// old = a, new = b
73-
let idx = (old_flag * (1 - new_flag)) * (i as Field);
73+
let idx = (old_flag * (1 - new_flag)) * (i);
7474
transition_index = transition_index + idx;
75-
std::as_witness(transition_index);
75+
std::as_witness(transition_index as Field);
7676
}
7777

78-
assert(transition_index == boundary as Field);
78+
assert(transition_index == boundary);
7979
flags
8080
}
8181

8282
#[test]
8383
fn test_get_validity_flags() {
8484
for i in 0..32 {
85-
let flags: [Field; 32] = get_validity_flags(i);
85+
let flags: [u32; 32] = get_validity_flags(i);
8686
for j in 0..32 {
87-
assert(flags[j] == (j < i) as Field);
87+
assert(flags[j] == (j < i) as u32);
8888
}
8989
}
9090
}
9191

9292
#[test(should_fail)]
9393
fn test_get_validity_flags_fail() {
94-
let _: [Field; 33] = get_validity_flags(33);
94+
let _: [u32; 33] = get_validity_flags(33);
9595
}
9696

9797
#[test(should_fail)]
9898
fn test_get_validity_flags_bad_index_fail_a() {
99-
let bad_flags: [Field; 10] = [1, 1, 1, 0, 0, 0, 1, 0, 0, 0];
99+
let bad_flags: [u32; 10] = [1, 1, 1, 0, 0, 0, 1, 0, 0, 0];
100100
let _ = get_validity_flags_inner(3, bad_flags);
101101
}
102102
#[test(should_fail)]
103103
fn test_get_validity_flags_bad_index_fail_b() {
104-
let bad_flags: [Field; 10] = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0];
104+
let bad_flags: [u32; 10] = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0];
105105
let _ = get_validity_flags_inner(3, bad_flags);
106106
}
107107

108108
#[test(should_fail)]
109109
fn test_get_validity_flags_bad_index_fail_c() {
110-
let bad_flags: [Field; 10] = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0];
110+
let bad_flags: [u32; 10] = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0];
111111
let _ = get_validity_flags_inner(3, bad_flags);
112112
}
113113

114114
#[test]
115115
fn test_get_validity_flags_good_index_d() {
116-
let bad_flags: [Field; 10] = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0];
116+
let bad_flags: [u32; 10] = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0];
117117
let _ = get_validity_flags_inner(3, bad_flags);
118118
}
119119

120120
#[test(should_fail)]
121121
fn test_get_validity_flags_bad_index_fail_e() {
122-
let bad_flags: [Field; 10] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
122+
let bad_flags: [u32; 10] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
123123
let _ = get_validity_flags_inner(11, bad_flags);
124124
}
125125

126126
// this test uses bad flags but manipulates transition_index to be satisfiable
127127
// nevertheless test will fail because our transition test (old * new = new) will fail
128128
#[test(should_fail)]
129129
fn test_get_validity_flags_bad_index_fail_f() {
130-
let mut bad_flags: [Field; 10] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
130+
let mut bad_flags: [u32; 10] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
131131

132132
let fake_index_a = 2;
133133
let fake_value_a = 100;

src/_string_tools/slice_packed_field.nr

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::utils::cast_num_to_u32;
2+
13
/**
24
* @file methods to extract data efficiently from Field elements that represent 31 bytes of packed data
35
**/
@@ -463,7 +465,7 @@ global PATH_LOOKUP: [[bool; 5]; 32] = [
463465
* some of the chunks will describe the bytes [0, ..., num_bytes - 1]
464466
* some of the chunks will describe the bytes [num_bytes, ..., 31]
465467
**/
466-
unconstrained fn __slice_field(f: Field, num_bytes: Field) -> [Field; 5] {
468+
unconstrained fn __slice_field(f: Field, num_bytes: u32) -> [Field; 5] {
467469
let head_path = PATH_LOOKUP[num_bytes];
468470
let bytes: [u8; 32] = f.to_be_bytes();
469471
let bytes = bytes.map(|b: u8| b as Field);
@@ -558,17 +560,17 @@ unconstrained fn __slice_field(f: Field, num_bytes: Field) -> [Field; 5] {
558560
chunks
559561
}
560562

561-
unconstrained fn __divmod(numerator: Field, denominator: Field) -> (Field, Field) {
562-
let quotient = numerator as u32 / denominator as u32;
563-
let remainder = numerator as u32 % denominator as u32;
564-
(quotient as Field, remainder as Field)
563+
unconstrained fn __divmod(numerator: u16, denominator: u16) -> (u16, u16) {
564+
let quotient = numerator / denominator;
565+
let remainder = numerator % denominator;
566+
(quotient, remainder)
565567
}
566568

567569
/**
568570
* @brief cheeky divmod method for dividing a u16 by 31
569571
* we know the quotient will fit into a 14 bit range check which will save us some fractional gates
570572
**/
571-
fn divmod_31(numerator: Field) -> (Field, Field) {
573+
fn divmod_31(numerator: u16) -> (u16, u16) {
572574
// Safety: we check the bit lengths of qf and rf and their relation to the numerator with assertions later
573575
let (quotient, remainder) = unsafe { __divmod(numerator, 31) };
574576

@@ -582,7 +584,7 @@ fn divmod_31(numerator: Field) -> (Field, Field) {
582584

583585
// n / d = q
584586
// d * q + r = n
585-
assert(qf * 31 as Field + rf == numerator as Field);
587+
assert(qf * 31 + rf == numerator as Field);
586588
(quotient, remainder)
587589
}
588590

@@ -603,7 +605,7 @@ unconstrained fn decompose(val: Field) -> [Field; 16] {
603605
// 5 gates?
604606
pub fn get_last_limb_path<let OutputFields: u32>(last_limb_index: Field) -> [Field; OutputFields] {
605607
// TODO we offset by 1 explain why (0 byte length produces 0 - 1 which = invalid array index. we just add 1 and increase array length by 1 to compensate)
606-
let path = LAST_LIMB_PATH[last_limb_index + 1]; // 2
608+
let path = LAST_LIMB_PATH[cast_num_to_u32(last_limb_index + 1)]; // 2
607609
// Safety: check the comments below
608610
let path_valid_bits = unsafe { decompose(path) };
609611
let mut path_valid_sum: Field = 0;
@@ -624,7 +626,7 @@ pub fn get_last_limb_path<let OutputFields: u32>(last_limb_index: Field) -> [Fie
624626
* where `head = f.slice(0, num_bytes)`, `tail = f.slice(num_bytes, 31)`
625627
* @details cost 46 gates
626628
**/
627-
pub fn slice_field(f: Field, num_bytes: Field) -> (Field, Field) {
629+
pub fn slice_field(f: Field, num_bytes: u32) -> (Field, Field) {
628630
// Safety: we check the bit lengths of the chunks with assertions later
629631
let chunks = unsafe { __slice_field(f, num_bytes) };
630632
chunks[0].assert_max_bit_size::<8>(); // 1.25 gates
@@ -676,62 +678,68 @@ pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
676678
start_byte: Field,
677679
num_bytes: Field,
678680
) -> [Field; OutputFields] {
681+
start_byte.assert_max_bit_size::<16>();
682+
num_bytes.assert_max_bit_size::<16>();
679683
// 3.5
680-
let (start_index, start_mod_31) = divmod_31(start_byte);
684+
let (start_index, start_mod_31) = divmod_31(start_byte as u16);
681685
let num_underflow_bytes = start_mod_31;
682686
// 3.5, 7
683-
let (num_bytes_div_31, num_bytes_mod_31) = divmod_31(num_bytes);
687+
let (num_bytes_div_31, num_bytes_mod_31) = divmod_31(num_bytes as u16);
684688

685689
// 2, 9
686-
let num_bytes_mod_31_is_0 = NUM_BYTES_MOD_31_IS_ZERO[num_bytes_mod_31];
690+
let num_bytes_mod_31_is_0 = NUM_BYTES_MOD_31_IS_ZERO[num_bytes_mod_31 as u32];
687691
// 2, 11
688-
let num_bytes_div_31_is_0 = NUM_BYTES_MOD_31_IS_ZERO[num_bytes_div_31];
692+
let num_bytes_div_31_is_0 = NUM_BYTES_MOD_31_IS_ZERO[num_bytes_div_31 as u32];
689693

690694
// 1, 12
691-
let lookup = (-num_bytes_div_31_is_0 * num_bytes) - start_mod_31 + 62;
695+
let lookup = (-num_bytes_div_31_is_0 * num_bytes) - start_mod_31 as Field + 62;
692696
std::as_witness(lookup);
693697
// 3, 15
694-
let bytes_fit_into_limb = INTEGER_UP_TO_62_IS_GREATER_THAN_31[lookup] * num_bytes_div_31_is_0;
698+
let bytes_fit_into_limb =
699+
INTEGER_UP_TO_62_IS_GREATER_THAN_31[cast_num_to_u32(lookup)] * num_bytes_div_31_is_0;
695700
std::as_witness(bytes_fit_into_limb);
696701

697702
// 2, 17
698-
let num_unused_bytes_in_start_limb =
699-
(num_bytes + start_mod_31 - 31) * bytes_fit_into_limb + (31 - start_mod_31);
703+
let num_unused_bytes_in_start_limb = (num_bytes + start_mod_31 as Field - 31)
704+
* bytes_fit_into_limb
705+
+ (31 - start_mod_31 as Field);
700706
std::as_witness(num_unused_bytes_in_start_limb);
701707
let num_remaining_bytes = num_bytes - num_unused_bytes_in_start_limb;
702708

703709
// 4.5, 21.5
704-
let mut (num_whole_limbs, num_overflow_bytes) = divmod_31(num_remaining_bytes);
710+
num_remaining_bytes.assert_max_bit_size::<16>();
711+
let mut (num_whole_limbs, num_overflow_bytes) = divmod_31(num_remaining_bytes as u16);
705712
// 44, 65.5
706-
let (_, tail) = slice_field(data[start_index], num_underflow_bytes);
713+
let (_, tail) = slice_field(data[start_index as u32], num_underflow_bytes as u32);
707714

708715
let mut previous = tail;
709716

710717
let mut result = [0; OutputFields];
711718

712719
// 4, 69.5
713-
let extra_head_section = INTEGER_UP_TO_62_IS_GREATER_THAN_31[num_overflow_bytes - start_mod_31
714-
+ 31]
720+
let extra_head_section = INTEGER_UP_TO_62_IS_GREATER_THAN_31[(
721+
31 + num_overflow_bytes - start_mod_31
722+
) as u32]
715723
* (1 - bytes_fit_into_limb);
716724

717725
// 1, 70.5
718-
let index_of_output_limb: Field = (num_bytes_div_31 - num_bytes_mod_31_is_0);
726+
let index_of_output_limb: Field = (num_bytes_div_31 as Field - num_bytes_mod_31_is_0);
719727
// 5, 75.5
720728
let path_valid_output: [Field; OutputFields] = get_last_limb_path(index_of_output_limb);
721729

722730
// 2, 77.5
723-
let tail_shift = BYTE_SHIFT[num_unused_bytes_in_start_limb];
731+
let tail_shift = BYTE_SHIFT[cast_num_to_u32(num_unused_bytes_in_start_limb)];
724732

725733
// 51, 128.5
726734
for i in 0..(OutputFields - 1) {
727735
// 0
728736
let slice_valid = path_valid_output[i];
729737
// 1
730-
let data_index = (start_index + 1 + i as Field);
738+
let data_index = (start_index as u32 + 1 + i);
731739
// 2, 3
732740
let input_slice = data[data_index];
733741
// 44, 47
734-
let (head, tail) = slice_field(input_slice, num_underflow_bytes);
742+
let (head, tail) = slice_field(input_slice, num_underflow_bytes as u32);
735743
// 1, 48
736744
let combined = previous * tail_shift + head;
737745
// 1, 49
@@ -741,26 +749,27 @@ pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
741749
}
742750

743751
// 2, 130.5
744-
let slice_size = (num_bytes + start_mod_31) * bytes_fit_into_limb + num_overflow_bytes;
752+
let slice_size =
753+
(num_bytes + start_mod_31 as Field) * bytes_fit_into_limb + num_overflow_bytes as Field;
745754

746755
// 1, 131.5
747756
let use_previous_for_last_limb: Field = extra_head_section + bytes_fit_into_limb;
748757

749758
// 1, 132.5
750759
let mut index_of_overflow_limb = start_index + num_whole_limbs + 1;
751760
// 2, 134.5
752-
let last_limb_from_data = data[index_of_overflow_limb];
761+
let last_limb_from_data = data[index_of_overflow_limb as u32];
753762
// 2, 136.5
754763
let slice_source =
755764
(previous - last_limb_from_data) * use_previous_for_last_limb + last_limb_from_data;
756765

757766
// 44, 180.5
758-
let (head, _) = slice_field(slice_source, slice_size);
767+
let (head, _) = slice_field(slice_source, cast_num_to_u32(slice_size));
759768

760769
// 3, 183.5
761-
let previous_shift = BYTE_SHIFT[31 - num_overflow_bytes]; // could save 1 gate by making different shift table
770+
let previous_shift = BYTE_SHIFT[31 - num_overflow_bytes as u32]; // could save 1 gate by making different shift table
762771
// 2, 185.5
763-
let last_limb_shift = BYTE_SHIFT[num_bytes_mod_31];
772+
let last_limb_shift = BYTE_SHIFT[num_bytes_mod_31 as u32];
764773
// 1, 186.5
765774
let mut last_limb = (previous * previous_shift);
766775
std::as_witness(last_limb);
@@ -891,7 +900,7 @@ mod test {
891900

892901
for i in 0..32 {
893902
println(f"i = {i}");
894-
let num_bytes = i as Field;
903+
let num_bytes = i;
895904
let (head, tail) = slice_field(input, num_bytes);
896905
let mut expected_head: Field = 0;
897906
let mut expected_tail: Field = 0;
@@ -901,7 +910,7 @@ mod test {
901910
}
902911
for j in 0..(31 - num_bytes as u32) {
903912
expected_tail *= 0x100;
904-
expected_tail += input_bytes[j as Field + num_bytes + 1] as Field;
913+
expected_tail += input_bytes[j + num_bytes + 1] as Field;
905914
}
906915
assert(expected_head == head);
907916
assert(expected_tail == tail);

0 commit comments

Comments
 (0)