Skip to content

Commit b7fc208

Browse files
committed
chore(zk): match zkv2 hash impl with the description
- encode the position of bits proven to be 0 in the hashes - hash the infinite norm instead of the euclidean one - hash the value of k with the statement
1 parent bcb1356 commit b7fc208

File tree

6 files changed

+480
-79
lines changed

6 files changed

+480
-79
lines changed

tfhe-zk-pok/src/backward_compatibility/pke_v2.rs

Lines changed: 163 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#![allow(non_snake_case)]
33

44
use std::convert::Infallible;
5+
use std::error::Error;
6+
use std::fmt::Display;
57

68
use tfhe_versionable::{Upgrade, Version, VersionsDispatch};
79

810
use crate::curve_api::{CompressedG1, CompressedG2, Compressible, Curve};
911
use crate::proofs::pke_v2::{
10-
CompressedComputeLoadProofFields, CompressedProof, ComputeLoadProofFields, PkeV2HashMode, Proof,
12+
CompressedComputeLoadProofFields, CompressedProof, ComputeLoadProofFields, PkeV2HashMode,
13+
PkeV2SupportedHashConfig, Proof,
1114
};
1215

1316
use super::IncompleteProof;
@@ -89,10 +92,10 @@ pub struct ProofV1<G: Curve> {
8992
compute_load_proof_fields: Option<ComputeLoadProofFields<G>>,
9093
}
9194

92-
impl<G: Curve> Upgrade<Proof<G>> for ProofV1<G> {
95+
impl<G: Curve> Upgrade<ProofV2<G>> for ProofV1<G> {
9396
type Error = Infallible;
9497

95-
fn upgrade(self) -> Result<Proof<G>, Self::Error> {
98+
fn upgrade(self) -> Result<ProofV2<G>, Self::Error> {
9699
let ProofV1 {
97100
C_hat_e,
98101
C_e,
@@ -108,7 +111,7 @@ impl<G: Curve> Upgrade<Proof<G>> for ProofV1<G> {
108111
compute_load_proof_fields,
109112
} = self;
110113

111-
Ok(Proof {
114+
Ok(ProofV2 {
112115
C_hat_e,
113116
C_e,
114117
C_r_tilde,
@@ -126,11 +129,92 @@ impl<G: Curve> Upgrade<Proof<G>> for ProofV1<G> {
126129
}
127130
}
128131

132+
#[derive(Version)]
133+
pub struct ProofV2<G: Curve> {
134+
C_hat_e: G::G2,
135+
C_e: G::G1,
136+
C_r_tilde: G::G1,
137+
C_R: G::G1,
138+
C_hat_bin: G::G2,
139+
C_y: G::G1,
140+
C_h1: G::G1,
141+
C_h2: G::G1,
142+
C_hat_t: G::G2,
143+
pi: G::G1,
144+
pi_kzg: G::G1,
145+
compute_load_proof_fields: Option<ComputeLoadProofFields<G>>,
146+
hash_mode: PkeV2HashMode,
147+
}
148+
149+
#[derive(Debug)]
150+
pub struct UnsupportedHashConfig(String);
151+
152+
impl Display for UnsupportedHashConfig {
153+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154+
write!(f, "Unsupported Hash config in pke V2 Proof: {}", self.0)
155+
}
156+
}
157+
158+
impl Error for UnsupportedHashConfig {}
159+
160+
impl TryFrom<PkeV2HashMode> for PkeV2SupportedHashConfig {
161+
type Error = UnsupportedHashConfig;
162+
163+
fn try_from(value: PkeV2HashMode) -> Result<Self, Self::Error> {
164+
match value {
165+
PkeV2HashMode::BackwardCompat => Ok(PkeV2SupportedHashConfig::V0_4_0),
166+
PkeV2HashMode::Classical => Err(UnsupportedHashConfig(String::from(
167+
"Proof use hash mode \"Classical\" which has never been part of a default configuration",
168+
))),
169+
PkeV2HashMode::Compact => Ok(PkeV2SupportedHashConfig::V0_7_0),
170+
}
171+
}
172+
}
173+
174+
impl<G: Curve> Upgrade<Proof<G>> for ProofV2<G> {
175+
type Error = UnsupportedHashConfig;
176+
177+
fn upgrade(self) -> Result<Proof<G>, Self::Error> {
178+
let ProofV2 {
179+
C_hat_e,
180+
C_e,
181+
C_r_tilde,
182+
C_R,
183+
C_hat_bin,
184+
C_y,
185+
C_h1,
186+
C_h2,
187+
C_hat_t,
188+
pi,
189+
pi_kzg,
190+
compute_load_proof_fields,
191+
hash_mode,
192+
} = self;
193+
194+
Ok(Proof {
195+
C_hat_e,
196+
C_e,
197+
C_r_tilde,
198+
C_R,
199+
C_hat_bin,
200+
C_y,
201+
C_h1,
202+
C_h2,
203+
C_hat_t,
204+
pi,
205+
pi_kzg,
206+
compute_load_proof_fields,
207+
hash_config: hash_mode.try_into()?,
208+
})
209+
}
210+
}
211+
129212
#[derive(VersionsDispatch)]
130213
pub enum ProofVersions<G: Curve> {
131214
V0(ProofV0<G>),
132215
V1(ProofV1<G>),
133-
V2(Proof<G>),
216+
V2(ProofV2<G>),
217+
V3(Proof<G>),
134218
}
135219

136220
#[derive(VersionsDispatch)]
@@ -230,14 +314,14 @@ where
230314
compute_load_proof_fields: Option<CompressedComputeLoadProofFields<G>>,
231315
}
232316

233-
impl<G: Curve> Upgrade<CompressedProof<G>> for CompressedProofV1<G>
317+
impl<G: Curve> Upgrade<CompressedProofV2<G>> for CompressedProofV1<G>
234318
where
235319
G::G1: Compressible,
236320
G::G2: Compressible,
237321
{
238322
type Error = Infallible;
239323

240-
fn upgrade(self) -> Result<CompressedProof<G>, Self::Error> {
324+
fn upgrade(self) -> Result<CompressedProofV2<G>, Self::Error> {
241325
let CompressedProofV1 {
242326
C_hat_e,
243327
C_e,
@@ -253,7 +337,7 @@ where
253337
compute_load_proof_fields,
254338
} = self;
255339

256-
Ok(CompressedProof {
340+
Ok(CompressedProofV2 {
257341
C_hat_e,
258342
C_e,
259343
C_r_tilde,
@@ -271,6 +355,69 @@ where
271355
}
272356
}
273357

358+
#[derive(Version)]
359+
pub struct CompressedProofV2<G: Curve>
360+
where
361+
G::G1: Compressible,
362+
G::G2: Compressible,
363+
{
364+
C_hat_e: CompressedG2<G>,
365+
C_e: CompressedG1<G>,
366+
C_r_tilde: CompressedG1<G>,
367+
C_R: CompressedG1<G>,
368+
C_hat_bin: CompressedG2<G>,
369+
C_y: CompressedG1<G>,
370+
C_h1: CompressedG1<G>,
371+
C_h2: CompressedG1<G>,
372+
C_hat_t: CompressedG2<G>,
373+
pi: CompressedG1<G>,
374+
pi_kzg: CompressedG1<G>,
375+
compute_load_proof_fields: Option<CompressedComputeLoadProofFields<G>>,
376+
hash_mode: PkeV2HashMode,
377+
}
378+
379+
impl<G: Curve> Upgrade<CompressedProof<G>> for CompressedProofV2<G>
380+
where
381+
G::G1: Compressible,
382+
G::G2: Compressible,
383+
{
384+
type Error = UnsupportedHashConfig;
385+
386+
fn upgrade(self) -> Result<CompressedProof<G>, Self::Error> {
387+
let CompressedProofV2 {
388+
C_hat_e,
389+
C_e,
390+
C_r_tilde,
391+
C_R,
392+
C_hat_bin,
393+
C_y,
394+
C_h1,
395+
C_h2,
396+
C_hat_t,
397+
pi,
398+
pi_kzg,
399+
compute_load_proof_fields,
400+
hash_mode,
401+
} = self;
402+
403+
Ok(CompressedProof {
404+
C_hat_e,
405+
C_e,
406+
C_r_tilde,
407+
C_R,
408+
C_hat_bin,
409+
C_y,
410+
C_h1,
411+
C_h2,
412+
C_hat_t,
413+
pi,
414+
pi_kzg,
415+
compute_load_proof_fields,
416+
hash_config: hash_mode.try_into()?,
417+
})
418+
}
419+
}
420+
274421
#[derive(VersionsDispatch)]
275422
pub enum CompressedProofVersions<G: Curve>
276423
where
@@ -279,7 +426,8 @@ where
279426
{
280427
V0(CompressedProofV0<G>),
281428
V1(CompressedProofV1<G>),
282-
V2(CompressedProof<G>),
429+
V2(CompressedProofV2<G>),
430+
V3(CompressedProof<G>),
283431
}
284432

285433
#[derive(VersionsDispatch)]
@@ -297,3 +445,9 @@ pub enum PkeV2HashModeVersions {
297445
#[allow(dead_code)]
298446
V0(PkeV2HashMode),
299447
}
448+
449+
#[derive(VersionsDispatch)]
450+
pub enum PkeV2SupportedHashConfigVersions {
451+
#[allow(dead_code)]
452+
V0(PkeV2SupportedHashConfig),
453+
}

0 commit comments

Comments
 (0)