Skip to content

Commit

Permalink
Merge pull request #254 from dusk-network/mocello/236_vle
Browse files Browse the repository at this point in the history
Add encryption and decryption
  • Loading branch information
moCello authored Mar 21, 2024
2 parents f892c24 + d0f5c03 commit fe0e628
Show file tree
Hide file tree
Showing 20 changed files with 854 additions and 779 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: dusk-network/.github/.github/workflows/code-analysis.yml@main
with:
clippy_default: false
clippy_args: --features=rkyv/size_32
clippy_args: --all-features

analyze:
name: Dusk Analyzer
Expand All @@ -24,16 +24,16 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- run: cargo bench --features=cipher,zk --no-run
- run: cargo bench --all-features --no-run

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

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

## [Unreleased]

### Added

- Add variable length encryption and decryption [#236]
- Add variable length encryption and decryption gadgets [#236]
- Add `encryption` feature [#236]

### Removed

- Remove `PoseidonCipher` struct as it is replaced by encryption functions [#236]
- Remove `cipher` feature [#236]
- Remove `rkyv` dependency and all related features [#236]
- Remove `bytecheck` dependency [#236]
- Remove `dusk-bytes` dependency [#236]

### Changed

- Append the tag as a constant when initializing the gadget state [#236]

## [0.36.0] - 2024-03-13

### Added
Expand Down Expand Up @@ -478,6 +496,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#246]: https://github.com/dusk-network/poseidon252/issues/246
[#243]: https://github.com/dusk-network/poseidon252/issues/243
[#240]: https://github.com/dusk-network/poseidon252/issues/240
[#236]: https://github.com/dusk-network/poseidon252/issues/236
[#215]: https://github.com/dusk-network/poseidon252/issues/215
[#212]: https://github.com/dusk-network/poseidon252/issues/212
[#206]: https://github.com/dusk-network/poseidon252/issues/206
Expand Down
30 changes: 9 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,23 @@ edition = "2021"
license = "MPL-2.0"

[dependencies]
dusk-bls12_381 = { version = "0.13", default-features = false }
dusk-bls12_381 = { version = "0.13", default-features = false, features = ["zeroize"] }
dusk-jubjub = { version = "0.14", default-features = false }
dusk-bytes = "0.1"
dusk-plonk = { version = "0.19", default-features = false, features = ["alloc"], optional = true }
dusk-safe = "0.1"
rkyv = { version = "0.7", optional = true, default-features = false }
bytecheck = { version = "0.6", optional = true, default-features = false }
dusk-plonk = { version = "0.19.2-rc.0", default-features = false, features = ["alloc", "zeroize"], optional = true }
dusk-safe = "0.2.0-rc.0"

[dev-dependencies]
criterion = "0.3"
rand = { version = "0.8", default-features = false, features = ["getrandom", "std_rng"] }
ff = { version = "0.13", default-features = false }
once_cell = "1"
dusk-bytes = "0.1"

[features]
zk = [
"dusk-plonk",
]
cipher = []
size_16 = ["rkyv/size_16"]
size_32 = ["rkyv/size_32"]
size_64 = ["rkyv/size_64"]
rkyv-impl = [
"rkyv/validation",
"rkyv/alloc",
"rkyv",
"bytecheck",
"dusk-bls12_381/rkyv-impl"
]
encryption = ["dusk-safe/encryption"]

[profile.dev]
opt-level = 3
Expand All @@ -64,11 +52,11 @@ harness = false
required-features = ["zk"]

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

[[bench]]
name = "cipher_decrypt"
name = "decrypt"
harness = false
required-features = ["cipher", "zk"]
required-features = ["zk", "encryption"]
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ The library provides the two hashing techniques of Poseidon:
- The 'normal' hashing functionalities operating on `BlsScalar`.
- The 'gadget' hashing functionalities that build a circuit which outputs the hash.

## Example
## Examples

### Hash

```rust
use rand::rngs::StdRng;
Expand Down Expand Up @@ -50,13 +52,48 @@ let merkle_hash = Hash::digest(Domain::Merkle4, &input[..4]);
assert_ne!(merkle_hash, Hash::digest(Domain::Other, &input[..4]));
```

### Encryption

```rust
#![cfg(feature = "encryption")]

use dusk_bls12_381::BlsScalar;
use dusk_jubjub::{JubJubScalar, GENERATOR_EXTENDED, dhke};
use dusk_poseidon::{decrypt, encrypt, Error};
use ff::Field;
use rand::rngs::StdRng;
use rand::SeedableRng;

// generate the keys and nonce needed for the encryption
let mut rng = StdRng::seed_from_u64(0x42424242);
let alice_secret = JubJubScalar::random(&mut rng);
let alice_public = GENERATOR_EXTENDED * &alice_secret;
let bob_secret = JubJubScalar::random(&mut rng);
let bob_public = GENERATOR_EXTENDED * &bob_secret;
let nonce = BlsScalar::random(&mut rng);

// Alice encrypts a message of 3 BlsScalar using Diffie-Hellman key exchange
// with Bob's public key
let message = vec![BlsScalar::from(10), BlsScalar::from(20), BlsScalar::from(30)];
let shared_secret = dhke(&alice_secret, &bob_public);
let cipher = encrypt(&message, &shared_secret, &nonce)
.expect("Encryption should pass");

// Bob decrypts the cipher using Diffie-Hellman key exchange with Alice's public key
let shared_secret = dhke(&bob_secret, &alice_public);
let decrypted_message = decrypt(&cipher, &shared_secret, &nonce)
.expect("Decryption should pass");

assert_eq!(decrypted_message, message);
```

## Benchmarks

There are benchmarks for `sponge` and `cipher` in their native form, operating on `Scalar`, and as a zero-knowledge gadget, using `Witness`.
There are benchmarks for hashing, encrypting and decrypting in their native form, operating on `Scalar`, and for a zero-knowledge circuit proof generation and verification.

To run all benchmarks on your machine, run
```shell
cargo bench --features=zk,cipher
cargo bench --features=zk,encryption
```
in the repository.

Expand Down
112 changes: 0 additions & 112 deletions benches/cipher_encrypt.rs

This file was deleted.

Loading

0 comments on commit fe0e628

Please sign in to comment.