Skip to main content

libpep/factors/
types.rs

1//! Cryptographic factor types for rerandomization, reshuffling, and rekeying operations.
2
3use crate::arithmetic::scalars::ScalarNonZero;
4use crate::factors::EncryptionContext;
5use derive_more::From;
6
7/// High-level type for the factor used to [`rerandomize`](crate::core::primitives::rerandomize) an [ElGamal](crate::core::elgamal::ElGamal) ciphertext.
8#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
9pub struct RerandomizeFactor(pub(crate) ScalarNonZero);
10
11/// High-level type for the factor used to [`reshuffle`](crate::core::primitives::reshuffle) an [ElGamal](crate::core::elgamal::ElGamal) ciphertext.
12#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
13pub struct ReshuffleFactor(pub ScalarNonZero);
14
15/// Trait for rekey factors that can be extracted to a scalar.
16pub trait RekeyFactor {
17    fn scalar(&self) -> ScalarNonZero;
18}
19
20/// High-level type for the factor used to [`rekey`](crate::core::primitives::rekey) an [ElGamal](crate::core::elgamal::ElGamal) ciphertext for pseudonyms.
21#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
22pub struct PseudonymRekeyFactor(pub(crate) ScalarNonZero);
23
24impl RekeyFactor for PseudonymRekeyFactor {
25    fn scalar(&self) -> ScalarNonZero {
26        self.0
27    }
28}
29
30/// High-level type for the factor used to [`rekey`](crate::core::primitives::rekey) an [ElGamal](crate::core::elgamal::ElGamal) ciphertext for attributes.
31#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
32pub struct AttributeRekeyFactor(pub(crate) ScalarNonZero);
33
34impl RekeyFactor for AttributeRekeyFactor {
35    fn scalar(&self) -> ScalarNonZero {
36        self.0
37    }
38}
39
40/// High-level type for the factors used to [`rsk`](crate::core::primitives::rsk) an [ElGamal](crate::core::elgamal::ElGamal) ciphertext for pseudonyms.
41/// Contains both the reshuffle factor (`s`) and the rekey factor (`k`).
42#[derive(Eq, PartialEq, Clone, Copy, Debug, From)]
43pub struct PseudonymRSKFactors {
44    /// Reshuffle factor - transforms pseudonyms between different domains
45    pub s: ReshuffleFactor,
46    /// Rekey factor - transforms pseudonyms between different sessions
47    pub k: PseudonymRekeyFactor,
48}
49
50/// The information required to perform n-PEP pseudonymization from one domain and session to another.
51/// The pseudonymization info consists of a reshuffle and rekey factor.
52/// For efficiency, we do not actually use the [`rsk2`](crate::core::primitives::rsk2) operation, but instead use the regular [`rsk`](crate::core::primitives::rsk) operation
53/// with precomputed reshuffle and rekey factors, which is equivalent but more efficient.
54pub type PseudonymizationInfo = PseudonymRSKFactors;
55
56/// The information required to perform n-PEP rekeying of pseudonyms from one session to another.
57/// For efficiency, we do not actually use the [`rekey2`](crate::core::primitives::rekey2) operation, but instead use the regular [`rekey`](crate::core::primitives::rekey) operation
58/// with a precomputed rekey factor, which is equivalent but more efficient.
59pub type PseudonymRekeyInfo = PseudonymRekeyFactor;
60
61/// The information required to perform n-PEP rekeying of attributes from one session to another.
62/// For efficiency, we do not actually use the [`rekey2`](crate::core::primitives::rekey2) operation, but instead use the regular [`rekey`](crate::core::primitives::rekey) operation
63/// with a precomputed rekey factor, which is equivalent but more efficient.
64pub type AttributeRekeyInfo = AttributeRekeyFactor;
65
66impl From<PseudonymizationInfo> for PseudonymRekeyInfo {
67    fn from(x: PseudonymizationInfo) -> Self {
68        x.k
69    }
70}
71
72/// The information required for transcryption, containing both pseudonymization info and attribute rekey info.
73#[derive(Eq, PartialEq, Clone, Copy, Debug)]
74pub struct TranscryptionInfo {
75    pub pseudonym: PseudonymizationInfo,
76    pub attribute: AttributeRekeyInfo,
77}
78
79impl TranscryptionInfo {
80    /// Compute the transcryption info given pseudonymization domains, sessions and secrets.
81    pub fn new(
82        domain_from: &crate::factors::contexts::PseudonymizationDomain,
83        domain_to: &crate::factors::contexts::PseudonymizationDomain,
84        session_from: &crate::factors::contexts::EncryptionContext,
85        session_to: &crate::factors::contexts::EncryptionContext,
86        pseudonymization_secret: &crate::factors::PseudonymizationSecret,
87        encryption_secret: &crate::factors::EncryptionSecret,
88    ) -> Self {
89        Self {
90            pseudonym: PseudonymizationInfo::new(
91                domain_from,
92                domain_to,
93                session_from,
94                session_to,
95                pseudonymization_secret,
96                encryption_secret,
97            ),
98            attribute: AttributeRekeyInfo::new(session_from, session_to, encryption_secret),
99        }
100    }
101
102    /// Reverse the transcryption info (i.e., switch the direction of the transcryption).
103    pub fn reverse(&self) -> Self {
104        Self {
105            pseudonym: self.pseudonym.reverse(),
106            attribute: self.attribute.reverse(),
107        }
108    }
109}
110
111/// A trait for types that can provide rekey information for a specific rekey info type.
112///
113/// This trait is parameterized by the rekey info type, allowing different implementations
114/// for different encrypted types (e.g., `AttributeRekeyInfo` vs `PseudonymRekeyInfo`).
115///
116/// # Examples
117///
118/// ```rust,ignore
119/// // Transcryptor implements RekeyInfoProvider for both types
120/// let attr_info: AttributeRekeyInfo = transcryptor.rekey_info(&from_ctx, &to_ctx);
121/// let pseudo_info: PseudonymRekeyInfo = transcryptor.rekey_info(&from_ctx, &to_ctx);
122/// ```
123pub trait RekeyInfoProvider<Info> {
124    /// Get the rekey information for transcryption between encryption contexts.
125    fn rekey_info(&self, session_from: &EncryptionContext, session_to: &EncryptionContext) -> Info;
126}