Skip to content

Commit

Permalink
Cleanup API 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JobDoesburg committed Oct 31, 2024
1 parent 454af8f commit 3f28e08
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 66 deletions.
8 changes: 4 additions & 4 deletions src/distributed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::de::{Error, Visitor};

/// GLOBAL KEY BLINDING
#[derive(Copy, Clone, Debug, From)]
pub struct BlindingFactor(ScalarNonZero);
pub struct BlindingFactor(pub(crate) ScalarNonZero);
impl BlindingFactor {
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let scalar = ScalarNonZero::random(rng);
Expand Down Expand Up @@ -40,7 +40,7 @@ impl BlindingFactor {
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct BlindedGlobalSecretKey(ScalarNonZero);
pub struct BlindedGlobalSecretKey(pub(crate) ScalarNonZero);
impl Serialize for BlindedGlobalSecretKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn make_blinded_global_secret_key(
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct SessionKeyShare(ScalarNonZero);
pub struct SessionKeyShare(pub(crate) ScalarNonZero);
impl Serialize for SessionKeyShare {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -147,7 +147,7 @@ impl PEPSystem {
}
pub fn session_key_share(&self, context: &EncryptionContext) -> SessionKeyShare {
let k = make_rekey_factor(&self.rekeying_secret, &context);
make_session_key_share(&k, &self.blinding_factor)
make_session_key_share(&k.0, &self.blinding_factor)
}
pub fn rekey_info(
&self,
Expand Down
40 changes: 20 additions & 20 deletions src/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};

/// GLOBAL KEYS
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From, Serialize, Deserialize)]
pub struct GlobalPublicKey(pub GroupElement);
#[derive(Copy, Clone, Debug, From)]
pub struct GlobalSecretKey(pub(crate) ScalarNonZero);
Expand Down Expand Up @@ -38,8 +38,8 @@ impl EncryptionSecret {
}

/// SESSION KEYS
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct SessionPublicKey(GroupElement);
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From, Serialize, Deserialize)]
pub struct SessionPublicKey(pub GroupElement);
#[derive(Copy, Clone, Debug, From)]
pub struct SessionSecretKey(pub(crate) ScalarNonZero);
/// Generate a subkey from a global secret key, a context, and an encryption secret
Expand All @@ -49,19 +49,19 @@ pub fn make_session_keys(
encryption_secret: &EncryptionSecret,
) -> (SessionPublicKey, SessionSecretKey) {
let k = make_rekey_factor(encryption_secret, context);
let sk = *k * global.0;
let sk = k.0 * global.0;
let pk = sk * G;
(SessionPublicKey(pk), SessionSecretKey(sk))
}

/// PSEUDONYMS AND DATA
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct Pseudonym {
value: GroupElement,
pub(crate) value: GroupElement,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct DataPoint {
value: GroupElement,
pub(crate) value: GroupElement,
}
impl Pseudonym {
pub fn from_point(value: GroupElement) -> Self {
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn decrypt_data(data: &EncryptedDataPoint, sk: &SessionSecretKey) -> DataPoi
DataPoint::from_point(decrypt(&data, &sk.0))
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
pub struct RerandomizeFactor(ScalarNonZero);
#[cfg(not(feature = "elgamal2"))]
/// Rerandomize the ciphertext of an encrypted pseudonym
Expand Down Expand Up @@ -247,10 +247,10 @@ pub enum AudienceType {
Unknown = 0x00,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct ReshuffleFactor(ScalarNonZero);
#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)]
pub struct RekeyFactor(ScalarNonZero);
#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
pub struct ReshuffleFactor(pub(crate) ScalarNonZero);
#[derive(Copy, Clone, Eq, PartialEq, Debug, From)]
pub struct RekeyFactor(pub(crate) ScalarNonZero);

#[derive(Eq, PartialEq, Clone, Copy, Debug, From)]
pub struct Reshuffle2Factors {
Expand Down Expand Up @@ -344,10 +344,10 @@ pub fn pseudonymize(
) -> EncryptedPseudonym {
EncryptedPseudonym::from(rsk2(
&p.value,
&pseudonymization_info.s.from,
&pseudonymization_info.s.to,
&pseudonymization_info.k.from,
&pseudonymization_info.k.to,
&pseudonymization_info.s.from.0,
&pseudonymization_info.s.to.0,
&pseudonymization_info.k.from.0,
&pseudonymization_info.k.to.0,
))
}

Expand All @@ -359,20 +359,20 @@ pub fn pseudonymize_from_global(
) -> EncryptedPseudonym {
EncryptedPseudonym::from(rsk2(
&p.value,
&reshuffle_factors.from,
&reshuffle_factors.to,
&reshuffle_factors.from.0,
&reshuffle_factors.to.0,
&ScalarNonZero::one(),
&rekey_to,
&rekey_to.0,
))
}


/// Rekey an encrypted data point, encrypted with one session key, to be decrypted by another session key
pub fn rekey(p: &EncryptedDataPoint, rekey_info: &RekeyInfo) -> EncryptedDataPoint {
EncryptedDataPoint::from(rekey2(&p.value, &rekey_info.from, &rekey_info.to))
EncryptedDataPoint::from(rekey2(&p.value, &rekey_info.from.0, &rekey_info.to.0))
}

/// Rekey an encrypted data point, encrypted for a global key, to be decrypted by a session key
pub fn rekey_from_global(p: &EncryptedDataPoint, rekey_to: RekeyFactor) -> EncryptedDataPoint {
EncryptedDataPoint::from(crate::primitives::rekey(&p.value, &rekey_to))
EncryptedDataPoint::from(crate::primitives::rekey(&p.value, &rekey_to.0))
}
28 changes: 14 additions & 14 deletions src/high_level_proved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl PseudonymizationContextVerifiers {
rng: &mut R,
) -> (Self, PseudonymizationFactorVerifiersProof) {
let factor = make_pseudonymisation_factor(secret, context);
let (verifiers, proof) = PseudonymizationFactorVerifiers::new(&*factor, rng);
let (verifiers, proof) = PseudonymizationFactorVerifiers::new(&factor.0, rng);
(PseudonymizationContextVerifiers(verifiers), proof)
}
}
Expand All @@ -31,7 +31,7 @@ impl EncryptionContextVerifiers {
rng: &mut R,
) -> (Self, RekeyFactorVerifiersProof) {
let factor = make_rekey_factor(secret, context);
let (verifiers, proof) = RekeyFactorVerifiers::new(&*factor, rng);
let (verifiers, proof) = RekeyFactorVerifiers::new(&factor.0, rng);
(EncryptionContextVerifiers(verifiers), proof)
}
}
Expand All @@ -46,13 +46,13 @@ pub struct PseudonymizationInfoProof {
pub struct RekeyInfoProof(pub Rekey2FactorsProof);
impl PseudonymizationInfoProof {
pub fn new<R: RngCore + CryptoRng>(factors: &PseudonymizationInfo, rng: &mut R) -> Self {
let reshuffle_proof = Reshuffle2FactorsProof::new(&factors.s.from, &factors.s.to, rng);
let rekey_proof = Rekey2FactorsProof::new(&factors.k.from, &factors.k.to, rng);
let reshuffle_proof = Reshuffle2FactorsProof::new(&factors.s.from.0, &factors.s.to.0, rng);
let rekey_proof = Rekey2FactorsProof::new(&factors.k.from.0, &factors.k.to.0, rng);
let rsk_proof = RSK2FactorsProof::new(
&factors.s.from,
&factors.s.to,
&factors.k.from,
&factors.k.to,
&factors.s.from.0,
&factors.s.to.0,
&factors.k.from.0,
&factors.k.to.0,
rng,
);
PseudonymizationInfoProof {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl PseudonymizationInfoProof {
}
impl RekeyInfoProof {
pub fn new<R: RngCore + CryptoRng>(factors: &RekeyInfo, rng: &mut R) -> Self {
let rekey_proof = Rekey2FactorsProof::new(&factors.from, &factors.to, rng);
let rekey_proof = Rekey2FactorsProof::new(&factors.from.0, &factors.to.0, rng);
RekeyInfoProof(rekey_proof)
}
#[must_use]
Expand Down Expand Up @@ -141,10 +141,10 @@ pub fn proved_pseudonymize<R: RngCore + CryptoRng>(
) -> ProvedEncryptedPseudonym {
ProvedEncryptedPseudonym::new(prove_rsk2(
&p,
&pseudonymization_info.s.from,
&pseudonymization_info.s.to,
&pseudonymization_info.k.from,
&pseudonymization_info.k.to,
&pseudonymization_info.s.from.0,
&pseudonymization_info.s.to.0,
&pseudonymization_info.k.from.0,
&pseudonymization_info.k.to.0,
rng,
))
}
Expand All @@ -155,7 +155,7 @@ pub fn proved_rekey<R: RngCore + CryptoRng>(
rekey_info: &RekeyInfo,
rng: &mut R,
) -> ProvedEncryptedDataPoint {
ProvedEncryptedDataPoint::new(prove_rekey2(&p, &rekey_info.from, &rekey_info.to, rng))
ProvedEncryptedDataPoint::new(prove_rekey2(&p, &rekey_info.from.0, &rekey_info.to.0, rng))
}

#[must_use]
Expand Down
4 changes: 2 additions & 2 deletions src/wasm/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ impl WASMGroupElement {
}
#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.encode_to_hex()
self.0.encode_hex()
}

#[wasm_bindgen(js_name = toBase64)]
pub fn to_base_64(&self) -> String {
self.0.encode_to_base64()
self.0.encode_base64()
}
#[wasm_bindgen(js_name = fromBase64)]
pub fn from_base_64(s: &str) -> Option<WASMGroupElement> {
Expand Down
21 changes: 10 additions & 11 deletions src/wasm/distributed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ pub fn wasm_make_blinded_global_secret_key(
make_blinded_global_secret_key(
&GlobalSecretKey::from(ScalarNonZero::from(global_secret_key.0)),
&bs,
)
.0,
).unwrap().0,
))
}

Expand All @@ -93,25 +92,25 @@ impl WASMPEPSystem {
blinding_factor: &WASMBlindingFactor,
) -> Self {
Self(PEPSystem::new(
PseudonymizationSecret(pseudonymisation_secret.to_string()),
EncryptionSecret(rekeying_secret.to_string()),
PseudonymizationSecret::from(pseudonymisation_secret.as_bytes().into()),
EncryptionSecret::from(rekeying_secret.as_bytes().into()),
BlindingFactor::from(ScalarNonZero::from(blinding_factor.0)),
))
}

#[wasm_bindgen(js_name = sessionKeyShare)]
pub fn wasm_session_key_share(&self, context: &str) -> WASMSessionKeyShare {
WASMSessionKeyShare::from(WASMScalarNonZero::from(
self.session_key_share(&EncryptionContext(context.to_string()))
self.session_key_share(&EncryptionContext::from(context.to_string()))
.0,
))
}

#[wasm_bindgen(js_name = rekeyInfo)]
pub fn wasm_rekey_info(&self, from_enc: &str, to_enc: &str) -> WASMRekeyInfo {
WASMRekeyInfo::from(self.rekey_info(
&EncryptionContext(from_enc.to_string()),
&EncryptionContext(to_enc.to_string()),
&EncryptionContext::from(from_enc.to_string()),
&EncryptionContext::from(to_enc.to_string()),
))
}

Expand All @@ -124,10 +123,10 @@ impl WASMPEPSystem {
to_enc: &str,
) -> WASMPseudonymizationInfo {
WASMPseudonymizationInfo::from(self.pseudonymization_info(
&PseudonymizationContext(from_pseudo.to_string()),
&PseudonymizationContext(to_pseudo.to_string()),
&EncryptionContext(from_enc.to_string()),
&EncryptionContext(to_enc.to_string()),
&PseudonymizationContext::from(from_pseudo.to_string()),
&PseudonymizationContext::from(to_pseudo.to_string()),
&EncryptionContext::from(from_enc.to_string()),
&EncryptionContext::from(to_enc.to_string()),
))
}

Expand Down
28 changes: 13 additions & 15 deletions src/wasm/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ pub fn wasm_make_session_keys(
) -> WASMSessionKeyPair {
let (public, secret) = make_session_keys(
&GlobalSecretKey(***global),
&EncryptionContext(context.to_string()),
&EncryptionSecret(encryption_secret.to_string()),
&EncryptionContext::from(context.to_string()),
&EncryptionSecret::from(encryption_secret.into()),
);
WASMSessionKeyPair {
public: WASMSessionPublicKey::from(WASMGroupElement::from(public.0)),
Expand Down Expand Up @@ -153,8 +153,7 @@ pub fn wasm_decrypt_pseudonym(
decrypt_pseudonym(
&EncryptedPseudonym::from(ElGamal::from(p.value)),
&SessionSecretKey::from(ScalarNonZero::from(sk.0)),
)
.value,
).value,
))
}

Expand Down Expand Up @@ -185,8 +184,7 @@ pub fn wasm_decrypt_data(
decrypt_data(
&EncryptedDataPoint::from(ElGamal::from(data.value)),
&SessionSecretKey::from(ScalarNonZero::from(sk.0)),
)
.value,
).value,
))
}

Expand Down Expand Up @@ -287,12 +285,12 @@ impl WASMPseudonymizationInfo {
encryption_secret: &str,
) -> Self {
let x = PseudonymizationInfo::new(
&PseudonymizationContext(from_pseudo_context.to_string()),
&PseudonymizationContext(to_pseudo_context.to_string()),
&EncryptionContext(from_enc_context.to_string()),
&EncryptionContext(to_enc_context.to_string()),
&PseudonymizationSecret(pseudonymization_secret.to_string()),
&EncryptionSecret(encryption_secret.to_string()),
&PseudonymizationContext::from(from_pseudo_context.to_string()),
&PseudonymizationContext::from(to_pseudo_context.to_string()),
&EncryptionContext::from(from_enc_context.to_string()),
&EncryptionContext::from(to_enc_context.to_string()),
&PseudonymizationSecret::from(pseudonymization_secret.as_bytes().to_vec()),
&EncryptionSecret::from(encryption_secret.as_bytes().to_vec()),
);
let k = WASMRekey2Factors {
from: WASMRekeyFactor(WASMScalarNonZero::from(x.k.from.0)),
Expand All @@ -319,9 +317,9 @@ impl WASMRekeyInfo {
#[wasm_bindgen(constructor)]
pub fn new(from_enc_context: &str, to_enc_context: &str, encryption_secret: &str) -> Self {
let x = RekeyInfo::new(
&EncryptionContext(from_enc_context.to_string()),
&EncryptionContext(to_enc_context.to_string()),
&EncryptionSecret(encryption_secret.to_string()),
&EncryptionContext::from(from_enc_context.to_string()),
&EncryptionContext::from(to_enc_context.to_string()),
&EncryptionSecret::from(encryption_secret.as_bytes().into()),
);
let k = WASMRekey2Factors {
from: WASMRekeyFactor(WASMScalarNonZero::from(x.from.0)),
Expand Down

0 comments on commit 3f28e08

Please sign in to comment.