fancy_garbling/fancy/
reveal.rs

1use super::*;
2use crate::util;
3
4/// Trait to describe Fancy objects which can reveal outputs to both parties. For many
5/// simple Fancy objects in this library such as Dummy, this is simply output. For Garbler
6/// and Evaluator, it is more complicated since the BMR16 protocol outputs to the
7/// Evaluator only.
8pub trait FancyReveal: Fancy {
9    /// Reveal the contents of `x` to all parties.
10    fn reveal(&mut self, x: &Self::Item) -> Result<u16, Self::Error>;
11
12    /// Reveal a slice of items to all parties.
13    fn reveal_many(&mut self, xs: &[Self::Item]) -> Result<Vec<u16>, Self::Error> {
14        let mut zs = Vec::with_capacity(xs.len());
15        for x in xs.iter() {
16            zs.push(self.reveal(x)?);
17        }
18        Ok(zs)
19    }
20
21    /// Reveal a bundle to all parties.
22    fn reveal_bundle(&mut self, x: &Bundle<Self::Item>) -> Result<Vec<u16>, Self::Error> {
23        self.reveal_many(x.wires())
24    }
25
26    /// Reveal many bundles to all parties.
27    fn reveal_many_bundles(
28        &mut self,
29        xs: &[Bundle<Self::Item>],
30    ) -> Result<Vec<Vec<u16>>, Self::Error> {
31        let mut zs = Vec::with_capacity(xs.len());
32        for x in xs.iter() {
33            zs.push(self.reveal_bundle(x)?);
34        }
35        Ok(zs)
36    }
37
38    /// Reveal a CRT bundle to all parties.
39    fn crt_reveal(&mut self, x: &CrtBundle<Self::Item>) -> Result<u128, Self::Error> {
40        let q = x.composite_modulus();
41        let xs = self.reveal_many(x.wires())?;
42        Ok(util::crt_inv_factor(&xs, q))
43    }
44
45    /// Reveal many CRT bundles to all parties.
46    fn crt_reveal_many(&mut self, xs: &[CrtBundle<Self::Item>]) -> Result<Vec<u128>, Self::Error> {
47        let mut zs = Vec::with_capacity(xs.len());
48        for x in xs.iter() {
49            zs.push(self.crt_reveal(x)?);
50        }
51        Ok(zs)
52    }
53
54    /// Reveal a binary bundle to all parties.
55    fn bin_reveal(&mut self, x: &BinaryBundle<Self::Item>) -> Result<u128, Self::Error> {
56        let bits = self.reveal_many(x.wires())?;
57        Ok(util::u128_from_bits(&bits))
58    }
59
60    /// Reveal many binary bundles to all parties.
61    fn bin_reveal_many(
62        &mut self,
63        xs: &[BinaryBundle<Self::Item>],
64    ) -> Result<Vec<u128>, Self::Error> {
65        let mut zs = Vec::with_capacity(xs.len());
66        for x in xs.iter() {
67            zs.push(self.bin_reveal(x)?);
68        }
69        Ok(zs)
70    }
71}