Skip to main content

group_threshold_cryptography_pre_release/
context.rs

1use std::ops::Mul;
2
3use ark_ec::{pairing::Pairing, CurveGroup};
4
5use crate::{
6    prepare_combine_simple, BlindedKeyShare, Ciphertext, CiphertextHeader,
7    DecryptionShareFast, DecryptionSharePrecomputed, DecryptionShareSimple,
8    PrivateKeyShare, PublicKeyShare, Result,
9};
10
11#[derive(Clone, Debug)]
12pub struct PublicDecryptionContextFast<E: Pairing> {
13    pub domain: E::ScalarField,
14    pub public_key_share: PublicKeyShare<E>,
15    pub blinded_key_share: BlindedKeyShare<E>,
16    // This decrypter's contribution to N(0), namely (-1)^|domain| * \prod_i omega_i
17    pub lagrange_n_0: E::ScalarField,
18    pub h_inv: E::G2Prepared,
19}
20
21#[derive(Clone, Debug)]
22pub struct PublicDecryptionContextSimple<E: Pairing> {
23    pub domain: E::ScalarField,
24    pub public_key_share: PublicKeyShare<E>,
25    pub blinded_key_share: BlindedKeyShare<E>,
26    pub h: E::G2Affine,
27    pub validator_public_key: E::G2,
28}
29
30#[derive(Clone, Debug)]
31pub struct SetupParams<E: Pairing> {
32    pub b: E::ScalarField,
33    pub b_inv: E::ScalarField,
34    pub g: E::G1Affine,
35    pub g_inv: E::G1Prepared,
36    pub h_inv: E::G2Prepared,
37    pub h: E::G2Affine,
38}
39
40#[derive(Clone, Debug)]
41pub struct PrivateDecryptionContextFast<E: Pairing> {
42    pub index: usize,
43    pub setup_params: SetupParams<E>,
44    pub private_key_share: PrivateKeyShare<E>,
45    pub public_decryption_contexts: Vec<PublicDecryptionContextFast<E>>,
46}
47
48impl<E: Pairing> PrivateDecryptionContextFast<E> {
49    pub fn create_share(
50        &self,
51        ciphertext: &Ciphertext<E>,
52        aad: &[u8],
53    ) -> Result<DecryptionShareFast<E>> {
54        ciphertext.check(aad, &self.setup_params.g_inv)?;
55
56        let decryption_share = ciphertext
57            .commitment
58            .mul(self.setup_params.b_inv)
59            .into_affine();
60
61        Ok(DecryptionShareFast {
62            decrypter_index: self.index,
63            decryption_share,
64        })
65    }
66}
67
68#[derive(Clone, Debug)]
69pub struct PrivateDecryptionContextSimple<E: Pairing> {
70    pub index: usize,
71    pub setup_params: SetupParams<E>,
72    pub private_key_share: PrivateKeyShare<E>,
73    pub public_decryption_contexts: Vec<PublicDecryptionContextSimple<E>>,
74    // TODO: Remove/replace with `setup_params.b` after refactoring
75    pub validator_private_key: E::ScalarField,
76}
77
78impl<E: Pairing> PrivateDecryptionContextSimple<E> {
79    pub fn create_share(
80        &self,
81        ciphertext_header: &CiphertextHeader<E>,
82        aad: &[u8],
83    ) -> Result<DecryptionShareSimple<E>> {
84        DecryptionShareSimple::create(
85            &self.validator_private_key,
86            &self.private_key_share,
87            ciphertext_header,
88            aad,
89            &self.setup_params.g_inv,
90        )
91    }
92
93    pub fn create_share_precomputed(
94        &self,
95        ciphertext_header: &CiphertextHeader<E>,
96        aad: &[u8],
97    ) -> Result<DecryptionSharePrecomputed<E>> {
98        let domain = self
99            .public_decryption_contexts
100            .iter()
101            .map(|c| c.domain)
102            .collect::<Vec<_>>();
103        let lagrange_coeffs = prepare_combine_simple::<E>(&domain);
104
105        DecryptionSharePrecomputed::new(
106            self.index,
107            &self.validator_private_key,
108            &self.private_key_share,
109            ciphertext_header,
110            aad,
111            &lagrange_coeffs[self.index],
112            &self.setup_params.g_inv,
113        )
114    }
115}