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::{ConstraintSystem, SynthesisError};
  7
  8pub mod boolean;
  9pub mod uint32;
 10pub mod uint64;
 11pub mod uint8;
 12
 13pub trait ToBitsGadget<ConstraintF: Field> {
 14    /// Outputs the canonical bit-wise representation of `self`.
 15    ///
 16    /// This is the correct default for 99% of use cases.
 17    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 18        &self,
 19        cs: CS,
 20    ) -> Result<Vec<Boolean>, SynthesisError>;
 21
 22    /// Outputs a possibly non-unique bit-wise representation of `self`.
 23    ///
 24    /// If you're not absolutely certain that your usecase can get away with a
 25    /// non-canonical representation, please use `self.to_bits(cs)` instead.
 26    fn to_non_unique_bits<CS: ConstraintSystem<ConstraintF>>(
 27        &self,
 28        cs: CS,
 29    ) -> Result<Vec<Boolean>, SynthesisError> {
 30        self.to_bits(cs)
 31    }
 32}
 33
 34impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
 35    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 36        &self,
 37        _: CS,
 38    ) -> Result<Vec<Boolean>, SynthesisError> {
 39        Ok(vec![self.clone()])
 40    }
 41}
 42
 43impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
 44    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 45        &self,
 46        _cs: CS,
 47    ) -> Result<Vec<Boolean>, SynthesisError> {
 48        Ok(self.to_vec())
 49    }
 50}
 51
 52impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
 53    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 54        &self,
 55        _cs: CS,
 56    ) -> Result<Vec<Boolean>, SynthesisError> {
 57        Ok(self.clone())
 58    }
 59}
 60
 61impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for UInt8 {
 62    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 63        &self,
 64        _cs: CS,
 65    ) -> Result<Vec<Boolean>, SynthesisError> {
 66        Ok(self.into_bits_le())
 67    }
 68}
 69
 70impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
 71    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 72        &self,
 73        _cs: CS,
 74    ) -> Result<Vec<Boolean>, SynthesisError> {
 75        let mut result = Vec::with_capacity(&self.len() * 8);
 76        for byte in self {
 77            result.extend_from_slice(&byte.into_bits_le());
 78        }
 79        Ok(result)
 80    }
 81}
 82
 83impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<UInt8> {
 84    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 85        &self,
 86        _cs: CS,
 87    ) -> Result<Vec<Boolean>, SynthesisError> {
 88        let mut result = Vec::with_capacity(&self.len() * 8);
 89        for byte in self {
 90            result.extend_from_slice(&byte.into_bits_le());
 91        }
 92        Ok(result)
 93    }
 94}
 95
 96pub trait ToBytesGadget<ConstraintF: Field> {
 97    /// Outputs a canonical byte-wise representation of `self`.
 98    ///
 99    /// This is the correct default for 99% of use cases.
100    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
101        &self,
102        cs: CS,
103    ) -> Result<Vec<UInt8>, SynthesisError>;
104
105    /// Outputs a possibly non-unique byte decomposition of `self`.
106    ///
107    /// If you're not absolutely certain that your usecase can get away with a
108    /// non-canonical representation, please use `self.to_bytes(cs)` instead.
109    fn to_non_unique_bytes<CS: ConstraintSystem<ConstraintF>>(
110        &self,
111        cs: CS,
112    ) -> Result<Vec<UInt8>, SynthesisError> {
113        self.to_bytes(cs)
114    }
115}
116
117impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
118    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
119        &self,
120        _cs: CS,
121    ) -> Result<Vec<UInt8>, SynthesisError> {
122        Ok(self.to_vec())
123    }
124}
125
126impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF>
127    for &'a T
128{
129    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
130        &self,
131        cs: CS,
132    ) -> Result<Vec<UInt8>, SynthesisError> {
133        (*self).to_bytes(cs)
134    }
135}
136
137impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
138    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
139        &self,
140        _cs: CS,
141    ) -> Result<Vec<UInt8>, SynthesisError> {
142        Ok(self.to_vec())
143    }
144}