Skip to content

Commit

Permalink
Merge pull request #51 from dfns/udigest_as
Browse files Browse the repository at this point in the history
Use `#[udigest(as = ...)]` attribute
  • Loading branch information
survived authored Aug 27, 2024
2 parents b5d32fc + 68dd4eb commit d470bf6
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 60 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.4.1
* Prettify code by using `#[udigest(as = ...)]` attribute [#51]

[#51]: https://github.com/dfns/paillier-zk/pull/51

## v0.4.0
* security fix: derive challenges for zero-knowledge proof unambiguously

Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "paillier-zk"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "ZK-proofs for Paillier encryption scheme"
Expand All @@ -22,7 +22,7 @@ thiserror = "1"
serde = { version = "1", features = ["derive"], optional = true }
serde_with = { version = "3", default-features = false, features = ["macros"], optional = true }

udigest = { version = "0.2", default-features = false, features = ["inline-struct", "derive"] }
udigest = { version = "0.2.1", default-features = false, features = ["inline-struct", "derive"] }
rand_hash = "0.1"

[dev-dependencies]
Expand All @@ -49,4 +49,3 @@ required-features = ["serde"]

[package.metadata.docs.rs]
all-features = true

40 changes: 22 additions & 18 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,29 @@ pub fn fail_if_ne<T: PartialEq, E>(err: E, lhs: T, rhs: T) -> Result<(), E> {
}
}

/// Digests an integer
///
/// To be used within `#[udigest(with = "...")]` attribute
pub fn digest_integer<B: udigest::Buffer>(
value: &Integer,
encoder: udigest::encoding::EncodeValue<B>,
) {
let digits = value.to_digits::<u8>(rug::integer::Order::Msf);
encoder.encode_leaf_value(digits)
}
pub mod encoding {
/// Digests a rug integer
pub struct Integer;
impl udigest::DigestAs<rug::Integer> for Integer {
fn digest_as<B: udigest::Buffer>(
value: &rug::Integer,
encoder: udigest::encoding::EncodeValue<B>,
) {
let digits = value.to_digits::<u8>(rug::integer::Order::Msf);
encoder.encode_leaf_value(digits)
}
}

/// Digests any encryption key
///
/// To be used within `#[udigest(with = "...")]` attribute
pub fn digest_encryption_key<B: udigest::Buffer>(
value: &&dyn fast_paillier::AnyEncryptionKey,
encoder: udigest::encoding::EncodeValue<B>,
) {
digest_integer::<B>(value.n(), encoder)
/// Digests any encryption key
pub struct AnyEncryptionKey;
impl udigest::DigestAs<&dyn fast_paillier::AnyEncryptionKey> for AnyEncryptionKey {
fn digest_as<B: udigest::Buffer>(
value: &&dyn fast_paillier::AnyEncryptionKey,
encoder: udigest::encoding::EncodeValue<B>,
) {
Integer::digest_as(value.n(), encoder)
}
}
}

/// A common logic shared across tests and doctests
Expand Down
12 changes: 6 additions & 6 deletions src/group_element_vs_paillier_encryption_in_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub struct SecurityParams {
/// Epsilon in paper, slackness parameter
pub epsilon: usize,
/// q in paper. Security parameter for challenge
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub q: Integer,
}

Expand All @@ -127,10 +127,10 @@ pub struct SecurityParams {
#[udigest(bound = "")]
pub struct Data<'a, C: Curve> {
/// N0 in paper, public key that C was encrypted on
#[udigest(with = crate::common::digest_encryption_key)]
#[udigest(as = crate::common::encoding::AnyEncryptionKey)]
pub key0: &'a dyn AnyEncryptionKey,
/// C in paper, logarithm of X encrypted on N0
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub c: &'a Ciphertext,
/// A basepoint, generator in group
pub b: &'a Point<C>,
Expand All @@ -152,12 +152,12 @@ pub struct PrivateData<'a> {
#[udigest(bound = "")]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(bound = ""))]
pub struct Commitment<C: Curve> {
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub s: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub a: Ciphertext,
pub y: Point<C>,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub d: Integer,
}

Expand Down
18 changes: 9 additions & 9 deletions src/no_small_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ pub struct SecurityParams {
/// Epsilon in paper, slackness parameter
pub epsilon: usize,
/// q in paper. Security parameter for challenge
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub q: Integer,
}

/// Public data that both parties know
#[derive(Debug, Clone, Copy, udigest::Digestable)]
pub struct Data<'a> {
/// N0 - rsa modulus
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub n: &'a Integer,
/// A number close to square root of n
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub n_root: &'a Integer,
}

Expand Down Expand Up @@ -135,17 +135,17 @@ pub struct PrivateCommitment {
#[derive(Debug, Clone, udigest::Digestable)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Commitment {
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub p: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub q: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub a: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub b: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub t: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub sigma: Integer,
}

Expand Down
24 changes: 12 additions & 12 deletions src/paillier_affine_operation_in_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub struct SecurityParams {
/// Epsilon in paper, slackness parameter
pub epsilon: usize,
/// q in paper. Security parameter for challenge
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub q: Integer,
}

Expand All @@ -184,19 +184,19 @@ pub struct SecurityParams {
#[udigest(bound = "")]
pub struct Data<'a, C: Curve> {
/// N0 in paper, public key that C was encrypted on
#[udigest(with = crate::common::digest_encryption_key)]
#[udigest(as = crate::common::encoding::AnyEncryptionKey)]
pub key0: &'a dyn AnyEncryptionKey,
/// N1 in paper, public key that y -> Y was encrypted on
#[udigest(with = crate::common::digest_encryption_key)]
#[udigest(as = crate::common::encoding::AnyEncryptionKey)]
pub key1: &'a dyn AnyEncryptionKey,
/// C or C0 in paper, some data encrypted on N0
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub c: &'a Ciphertext,
/// D or C in paper, result of affine transformation of C0 with x and y
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub d: &'a Integer,
/// Y in paper, y encrypted on N1
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub y: &'a Ciphertext,
/// X in paper, obtained as g^x
pub x: &'a Point<C>,
Expand All @@ -221,18 +221,18 @@ pub struct PrivateData<'a> {
#[udigest(bound = "")]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(bound = ""))]
pub struct Commitment<C: Curve> {
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub a: Integer,
pub b_x: Point<C>,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub b_y: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub e: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub s: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub f: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub t: Integer,
}

Expand Down
4 changes: 2 additions & 2 deletions src/paillier_blum_modulus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, udigest::Digestable)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Data {
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub n: Integer,
}

Expand All @@ -80,7 +80,7 @@ pub struct PrivateData {
#[derive(Debug, Clone, udigest::Digestable)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Commitment {
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub w: Integer,
}

Expand Down
10 changes: 5 additions & 5 deletions src/paillier_encryption_in_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ pub struct SecurityParams {
#[derive(Debug, Clone, Copy, udigest::Digestable)]
pub struct Data<'a> {
/// N0 in paper, public key that k -> K was encrypted on
#[udigest(with = crate::common::digest_encryption_key)]
#[udigest(as = crate::common::encoding::AnyEncryptionKey)]
pub key: &'a dyn AnyEncryptionKey,
/// K in paper
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = &crate::common::encoding::Integer)]
pub ciphertext: &'a Ciphertext,
}

Expand All @@ -134,11 +134,11 @@ pub struct PrivateData<'a> {
#[derive(Debug, Clone, udigest::Digestable)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Commitment {
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub s: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub a: Integer,
#[udigest(with = crate::common::digest_integer)]
#[udigest(as = crate::common::encoding::Integer)]
pub c: Integer,
}

Expand Down

0 comments on commit d470bf6

Please sign in to comment.