Skip to content

Commit

Permalink
Batch
Browse files Browse the repository at this point in the history
  • Loading branch information
JobDoesburg committed Nov 7, 2024
1 parent 0e6ae56 commit c845a52
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/distributed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ impl PEPSystem {
pseudonymize(p, pseudonymization_info)
}

pub fn rekey_batch<R: RngCore + CryptoRng>(&self, encrypted: &[EncryptedDataPoint], rekey_info: &RekeyInfo, rng: &mut R) -> Box<[EncryptedDataPoint]> {
rekey_batch(encrypted, rekey_info, rng)
}

pub fn pseudonymize_batch<R: RngCore + CryptoRng>(&self, encrypted: &[EncryptedPseudonym], pseudonymization_info: &PseudonymizationInfo, rng: &mut R) -> Box<[EncryptedPseudonym]> {
pseudonymize_batch(encrypted, pseudonymization_info, rng)
}

pub fn transcrypt<E: Encrypted>(
&self,
encrypted: &E,
Expand Down
23 changes: 22 additions & 1 deletion src/high_level/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ pub fn rekey(p: &EncryptedDataPoint, rekey_info: &RekeyInfo) -> EncryptedDataPoi
EncryptedDataPoint::from(rekey2(&p.value, &rekey_info.from.0, &rekey_info.to.0))
}

pub fn pseudonymize_batch<R: RngCore + CryptoRng>(
encrypted: &[EncryptedPseudonym],
pseudonymization_info: &PseudonymizationInfo,
rng: &mut R,
) -> Box<[EncryptedPseudonym]> {
let mut encrypted_copy = encrypted.to_vec();
encrypted_copy.shuffle(rng);
encrypted_copy.iter()
.map(|x| pseudonymize(x, pseudonymization_info))
.collect()
}
pub fn rekey_batch<R: RngCore + CryptoRng>(encrypted: &[EncryptedDataPoint], rekey_info: &RekeyInfo, rng: &mut R) -> Box<[EncryptedDataPoint]> {
let mut encrypted_copy = encrypted.to_vec();
encrypted_copy.shuffle(rng);
encrypted_copy.iter()
.map(|x| rekey(x, rekey_info))
.collect()
}

pub fn transcrypt<E: Encrypted>(encrypted: &E, transcryption_info: &TranscryptionInfo) -> E {
if E::IS_PSEUDONYM {
E::from_value(rsk2(
Expand All @@ -109,4 +128,6 @@ pub fn transcrypt<E: Encrypted>(encrypted: &E, transcryption_info: &Transcryptio
} else {
E::from_value(rekey2(&encrypted.value(), &transcryption_info.k.from.0, &transcryption_info.k.to.0))
}
}
}

// TODO: batch transcrypt
26 changes: 11 additions & 15 deletions src/tests/high_level.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use crate::arithmetic::GroupElement;
use crate::high_level::contexts::*;
use crate::high_level::keys::*;
Expand Down Expand Up @@ -185,20 +184,11 @@ fn test_batch() {
let (_session2_public, session2_secret) =

Check failure on line 184 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused variable: `session2_secret`

Check failure on line 184 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test (legacy-pep-repo-compatible)

unused variable: `session2_secret`

Check failure on line 184 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test (elgamal2,legacy-pep-repo-compatible)

unused variable: `session2_secret`
make_session_keys(&global_secret, &enc_context2, &enc_secret);


let mut ciphertexts = Vec::new();

let mut data = vec![];
let mut pseudonyms = vec![];
for _ in 0..10 {
let mut entity_data: [dyn Encrypted<UnencryptedType=dyn Any>] = *[];
let pseudonym = Pseudonym::random(rng);
let enc_pseudonym = encrypt(&pseudonym, &session1_public, rng);
entity_data.push(enc_pseudonym);
for _ in 0..5 {
let data = DataPoint::random(rng);
let enc_data = encrypt(&data, &session1_public, rng);
entity_data.push(enc_data);
}
ciphertexts.push(entity_data);
data.push(encrypt(&DataPoint::random(rng), &session1_public, rng));
pseudonyms.push(encrypt(&Pseudonym::random(rng), &session1_public, rng));
}

let transcryption_info = TranscryptionInfo::new(
Expand All @@ -209,5 +199,11 @@ fn test_batch() {
&pseudo_secret,
&enc_secret,
);
let mut new_ciphertexts = transcrypt_batch(&mut ciphertexts, &transcryption_info, rng);

let rekey_info = RekeyInfo::from(transcryption_info);

let rekeyed = rekey_batch(&data, &rekey_info, rng);

Check failure on line 205 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused variable: `rekeyed`

Check failure on line 205 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test (legacy-pep-repo-compatible)

unused variable: `rekeyed`

Check failure on line 205 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test (elgamal2,legacy-pep-repo-compatible)

unused variable: `rekeyed`
let pseudonymized = pseudonymize_batch(&pseudonyms, &transcryption_info, rng);

Check failure on line 206 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused variable: `pseudonymized`

Check failure on line 206 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test (legacy-pep-repo-compatible)

unused variable: `pseudonymized`

Check failure on line 206 in src/tests/high_level.rs

View workflow job for this annotation

GitHub Actions / cargo test (elgamal2,legacy-pep-repo-compatible)

unused variable: `pseudonymized`

// TODO check that the batch is indeed shuffled
}

0 comments on commit c845a52

Please sign in to comment.