Skip to main content

mpvss_rs/
sharebox.rs

1// Copyright 2020-2026 MathxH Chen.
2//
3// Code is licensed under MIT Apache Dual License
4
5#![allow(non_snake_case)]
6
7use num_bigint::BigInt;
8use num_traits::identities::Zero;
9use std::collections::HashMap;
10use std::vec::Vec;
11
12use crate::group::Group;
13
14// ============================================================================
15// Generic ShareBox Types for 1.0.0 API
16// ============================================================================
17
18/// Generic share box for any cryptographic group.
19///
20/// Used to store a decrypted share along with its DLEQ proof.
21#[derive(Debug, Clone)]
22pub struct GenericShareBox<G: Group> {
23    pub publickey: G::Element,
24    pub share: G::Element,
25    pub challenge: G::Scalar,
26    pub response: G::Scalar,
27}
28
29impl<G: Group> Default for GenericShareBox<G>
30where
31    G::Element: Default,
32    G::Scalar: Default,
33{
34    fn default() -> Self {
35        GenericShareBox {
36            publickey: Default::default(),
37            share: Default::default(),
38            challenge: Default::default(),
39            response: Default::default(),
40        }
41    }
42}
43
44impl<G: Group> GenericShareBox<G> {
45    pub fn new() -> Self
46    where
47        G::Element: Default,
48        G::Scalar: Default,
49    {
50        Self::default()
51    }
52
53    pub fn init(
54        &mut self,
55        publickey: G::Element,
56        share: G::Element,
57        challenge: G::Scalar,
58        response: G::Scalar,
59    ) {
60        self.publickey = publickey;
61        self.share = share;
62        self.challenge = challenge;
63        self.response = response;
64    }
65}
66
67/// Generic distribution shares box for any cryptographic group.
68///
69/// Used to store all encrypted shares with commitments and proofs.
70///
71/// Note: Uses HashMap with Vec<u8> keys (serialized elements) instead of
72/// G::Element directly, to support group elements that don't implement Hash
73/// (e.g., EC points like AffinePoint).
74#[derive(Debug, Clone)]
75pub struct GenericDistributionSharesBox<G: Group> {
76    pub commitments: Vec<G::Element>,
77    /// Maps serialized element bytes to position
78    pub positions: HashMap<Vec<u8>, i64>,
79    /// Maps serialized element bytes to encrypted share
80    pub shares: HashMap<Vec<u8>, G::Element>,
81    pub publickeys: Vec<G::Element>,
82    pub challenge: G::Scalar,
83    /// Maps serialized element bytes to response
84    pub responses: HashMap<Vec<u8>, G::Scalar>,
85    pub U: BigInt, // Secret encoded as BigInt for cross-group compatibility
86}
87
88impl<G: Group> Default for GenericDistributionSharesBox<G>
89where
90    G::Scalar: Default,
91{
92    fn default() -> Self {
93        GenericDistributionSharesBox {
94            commitments: Vec::new(),
95            positions: HashMap::new(),
96            shares: HashMap::new(),
97            publickeys: Vec::new(),
98            challenge: Default::default(),
99            responses: HashMap::new(),
100            U: BigInt::zero(),
101        }
102    }
103}
104
105impl<G: Group> GenericDistributionSharesBox<G> {
106    pub fn new() -> Self
107    where
108        G::Scalar: Default,
109    {
110        Self::default()
111    }
112
113    /// Initialize the distribution shares box.
114    ///
115    /// Note: positions, shares, and responses should use Vec<u8> keys (serialized elements).
116    #[allow(clippy::too_many_arguments)]
117    pub fn init(
118        &mut self,
119        commitments: &[G::Element],
120        positions: HashMap<Vec<u8>, i64>,
121        shares: HashMap<Vec<u8>, G::Element>,
122        publickeys: &[G::Element],
123        challenge: &G::Scalar,
124        responses: HashMap<Vec<u8>, G::Scalar>,
125        U: &BigInt,
126    ) {
127        self.commitments = commitments.to_vec();
128        self.positions = positions;
129        self.shares = shares;
130        self.publickeys = publickeys.to_vec();
131        self.challenge = challenge.clone();
132        self.responses = responses;
133        self.U = U.clone();
134    }
135}
136
137// ============================================================================
138// Type Aliases (Primary API)
139// ============================================================================
140
141/// Type alias for the generic ShareBox - primary API for 1.0.0
142/// Replaces the old non-generic ShareBox struct
143pub type ShareBox<G> = GenericShareBox<G>;
144
145/// Type alias for the generic DistributionSharesBox - primary API for 1.0.0
146/// Replaces the old non-generic DistributionSharesBox struct
147pub type DistributionSharesBox<G> = GenericDistributionSharesBox<G>;