use acir::{circuit::directives::Directive, native_types::WitnessMap, FieldElement};
use num_bigint::BigUint;
use crate::OpcodeResolutionError;
use super::{get_value, insert_value, ErrorLocation};
pub(crate) fn solve_directives(
initial_witness: &mut WitnessMap,
directive: &Directive,
) -> Result<(), OpcodeResolutionError> {
match directive {
Directive::ToLeRadix { a, b, radix } => {
let value_a = get_value(a, initial_witness)?;
let big_integer = BigUint::from_bytes_be(&value_a.to_be_bytes());
let decomposed_integer = big_integer.to_radix_le(*radix);
if b.len() < decomposed_integer.len() {
return Err(OpcodeResolutionError::UnsatisfiedConstrain {
opcode_location: ErrorLocation::Unresolved,
payload: None,
});
}
for (i, witness) in b.iter().enumerate() {
let value = match decomposed_integer.get(i) {
Some(digit) => FieldElement::from_be_bytes_reduce(&[*digit]),
None => FieldElement::zero(),
};
insert_value(witness, value, initial_witness)?;
}
Ok(())
}
}
}