From 48e969b7153af7eadc570fda4f3cae55d112656a Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Wed, 21 Aug 2024 17:38:32 +0200 Subject: [PATCH 1/3] Use `#[udigest(as = ...)]` attribute Signed-off-by: Denis Varlakov --- Cargo.lock | 10 ++--- Cargo.toml | 9 ++++- src/common.rs | 40 ++++++++++--------- ...element_vs_paillier_encryption_in_range.rs | 12 +++--- src/no_small_factor.rs | 18 ++++----- src/paillier_affine_operation_in_range.rs | 24 +++++------ src/paillier_blum_modulus.rs | 4 +- src/paillier_encryption_in_range.rs | 10 ++--- 8 files changed, 68 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d8cfaa..f9393a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -846,9 +846,8 @@ checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "udigest" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a435a16abb7029ba807a45242367b087dd767e87e2e5ebc5f0e4189ea114a23" +version = "0.2.1" +source = "git+https://github.com/dfns/udigest?branch=udigest_as#3ed5e22a0d628a69d7a6f2ec5ad3403200435232" dependencies = [ "digest", "udigest-derive", @@ -856,9 +855,8 @@ dependencies = [ [[package]] name = "udigest-derive" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee6296c12e792dbc59565a58920d8d1842997ad5a72ddf2a51f70d70bd7af2ba" +version = "0.3.0" +source = "git+https://github.com/dfns/udigest?branch=udigest_as#3ed5e22a0d628a69d7a6f2ec5ad3403200435232" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index ee2b240..1cdd1d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] @@ -50,3 +50,10 @@ required-features = ["serde"] [package.metadata.docs.rs] all-features = true +[patch.crates-io.udigest] +git = "https://github.com/dfns/udigest" +branch = "udigest_as" + +[patch.crates-io.udigest-derive] +git = "https://github.com/dfns/udigest" +branch = "udigest_as" diff --git a/src/common.rs b/src/common.rs index 08de152..c4641f1 100644 --- a/src/common.rs +++ b/src/common.rs @@ -267,25 +267,29 @@ pub fn fail_if_ne(err: E, lhs: T, rhs: T) -> Result<(), E> { } } -/// Digests an integer -/// -/// To be used within `#[udigest(with = "...")]` attribute -pub fn digest_integer( - value: &Integer, - encoder: udigest::encoding::EncodeValue, -) { - let digits = value.to_digits::(rug::integer::Order::Msf); - encoder.encode_leaf_value(digits) -} +pub mod encoding { + /// Digests a rug integer + pub struct Integer; + impl udigest::DigestAs for Integer { + fn digest_as( + value: &rug::Integer, + encoder: udigest::encoding::EncodeValue, + ) { + let digits = value.to_digits::(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( - value: &&dyn fast_paillier::AnyEncryptionKey, - encoder: udigest::encoding::EncodeValue, -) { - digest_integer::(value.n(), encoder) + /// Digests any encryption key + pub struct AnyEncryptionKey; + impl udigest::DigestAs<&dyn fast_paillier::AnyEncryptionKey> for AnyEncryptionKey { + fn digest_as( + value: &&dyn fast_paillier::AnyEncryptionKey, + encoder: udigest::encoding::EncodeValue, + ) { + Integer::digest_as(value.n(), encoder) + } + } } /// A common logic shared across tests and doctests diff --git a/src/group_element_vs_paillier_encryption_in_range.rs b/src/group_element_vs_paillier_encryption_in_range.rs index ccdd04c..382900b 100644 --- a/src/group_element_vs_paillier_encryption_in_range.rs +++ b/src/group_element_vs_paillier_encryption_in_range.rs @@ -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, } @@ -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, @@ -152,12 +152,12 @@ pub struct PrivateData<'a> { #[udigest(bound = "")] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(bound = ""))] 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: Ciphertext, pub y: Point, - #[udigest(with = crate::common::digest_integer)] + #[udigest(as = crate::common::encoding::Integer)] pub d: Integer, } diff --git a/src/no_small_factor.rs b/src/no_small_factor.rs index 96bc452..ff32dc3 100644 --- a/src/no_small_factor.rs +++ b/src/no_small_factor.rs @@ -95,7 +95,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, } @@ -103,10 +103,10 @@ pub struct SecurityParams { #[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, } @@ -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, } diff --git a/src/paillier_affine_operation_in_range.rs b/src/paillier_affine_operation_in_range.rs index bd40af6..c9d63c9 100644 --- a/src/paillier_affine_operation_in_range.rs +++ b/src/paillier_affine_operation_in_range.rs @@ -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, } @@ -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, @@ -221,18 +221,18 @@ pub struct PrivateData<'a> { #[udigest(bound = "")] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(bound = ""))] pub struct Commitment { - #[udigest(with = crate::common::digest_integer)] + #[udigest(as = crate::common::encoding::Integer)] pub a: Integer, pub b_x: Point, - #[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, } diff --git a/src/paillier_blum_modulus.rs b/src/paillier_blum_modulus.rs index 2977dde..fcf60fe 100644 --- a/src/paillier_blum_modulus.rs +++ b/src/paillier_blum_modulus.rs @@ -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, } @@ -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, } diff --git a/src/paillier_encryption_in_range.rs b/src/paillier_encryption_in_range.rs index 0667310..8801b69 100644 --- a/src/paillier_encryption_in_range.rs +++ b/src/paillier_encryption_in_range.rs @@ -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, } @@ -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, } From b10890719b498c3e6bd7f573829ea2a4e1867340 Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Wed, 21 Aug 2024 17:40:30 +0200 Subject: [PATCH 2/3] Bump version, update the changelog Signed-off-by: Denis Varlakov --- CHANGELOG.md | 5 +++++ Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b9cb06..f832611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.lock b/Cargo.lock index f9393a9..ebe1151 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -518,7 +518,7 @@ dependencies = [ [[package]] name = "paillier-zk" -version = "0.4.0" +version = "0.4.1" dependencies = [ "anyhow", "digest", diff --git a/Cargo.toml b/Cargo.toml index 1cdd1d7..93213e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" From 68dd4eb7cf92e3d74870295dcf23ac3a5e5a9e2a Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Tue, 27 Aug 2024 11:19:09 +0200 Subject: [PATCH 3/3] Update deps Signed-off-by: Denis Varlakov --- Cargo.lock | 6 ++++-- Cargo.toml | 8 -------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebe1151..8c0ae86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -847,7 +847,8 @@ checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "udigest" version = "0.2.1" -source = "git+https://github.com/dfns/udigest?branch=udigest_as#3ed5e22a0d628a69d7a6f2ec5ad3403200435232" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f8c862324ffd970533e7fde91b00bf06cd973e2023bfa6c7df5f96a1c959a9d" dependencies = [ "digest", "udigest-derive", @@ -856,7 +857,8 @@ dependencies = [ [[package]] name = "udigest-derive" version = "0.3.0" -source = "git+https://github.com/dfns/udigest?branch=udigest_as#3ed5e22a0d628a69d7a6f2ec5ad3403200435232" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6132fb8382b1bdec1cd01062dbe1e88074610e8fe37c2d504043a45f6400108" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 93213e9..5394796 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,11 +49,3 @@ required-features = ["serde"] [package.metadata.docs.rs] all-features = true - -[patch.crates-io.udigest] -git = "https://github.com/dfns/udigest" -branch = "udigest_as" - -[patch.crates-io.udigest-derive] -git = "https://github.com/dfns/udigest" -branch = "udigest_as"