//! This module implements various gadgets necessary for doing non-native arithmetic //! Code in this module is adapted from [bellman-bignat](https://github.com/alex-ozdemir/bellman-bignat), which is licenced under MIT use bellperson::SynthesisError; use ff::PrimeField; trait OptionExt { fn grab(&self) -> Result<&T, SynthesisError>; fn grab_mut(&mut self) -> Result<&mut T, SynthesisError>; } impl OptionExt for Option { fn grab(&self) -> Result<&T, SynthesisError> { self.as_ref().ok_or(SynthesisError::AssignmentMissing) } fn grab_mut(&mut self) -> Result<&mut T, SynthesisError> { self.as_mut().ok_or(SynthesisError::AssignmentMissing) } } trait BitAccess { fn get_bit(&self, i: usize) -> Option; } impl BitAccess for Scalar { fn get_bit(&self, i: usize) -> Option { if i as u32 >= Scalar::NUM_BITS { return None; } let (byte_pos, bit_pos) = (i / 8, i % 8); let byte = self.to_repr().as_ref()[byte_pos]; let bit = byte >> bit_pos & 1; Some(bit == 1) } } pub mod bignat; pub mod util;