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