util.rs 951 B

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