mpvss-rs 2.0.0

A Simple Publicly Verifiable Secret Sharing Library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright 2020-2026 MathxH Chen.
//
// Code is licensed under MIT Apache Dual License

//! Generic DLEQ (Discrete Logarithm Equality) proof for multiple cryptographic groups.
//!
//! This module implements the Chaum-Pedersen protocol for proving that log_g1(h1) = log_g2(h2).
//! The generic implementation works with any group implementing the `Group` trait.
//!
//! # Chaum and Pedersen Scheme
//!
//! To prove that log_g1(h1) = log_g2(h2) for generators g1, h1, g2, h2 ∈ Gq,
//! where Gq is a group of order q and q is prime.
//!
//! We denote this protocol by DLEQ(g1, h1, g2, h2), and it consists of the following steps,
//! where the prover knows α such that h1 = g1^α and h2 = g2^α:
//!
//! - The prover sends a1 = g1^w and a2 = g2^w to the verifier, with w ∈ R Zq
//! - The verifier sends a random challenge c ∈ R Zq to the prover
//! - The prover responds with r = w - αc (mod q)
//! - The verifier checks that a1 = (g1^r) * (h1^c) and a2 = (g2^r) * (h2^c)

use sha2::{Digest, Sha256};
use std::sync::Arc;

use crate::group::Group;

// ============================================================================
// Internal Prover/Verifier Structures
// ============================================================================

/// Internal prover structure for DLEQ proof generation
struct Prover {}

impl Prover {
    /// Send a1 = g1^w (MODP) or w*g1 (EC)
    fn send<G: Group>(group: &G, g: &G::Element, w: &G::Scalar) -> G::Element {
        group.exp(g, w)
    }

    /// Compute response r = w - alpha*c (mod order)
    fn response<G: Group>(
        group: &G,
        w: &G::Scalar,
        alpha: &G::Scalar,
        c: &G::Scalar,
    ) -> G::Scalar {
        let alpha_c = group.scalar_mul(alpha, c);
        group.scalar_sub(w, &alpha_c)
    }
}

/// Internal verifier structure for DLEQ proof verification
struct Verifier {}

impl Verifier {
    #[inline]
    fn update_framed_hash(hasher: &mut Sha256, bytes: &[u8]) {
        hasher.update((bytes.len() as u64).to_be_bytes());
        hasher.update(bytes);
    }

    /// Compute verifier-side commitments:
    /// a1 = g1^r * h1^c, a2 = g2^r * h2^c
    #[allow(clippy::too_many_arguments)]
    fn commitments<G: Group>(
        group: &G,
        g1: &G::Element,
        h1: &G::Element,
        g2: &G::Element,
        h2: &G::Element,
        response: &G::Scalar,
        c: &G::Scalar,
    ) -> (G::Element, G::Element) {
        let g1_r = group.exp(g1, response);
        let h1_c = group.exp(h1, c);
        let a1 = group.mul(&g1_r, &h1_c);

        let g2_r = group.exp(g2, response);
        let h2_c = group.exp(h2, c);
        let a2 = group.mul(&g2_r, &h2_c);

        (a1, a2)
    }

    /// Append (h1, h2, a1, a2) to Fiat-Shamir transcript using framed encoding.
    fn append_transcript<G: Group>(
        group: &G,
        h1: &G::Element,
        h2: &G::Element,
        a1: &G::Element,
        a2: &G::Element,
        hasher: &mut Sha256,
    ) {
        Self::update_framed_hash(hasher, &group.element_to_bytes(h1));
        Self::update_framed_hash(hasher, &group.element_to_bytes(h2));
        Self::update_framed_hash(hasher, &group.element_to_bytes(a1));
        Self::update_framed_hash(hasher, &group.element_to_bytes(a2));
    }

    /// Compute verifier commitments and append them to transcript.
    #[allow(clippy::too_many_arguments)]
    fn update<G: Group>(
        group: &G,
        g1: &G::Element,
        h1: &G::Element,
        g2: &G::Element,
        h2: &G::Element,
        response: &G::Scalar,
        c: &G::Scalar,
        hasher: &mut Sha256,
    ) -> (G::Element, G::Element) {
        let (a1, a2) = Self::commitments(group, g1, h1, g2, h2, response, c);
        Self::append_transcript(group, h1, h2, &a1, &a2, hasher);
        (a1, a2)
    }

    /// Check that the challenge matches the computed hash
    fn check<G: Group>(group: &G, c: &G::Scalar, hasher: &Sha256) -> bool
    where
        G::Scalar: Clone + Eq,
    {
        let challenge_hash = hasher.clone().finalize();
        let computed = group.hash_to_scalar(&challenge_hash);
        computed == *c
    }
}

// ============================================================================
// Generic DLEQ Proof Structure
// ============================================================================

/// Generic DLEQ (Discrete Logarithm Equality) proof.
///
/// Proves that log_g1(h1) = log_g2(h2) for given generators.
/// This is the Chaum-Pedersen protocol adapted for generic groups.
///
/// # Type Parameters
/// - `G`: A type implementing the `Group` trait
///
/// # Example
///
/// ```rust
/// use mpvss_rs::groups::ModpGroup;
/// use mpvss_rs::dleq::DLEQ;
///
/// let group = ModpGroup::new();
/// let mut dleq = DLEQ::new(group);
/// // ... set up g1, h1, g2, h2, alpha, w
/// // dleq.init(g1, h1, g2, h2, alpha, w);
/// ```
#[derive(Debug, Clone)]
pub struct DLEQ<G: Group> {
    pub g1: G::Element,
    pub h1: G::Element,
    pub g2: G::Element,
    pub h2: G::Element,
    pub w: G::Scalar,
    pub alpha: G::Scalar,
    pub c: Option<G::Scalar>,
    pub r: Option<G::Scalar>,
    pub group: Arc<G>,
}

impl<G: Group> DLEQ<G> {
    /// Create a new DLEQ proof structure.
    pub fn new(group: Arc<G>) -> Self
    where
        G::Scalar: Default,
        G::Element: Default,
    {
        DLEQ {
            g1: Default::default(),
            h1: Default::default(),
            g2: Default::default(),
            h2: Default::default(),
            w: Default::default(),
            alpha: Default::default(),
            c: None,
            r: None,
            group,
        }
    }

    /// Initialize DLEQ with all parameters.
    #[allow(clippy::too_many_arguments)]
    pub fn init(
        &mut self,
        g1: G::Element,
        h1: G::Element,
        g2: G::Element,
        h2: G::Element,
        alpha: G::Scalar,
        w: G::Scalar,
    ) {
        self.g1 = g1;
        self.h1 = h1;
        self.g2 = g2;
        self.h2 = h2;
        self.alpha = alpha;
        self.w = w;
    }

    /// Compute a1 = g1^w (MODP) or w*g1 (EC)
    ///
    /// This is the first commitment sent by the prover.
    pub fn get_a1(&self) -> G::Element {
        Prover::send(self.group.as_ref(), &self.g1, &self.w)
    }

    /// Compute a2 = g2^w (MODP) or w*g2 (EC)
    ///
    /// This is the second commitment sent by the prover.
    pub fn get_a2(&self) -> G::Element {
        Prover::send(self.group.as_ref(), &self.g2, &self.w)
    }

    /// Compute response r = w - alpha*c (mod order)
    ///
    /// The response is computed after receiving the challenge c.
    pub fn get_r(&self) -> Option<G::Scalar>
    where
        G::Scalar: Clone,
    {
        self.c.as_ref().map(|c| {
            Prover::response(self.group.as_ref(), &self.w, &self.alpha, c)
        })
    }

    /// Compute verifier-side commitments:
    /// a1 = g1^r * h1^c, a2 = g2^r * h2^c
    #[allow(clippy::too_many_arguments)]
    pub fn verifier_commitments(
        group: &G,
        g1: &G::Element,
        h1: &G::Element,
        g2: &G::Element,
        h2: &G::Element,
        response: &G::Scalar,
        c: &G::Scalar,
    ) -> (G::Element, G::Element) {
        Verifier::commitments(group, g1, h1, g2, h2, response, c)
    }

    /// Append (h1, h2, a1, a2) to Fiat-Shamir transcript.
    pub fn append_transcript_hash(
        group: &G,
        h1: &G::Element,
        h2: &G::Element,
        a1: &G::Element,
        a2: &G::Element,
        hasher: &mut Sha256,
    ) {
        Verifier::append_transcript(group, h1, h2, a1, a2, hasher);
    }

    /// Compute verifier commitments from (response, challenge) and append them.
    #[allow(clippy::too_many_arguments)]
    pub fn verifier_update_hash(
        group: &G,
        g1: &G::Element,
        h1: &G::Element,
        g2: &G::Element,
        h2: &G::Element,
        response: &G::Scalar,
        c: &G::Scalar,
        hasher: &mut Sha256,
    ) -> (G::Element, G::Element) {
        Verifier::update(group, g1, h1, g2, h2, response, c, hasher)
    }

    /// Verify the DLEQ proof.
    ///
    /// Returns `true` if the proof is valid, `false` otherwise.
    pub fn verify(&self) -> bool
    where
        G::Scalar: Clone,
        G::Element: Clone,
    {
        let c = match &self.c {
            Some(c) => c,
            None => return false,
        };
        let r = match &self.r {
            Some(r) => r,
            None => return false,
        };

        let mut hasher = Sha256::new();
        let _ = Self::verifier_update_hash(
            self.group.as_ref(),
            &self.g1,
            &self.h1,
            &self.g2,
            &self.h2,
            r,
            c,
            &mut hasher,
        );

        self.check(&hasher)
    }

    /// Update the challenge hasher with DLEQ data.
    ///
    /// This is used to compute the common challenge c from the commitments.
    pub fn update_hash(&self, hasher: &mut Sha256)
    where
        G::Element: Clone,
    {
        let a1 = self.get_a1();
        let a2 = self.get_a2();
        Self::append_transcript_hash(
            self.group.as_ref(),
            &self.h1,
            &self.h2,
            &a1,
            &a2,
            hasher,
        );
    }

    /// Check that the challenge matches the computed hash.
    ///
    /// This is the final verification step.
    pub fn check(&self, hasher: &Sha256) -> bool
    where
        G::Scalar: Clone + Eq,
    {
        match &self.c {
            Some(c) => Verifier::check(self.group.as_ref(), c, hasher),
            None => false,
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::groups::ModpGroup;
    use num_bigint::{BigInt, RandBigInt, ToBigInt};
    use num_integer::Integer;

    #[test]
    fn test_generic_dleq_new() {
        let group = ModpGroup::new();
        let dleq = DLEQ::new(group);
        assert_eq!(dleq.c, None);
        assert_eq!(dleq.r, None);
    }

    #[test]
    fn test_generic_dleq_init() {
        let group = ModpGroup::new();
        let mut dleq = DLEQ::new(group.clone());

        let g1 = BigInt::from(8443);
        let h1 = BigInt::from(531216);
        let g2 = BigInt::from(1299721);
        let h2 = BigInt::from(14767239);
        let w = BigInt::from(81647);
        let alpha = BigInt::from(163027);

        dleq.init(g1.clone(), h1, g2.clone(), h2, alpha, w.clone());

        // Compute expected a1 and a2 values
        let q = group.modulus();
        let a1_expected = g1.modpow(&w, q);
        let a2_expected = g2.modpow(&w, q);

        assert_eq!(dleq.get_a1(), a1_expected);
        assert_eq!(dleq.get_a2(), a2_expected);
    }

    #[test]
    fn test_generic_dleq_get_r() {
        let group = ModpGroup::new();
        let mut dleq = DLEQ::new(group.clone());

        let g1 = BigInt::from(8443);
        let h1 = BigInt::from(531216);
        let g2 = BigInt::from(1299721);
        let h2 = BigInt::from(14767239);
        let w = BigInt::from(81647);
        let alpha = BigInt::from(163027);

        dleq.init(g1, h1, g2, h2, alpha, w);
        dleq.c = Some(BigInt::from(127997));

        let r = dleq.get_r().unwrap();
        // The response is computed as (w - alpha*c) mod order
        // where order is q-1 for the MODP group
        let order = group.order();
        let expected_r = (BigInt::from(81647)
            - BigInt::from(163027) * BigInt::from(127997))
        .mod_floor(order);

        assert_eq!(r, expected_r);
    }

    #[test]
    fn test_generic_dleq_verify() {
        let group = ModpGroup::new();
        let mut rng = rand::thread_rng();
        let q = group.modulus().clone();

        // Generate random values
        let alpha: BigInt = rng
            .gen_biguint_below(&q.to_biguint().unwrap())
            .to_bigint()
            .unwrap();
        let w: BigInt = rng
            .gen_biguint_below(&q.to_biguint().unwrap())
            .to_bigint()
            .unwrap();

        let g1 = BigInt::from(8443);
        let h1 = g1.modpow(&alpha, &q);
        let g2 = BigInt::from(1299721);
        let h2 = g2.modpow(&alpha, &q);

        let mut dleq = DLEQ::new(group.clone());
        dleq.init(g1, h1, g2, h2, alpha, w);

        // Compute challenge
        let mut hasher = Sha256::new();
        dleq.update_hash(&mut hasher);
        let c = group.hash_to_scalar(&hasher.finalize());
        dleq.c = Some(c.clone());

        // Compute response
        let r = dleq.get_r().unwrap();
        dleq.r = Some(r);

        // Verify should succeed
        assert!(dleq.verify());
    }
}