From 59f0d8c9b6e6cd3ddfbccb376b0c1668548b7d45 Mon Sep 17 00:00:00 2001 From: xevisalle Date: Tue, 30 Apr 2024 17:39:54 +0200 Subject: [PATCH] Add ZK feature --- Cargo.toml | 5 ++++- src/encryption/elgamal.rs | 3 +++ tests/encryption.rs | 9 ++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 229913d..66dcb80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ exclude = [".github/workflows/dusk-ci.yml", ".gitignore"] [dependencies] rand_core = { version = "0.6", default-features = false } dusk-bytes = "0.1" -dusk-plonk = { version = "0.19", default-features = false } +dusk-plonk = { version = "0.19", default-features = false, optional = true } dusk-bls12_381 = { version = "0.13", default-features = false } bls12_381-bls = { version = "0.3", default-features = false } dusk-jubjub = { version = "0.14", default-features = false, features = ["zeroize"] } @@ -41,3 +41,6 @@ rkyv-impl = [ "rkyv", "bytecheck" ] +zk = [ + "dusk-plonk", +] diff --git a/src/encryption/elgamal.rs b/src/encryption/elgamal.rs index 1598b16..9c4ba93 100644 --- a/src/encryption/elgamal.rs +++ b/src/encryption/elgamal.rs @@ -10,6 +10,8 @@ //! Reference: https://link.springer.com/chapter/10.1007/3-540-39568-7_2 use dusk_jubjub::{JubJubExtended, JubJubScalar, GENERATOR}; + +#[cfg(feature = "zk")] use dusk_plonk::prelude::*; /// Encrypts a JubJubExtended plaintext given a public key and a fresh random @@ -37,6 +39,7 @@ pub fn decrypt( /// Encrypt in-circuit a plaintext, returning /// a ciphertext (WitnessPoint, WitnessPoint) +#[cfg(feature = "zk")] pub fn zk_encrypt( composer: &mut Composer, public_key: &JubJubAffine, diff --git a/tests/encryption.rs b/tests/encryption.rs index de1d9e6..8aa2153 100644 --- a/tests/encryption.rs +++ b/tests/encryption.rs @@ -5,15 +5,19 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use dusk_jubjub::{JubJubAffine, JubJubScalar, GENERATOR, GENERATOR_EXTENDED}; -use dusk_plonk::prelude::*; use ff::Field; use rand_core::OsRng; use phoenix_core::{aes, elgamal, PublicKey, SecretKey}; +#[cfg(feature = "zk")] +use dusk_plonk::prelude::*; +#[cfg(feature = "zk")] static LABEL: &[u8; 12] = b"dusk-network"; +#[cfg(feature = "zk")] const CAPACITY: usize = 12; // capacity required for the setup +#[cfg(feature = "zk")] #[derive(Default, Debug)] pub struct ElGamalCircuit { public_key: JubJubAffine, @@ -23,6 +27,7 @@ pub struct ElGamalCircuit { ciphertext_2: JubJubAffine, } +#[cfg(feature = "zk")] impl ElGamalCircuit { pub fn new( public_key: &JubJubExtended, @@ -41,6 +46,7 @@ impl ElGamalCircuit { } } +#[cfg(feature = "zk")] impl Circuit for ElGamalCircuit { fn circuit(&self, composer: &mut Composer) -> Result<(), Error> { let (ciphertext_1, ciphertext_2) = elgamal::zk_encrypt( @@ -96,6 +102,7 @@ fn test_elgamal_encrypt_and_decrypt() { assert_ne!(message, dec_message_wrong); } +#[cfg(feature = "zk")] #[test] fn test_elgamal_zk_encrypt() { let sk = SecretKey::random(&mut OsRng);