Index Mathematics Conjectures nbv lettuce Widgets

r1cs_assert_field

stdlib/r1cs/r1cs_assert_field.nbv

r1cs_assert_field(static wtns_len, static r_a, static r_b, static r_c)

asserts that an R1CS has a quadratic constraint between -1 and 1: -1 * -1 = 1

returns the row index of the constraint in the R1CS

Calls
is_zero, negone, valloc
Precompiles
assert_eq!, clone!, div_exact!, if_eq!, len!, loop!, slice!
Used by
r1cs/r1cs_into_output

Source

# asserts that an R1CS has a quadratic constraint
# between -1 and 1: -1 * -1 = 1
#
# returns the row index of the constraint in the R1CS
(
  static wtns_len,
  static r_a,
  static r_b,
  static r_c
)

static width = wtns_len
static height = div_exact! (len! r_a), wtns_len
assert_eq! height, div_exact! (len! r_b), wtns_len
assert_eq! height, div_exact! (len! r_c), wtns_len

static mut has_found = 0
static mut c_i = 0

static ONE = valloc(width, 1)
loop! height, i {
  static a_row = slice! (clone! r_a), i * width, width
  static b_row = slice! (clone! r_b), i * width, width
  static c_row = slice! (clone! r_c), i * width, width

  static trailing_a = slice! (clone! a_row), 1
  static trailing_b = slice! (clone! b_row), 1
  static trailing_c = slice! (clone! c_row), 1

  if_eq! 1, is_zero(trailing_a) {
    if_eq! 1, is_zero(clone! trailing_b) {
      if_eq! 1, is_zero(clone! trailing_c) {
        if_eq! negone(), a_row[0] {
          if_eq! negone(), b_row[0] {
            if_eq! 1, c_row[0] {
              assert_eq! has_found, 0, "multiple field constraints in r1cs"
              has_found = 1
              c_i = i
            }
          }
        }
      }
    }
  }
}

assert_eq! has_found, 1, "no field constraint in r1cs"

c_i