util.rs 1012 B

1234567891011121314151617181920212223242526272829303132
  1. use ff::Field;
  2. use halo2::arithmetic::{CurveAffine, FieldExt};
  3. /// Evaluate y = f(x) given the coefficients of f(x)
  4. pub fn evaluate<C: CurveAffine>(x: u8, coeffs: &[C::Base]) -> C::Base {
  5. let x = C::Base::from_u64(x as u64);
  6. coeffs
  7. .iter()
  8. .rev()
  9. .cloned()
  10. .reduce(|acc, coeff| acc * x + coeff)
  11. .unwrap_or_else(C::Base::zero)
  12. }
  13. /// Takes in an FnMut closure and returns a constant-length array with elements of
  14. /// type `Output`.
  15. pub fn gen_const_array<Output: Copy + Default, const LEN: usize>(
  16. closure: impl FnMut(usize) -> Output,
  17. ) -> [Output; LEN] {
  18. gen_const_array_with_default(Default::default(), closure)
  19. }
  20. pub(crate) fn gen_const_array_with_default<Output: Copy, const LEN: usize>(
  21. default_value: Output,
  22. mut closure: impl FnMut(usize) -> Output,
  23. ) -> [Output; LEN] {
  24. let mut ret: [Output; LEN] = [default_value; LEN];
  25. for (bit, val) in ret.iter_mut().zip((0..LEN).map(|idx| closure(idx))) {
  26. *bit = val;
  27. }
  28. ret
  29. }