to_bits
stdlib/bits/to_bits.nbv
to_bits(input)
convert an element into a constrained vector of bits
- Calls
bits/lte_negone- Precompiles
assert_eq!,clone!,constrain_eq!,field_bit_width!,len!,loop!,to_bits_unconstrained!- Used by
oraccle/oraccle_sample_ternary_vec,polynomial/poly_norm_l1
Source
# convert an element into a constrained vector of bits
(input)
# only operate on a scalar
assert_eq! 1, len! input
static width = field_bit_width!
let bits = to_bits_unconstrained! input
assert_eq! (len! bits), width
let mut sum = 0
static mut power = 1
loop! width, i {
# constrain each entry to be 0 or 1
constrain_eq! 0, bits[i] * (1 - bits[i])
sum = sum + power * bits[i]
power = power * 2
}
# constrain the combination of bits
constrain_eq! input, sum
# alias check for potential overflows
constrain_eq! 1, lte_negone(clone! bits)
bits