Skip to main content

primitives/sharing/authenticated/pairwise/
open_share.rs

1use std::mem::MaybeUninit;
2
3use serde::{de::DeserializeOwned, Deserialize, Serialize};
4
5use crate::{
6    algebra::{
7        elliptic_curve::{BaseField, Point, ScalarField},
8        field::{binary::Gf2_128, FieldElement, SubfieldElement},
9    },
10    errors::PrimitiveError,
11    types::heap_array::{CurvePoints, FieldElements, SubfieldElements},
12    utils::codec::InPlaceCodec,
13};
14
15/// Generic opening of a [`PairwiseAuthShare`]: the value and the corresponding MAC,
16/// shared towards a remote peer for verification.
17///
18/// All four concrete opening types are type aliases of this struct:
19/// - [`OpenFieldShare<F>`]     = `PairwiseAuthOpenShare<SubfieldElement<F>,   FieldElement<F>>`
20/// - [`OpenFieldShares<F, M>`] = `PairwiseAuthOpenShare<SubfieldElements<F,M>,FieldElements<F,M>>`
21/// - [`OpenPointShare<C>`]     = `PairwiseAuthOpenShare<Point<C>,             Point<C>>`
22/// - [`OpenPointShares<C, M>`] = `PairwiseAuthOpenShare<CurvePoints<C,M>,     CurvePoints<C,M>>`
23#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(bound = "V: Serialize + DeserializeOwned, Mac: Serialize + DeserializeOwned")]
25#[repr(C)]
26pub struct PairwiseAuthOpenShare<V, Mac> {
27    pub value: V,
28    pub mac: Mac,
29}
30
31// SAFETY: encodes as `value` then `mac`, back to back. `write_le_bytes` initializes both halves
32// (hence every byte); round-trip is unbiased since each field's is.
33unsafe impl<V: InPlaceCodec, Mac: InPlaceCodec> InPlaceCodec for PairwiseAuthOpenShare<V, Mac> {
34    const ENCODED_SIZE: usize = V::ENCODED_SIZE + Mac::ENCODED_SIZE;
35
36    fn write_le_bytes(&self, out: &mut [MaybeUninit<u8>]) {
37        let (value, mac) = out.split_at_mut(V::ENCODED_SIZE);
38        self.value.write_le_bytes(value);
39        self.mac.write_le_bytes(mac);
40    }
41
42    fn read_le_bytes(bytes: &[u8]) -> Result<Self, PrimitiveError> {
43        let (value, mac) = bytes.split_at(V::ENCODED_SIZE);
44        Ok(Self {
45            value: V::read_le_bytes(value)?,
46            mac: Mac::read_le_bytes(mac)?,
47        })
48    }
49}
50
51impl<V, Mac> PairwiseAuthOpenShare<V, Mac> {
52    pub fn new(value: V, mac: Mac) -> Self {
53        Self { value, mac }
54    }
55
56    pub fn get_value(&self) -> &V {
57        &self.value
58    }
59
60    pub fn get_mac(&self) -> &Mac {
61        &self.mac
62    }
63}
64
65// --- Aliases --- //
66
67// Single-element
68
69/// Opening of a single authenticated field share.
70/// Alias for [`PairwiseAuthOpenShare<SubfieldElement<F>, FieldElement<F>>`].
71pub type OpenFieldShare<F> = PairwiseAuthOpenShare<SubfieldElement<F>, FieldElement<F>>;
72/// Opening of a single authenticated curve-point share.
73/// Alias for [`PairwiseAuthOpenShare<Point<C>, Point<C>>`].
74pub type OpenPointShare<C> = PairwiseAuthOpenShare<Point<C>, Point<C>>;
75/// Opening of a single authenticated share of a scalar field element (`ScalarField<C>`).
76pub type OpenScalarShare<C> = OpenFieldShare<ScalarField<C>>;
77/// Opening of a single authenticated share of a base field element (`BaseField<C>`).
78pub type OpenBaseFieldShare<C> = OpenFieldShare<BaseField<C>>;
79/// Opening of a single authenticated share of a GF(2^128) bit.
80pub type OpenBitShare = OpenFieldShare<Gf2_128>;
81/// Opening of a batch of `M` authenticated field shares.
82/// Alias for [`PairwiseAuthOpenShare<SubfieldElements<F,M>, FieldElements<F,M>>`].
83pub type OpenFieldShares<F, M> = PairwiseAuthOpenShare<SubfieldElements<F, M>, FieldElements<F, M>>;
84
85// Batched
86
87/// Opening of a batch of `M` authenticated curve-point shares.
88/// Alias for [`PairwiseAuthOpenShare<CurvePoints<C,M>, CurvePoints<C,M>>`].
89pub type OpenPointShares<C, M> = PairwiseAuthOpenShare<CurvePoints<C, M>, CurvePoints<C, M>>;
90/// Opening of a batch of authenticated scalar field shares. See [`PairwiseAuthOpenShare`].
91pub type OpenScalarShares<C, M> = OpenFieldShares<ScalarField<C>, M>;
92/// Opening of a batch of authenticated base field shares. See [`PairwiseAuthOpenShare`].
93pub type OpenBaseFieldShares<C, M> = OpenFieldShares<BaseField<C>, M>;