poly_norm_l1
stdlib/polynomial/poly_norm_l1.nbv
poly_norm_l1(N, poly)
returns the sum of the coefficients in the integers here we check that we don't overflow the field this impl only supports l1 norms < E::Q / 2
- Calls
bits/lt_bits,bits/to_bits- Precompiles
assert_eq!,clone!,constrain_eq!,div_floor!,len!,loop!,to_bits_static!
Source
# returns the sum of the coefficients in the integers
# here we check that we don't overflow the field
# this impl only supports l1 norms < E::Q / 2
(N, poly)
assert_eq! N, len! poly
static midpoint = div_floor! (0 - 1), 2
let midpoint_bits = to_bits_static! midpoint
let mut sum = 0
loop! N, i {
let v = poly[i]
# we're evaluating the norm of the value
let is_positive = lt_bits(to_bits(v), clone! midpoint_bits)
let abs_diff = (v * is_positive) + ((1 - is_positive) * (0 - v))
let max_abs_diff_bits = to_bits(0 - 1 - sum)
constrain_eq! 1, lt_bits(to_bits(abs_diff), max_abs_diff_bits)
sum = sum + abs_diff
}
sum