fancy_garbling/fancy/
reveal.rs

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