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::bits::{boolean::Boolean, uint8::UInt8};
  2use algebra::Field;
  3use r1cs_core::{ConstraintSystem, SynthesisError};
  4
  5pub mod boolean;
  6pub mod uint32;
  7pub mod uint8;
  8
  9pub trait ToBitsGadget<ConstraintF: Field> {
 10    fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, cs: CS) -> Result<Vec<Boolean>, SynthesisError>;
 11
 12    /// Additionally checks if the produced list of booleans is 'valid'.
 13    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 14        &self,
 15        cs: CS,
 16    ) -> Result<Vec<Boolean>, SynthesisError>;
 17}
 18
 19impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
 20    fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _: CS) -> Result<Vec<Boolean>, SynthesisError> {
 21        Ok(vec![self.clone()])
 22    }
 23
 24    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 25        &self,
 26        _: CS,
 27    ) -> Result<Vec<Boolean>, SynthesisError> {
 28        Ok(vec![self.clone()])
 29    }
 30}
 31
 32impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
 33    fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
 34        Ok(self.to_vec())
 35    }
 36
 37    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 38        &self,
 39        _cs: CS,
 40    ) -> Result<Vec<Boolean>, SynthesisError> {
 41        Ok(self.to_vec())
 42    }
 43}
 44impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
 45    fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
 46        Ok(self.clone())
 47    }
 48
 49    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 50        &self,
 51        _cs: CS,
 52    ) -> Result<Vec<Boolean>, SynthesisError> {
 53        Ok(self.clone())
 54    }
 55}
 56
 57impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
 58    fn to_bits<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<Boolean>, SynthesisError> {
 59        let mut result = Vec::with_capacity(&self.len() * 8);
 60        for byte in self {
 61            result.extend_from_slice(&byte.into_bits_le());
 62        }
 63        Ok(result)
 64    }
 65
 66    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 67        &self,
 68        cs: CS,
 69    ) -> Result<Vec<Boolean>, SynthesisError> {
 70        self.to_bits(cs)
 71    }
 72}
 73
 74pub trait ToBytesGadget<ConstraintF: Field> {
 75    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, cs: CS) -> Result<Vec<UInt8>, SynthesisError>;
 76
 77    /// Additionally checks if the produced list of booleans is 'valid'.
 78    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
 79        &self,
 80        cs: CS,
 81    ) -> Result<Vec<UInt8>, SynthesisError>;
 82}
 83
 84impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
 85    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
 86        Ok(self.to_vec())
 87    }
 88
 89    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
 90        &self,
 91        cs: CS,
 92    ) -> Result<Vec<UInt8>, SynthesisError> {
 93        self.to_bytes(cs)
 94    }
 95}
 96
 97impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF> for &'a T {
 98    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
 99        (*self).to_bytes(cs)
100    }
101
102    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
103        &self,
104        cs: CS,
105    ) -> Result<Vec<UInt8>, SynthesisError> {
106        self.to_bytes(cs)
107    }
108}
109
110impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
111    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(&self, _cs: CS) -> Result<Vec<UInt8>, SynthesisError> {
112        Ok(self.to_vec())
113    }
114
115    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
116        &self,
117        cs: CS,
118    ) -> Result<Vec<UInt8>, SynthesisError> {
119        self.to_bytes(cs)
120    }
121}