A fixed-point arithmetic library providing constrained fixed-point operations for Stwo-based circuits. The library implements fixed-point arithmetic using M31 field elements with configurable decimal precision.
// Create fixed-point numbers
let lhs = BaseField::from_f64(3.14);
let rhs = BaseField::from_f64(2.0);
// Basic arithmetic operations
let sum = lhs.fixed_add(rhs);
let diff = lhs.fixed_sub(rhs);
let (prod, rem) = lhs.fixed_mul_rem(rhs);
let (quot, rem) = lhs.fixed_div_rem(rhs);
To use fixed-point operations in your Stwo Prover circuit, use the constraint evaluation functions:
use numerair::eval::{eval_add, eval_mul, eval_sub};
// In your circuit component's evaluate function:
fn evaluate<E: EvalAtRow>(&self, mut eval: E) -> E {
let lhs = eval.next_trace_mask();
let rhs = eval.next_trace_mask();
let rem = eval.next_trace_mask();
let res = eval.next_trace_mask();
// Constrain mul.
eval.eval_fixed_mul(lhs, rhs, SCALE_FACTOR.into(), res, rem)
eval
}
Contributions are welcome! Please submit pull requests with:
- New arithmetic operations
- Improved constraints
- Additional tests
- Documentation improvements
- Performance optimizations