fancy_garbling/fancy/
input.rs

1use super::*;
2use crate::util;
3use itertools::Itertools;
4
5/// Convenience functions for encoding input to Fancy objects.
6pub trait FancyInput {
7    /// The type that this Fancy object operates over.
8    type Item: Clone + HasModulus;
9
10    /// The type of error that this Fancy object emits.
11    type Error: From<FancyError>;
12
13    ////////////////////////////////////////////////////////////////////////////////
14    // required methods
15
16    /// Encode many values where the actual input is known.
17    ///
18    /// When writing a garbler, the return value must correspond to the zero
19    /// wire label.
20    fn encode_many(
21        &mut self,
22        values: &[u16],
23        moduli: &[u16],
24    ) -> Result<Vec<Self::Item>, Self::Error>;
25
26    /// Receive many values where the input is not known.
27    fn receive_many(&mut self, moduli: &[u16]) -> Result<Vec<Self::Item>, Self::Error>;
28
29    ////////////////////////////////////////////////////////////////////////////////
30    // optional methods
31
32    /// Encode a single value.
33    ///
34    /// When writing a garbler, the return value must correspond to the zero
35    /// wire label.
36    fn encode(&mut self, value: u16, modulus: u16) -> Result<Self::Item, Self::Error> {
37        let mut xs = self.encode_many(&[value], &[modulus])?;
38        Ok(xs.remove(0))
39    }
40
41    /// Receive a single value.
42    fn receive(&mut self, modulus: u16) -> Result<Self::Item, Self::Error> {
43        let mut xs = self.receive_many(&[modulus])?;
44        Ok(xs.remove(0))
45    }
46
47    /// Encode a bundle.
48    fn encode_bundle(
49        &mut self,
50        values: &[u16],
51        moduli: &[u16],
52    ) -> Result<Bundle<Self::Item>, Self::Error> {
53        self.encode_many(values, moduli).map(Bundle::new)
54    }
55
56    /// Receive a bundle.
57    fn receive_bundle(&mut self, moduli: &[u16]) -> Result<Bundle<Self::Item>, Self::Error> {
58        self.receive_many(moduli).map(Bundle::new)
59    }
60
61    /// Encode many input bundles.
62    fn encode_bundles(
63        &mut self,
64        values: &[Vec<u16>],
65        moduli: &[Vec<u16>],
66    ) -> Result<Vec<Bundle<Self::Item>>, Self::Error> {
67        let qs = moduli.iter().flatten().cloned().collect_vec();
68        let xs = values.iter().flatten().cloned().collect_vec();
69        if xs.len() != qs.len() {
70            return Err(
71                FancyError::InvalidArg("unequal number of values and moduli".to_string()).into(),
72            );
73        }
74        let mut wires = self.encode_many(&xs, &qs)?;
75        let buns = moduli
76            .iter()
77            .map(|qs| {
78                let ws = wires.drain(0..qs.len()).collect_vec();
79                Bundle::new(ws)
80            })
81            .collect_vec();
82        Ok(buns)
83    }
84
85    /// Receive many input bundles.
86    fn receive_many_bundles(
87        &mut self,
88        moduli: &[Vec<u16>],
89    ) -> Result<Vec<Bundle<Self::Item>>, Self::Error> {
90        let qs = moduli.iter().flatten().cloned().collect_vec();
91        let mut wires = self.receive_many(&qs)?;
92        let buns = moduli
93            .iter()
94            .map(|qs| {
95                let ws = wires.drain(0..qs.len()).collect_vec();
96                Bundle::new(ws)
97            })
98            .collect_vec();
99        Ok(buns)
100    }
101
102    /// Encode a CRT input bundle.
103    fn crt_encode(
104        &mut self,
105        value: u128,
106        modulus: u128,
107    ) -> Result<CrtBundle<Self::Item>, Self::Error> {
108        let qs = util::factor(modulus);
109        let xs = util::crt(value, &qs);
110        self.encode_bundle(&xs, &qs).map(CrtBundle::from)
111    }
112
113    /// Receive an CRT input bundle.
114    fn crt_receive(&mut self, modulus: u128) -> Result<CrtBundle<Self::Item>, Self::Error> {
115        let qs = util::factor(modulus);
116        self.receive_bundle(&qs).map(CrtBundle::from)
117    }
118
119    /// Encode many CRT input bundles.
120    fn crt_encode_many(
121        &mut self,
122        values: &[u128],
123        modulus: u128,
124    ) -> Result<Vec<CrtBundle<Self::Item>>, Self::Error> {
125        let mods = util::factor(modulus);
126        let nmods = mods.len();
127        let xs = values
128            .iter()
129            .flat_map(|x| util::crt(*x, &mods))
130            .collect_vec();
131        let qs = itertools::repeat_n(mods, values.len())
132            .flatten()
133            .collect_vec();
134        let mut wires = self.encode_many(&xs, &qs)?;
135        let buns = (0..values.len())
136            .map(|_| {
137                let ws = wires.drain(0..nmods).collect_vec();
138                CrtBundle::new(ws)
139            })
140            .collect_vec();
141        Ok(buns)
142    }
143
144    /// Receive many CRT input bundles.
145    fn crt_receive_many(
146        &mut self,
147        n: usize,
148        modulus: u128,
149    ) -> Result<Vec<CrtBundle<Self::Item>>, Self::Error> {
150        let mods = util::factor(modulus);
151        let nmods = mods.len();
152        let qs = itertools::repeat_n(mods, n).flatten().collect_vec();
153        let mut wires = self.receive_many(&qs)?;
154        let buns = (0..n)
155            .map(|_| {
156                let ws = wires.drain(0..nmods).collect_vec();
157                CrtBundle::new(ws)
158            })
159            .collect_vec();
160        Ok(buns)
161    }
162
163    /// Encode a binary input bundle.
164    fn bin_encode(
165        &mut self,
166        value: u128,
167        nbits: usize,
168    ) -> Result<BinaryBundle<Self::Item>, Self::Error> {
169        let xs = util::u128_to_bits(value, nbits);
170        self.encode_bundle(&xs, &vec![2; nbits])
171            .map(BinaryBundle::from)
172    }
173
174    /// Receive an binary input bundle.
175    fn bin_receive(&mut self, nbits: usize) -> Result<BinaryBundle<Self::Item>, Self::Error> {
176        self.receive_bundle(&vec![2; nbits]).map(BinaryBundle::from)
177    }
178
179    /// Encode many binary input bundles.
180    fn bin_encode_many(
181        &mut self,
182        values: &[u128],
183        nbits: usize,
184    ) -> Result<Vec<BinaryBundle<Self::Item>>, Self::Error> {
185        let xs = values
186            .iter()
187            .flat_map(|x| util::u128_to_bits(*x, nbits))
188            .collect_vec();
189        let mut wires = self.encode_many(&xs, &vec![2; values.len() * nbits])?;
190        let buns = (0..values.len())
191            .map(|_| {
192                let ws = wires.drain(0..nbits).collect_vec();
193                BinaryBundle::new(ws)
194            })
195            .collect_vec();
196        Ok(buns)
197    }
198
199    /// Receive many binary input bundles.
200    fn bin_receive_many(
201        &mut self,
202        ninputs: usize,
203        nbits: usize,
204    ) -> Result<Vec<BinaryBundle<Self::Item>>, Self::Error> {
205        let mut wires = self.receive_many(&vec![2; ninputs * nbits])?;
206        let buns = (0..ninputs)
207            .map(|_| {
208                let ws = wires.drain(0..nbits).collect_vec();
209                BinaryBundle::new(ws)
210            })
211            .collect_vec();
212        Ok(buns)
213    }
214}