Skip to content

Commit

Permalink
Merge pull request #241 from dusk-network/mocello/184_features
Browse files Browse the repository at this point in the history
Restructure crate features
  • Loading branch information
moCello authored Jan 17, 2024
2 parents 770d08b + 7b66c93 commit 89dfae6
Show file tree
Hide file tree
Showing 15 changed files with 505 additions and 456 deletions.
26 changes: 20 additions & 6 deletions .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@ jobs:
name: Dusk Analyzer
uses: dusk-network/.github/.github/workflows/dusk-analysis.yml@main

test_std:
name: Tests std
build_benches:
name: Build Benchmarks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- run: cargo bench --features=cipher,zk --no-run

check_merkle:
name: Check merkle compiles without zk
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --features=merkle --no-run

check_cipher:
name: Check cipher compiles without zk
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --features=merkle,rkyv-impl,size_32
test_flags: --features=cipher --no-run

test_no_std:
name: Tests no_std
test_all:
name: Tests all
uses: dusk-network/.github/.github/workflows/run-tests.yml@main
with:
test_flags: --no-default-features
test_flags: --features=zk,cipher,merkle,rkyv-impl,size_32
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Restructure crate features [#184]

### Removed

- Remove `default` and `alloc` features [#184]

### Added

- Add `zk` and `cipher` features [#184]

## [0.33.0] - 2024-01-03

### Changed
Expand Down Expand Up @@ -417,12 +429,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- ISSUES -->
[#215]: https://github.com/dusk-network/poseidon252/issues/215
[#212]: https://github.com/dusk-network/poseidon252/issues/212
[#198]: https://github.com/dusk-network/poseidon252/issues/198
[#206]: https://github.com/dusk-network/poseidon252/issues/206
[#203]: https://github.com/dusk-network/poseidon252/issues/203
[#200]: https://github.com/dusk-network/poseidon252/issues/200
[#198]: https://github.com/dusk-network/poseidon252/issues/198
[#197]: https://github.com/dusk-network/Poseidon252/issues/197
[#189]: https://github.com/dusk-network/poseidon252/issues/189
[#184]: https://github.com/dusk-network/poseidon252/issues/184
[#181]: https://github.com/dusk-network/poseidon252/issues/181
[#180]: https://github.com/dusk-network/poseidon252/issues/180
[#175]: https://github.com/dusk-network/poseidon252/issues/175
Expand Down
15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dusk-bls12_381 = { version = "0.13", default-features = false }
dusk-jubjub = { version = "0.14", default-features = false }
dusk-bytes = "0.1"
dusk-hades = "0.24"
dusk-plonk = { version = "0.19", default-features = false, features = ["alloc"] }
dusk-plonk = { version = "0.19", default-features = false, features = ["alloc"], optional = true }
rkyv = { version = "0.7", optional = true, default-features = false }
bytecheck = { version = "0.6", optional = true, default-features = false }

Expand All @@ -24,16 +24,12 @@ rand = { version = "0.8", default-features = false, features = ["getrandom", "st
ff = { version = "0.13", default-features = false }

[features]
default = [
"dusk-plonk/std",
"dusk-jubjub/default",
"dusk-bls12_381/default",
"alloc",
]
alloc = [
zk = [
"dusk-plonk",
"dusk-hades/plonk"
]
merkle = []
cipher = []
size_16 = ["rkyv/size_16"]
size_32 = ["rkyv/size_32"]
size_64 = ["rkyv/size_64"]
Expand Down Expand Up @@ -66,11 +62,14 @@ codegen-units = 1
[[bench]]
name = "sponge"
harness = false
required-features = ["zk"]

[[bench]]
name = "cipher_encrypt"
harness = false
required-features = ["cipher", "zk"]

[[bench]]
name = "cipher_decrypt"
harness = false
required-features = ["cipher", "zk"]
13 changes: 6 additions & 7 deletions benches/cipher_decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ pub struct CipherDecrypt {

impl CipherDecrypt {
pub fn random(rng: &mut StdRng) -> Self {
let shared =
GENERATOR.to_niels().mul(&JubJubScalar::random(rng)).into();
let shared = GENERATOR
.to_niels()
.mul(&JubJubScalar::random(&mut *rng))
.into();
let nonce = BlsScalar::random(&mut *rng);
let message =
[BlsScalar::random(&mut *rng), BlsScalar::random(&mut *rng)];
Expand All @@ -44,14 +46,11 @@ impl CipherDecrypt {
}

impl Circuit for CipherDecrypt {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
let shared = composer.append_point(self.shared);
let nonce = composer.append_witness(self.nonce);

let mut cipher_circuit = [C::ZERO; CIPHER_SIZE];
let mut cipher_circuit = [Composer::ZERO; CIPHER_SIZE];
self.cipher
.cipher()
.iter()
Expand Down
13 changes: 6 additions & 7 deletions benches/cipher_encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ pub struct CipherEncrypt {

impl CipherEncrypt {
pub fn random(rng: &mut StdRng) -> Self {
let shared =
GENERATOR.to_niels().mul(&JubJubScalar::random(rng)).into();
let shared = GENERATOR
.to_niels()
.mul(&JubJubScalar::random(&mut *rng))
.into();
let nonce = BlsScalar::random(&mut *rng);
let message =
[BlsScalar::random(&mut *rng), BlsScalar::random(&mut *rng)];
Expand All @@ -42,14 +44,11 @@ impl CipherEncrypt {
}

impl Circuit for CipherEncrypt {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
let shared = composer.append_point(self.shared);
let nonce = composer.append_witness(self.nonce);

let mut message_circuit = [C::ZERO; MESSAGE_CAPACITY];
let mut message_circuit = [Composer::ZERO; MESSAGE_CAPACITY];
self.message
.iter()
.zip(message_circuit.iter_mut())
Expand Down
7 changes: 2 additions & 5 deletions benches/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ impl SpongeCircuit {
}

impl Circuit for SpongeCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
let mut w_message = [C::ZERO; WIDTH];
fn circuit(&self, composer: &mut Composer) -> Result<(), Error> {
let mut w_message = [Composer::ZERO; WIDTH];
w_message
.iter_mut()
.zip(self.message)
Expand Down
116 changes: 112 additions & 4 deletions src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ use bytecheck::CheckBytes;
#[cfg(feature = "rkyv-impl")]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(feature = "zk")]
pub use zk::{decrypt, encrypt};

const MESSAGE_CAPACITY: usize = 2;
const CIPHER_SIZE: usize = MESSAGE_CAPACITY + 1;
const CIPHER_BYTES_SIZE: usize = CIPHER_SIZE * BlsScalar::SIZE;
Expand Down Expand Up @@ -249,8 +252,113 @@ impl PoseidonCipher {
}
}

#[cfg(feature = "alloc")]
mod zk;
#[cfg(feature = "zk")]
mod zk {
use super::PoseidonCipher;
use dusk_hades::GadgetStrategy;

use dusk_plonk::prelude::*;

impl PoseidonCipher {
/// Returns the initial state of the encryption within a composer
/// circuit
pub fn initial_state_circuit(
composer: &mut Composer,
ks0: Witness,
ks1: Witness,
nonce: Witness,
) -> [Witness; dusk_hades::WIDTH] {
let domain = BlsScalar::from_raw([0x100000000u64, 0, 0, 0]);
let domain = composer.append_constant(domain);

let length = BlsScalar::from_raw([
PoseidonCipher::capacity() as u64,
0,
0,
0,
]);
let length = composer.append_constant(length);

[domain, length, ks0, ks1, nonce]
}
}

/// Given a shared secret calculated using any key protocol compatible with
/// bls and jubjub, perform the encryption of the message.
///
/// The returned set of variables is the cipher text
pub fn encrypt(
composer: &mut Composer,
shared_secret: &WitnessPoint,
nonce: Witness,
message: &[Witness],
) -> [Witness; PoseidonCipher::cipher_size()] {
let ks0 = *shared_secret.x();
let ks1 = *shared_secret.y();

#[cfg(feature = "alloc")]
pub use zk::{decrypt, encrypt};
let mut cipher = [Composer::ZERO; PoseidonCipher::cipher_size()];

let mut state =
PoseidonCipher::initial_state_circuit(composer, ks0, ks1, nonce);

GadgetStrategy::gadget(composer, &mut state);

(0..PoseidonCipher::capacity()).for_each(|i| {
let x = if i < message.len() {
message[i]
} else {
Composer::ZERO
};

let constraint =
Constraint::new().left(1).a(state[i + 1]).right(1).b(x);

state[i + 1] = composer.gate_add(constraint);

cipher[i] = state[i + 1];
});

GadgetStrategy::gadget(composer, &mut state);
cipher[PoseidonCipher::capacity()] = state[1];

cipher
}

/// Given a shared secret calculated using any key protocol compatible with
/// bls and jubjub, perform the decryption of the cipher.
///
/// The returned set of variables is the original message
pub fn decrypt(
composer: &mut Composer,
shared_secret: &WitnessPoint,
nonce: Witness,
cipher: &[Witness],
) -> [Witness; PoseidonCipher::capacity()] {
let ks0 = *shared_secret.x();
let ks1 = *shared_secret.y();

let mut message = [Composer::ZERO; PoseidonCipher::capacity()];
let mut state =
PoseidonCipher::initial_state_circuit(composer, ks0, ks1, nonce);

GadgetStrategy::gadget(composer, &mut state);

(0..PoseidonCipher::capacity()).for_each(|i| {
let constraint = Constraint::new()
.left(1)
.a(cipher[i])
.right(-BlsScalar::one())
.b(state[i + 1]);

message[i] = composer.gate_add(constraint);

state[i + 1] = cipher[i];
});

GadgetStrategy::gadget(composer, &mut state);

composer.assert_equal(cipher[PoseidonCipher::capacity()], state[1]);

message
}
}
Loading

0 comments on commit 89dfae6

Please sign in to comment.