Skip to content

Commit

Permalink
Add ZK feature
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle committed Apr 30, 2024
1 parent 88f2e4c commit 59f0d8c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -41,3 +41,6 @@ rkyv-impl = [
"rkyv",
"bytecheck"
]
zk = [
"dusk-plonk",
]
3 changes: 3 additions & 0 deletions src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion tests/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,6 +27,7 @@ pub struct ElGamalCircuit {
ciphertext_2: JubJubAffine,
}

#[cfg(feature = "zk")]
impl ElGamalCircuit {
pub fn new(
public_key: &JubJubExtended,
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 59f0d8c

Please sign in to comment.