commonware-cryptography 2026.5.0

Generate keys, sign arbitrary messages, and deterministically verify signatures.
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! This module provides a proof that a plain commitment and a Pedersen
//! commitment share the same committed value.
//!
//! # What This Lets You Do
//!
//! This proof lets a prover link a transparent commitment `X = x * G` with a
//! hiding Pedersen commitment `C = x * G + x_blind * H`, showing that both use
//! the same committed value `x`.
//!
//! This is useful when one part of a protocol wants to work with a plain
//! commitment while another part wants the same value hidden behind a Pedersen
//! commitment. The proof lets the prover bridge those two views without
//! revealing either the committed value or the Pedersen blinding.
//!
//! Concretely, the prover shows knowledge of openings `(x, x_blind)` such that:
//!
//! - `X = x * G`, and
//! - `C = x * G + x_blind * H`.
//!
//! # Usage
//!
//! Construct a [`Setup`] with a value generator and an independent blinding
//! generator. Then create a [`Witness`] and derive its public [`Claim`] with
//! [`Witness::claim`].
//!
//! Given a [`Setup`], [`Claim`], and [`Witness`], call [`prove`] to create a
//! [`Proof`]. The proof is bound to the current [`Transcript`] state, so the
//! verifier must replay the same transcript history before calling [`verify`].
//!
//! [`verify`] checks the proof against a [`Synthetic`] setup, allowing for easy
//! batching with other proofs of this kind, or other proofs entirely. For example,
//! you can batch this proof with the result of [`crate::zk::bulletproofs`].
//!
//! ## Example
//!
//! ```rust
//! # use commonware_cryptography::{
//! #     bls12381::primitives::group::{G1, Scalar},
//! #     transcript::Transcript,
//! #     zk::pedersen_to_plain::{prove, verify, Setup, Witness},
//! # };
//! # use commonware_math::{
//! #     algebra::{Additive, CryptoGroup, HashToGroup},
//! #     synthetic::Synthetic,
//! # };
//! # use commonware_parallel::Sequential;
//! # use commonware_utils::test_rng;
//! # type F = Scalar;
//! # type G = G1;
//! let setup = Setup {
//!     value_generator: G::generator(),
//!     blinding_generator: G::hash_to_group(
//!         b"_COMMONWARE_CRYPTOGRAPHY_ZK_PEDERSEN_TO_PLAIN",
//!         b"blinding",
//!     ),
//! };
//!
//! let witness = Witness {
//!     value: F::from(3u64),
//!     blinding: F::from(5u64),
//! };
//! let claim = witness.claim(&setup);
//!
//! let mut prover_rng = test_rng();
//! let mut prover_transcript = Transcript::new(b"pedersen-to-plain-example");
//! prover_transcript.commit(b"context".as_slice());
//! let proof = prove(
//!     &mut prover_rng,
//!     &mut prover_transcript,
//!     &setup,
//!     &claim,
//!     &witness,
//! );
//!
//! let mut verifier_rng = test_rng();
//! let mut verifier_transcript = Transcript::new(b"pedersen-to-plain-example");
//! verifier_transcript.commit(b"context".as_slice());
//! let [g, h] = Synthetic::<F, G>::generators_array();
//! let synthetic_setup = Setup {
//!     value_generator: g,
//!     blinding_generator: h,
//! };
//! let valid = verify(
//!     &mut verifier_rng,
//!     &mut verifier_transcript,
//!     &synthetic_setup,
//!     &claim,
//!     proof,
//! )
//! .eval(
//!     &[setup.value_generator, setup.blinding_generator],
//!     &Sequential,
//! ) == G::zero();
//! assert!(valid);
//! ```

use crate::transcript::Transcript;
use bytes::{Buf, BufMut};
use commonware_codec::{Encode, EncodeSize, Error, Read, Write};
use commonware_math::{
    algebra::{CryptoGroup, Field, Random, Space},
    synthetic::Synthetic,
};
use rand_core::CryptoRngCore;

/// Generators used by the proof system.
///
/// The blinding generator must not have a known discrete-log relationship
/// relative to the value generator.
#[derive(Clone, Debug, PartialEq)]
pub struct Setup<G> {
    /// The generator used in both the plain and Pedersen commitments.
    pub value_generator: G,
    /// The generator used only for the Pedersen blinding term.
    pub blinding_generator: G,
}

impl<G: Write> Write for Setup<G> {
    fn write(&self, buf: &mut impl BufMut) {
        self.value_generator.write(buf);
        self.blinding_generator.write(buf);
    }
}

impl<G: EncodeSize> EncodeSize for Setup<G> {
    fn encode_size(&self) -> usize {
        self.value_generator.encode_size() + self.blinding_generator.encode_size()
    }
}

impl<G: Read> Read for Setup<G>
where
    G::Cfg: Clone,
{
    type Cfg = G::Cfg;

    fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, Error> {
        Ok(Self {
            value_generator: G::read_cfg(buf, cfg)?,
            blinding_generator: G::read_cfg(buf, cfg)?,
        })
    }
}

#[cfg(any(test, feature = "arbitrary"))]
impl<G> arbitrary::Arbitrary<'_> for Setup<G>
where
    G: for<'a> arbitrary::Arbitrary<'a>,
{
    fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
        Ok(Self {
            value_generator: u.arbitrary()?,
            blinding_generator: u.arbitrary()?,
        })
    }
}

/// A prover-side witness for the relation.
#[derive(Clone, Debug, PartialEq)]
pub struct Witness<F> {
    pub value: F,
    pub blinding: F,
}

impl<F> Witness<F> {
    /// Create the public [`Claim`] corresponding to this witness.
    pub fn claim<G: Space<F>>(&self, setup: &Setup<G>) -> Claim<G> {
        let plain = setup.value_generator.clone() * &self.value;
        Claim {
            pedersen: plain.clone() + &(setup.blinding_generator.clone() * &self.blinding),
            plain,
        }
    }
}

/// The public statement for the protocol.
#[derive(Clone, Debug, PartialEq)]
pub struct Claim<G> {
    pub plain: G,
    pub pedersen: G,
}

impl<G: Write> Write for Claim<G> {
    fn write(&self, buf: &mut impl BufMut) {
        self.plain.write(buf);
        self.pedersen.write(buf);
    }
}

impl<G: EncodeSize> EncodeSize for Claim<G> {
    fn encode_size(&self) -> usize {
        self.plain.encode_size() + self.pedersen.encode_size()
    }
}

impl<G: Read> Read for Claim<G>
where
    G::Cfg: Clone,
{
    type Cfg = G::Cfg;

    fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, Error> {
        Ok(Self {
            plain: G::read_cfg(buf, cfg)?,
            pedersen: G::read_cfg(buf, cfg)?,
        })
    }
}

#[cfg(any(test, feature = "arbitrary"))]
impl<G> arbitrary::Arbitrary<'_> for Claim<G>
where
    G: for<'a> arbitrary::Arbitrary<'a>,
{
    fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
        Ok(Self {
            plain: u.arbitrary()?,
            pedersen: u.arbitrary()?,
        })
    }
}

/// A proof that the plain and Pedersen commitments share the same committed value.
#[derive(Clone, Debug, PartialEq)]
pub struct Proof<F, G> {
    plain_mask: G,
    pedersen_mask: G,
    value_response: F,
    blinding_response: F,
}

impl<F: Write, G: Write> Write for Proof<F, G> {
    fn write(&self, buf: &mut impl BufMut) {
        self.plain_mask.write(buf);
        self.pedersen_mask.write(buf);
        self.value_response.write(buf);
        self.blinding_response.write(buf);
    }
}

impl<F: EncodeSize, G: EncodeSize> EncodeSize for Proof<F, G> {
    fn encode_size(&self) -> usize {
        self.plain_mask.encode_size()
            + self.pedersen_mask.encode_size()
            + self.value_response.encode_size()
            + self.blinding_response.encode_size()
    }
}

impl<F: Read, G: Read> Read for Proof<F, G>
where
    G::Cfg: Clone,
    F::Cfg: Clone,
{
    type Cfg = (G::Cfg, F::Cfg);

    fn read_cfg(buf: &mut impl Buf, (g_cfg, f_cfg): &Self::Cfg) -> Result<Self, Error> {
        Ok(Self {
            plain_mask: G::read_cfg(buf, g_cfg)?,
            pedersen_mask: G::read_cfg(buf, g_cfg)?,
            value_response: F::read_cfg(buf, f_cfg)?,
            blinding_response: F::read_cfg(buf, f_cfg)?,
        })
    }
}

#[cfg(any(test, feature = "arbitrary"))]
impl<F, G> arbitrary::Arbitrary<'_> for Proof<F, G>
where
    F: for<'a> arbitrary::Arbitrary<'a>,
    G: for<'a> arbitrary::Arbitrary<'a>,
{
    fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
        Ok(Self {
            plain_mask: u.arbitrary()?,
            pedersen_mask: u.arbitrary()?,
            value_response: u.arbitrary()?,
            blinding_response: u.arbitrary()?,
        })
    }
}

/// Create a proof for a claimed witness.
///
/// This proves that the plain and Pedersen commitments in [`Claim`] open to
/// the same committed value, using the openings in [`Witness`].
///
/// This is a low-level constructor and assumes that `claim` and `witness`
/// correspond. It does not check that relationship for you.
pub fn prove<F: Field + Random, G: CryptoGroup<Scalar = F> + Encode>(
    rng: &mut impl CryptoRngCore,
    transcript: &mut Transcript,
    setup: &Setup<G>,
    claim: &Claim<G>,
    witness: &Witness<F>,
) -> Proof<F, G>
where
    Claim<G>: Encode,
{
    // We prove that the published commitments:
    //
    //   X = x G
    //   C = x G + x_blind H
    //
    // share the same committed value x. We do this with a single Schnorr-style
    // protocol over both equations. The prover samples masks k and k_blind,
    // sends:
    //
    //   K_plain = k G
    //   K_pedersen = k G + k_blind H
    //
    // derives a challenge e from the transcript, and responds with:
    //
    //   s = k + e x
    //   s_blind = k_blind + e x_blind
    //
    // The verifier can then check:
    //
    //   s G = K_plain + e X
    //   s G + s_blind H = K_pedersen + e C
    //
    // which is exactly what verify checks directly.
    transcript.commit(claim.encode());

    let value_mask = F::random(&mut *rng);
    let blinding_mask = F::random(&mut *rng);
    let plain_mask = setup.value_generator.clone() * &value_mask;
    let pedersen_mask = plain_mask.clone() + &(setup.blinding_generator.clone() * &blinding_mask);

    transcript.commit(plain_mask.encode());
    transcript.commit(pedersen_mask.encode());
    let challenge = F::random(transcript.noise(b"challenge"));

    Proof {
        plain_mask,
        pedersen_mask,
        value_response: value_mask + &(challenge.clone() * &witness.value),
        blinding_response: blinding_mask + &(challenge * &witness.blinding),
    }
}

/// Verify a [`Proof`] against a [`Claim`].
///
/// Returns `true` if the proof is valid for the current transcript state.
pub fn verify<F: Field + Random, G: CryptoGroup<Scalar = F> + Encode + PartialEq>(
    rng: &mut impl CryptoRngCore,
    transcript: &mut Transcript,
    setup: &Setup<Synthetic<F, G>>,
    claim: &Claim<G>,
    proof: Proof<F, G>,
) -> Synthetic<F, G>
where
    Claim<G>: Encode,
{
    let Proof {
        plain_mask,
        pedersen_mask,
        value_response,
        blinding_response,
    } = proof;

    transcript.commit(claim.encode());
    transcript.commit(plain_mask.encode());
    transcript.commit(pedersen_mask.encode());
    let challenge = F::random(transcript.noise(b"challenge"));

    let plain_valid = Synthetic::concrete([
        (F::one(), plain_mask),
        (challenge.clone(), claim.plain.clone()),
    ]) - &(setup.value_generator.clone() * &value_response);
    let pedersen_valid = Synthetic::concrete([
        (F::one(), pedersen_mask),
        (challenge, claim.pedersen.clone()),
    ]) - &(setup.value_generator.clone() * &value_response)
        - &(setup.blinding_generator.clone() * &blinding_response);
    pedersen_valid + &(plain_valid * &F::random(&mut *rng))
}

#[cfg(all(test, feature = "arbitrary"))]
mod conformance {
    use super::{Claim, Proof, Setup};
    use commonware_codec::conformance::CodecConformance;
    use commonware_math::test::{F as TestF, G as TestG};

    commonware_conformance::conformance_tests! {
        CodecConformance<Setup<TestG>>,
        CodecConformance<Claim<TestG>>,
        CodecConformance<Proof<TestF, TestG>>,
    }
}

#[commonware_macros::stability(ALPHA)]
#[cfg(any(test, feature = "fuzz"))]
pub mod fuzz {
    use super::*;
    use crate::bls12381::primitives::group::{Scalar as F, G1 as G};
    use arbitrary::{Arbitrary, Unstructured};
    use commonware_math::algebra::{Additive, CryptoGroup, HashToGroup};
    use commonware_parallel::Sequential;
    use commonware_utils::test_rng;
    use std::sync::OnceLock;

    const NAMESPACE: &[u8] = b"_COMMONWARE_CRYPTOGRAPHY_ZK_PEDERSEN_TO_PLAIN";
    const BAD_NAMESPACE: &[u8] = b"_COMMONWARE_CRYPTOGRAPHY_ZK_PEDERSEN_TO_PLAIN_BUT_DIFFERENT";

    pub(super) fn test_setup() -> &'static Setup<G> {
        static TEST_SETUP: OnceLock<Setup<G>> = OnceLock::new();
        TEST_SETUP.get_or_init(|| Setup {
            value_generator: G::generator(),
            blinding_generator: G::hash_to_group(NAMESPACE, b"blinding generator"),
        })
    }

    struct Prover<'a> {
        setup: &'a Setup<G>,
        claim: Claim<G>,
        proof: Proof<F, G>,
        bad_namespace: bool,
        honest: bool,
    }

    impl<'a> Prover<'a> {
        fn new(setup: &'a Setup<G>, value: F, blinding: F) -> Self {
            let witness = Witness { value, blinding };
            let claim = witness.claim(setup);
            let proof = prove(
                &mut test_rng(),
                &mut Transcript::new(NAMESPACE),
                setup,
                &claim,
                &witness,
            );
            Self {
                setup,
                claim,
                proof,
                bad_namespace: false,
                honest: true,
            }
        }

        #[allow(clippy::missing_const_for_fn)]
        fn bad_namespace(&mut self) {
            self.honest = false;
            self.bad_namespace = true;
        }

        fn tweak_plain_claim(&mut self, delta: F) {
            if delta == F::zero() {
                return;
            }
            self.honest = false;
            self.claim.plain += &(self.setup.value_generator * &delta);
        }

        fn tweak_pedersen_claim(&mut self, value_delta: F, blinding_delta: F) {
            if value_delta == F::zero() && blinding_delta == F::zero() {
                return;
            }
            self.honest = false;
            self.claim.pedersen += &((self.setup.value_generator * &value_delta)
                + &(self.setup.blinding_generator * &blinding_delta));
        }

        fn tweak_mask(&mut self, tweak_plain: bool, delta: G) {
            if delta == G::zero() {
                return;
            }
            self.honest = false;
            if tweak_plain {
                self.proof.plain_mask += &delta;
            } else {
                self.proof.pedersen_mask += &delta;
            }
        }

        fn tweak_response(&mut self, tweak_value: bool, delta: F) {
            if delta == F::zero() {
                return;
            }
            self.honest = false;
            if tweak_value {
                self.proof.value_response += &delta;
            } else {
                self.proof.blinding_response += &delta;
            }
        }

        #[allow(clippy::missing_const_for_fn)]
        fn honest(&self) -> bool {
            self.honest
        }

        fn verify(self, rng: &mut impl CryptoRngCore) -> bool {
            let ns = if self.bad_namespace {
                BAD_NAMESPACE
            } else {
                NAMESPACE
            };
            let [g, h] = Synthetic::generators_array();
            verify(
                rng,
                &mut Transcript::new(ns),
                &Setup {
                    value_generator: g,
                    blinding_generator: h,
                },
                &self.claim,
                self.proof,
            )
            .eval(
                &[self.setup.value_generator, self.setup.blinding_generator],
                &Sequential,
            ) == G::zero()
        }
    }

    #[derive(Debug)]
    pub struct Plan {
        value: F,
        blinding: F,
    }

    impl<'a> Arbitrary<'a> for Plan {
        fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
            Ok(Self {
                value: u.arbitrary()?,
                blinding: u.arbitrary()?,
            })
        }
    }

    impl Plan {
        pub fn run(self, u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
            let setup = test_setup();
            let mut prover = Prover::new(setup, self.value, self.blinding);
            if u.arbitrary::<bool>()? {
                match u.arbitrary::<u8>()? {
                    x if x < 51 => prover.tweak_plain_claim(u.arbitrary()?),
                    x if x < 102 => prover.tweak_pedersen_claim(u.arbitrary()?, u.arbitrary()?),
                    x if x < 153 => prover.tweak_mask(u.arbitrary()?, u.arbitrary()?),
                    x if x < 204 => prover.tweak_response(u.arbitrary()?, u.arbitrary()?),
                    _ => prover.bad_namespace(),
                }
            }
            match (prover.honest(), prover.verify(&mut test_rng())) {
                (true, true) | (false, false) => {}
                (true, false) => panic!("prover honest, but proof didn't verify"),
                (false, true) => panic!("prover malicious, but proof verifies"),
            }
            Ok(())
        }
    }

    #[test]
    fn prover_tweaks_cover_noops_and_failures() {
        let setup = test_setup();

        let mut honest = Prover::new(setup, F::from(3u64), F::from(5u64));
        honest.tweak_plain_claim(F::zero());
        honest.tweak_pedersen_claim(F::zero(), F::zero());
        honest.tweak_mask(true, G::zero());
        honest.tweak_response(false, F::zero());
        assert!(honest.honest());
        assert!(honest.verify(&mut test_rng()));

        type Tweak = Box<dyn FnOnce(&mut Prover<'static>)>;
        let failures: [Tweak; 5] = [
            Box::new(|p| p.tweak_plain_claim(F::from(1u64))),
            Box::new(|p| p.tweak_pedersen_claim(F::from(1u64), F::from(1u64))),
            Box::new(|p| p.tweak_mask(false, G::generator())),
            Box::new(|p| p.tweak_response(true, F::from(1u64))),
            Box::new(|p| p.bad_namespace()),
        ];
        for tweak in failures {
            let mut prover = Prover::new(setup, F::from(3u64), F::from(5u64));
            tweak(&mut prover);
            assert!(!prover.honest());
            assert!(!prover.verify(&mut test_rng()));
        }
    }
}

#[cfg(test)]
mod test {
    use super::{fuzz, Claim, Proof, Setup};
    use commonware_codec::{Decode, Encode};
    use commonware_invariants::minifuzz;
    use commonware_math::test::{F, G};

    fn assert_setup_roundtrip(setup: &Setup<G>) {
        let encoded = setup.encode();
        let decoded: Setup<G> =
            Setup::decode_cfg(encoded.clone(), &()).expect("setup should decode with unit cfg");
        assert_eq!(setup, &decoded);
        assert_eq!(decoded.encode(), encoded);
    }

    fn assert_claim_roundtrip(claim: &Claim<G>) {
        let encoded = claim.encode();
        let decoded: Claim<G> =
            Claim::decode_cfg(encoded.clone(), &()).expect("claim should decode with unit cfg");
        assert_eq!(claim, &decoded);
        assert_eq!(decoded.encode(), encoded);
    }

    fn assert_proof_roundtrip(proof: &Proof<F, G>) {
        let encoded = proof.encode();
        let decoded: Proof<F, G> = Proof::decode_cfg(encoded.clone(), &((), ()))
            .expect("proof should decode with unit cfg");
        assert_eq!(proof, &decoded);
        assert_eq!(decoded.encode(), encoded);
    }

    #[test]
    fn test_codec_roundtrip() {
        minifuzz::test(|u| {
            assert_setup_roundtrip(&u.arbitrary::<Setup<G>>()?);
            assert_claim_roundtrip(&u.arbitrary::<Claim<G>>()?);
            assert_proof_roundtrip(&u.arbitrary::<Proof<F, G>>()?);
            Ok(())
        });
    }

    #[test]
    fn test_fuzz() {
        minifuzz::test(|u| {
            u.arbitrary::<fuzz::Plan>()?.run(u)?;
            Ok(())
        });
    }
}