diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 6b6ac7b..507f582 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -17,7 +17,7 @@ jobs: - name: Install Nargo uses: noir-lang/noirup@v0.1.4 with: - toolchain: 1.0.0-beta.6 + toolchain: 1.0.0-beta.11 - name: Install bb run: | diff --git a/src/_string_tools/slice_packed_field.nr b/src/_string_tools/slice_packed_field.nr index 41f77ff..e3ab43b 100644 --- a/src/_string_tools/slice_packed_field.nr +++ b/src/_string_tools/slice_packed_field.nr @@ -594,37 +594,11 @@ fn divmod_31(numerator: u16) -> (u16, u16) { (quotient, remainder) } -/** - * @brief converts a 16 bit value into 16 fake bools (Field elements that are 0 or 1) - **/ -unconstrained fn decompose(val: Field) -> [Field; 16] { - let mut r: [Field; 16] = [0; 16]; - - let mut it = val as u32; - for i in 0..16 { - r[i] = (it & 1) as Field; - it >>= 1; - } - r -} - // 5 gates? -pub fn get_last_limb_path(last_limb_index: Field) -> [Field; OutputFields] { +pub fn get_last_limb_path(last_limb_index: Field) -> [u1; OutputFields] { // 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) let path = LAST_LIMB_PATH[cast_num_to_u32(last_limb_index + 1)]; // 2 - // Safety: check the comments below - let path_valid_bits = unsafe { decompose(path) }; - let mut path_valid_sum: Field = 0; - let mut path_valid_output: [Field; OutputFields] = [0; OutputFields]; - for i in 0..OutputFields { - // we check that the path valid bits are binary - assert(path_valid_bits[i] * path_valid_bits[i] - path_valid_bits[i] == 0); - path_valid_sum += (path_valid_bits[i] * (1 << i as u8) as Field); - path_valid_output[i] = path_valid_bits[i]; - } - // we check that the path valid bits sum to the path - assert(path_valid_sum == path); - path_valid_output + path.to_le_bits::() } /** @@ -728,7 +702,7 @@ pub fn slice_fields( // 1, 70.5 let index_of_output_limb: Field = (num_bytes_div_31 as Field - num_bytes_mod_31_is_0 as Field); // 5, 75.5 - let path_valid_output: [Field; OutputFields] = get_last_limb_path(index_of_output_limb); + let path_valid_output: [u1; OutputFields] = get_last_limb_path(index_of_output_limb); // 2, 77.5 let tail_shift = BYTE_SHIFT[cast_num_to_u32(num_unused_bytes_in_start_limb)]; @@ -748,9 +722,9 @@ pub fn slice_fields( // 1, 48 let combined = previous * tail_shift + head; // 1, 49 - result[i] = combined * slice_valid; + result[i] = if slice_valid == 1 { combined } else { 0 }; // 2, 51 - previous = (tail - previous) * slice_valid + previous; + previous = if slice_valid == 1 { tail } else { previous }; } // 2, 130.5 @@ -787,9 +761,13 @@ pub fn slice_fields( std::as_witness(last_limb); let mut path: [Field; OutputFields] = [0; OutputFields]; - path[0] = (1 - path_valid_output[0]); + path[0] = (path_valid_output[0] == 0) as Field; for i in 1..OutputFields { - path[i] = path_valid_output[i] * -path_valid_output[i - 1] + path_valid_output[i - 1]; + path[i] = if path_valid_output[i - 1] == 1 { + (path_valid_output[i] == 0) as Field + } else { + 0 + }; } for i in 0..OutputFields {