Skip to content

Commit d4ccd24

Browse files
committed
chore(xof): add backward_compatibility layer for csprng byte start
Add an enum that allows to represent where the CSPRNG for the CompressedXofKeySet must start. This is to accommodate for the csprng bug that was fixed in tfhe-csprng 0.9 to still be able to load 1.5.4 (csprng 0.8.1) data correctly Defaults to first byte, backward_compatibility handles old data that was versioned with it.
1 parent 8810ad1 commit d4ccd24

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
1-
use tfhe_versionable::VersionsDispatch;
1+
use std::convert::Infallible;
2+
3+
use tfhe_csprng::seeders::XofSeed;
4+
use tfhe_versionable::{Upgrade, Version, VersionsDispatch};
25

36
use crate::high_level_api::xof_key_set::{CompressedXofKeySet, XofKeySet};
7+
use crate::xof_key_set::XofSeedStart;
8+
use crate::{CompressedCompactPublicKey, CompressedServerKey};
9+
10+
#[derive(Version)]
11+
pub struct CompressedXofKeySetV0 {
12+
seed: XofSeed,
13+
compressed_public_key: CompressedCompactPublicKey,
14+
compressed_server_key: CompressedServerKey,
15+
}
16+
17+
impl Upgrade<CompressedXofKeySet> for CompressedXofKeySetV0 {
18+
type Error = Infallible;
19+
20+
fn upgrade(self) -> Result<CompressedXofKeySet, Self::Error> {
21+
let Self {
22+
seed,
23+
compressed_public_key,
24+
compressed_server_key,
25+
} = self;
26+
27+
Ok(CompressedXofKeySet::from_raw_parts(
28+
// Start on second byte to keep backward compatibility with csprng bug
29+
XofSeedStart::SecondByte(seed),
30+
compressed_public_key,
31+
compressed_server_key,
32+
))
33+
}
34+
}
435

536
#[derive(VersionsDispatch)]
637
pub enum CompressedXofKeySetVersions {
7-
V0(CompressedXofKeySet),
38+
V0(CompressedXofKeySetV0),
39+
V1(CompressedXofKeySet),
840
}
941

1042
#[derive(VersionsDispatch)]
1143
pub enum XofKeySetVersions {
1244
V0(XofKeySet),
1345
}
46+
47+
#[derive(VersionsDispatch)]
48+
pub enum XofSeedStartVersions {
49+
V0(XofSeedStart),
50+
}

tfhe/src/high_level_api/xof_key_set/mod.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ mod internal;
22
#[cfg(test)]
33
mod test;
44

5-
use crate::backward_compatibility::xof_key_set::CompressedXofKeySetVersions;
5+
use crate::backward_compatibility::xof_key_set::{
6+
CompressedXofKeySetVersions, XofSeedStartVersions,
7+
};
68
use crate::core_crypto::commons::generators::MaskRandomGenerator;
79
use crate::keys::{
810
CompressedReRandomizationKey, IntegerServerKeyConformanceParams, ReRandomizationKeyGenInfo,
@@ -24,6 +26,7 @@ use crate::{
2426
CompressedReRandomizationKeySwitchingKey, CompressedServerKey, Config, ServerKey, Tag,
2527
};
2628
use serde::{Deserialize, Serialize};
29+
use tfhe_csprng::generators::aes_ctr::{AesCtrParams, TableIndex};
2730

2831
use crate::core_crypto::commons::generators::NoiseRandomGenerator;
2932
use crate::shortint::atomic_pattern::compressed::CompressedAtomicPatternServerKey;
@@ -55,14 +58,47 @@ use crate::high_level_api::keys::expanded::IntegerExpandedServerKey;
5558
// - Re-Rand Public Key (stored in ServerKey) derived from compute params
5659
// 11) SNS Compression Key
5760

61+
/// Holds a [XofSeed] and the byte at which the random generator should start.
62+
/// This maintains backward compatibility with tfhe-rs=1.5.4 (csprng=0.8.1)
63+
/// where the generator started at the second byte.
64+
///
65+
/// Default conversion [From] a [XofSeed] selects the first byte.
66+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Versionize)]
67+
#[versionize(XofSeedStartVersions)]
68+
pub enum XofSeedStart {
69+
FirstByte(XofSeed),
70+
SecondByte(XofSeed),
71+
}
72+
73+
impl From<XofSeed> for XofSeedStart {
74+
fn from(seed: XofSeed) -> Self {
75+
Self::FirstByte(seed)
76+
}
77+
}
78+
79+
impl From<XofSeedStart> for AesCtrParams {
80+
fn from(val: XofSeedStart) -> Self {
81+
match val {
82+
XofSeedStart::FirstByte(xof_seed) => Self {
83+
seed: xof_seed.into(),
84+
first_index: TableIndex::FIRST,
85+
},
86+
XofSeedStart::SecondByte(xof_seed) => Self {
87+
seed: xof_seed.into(),
88+
first_index: TableIndex::SECOND,
89+
},
90+
}
91+
}
92+
}
93+
5894
/// Compressed KeySet which respects the [Threshold (Fully) Homomorphic Encryption]
5995
/// regarding the random generator used, and the order of key generation
6096
///
6197
/// [Threshold (Fully) Homomorphic Encryption]: https://eprint.iacr.org/2025/699
6298
#[derive(Clone, Serialize, Deserialize, Versionize)]
6399
#[versionize(CompressedXofKeySetVersions)]
64100
pub struct CompressedXofKeySet {
65-
seed: XofSeed,
101+
seed: XofSeedStart,
66102
compressed_public_key: CompressedCompactPublicKey,
67103
compressed_server_key: CompressedServerKey,
68104
}
@@ -335,7 +371,7 @@ impl CompressedXofKeySet {
335371
);
336372

337373
Ok(Self {
338-
seed: pub_seed,
374+
seed: XofSeedStart::FirstByte(pub_seed),
339375
compressed_public_key,
340376
compressed_server_key,
341377
})
@@ -375,7 +411,7 @@ impl CompressedXofKeySet {
375411
}
376412

377413
pub fn from_raw_parts(
378-
pub_seed: XofSeed,
414+
pub_seed: impl Into<XofSeedStart>,
379415
mut compressed_public_key: CompressedCompactPublicKey,
380416
compressed_server_key: CompressedServerKey,
381417
) -> Self {
@@ -384,13 +420,19 @@ impl CompressedXofKeySet {
384420
.tag_mut()
385421
.set_data(compressed_server_key.tag.data());
386422
Self {
387-
seed: pub_seed,
423+
seed: pub_seed.into(),
388424
compressed_public_key,
389425
compressed_server_key,
390426
}
391427
}
392428

393-
pub fn into_raw_parts(self) -> (XofSeed, CompressedCompactPublicKey, CompressedServerKey) {
429+
pub fn into_raw_parts(
430+
self,
431+
) -> (
432+
XofSeedStart,
433+
CompressedCompactPublicKey,
434+
CompressedServerKey,
435+
) {
394436
let Self {
395437
seed,
396438
mut compressed_public_key,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:c9f23ebfa45863063a5ec97fead026a027fef5ba3206d981a5fef7df39adcf94
3-
size 138497811
2+
oid sha256:94eb63d050bc99eb8da1d52532b22bdbed3099ddcaddc884cd6a2854ab6713ff
3+
size 138497819
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:2d12df3e41e17b3f8bb1b7febd181e5f9e3c9e2a8ed168f0af8707eb6536e246
3-
size 155791615
2+
oid sha256:e97defb1929960a4bb7142d696ee6316ac92ab485606e7966f75340b72c861e6
3+
size 155791630

0 commit comments

Comments
 (0)