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    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 15        &self,
 16        cs: CS,
 17    ) -> Result<Vec<Boolean>, SynthesisError>;
 18
 19    /// Additionally checks if the produced list of booleans is 'valid'.
 20    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 21        &self,
 22        cs: CS,
 23    ) -> Result<Vec<Boolean>, SynthesisError>;
 24}
 25
 26impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Boolean {
 27    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 28        &self,
 29        _: CS,
 30    ) -> Result<Vec<Boolean>, SynthesisError> {
 31        Ok(vec![self.clone()])
 32    }
 33
 34    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 35        &self,
 36        _: CS,
 37    ) -> Result<Vec<Boolean>, SynthesisError> {
 38        Ok(vec![self.clone()])
 39    }
 40}
 41
 42impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [Boolean] {
 43    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 44        &self,
 45        _cs: CS,
 46    ) -> Result<Vec<Boolean>, SynthesisError> {
 47        Ok(self.to_vec())
 48    }
 49
 50    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 51        &self,
 52        _cs: CS,
 53    ) -> Result<Vec<Boolean>, SynthesisError> {
 54        Ok(self.to_vec())
 55    }
 56}
 57impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for Vec<Boolean> {
 58    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 59        &self,
 60        _cs: CS,
 61    ) -> Result<Vec<Boolean>, SynthesisError> {
 62        Ok(self.clone())
 63    }
 64
 65    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 66        &self,
 67        _cs: CS,
 68    ) -> Result<Vec<Boolean>, SynthesisError> {
 69        Ok(self.clone())
 70    }
 71}
 72
 73impl<ConstraintF: Field> ToBitsGadget<ConstraintF> for [UInt8] {
 74    fn to_bits<CS: ConstraintSystem<ConstraintF>>(
 75        &self,
 76        _cs: CS,
 77    ) -> Result<Vec<Boolean>, SynthesisError> {
 78        let mut result = Vec::with_capacity(&self.len() * 8);
 79        for byte in self {
 80            result.extend_from_slice(&byte.into_bits_le());
 81        }
 82        Ok(result)
 83    }
 84
 85    fn to_bits_strict<CS: ConstraintSystem<ConstraintF>>(
 86        &self,
 87        cs: CS,
 88    ) -> Result<Vec<Boolean>, SynthesisError> {
 89        self.to_bits(cs)
 90    }
 91}
 92
 93pub trait ToBytesGadget<ConstraintF: Field> {
 94    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
 95        &self,
 96        cs: CS,
 97    ) -> Result<Vec<UInt8>, SynthesisError>;
 98
 99    /// Additionally checks if the produced list of booleans is 'valid'.
100    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
101        &self,
102        cs: CS,
103    ) -> Result<Vec<UInt8>, SynthesisError>;
104}
105
106impl<ConstraintF: Field> ToBytesGadget<ConstraintF> for [UInt8] {
107    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
108        &self,
109        _cs: CS,
110    ) -> Result<Vec<UInt8>, SynthesisError> {
111        Ok(self.to_vec())
112    }
113
114    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
115        &self,
116        cs: CS,
117    ) -> Result<Vec<UInt8>, SynthesisError> {
118        self.to_bytes(cs)
119    }
120}
121
122impl<'a, ConstraintF: Field, T: 'a + ToBytesGadget<ConstraintF>> ToBytesGadget<ConstraintF>
123    for &'a T
124{
125    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
126        &self,
127        cs: CS,
128    ) -> Result<Vec<UInt8>, SynthesisError> {
129        (*self).to_bytes(cs)
130    }
131
132    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
133        &self,
134        cs: CS,
135    ) -> Result<Vec<UInt8>, SynthesisError> {
136        self.to_bytes(cs)
137    }
138}
139
140impl<'a, ConstraintF: Field> ToBytesGadget<ConstraintF> for &'a [UInt8] {
141    fn to_bytes<CS: ConstraintSystem<ConstraintF>>(
142        &self,
143        _cs: CS,
144    ) -> Result<Vec<UInt8>, SynthesisError> {
145        Ok(self.to_vec())
146    }
147
148    fn to_bytes_strict<CS: ConstraintSystem<ConstraintF>>(
149        &self,
150        cs: CS,
151    ) -> Result<Vec<UInt8>, SynthesisError> {
152        self.to_bytes(cs)
153    }
154}