spec.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use group::Curve;
  2. use halo2::{
  3. arithmetic::{CurveAffine, FieldExt},
  4. pasta::pallas,
  5. };
  6. use subtle::CtOption;
  7. use crate::constants::util::gen_const_array;
  8. /// Coordinate extractor for Pallas.
  9. ///
  10. /// Defined in [Zcash Protocol Spec § 5.4.9.7: Coordinate Extractor for Pallas][concreteextractorpallas].
  11. ///
  12. /// [concreteextractorpallas]: https://zips.z.cash/protocol/nu5.pdf#concreteextractorpallas
  13. pub(crate) fn extract_p(point: &pallas::Point) -> pallas::Base {
  14. point
  15. .to_affine()
  16. .coordinates()
  17. .map(|c| *c.x())
  18. .unwrap_or_else(pallas::Base::zero)
  19. }
  20. /// Coordinate extractor for Pallas.
  21. ///
  22. /// Defined in [Zcash Protocol Spec § 5.4.9.7: Coordinate Extractor for Pallas][concreteextractorpallas].
  23. ///
  24. /// [concreteextractorpallas]: https://zips.z.cash/protocol/nu5.pdf#concreteextractorpallas
  25. pub(crate) fn extract_p_bottom(point: CtOption<pallas::Point>) -> CtOption<pallas::Base> {
  26. point.map(|p| extract_p(&p))
  27. }
  28. /// The field element representation of a u64 integer represented by
  29. /// an L-bit little-endian bitstring.
  30. pub fn lebs2ip_field<F: FieldExt, const L: usize>(bits: &[bool; L]) -> F {
  31. F::from_u64(lebs2ip::<L>(bits))
  32. }
  33. /// The u64 integer represented by an L-bit little-endian bitstring.
  34. ///
  35. /// # Panics
  36. ///
  37. /// Panics if the bitstring is longer than 64 bits.
  38. pub fn lebs2ip<const L: usize>(bits: &[bool; L]) -> u64 {
  39. assert!(L <= 64);
  40. bits.iter()
  41. .enumerate()
  42. .fold(0u64, |acc, (i, b)| acc + if *b { 1 << i } else { 0 })
  43. }
  44. /// The sequence of bits representing a u64 in little-endian order.
  45. ///
  46. /// # Panics
  47. ///
  48. /// Panics if the expected length of the sequence `NUM_BITS` exceeds
  49. /// 64.
  50. pub fn i2lebsp<const NUM_BITS: usize>(int: u64) -> [bool; NUM_BITS] {
  51. assert!(NUM_BITS <= 64);
  52. gen_const_array(|mask: usize| (int & (1 << mask)) != 0)
  53. }