Skip to main content

fancy_garbling/
wire.rs

1//! Wirelabels for use in garbled circuits.
2//!
3//! This module contains a [`WireLabel`] trait, alongside various instantiations
4//! of this trait. The [`WireLabel`] trait is the core underlying primitive used
5//! in garbled circuits, and represents an encoding of the value on any given
6//! wire of the circuit.
7
8use rand::CryptoRng;
9use subtle::ConditionallySelectable;
10use swanky_cr_hash::TweakableCircularCorrelationRobustHash;
11use swanky_field_binary::F2;
12use swanky_field_traits::FiniteField;
13use vectoreyes::U8x16;
14
15pub(crate) mod mod2;
16pub use mod2::WireMod2;
17
18/// The [`BinaryWireLabel`] provides the subroutines to implement AND gates
19/// for the garbler and evaluator in [`fancy_traits::FancyBinary`].
20pub trait BinaryWireLabel: WireLabel<F2> + ConditionallySelectable {
21    /// Garbles an 'and' gate given two input wires and the delta.
22    ///
23    /// Outputs a tuple consisting of the two gates (that should be transfered to the evaluator)
24    /// and the next wirelabel for the garbler.
25    fn garble_and_gate(gate_num: usize, a: Self, b: Self, delta: Self) -> (U8x16, U8x16, Self);
26
27    /// Evaluates an 'and' gate given two inputs wires and two half-gates from the garbler.
28    ///
29    /// Outputs C = A & B
30    fn evaluate_and_gate(gate_num: usize, a: Self, b: Self, gate0: U8x16, gate1: U8x16) -> Self;
31}
32
33/// A trait that defines a wirelabel as used in garbled circuits.
34///
35/// At its core, a [`WireLabel`] is a way of encoding values, and operating on
36/// those encoded values.
37pub trait WireLabel<FF: FiniteField>:
38    Copy
39    + core::fmt::Debug
40    + core::default::Default
41    + core::ops::Add<Output = Self>
42    + core::ops::AddAssign
43    + core::ops::Sub<Output = Self>
44    + core::ops::SubAssign
45    + core::ops::Neg<Output = Self>
46    + core::ops::Mul<u16, Output = Self>
47    + core::ops::MulAssign<u16>
48{
49    /// Converts a [`WireLabel`] into its [`U8x16`] representation.
50    fn to_repr(&self) -> U8x16;
51
52    /// The color digit of the wire.
53    fn color(&self) -> u16;
54
55    /// Converts a [`U8x16`] into its [`WireLabel`] representation.
56    fn from_repr(inp: U8x16) -> Self;
57
58    /// A random `Δ` [`WireLabel`].
59    fn rand_delta<R: CryptoRng>(rng: &mut R) -> Self;
60
61    /// A random [`WireLabel`].
62    fn rand<R: CryptoRng>(rng: &mut R) -> Self;
63
64    /// Computes the hash of the [`WireLabel`].
65    fn hash(&self, tweak: u128) -> U8x16 {
66        TweakableCircularCorrelationRobustHash::fixed_key().hash(self.to_repr(), tweak)
67    }
68
69    /// Computes a [`WireLabel`] for `x`, returning both the zero
70    /// [`WireLabel`] as well as the [`WireLabel`] for `x`.
71    fn constant<RNG: CryptoRng>(x: FF, delta: Self, rng: &mut RNG) -> (Self, Self);
72}