Skip to content

Commit 23c03b1

Browse files
committed
Refactor CompactProof to use two type parameters R, L
BREAKING CHANGE: CompactProof now takes two type parameters instead of one. ## Motivation When implementing serde support for types that contain CompactProof<S>, the typenum types (like U16, U32) used for challenge lengths don't implement Serialize/Deserialize. This makes it impossible to derive serde for types containing CompactProof without complex workarounds. ## Changes - Changed CompactProof<S: Sigma> to CompactProof<R, L> where: - R is the response type - L is the challenge length (ArrayLength<u8>) - Added serde bounds to only serialize/deserialize R, not L - Updated FiatShamir::prove to return CompactProof<S::Response, S::ChallengeLength> - Updated FiatShamir::verify to accept &CompactProof<S::Response, S::ChallengeLength> - Updated all usages throughout sigma_fun and ecdsa_fun - Maintained bincode support with generic implementation for any challenge length ## Impact This is a breaking change for any code that directly uses CompactProof. Most users interact through FiatShamir which maintains the same API.
1 parent 6e3bad4 commit 23c03b1

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

ecdsa_fun/src/adaptor/encrypted_signature.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::DLEQ;
22
use crate::fun::{Point, Scalar, marker::*};
3-
use sigma_fun::CompactProof;
3+
use sigma_fun::{CompactProof, Sigma};
44

55
/// `PointNonce` is a [`NonZero`] Point that also has an x-coordinate that is NonZero
66
/// when reduced modulo the curve order.
@@ -40,7 +40,7 @@ pub(crate) struct EncryptedSignatureInternal {
4040
pub R: PointNonce,
4141
pub R_hat: Point,
4242
pub s_hat: Scalar<Public>,
43-
pub proof: CompactProof<DLEQ>,
43+
pub proof: CompactProof<<DLEQ as Sigma>::Response, <DLEQ as Sigma>::ChallengeLength>,
4444
}
4545

4646
/// An "encrypted" ECDSA signature A.K.A. adaptor signature.

sigma_fun/src/eq.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,13 @@ mod test {
192192
let proof_system = FiatShamir::<DLEQ, HashTranscript<Sha256, ChaCha20Rng>>::default();
193193
let proof = proof_system.prove(&x, &statement, Some(&mut rand::thread_rng()));
194194
let encoded = bincode::encode_to_vec(&proof, bincode::config::standard()).unwrap();
195-
let (decoded, _) = bincode::decode_from_slice::<crate::CompactProof<DLEQ>, _>(
196-
&encoded[..],
197-
bincode::config::standard(),
198-
)
195+
let (decoded, _) = bincode::decode_from_slice::<
196+
crate::CompactProof<
197+
<DLEQ as crate::Sigma>::Response,
198+
<DLEQ as crate::Sigma>::ChallengeLength,
199+
>,
200+
_,
201+
>(&encoded[..], bincode::config::standard())
199202
.unwrap();
200203
assert_eq!(decoded, proof);
201204
}
@@ -213,10 +216,13 @@ mod test {
213216
let proof_system = FiatShamir::<DLEQ, HashTranscript<Sha256, ChaCha20Rng>>::default();
214217
let proof = proof_system.prove(&x, &statement, Some(&mut rand::thread_rng()));
215218
let encoded = bincode::encode_to_vec(&proof, bincode::config::standard()).unwrap();
216-
let (decoded, _) = bincode::decode_from_slice::<crate::CompactProof<DLEQ>, _>(
217-
&encoded[..],
218-
bincode::config::standard(),
219-
)
219+
let (decoded, _) = bincode::decode_from_slice::<
220+
crate::CompactProof<
221+
<DLEQ as crate::Sigma>::Response,
222+
<DLEQ as crate::Sigma>::ChallengeLength,
223+
>,
224+
_,
225+
>(&encoded[..], bincode::config::standard())
220226
.unwrap();
221227
assert_eq!(decoded, proof);
222228
}

sigma_fun/src/ext/dl_secp256k1_ed25519_eq.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ pub struct CrossCurveDLEQProof {
7070
pub commitments: Vec<(PointP, PointQ)>,
7171
/// The core proof which shows the pairs of commitments commit to the same bit and the resulting
7272
/// sum is the claimed points.
73-
pub proof: crate::CompactProof<CoreProof>,
73+
pub proof: crate::CompactProof<
74+
<CoreProof as crate::Sigma>::Response,
75+
<CoreProof as crate::Sigma>::ChallengeLength,
76+
>,
7477
}
7578

7679
/// The proof system which prepares the high level statement to be proved/verified with

sigma_fun/src/fiat_shamir.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{ProverTranscript, Sigma, Transcript, generic_array::GenericArray};
1+
use crate::{
2+
ProverTranscript, Sigma, Transcript,
3+
generic_array::{ArrayLength, GenericArray},
4+
};
25
use rand_core::{CryptoRng, RngCore};
36

47
/// Applies the Fiat-Shamir transform to a given [`Sigma`] protocol given a [`Transcript`].
@@ -45,7 +48,7 @@ impl<S: Sigma, T: Transcript<S>> FiatShamir<S, T> {
4548
witness: &S::Witness,
4649
statement: &S::Statement,
4750
rng: Option<&mut Rng>,
48-
) -> CompactProof<S>
51+
) -> CompactProof<S::Response, S::ChallengeLength>
4952
where
5053
T: ProverTranscript<S>,
5154
{
@@ -58,15 +61,19 @@ impl<S: Sigma, T: Transcript<S>> FiatShamir<S, T> {
5861
let response =
5962
self.sigma
6063
.respond(witness, statement, announce_secret, &announce, &challenge);
61-
CompactProof::<S> {
64+
CompactProof {
6265
challenge,
6366
response,
6467
}
6568
}
6669

6770
/// Verifies the proof given the statement.
6871
#[must_use]
69-
pub fn verify(&self, statement: &S::Statement, proof: &CompactProof<S>) -> bool {
72+
pub fn verify(
73+
&self,
74+
statement: &S::Statement,
75+
proof: &CompactProof<S::Response, S::ChallengeLength>,
76+
) -> bool {
7077
let mut transcript = self.transcript.clone();
7178
transcript.add_statement(&self.sigma, statement);
7279
let implied_announcement =
@@ -89,21 +96,29 @@ impl<S: Sigma, T: Transcript<S>> FiatShamir<S, T> {
8996
/// the underlying group's Sigma protocol but this isn't implemented yet.
9097
///
9198
/// [`FiatShamir`]: crate::FiatShamir
92-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99+
#[cfg_attr(
100+
feature = "serde",
101+
derive(serde::Serialize, serde::Deserialize),
102+
serde(bound(
103+
serialize = "R: serde::Serialize",
104+
deserialize = "R: serde::Deserialize<'de>"
105+
))
106+
)]
93107
#[derive(Debug, Clone, PartialEq)]
94-
pub struct CompactProof<S: Sigma> {
108+
pub struct CompactProof<R, L: ArrayLength<u8>> {
95109
/// C
96-
pub challenge: GenericArray<u8, S::ChallengeLength>,
110+
pub challenge: GenericArray<u8, L>,
97111
/// R
98-
pub response: S::Response,
112+
pub response: R,
99113
}
100114

101115
/// Implements bincode encoding for `CompactProof` for any challenge length.
102116
#[cfg(feature = "bincode")]
103117
#[cfg_attr(docsrs, doc(cfg(feature = "bincode")))]
104-
impl<S: Sigma> bincode::Encode for CompactProof<S>
118+
impl<R, L> bincode::Encode for CompactProof<R, L>
105119
where
106-
S::Response: bincode::Encode,
120+
R: bincode::Encode,
121+
L: ArrayLength<u8>,
107122
{
108123
fn encode<E: bincode::enc::Encoder>(
109124
&self,
@@ -118,19 +133,19 @@ where
118133

119134
#[cfg(feature = "bincode")]
120135
#[cfg_attr(docsrs, doc(cfg(feature = "bincode")))]
121-
impl<S, Context> bincode::Decode<Context> for CompactProof<S>
136+
impl<R, L, Context> bincode::Decode<Context> for CompactProof<R, L>
122137
where
123-
S: Sigma,
124-
S::Response: bincode::Decode<Context>,
138+
R: bincode::Decode<Context>,
139+
L: ArrayLength<u8>,
125140
{
126141
fn decode<D: bincode::de::Decoder<Context = Context>>(
127142
decoder: &mut D,
128143
) -> Result<Self, bincode::error::DecodeError> {
129144
// Create a default GenericArray and read directly into it
130-
let mut challenge = GenericArray::<u8, S::ChallengeLength>::default();
145+
let mut challenge = GenericArray::<u8, L>::default();
131146
<D::R as bincode::de::read::Reader>::read(decoder.reader(), challenge.as_mut_slice())?;
132147

133-
let response = S::Response::decode(decoder)?;
148+
let response = R::decode(decoder)?;
134149

135150
Ok(CompactProof {
136151
challenge,
@@ -141,10 +156,10 @@ where
141156

142157
#[cfg(feature = "bincode")]
143158
#[cfg_attr(docsrs, doc(cfg(feature = "bincode")))]
144-
impl<'de, S: Sigma, Context> bincode::BorrowDecode<'de, Context> for CompactProof<S>
159+
impl<'de, R, L, Context> bincode::BorrowDecode<'de, Context> for CompactProof<R, L>
145160
where
146-
S: Sigma,
147-
S::Response: bincode::Decode<Context>,
161+
R: bincode::Decode<Context>,
162+
L: ArrayLength<u8>,
148163
{
149164
fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = Context>>(
150165
decoder: &mut D,

0 commit comments

Comments
 (0)