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
use bls12_381 as bls;

use crate::bls_extensions::*;
use crate::parameters::*;

pub type EncryptedValue = (bls::G1Projective, bls::G1Projective);

pub struct ElGamalPrivateKey<'a, R: RngInstance> {
    pub params: &'a Parameters<R>,
    pub private_key: bls::Scalar,
}

pub struct ElGamalPublicKey<'a, R: RngInstance> {
    pub params: &'a Parameters<R>,
    pub public_key: bls::G1Projective,
}

impl<'a, R: RngInstance> ElGamalPrivateKey<'a, R> {
    pub fn new(params: &'a Parameters<R>) -> Self {
        Self {
            params: params,
            private_key: params.random_scalar(),
        }
    }

    pub fn to_public(&self) -> ElGamalPublicKey<'a, R> {
        ElGamalPublicKey {
            params: self.params,
            public_key: self.params.g1 * self.private_key,
        }
    }

    pub fn decrypt(&self, ciphertext: &EncryptedValue) -> bls::G1Projective {
        let (a, b) = ciphertext;
        b - a * self.private_key
    }
}

impl<'a, R: RngInstance> ElGamalPublicKey<'a, R> {
    pub fn encrypt(
        &self,
        attribute: &bls::Scalar,
        attribute_key: &bls::Scalar,
        shared_value: &bls::G1Projective,
    ) -> EncryptedValue {
        (
            self.params.g1 * attribute_key,
            self.public_key * attribute_key + shared_value * attribute,
        )
    }
}