arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/bits/mod.rs
r1cs-std/src/bits/mod.rsRaw
  1use crate::{
  2    bits::{boolean::Boolean, uint8::UInt8},
  3    Vec,
  4};
  5use algebra::Field;
  6use r1cs_core::SynthesisError;
  7
  8pub mod boolean;
  9pub mod uint8;
 10#[macro_use]
 11pub mod uint;
 12
 13make_uint!(UInt16, 16, u16, uint16);
 14make_uint!(UInt32, 32, u32, uint32);
 15make_uint!(UInt64, 64, u64, uint64);
 16
 17pub trait ToBitsGadget<F: Field> {
 18    /// Outputs the canonical bit-wise representation of `self`.
 19    ///
 20    /// This is the correct default for 99% of use cases.
 21    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError>;
 22
 23    /// Outputs a possibly non-unique bit-wise representation of `self`.
 24    ///
 25    /// If you're not absolutely certain that your usecase can get away with a
 26    /// non-canonical representation, please use `self.to_bits(cs)` instead.
 27    fn to_non_unique_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 28        self.to_bits()
 29    }
 30}
 31
 32impl<F: Field> ToBitsGadget<F> for Boolean<F> {
 33    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 34        Ok(vec![self.clone()])
 35    }
 36}
 37
 38impl<F: Field> ToBitsGadget<F> for [Boolean<F>] {
 39    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 40        Ok(self.to_vec())
 41    }
 42}
 43
 44impl<F: Field> ToBitsGadget<F> for Vec<Boolean<F>> {
 45    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 46        Ok(self.clone())
 47    }
 48}
 49
 50impl<F: Field> ToBitsGadget<F> for UInt8<F> {
 51    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 52        Ok(self.into_bits_le())
 53    }
 54}
 55
 56impl<F: Field> ToBitsGadget<F> for [UInt8<F>] {
 57    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 58        let mut result = Vec::with_capacity(&self.len() * 8);
 59        for byte in self {
 60            result.extend_from_slice(&byte.into_bits_le());
 61        }
 62        Ok(result)
 63    }
 64}
 65
 66impl<F: Field> ToBitsGadget<F> for Vec<UInt8<F>> {
 67    fn to_bits(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
 68        let mut result = Vec::with_capacity(&self.len() * 8);
 69        for byte in self {
 70            result.extend_from_slice(&byte.into_bits_le());
 71        }
 72        Ok(result)
 73    }
 74}
 75
 76pub trait ToBytesGadget<F: Field> {
 77    /// Outputs a canonical byte-wise representation of `self`.
 78    ///
 79    /// This is the correct default for 99% of use cases.
 80    fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError>;
 81
 82    /// Outputs a possibly non-unique byte decomposition of `self`.
 83    ///
 84    /// If you're not absolutely certain that your usecase can get away with a
 85    /// non-canonical representation, please use `self.to_bytes(cs)` instead.
 86    fn to_non_unique_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> {
 87        self.to_bytes()
 88    }
 89}
 90
 91impl<F: Field> ToBytesGadget<F> for [UInt8<F>] {
 92    fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> {
 93        Ok(self.to_vec())
 94    }
 95}
 96
 97impl<'a, F: Field, T: 'a + ToBytesGadget<F>> ToBytesGadget<F> for &'a T {
 98    fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> {
 99        (*self).to_bytes()
100    }
101}
102
103impl<'a, F: Field> ToBytesGadget<F> for &'a [UInt8<F>] {
104    fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> {
105        Ok(self.to_vec())
106    }
107}