Skip to main content

primitives/sharing/authenticated/pairwise/
open_share.rs

1use std::mem::MaybeUninit;
2
3use serde::{de::DeserializeOwned, Deserialize, Serialize};
4use wincode::{
5    io::{Reader, Writer},
6    ReadResult,
7    SchemaRead,
8    SchemaWrite,
9    WriteResult,
10};
11
12use crate::{
13    algebra::{
14        elliptic_curve::{BaseField, Point, ScalarField},
15        field::{binary::Gf2_128, FieldElement, SubfieldElement},
16    },
17    types::heap_array::{CurvePoints, FieldElements, SubfieldElements},
18};
19
20/// Generic opening of a [`PairwiseAuthShare`]: the value and the corresponding MAC,
21/// shared towards a remote peer for verification.
22///
23/// All four concrete opening types are type aliases of this struct:
24/// - [`OpenFieldShare<F>`]     = `PairwiseAuthOpenShare<SubfieldElement<F>,   FieldElement<F>>`
25/// - [`OpenFieldShares<F, M>`] = `PairwiseAuthOpenShare<SubfieldElements<F,M>,FieldElements<F,M>>`
26/// - [`OpenPointShare<C>`]     = `PairwiseAuthOpenShare<Point<C>,             Point<C>>`
27/// - [`OpenPointShares<C, M>`] = `PairwiseAuthOpenShare<CurvePoints<C,M>,     CurvePoints<C,M>>`
28#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(bound = "V: Serialize + DeserializeOwned, Mac: Serialize + DeserializeOwned")]
30#[repr(C)]
31pub struct PairwiseAuthOpenShare<V, Mac> {
32    pub value: V,
33    pub mac: Mac,
34}
35
36impl<V, Mac> SchemaWrite for PairwiseAuthOpenShare<V, Mac>
37where
38    V: SchemaWrite<Src = V>,
39    Mac: SchemaWrite<Src = Mac>,
40{
41    type Src = Self;
42
43    fn size_of(src: &Self) -> WriteResult<usize> {
44        Ok(V::size_of(&src.value)? + Mac::size_of(&src.mac)?)
45    }
46
47    fn write(writer: &mut impl Writer, src: &Self) -> WriteResult<()> {
48        V::write(writer, &src.value)?;
49        Mac::write(writer, &src.mac)
50    }
51}
52
53impl<'de, V, Mac> SchemaRead<'de> for PairwiseAuthOpenShare<V, Mac>
54where
55    V: SchemaRead<'de, Dst = V>,
56    Mac: SchemaRead<'de, Dst = Mac>,
57{
58    type Dst = Self;
59
60    fn read(reader: &mut impl Reader<'de>, dst: &mut MaybeUninit<Self>) -> ReadResult<()> {
61        let mut value = MaybeUninit::<V>::uninit();
62        let mut mac = MaybeUninit::<Mac>::uninit();
63        V::read(reader, &mut value)?;
64        Mac::read(reader, &mut mac)?;
65        // SAFETY: all fields were initialised by the reads above
66        let value = unsafe { value.assume_init() };
67        let mac = unsafe { mac.assume_init() };
68        dst.write(PairwiseAuthOpenShare { value, mac });
69        Ok(())
70    }
71}
72
73impl<V, Mac> PairwiseAuthOpenShare<V, Mac> {
74    pub fn new(value: V, mac: Mac) -> Self {
75        Self { value, mac }
76    }
77
78    pub fn get_value(&self) -> &V {
79        &self.value
80    }
81
82    pub fn get_mac(&self) -> &Mac {
83        &self.mac
84    }
85}
86
87// --- Aliases --- //
88
89// Single-element
90
91/// Opening of a single authenticated field share.
92/// Alias for [`PairwiseAuthOpenShare<SubfieldElement<F>, FieldElement<F>>`].
93pub type OpenFieldShare<F> = PairwiseAuthOpenShare<SubfieldElement<F>, FieldElement<F>>;
94/// Opening of a single authenticated curve-point share.
95/// Alias for [`PairwiseAuthOpenShare<Point<C>, Point<C>>`].
96pub type OpenPointShare<C> = PairwiseAuthOpenShare<Point<C>, Point<C>>;
97/// Opening of a single authenticated share of a scalar field element (`ScalarField<C>`).
98pub type OpenScalarShare<C> = OpenFieldShare<ScalarField<C>>;
99/// Opening of a single authenticated share of a base field element (`BaseField<C>`).
100pub type OpenBaseFieldShare<C> = OpenFieldShare<BaseField<C>>;
101/// Opening of a single authenticated share of a GF(2^128) bit.
102pub type OpenBitShare = OpenFieldShare<Gf2_128>;
103/// Opening of a batch of `M` authenticated field shares.
104/// Alias for [`PairwiseAuthOpenShare<SubfieldElements<F,M>, FieldElements<F,M>>`].
105pub type OpenFieldShares<F, M> = PairwiseAuthOpenShare<SubfieldElements<F, M>, FieldElements<F, M>>;
106
107// Batched
108
109/// Opening of a batch of `M` authenticated curve-point shares.
110/// Alias for [`PairwiseAuthOpenShare<CurvePoints<C,M>, CurvePoints<C,M>>`].
111pub type OpenPointShares<C, M> = PairwiseAuthOpenShare<CurvePoints<C, M>, CurvePoints<C, M>>;
112/// Opening of a batch of authenticated scalar field shares. See [`PairwiseAuthOpenShare`].
113pub type OpenScalarShares<C, M> = OpenFieldShares<ScalarField<C>, M>;
114/// Opening of a batch of authenticated base field shares. See [`PairwiseAuthOpenShare`].
115pub type OpenBaseFieldShares<C, M> = OpenFieldShares<BaseField<C>, M>;