From f4e7696f44f59207ada2fc99c0e1636ffa3a21f0 Mon Sep 17 00:00:00 2001 From: moana Date: Thu, 16 May 2024 13:49:27 +0200 Subject: [PATCH] Add 'phoenix-core` to a workspace called phoenix Resolves #171 --- .gitignore | 4 +- Cargo.toml | 55 ++----------------- README.md | 30 +++------- circuits/CHANGELOG.md | 52 ++++++++++++++++++ circuits/Cargo.toml | 15 +++++ circuits/README.md | 11 ++++ {src => circuits/src}/encryption/elgamal.rs | 12 ++-- {src => circuits/src}/encryption/mod.rs | 3 - circuits/src/lib.rs | 17 ++++++ tests/gadgets.rs => circuits/tests/elgamal.rs | 23 +++++++- CHANGELOG.md => core/CHANGELOG.md | 8 ++- core/Cargo.toml | 42 ++++++++++++++ core/README.md | 5 ++ {src => core/src}/convert.rs | 0 {src => core/src}/crossover.rs | 0 {src => core/src}/encryption/aes.rs | 0 core/src/encryption/mod.rs | 8 +++ {src => core/src}/error.rs | 0 {src => core/src}/fee.rs | 0 {src => core/src}/fee/remainder.rs | 0 {src => core/src}/keys.rs | 0 {src => core/src}/keys/public.rs | 0 {src => core/src}/keys/secret.rs | 0 {src => core/src}/keys/stealth.rs | 0 {src => core/src}/keys/view.rs | 0 {src => core/src}/lib.rs | 2 - {src => core/src}/note.rs | 0 {src => core/src}/transaction.rs | 0 {src => core/src}/transaction/stake.rs | 0 {src => core/src}/transaction/transfer.rs | 0 {tests => core/tests}/crossover.rs | 0 {tests => core/tests}/encryption.rs | 26 +-------- {tests => core/tests}/keys.rs | 0 {tests => core/tests}/note_test.rs | 0 {tests => core/tests}/transaction.rs | 0 35 files changed, 197 insertions(+), 116 deletions(-) create mode 100644 circuits/CHANGELOG.md create mode 100644 circuits/Cargo.toml create mode 100644 circuits/README.md rename {src => circuits/src}/encryption/elgamal.rs (89%) rename {src => circuits/src}/encryption/mod.rs (88%) create mode 100644 circuits/src/lib.rs rename tests/gadgets.rs => circuits/tests/elgamal.rs (81%) rename CHANGELOG.md => core/CHANGELOG.md (97%) create mode 100644 core/Cargo.toml create mode 100644 core/README.md rename {src => core/src}/convert.rs (100%) rename {src => core/src}/crossover.rs (100%) rename {src => core/src}/encryption/aes.rs (100%) create mode 100644 core/src/encryption/mod.rs rename {src => core/src}/error.rs (100%) rename {src => core/src}/fee.rs (100%) rename {src => core/src}/fee/remainder.rs (100%) rename {src => core/src}/keys.rs (100%) rename {src => core/src}/keys/public.rs (100%) rename {src => core/src}/keys/secret.rs (100%) rename {src => core/src}/keys/stealth.rs (100%) rename {src => core/src}/keys/view.rs (100%) rename {src => core/src}/lib.rs (95%) rename {src => core/src}/note.rs (100%) rename {src => core/src}/transaction.rs (100%) rename {src => core/src}/transaction/stake.rs (100%) rename {src => core/src}/transaction/transfer.rs (100%) rename {tests => core/tests}/crossover.rs (100%) rename {tests => core/tests}/encryption.rs (53%) rename {tests => core/tests}/keys.rs (100%) rename {tests => core/tests}/note_test.rs (100%) rename {tests => core/tests}/transaction.rs (100%) diff --git a/.gitignore b/.gitignore index 4cbb223..58f4d26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ -/target -**/*.rs.bk -/src/main.rs +**/target Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 5cf6568..8679fc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,51 +1,6 @@ -[package] -name = "phoenix-core" -version = "0.27.0" -authors = ["zer0 ", "Victor Lopez +[#171]: https://github.com/dusk-network/phoenix-core/issues/171 + + +[Unreleased]: https://github.com/dusk-network/phoenix-core/compare/v0.27.0...HEAD +[0.27.0]: https://github.com/dusk-network/phoenix-core/compare/v0.26.0...v0.27.0 +[0.26.0]: https://github.com/dusk-network/phoenix-core/compare/v0.25.0...v0.26.0 +[0.25.0]: https://github.com/dusk-network/phoenix-core/compare/v0.24.0...v0.25.0 +[0.24.0]: https://github.com/dusk-network/phoenix-core/compare/v0.23.0...v0.24.0 +[0.23.0]: https://github.com/dusk-network/phoenix-core/compare/v0.22.0...v0.23.0 +[0.22.0]: https://github.com/dusk-network/phoenix-core/compare/v0.21.0...v0.22.0 +[0.21.0]: https://github.com/dusk-network/phoenix-core/compare/v0.19.0...v0.21.0 +[0.19.0]: https://github.com/dusk-network/phoenix-core/compare/v0.18.1...v0.19.0 +[0.18.1]: https://github.com/dusk-network/phoenix-core/compare/v0.18.0...v0.18.1 +[0.18.0]: https://github.com/dusk-network/phoenix-core/compare/v0.17.1...v0.18.0 +[0.17.1]: https://github.com/dusk-network/phoenix-core/compare/v0.17.0...v0.17.1 +[0.17.0]: https://github.com/dusk-network/phoenix-core/compare/v0.12.0...v0.17.0 +[0.12.0]: https://github.com/dusk-network/phoenix-core/compare/v0.11.0...v0.12.0 +[0.11.0]: https://github.com/dusk-network/phoenix-core/compare/v0.10.0...v0.11.0 +[0.10.0]: https://github.com/dusk-network/phoenix-core/compare/v0.9.1...v0.10.0 +[0.9.1]: https://github.com/dusk-network/phoenix-core/compare/v0.9.0...v0.9.1 +[0.9.0]: https://github.com/dusk-network/phoenix-core/compare/v0.8.0...v0.9.0 +[0.8.0]: https://github.com/dusk-network/phoenix-core/compare/v0.7.4...v0.8.0 +[0.7.4]: https://github.com/dusk-network/phoenix-core/compare/v0.7.3...v0.7.4 +[0.7.3]: https://github.com/dusk-network/phoenix-core/compare/v0.7.2...v0.7.3 +[0.7.2]: https://github.com/dusk-network/phoenix-core/compare/v0.7.1...v0.7.2 +[0.7.1]: https://github.com/dusk-network/phoenix-core/compare/v0.7.0...v0.7.1 +[0.7.0]: https://github.com/dusk-network/phoenix-core/compare/v0.6.0...v0.7.0 +[0.6.0]: https://github.com/dusk-network/phoenix-core/compare/v0.5.1...v0.6.0 +[0.5.1]: https://github.com/dusk-network/phoenix-core/compare/v0.5.0...v0.5.1 +[0.5.0]: https://github.com/dusk-network/phoenix-core/compare/v0.3.1...v0.5.0 +[0.3.1]: https://github.com/dusk-network/phoenix-core/compare/v0.3.0...v0.3.1 +[0.3.0]: https://github.com/dusk-network/phoenix-core/compare/v0.2.0...v0.3.0 +[0.2.0]: https://github.com/dusk-network/phoenix-core/compare/v0.1.0...v0.2.0 +[0.1.0]: https://github.com/dusk-network/phoenix-core/releases/tag/v0.1.0 diff --git a/circuits/Cargo.toml b/circuits/Cargo.toml new file mode 100644 index 0000000..30ad9dd --- /dev/null +++ b/circuits/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "phoenix-circuits" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +phoenix-core = { path = "../core" } +dusk-plonk = { version = "0.19", default-features = false } +dusk-jubjub = { version = "0.14", default-features = false } + +[dev-dependencies] +ff = { version = "0.13", default-features = false } +rand_core = { version = "0.6", default-features = false } diff --git a/circuits/README.md b/circuits/README.md new file mode 100644 index 0000000..23caa9d --- /dev/null +++ b/circuits/README.md @@ -0,0 +1,11 @@ +# Phoenix Circuits + +Phoenix is the transaction model used by Dusk, an open-source public blockchain with a UTXO-based architecture that allows the execution of obfuscated transactions and confidential smart contracts. + +This library contains the implementation of the Phoenix-circuits, to prove, in zero-knowledge, that the following conditions hold true: + +1. Membership: every note that is about to be spent is included in the Merkle tree of notes. +2. Ownership: the sender holds the note secret key for every note that is about to be spent. +3. Nullification: the nullifier is calculated correctly. +4. Minting: the value commitment for the newly minted notes are computed correctly. +5. Balance integrity: the sum of the value of all spent notes is equal to the value of the sum of all minted notes + the gas fee + a crossover, where a crossover refers to funds being transfered to a contract. diff --git a/src/encryption/elgamal.rs b/circuits/src/encryption/elgamal.rs similarity index 89% rename from src/encryption/elgamal.rs rename to circuits/src/encryption/elgamal.rs index 7a53f27..f73e388 100644 --- a/src/encryption/elgamal.rs +++ b/circuits/src/encryption/elgamal.rs @@ -10,8 +10,6 @@ //! 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 @@ -27,8 +25,8 @@ pub fn encrypt( (ciphertext_1, ciphertext_2) } -/// Decrypts a ciphertext given a secret key, -/// returning a JubJubExtended plaintext +/// Decrypts a ciphertext given a secret key, returning a JubJubExtended +/// plaintext pub fn decrypt( secret_key: &JubJubScalar, ciphertext_1: &JubJubExtended, @@ -38,9 +36,8 @@ pub fn decrypt( ciphertext_2 - ciphertext_1 * secret_key } -/// Encrypt in-circuit a plaintext WitnessPoint, returning -/// a ciphertext (WitnessPoint, WitnessPoint) -#[cfg(feature = "zk")] +/// Encrypt in-circuit a plaintext WitnessPoint, returning a ciphertext +/// (WitnessPoint, WitnessPoint) pub fn encrypt_gadget( composer: &mut Composer, public_key: WitnessPoint, @@ -56,7 +53,6 @@ pub fn encrypt_gadget( /// Decrypt in-circuit a ciphertext (WitnessPoint, WitnessPoint), /// returning a plaintext WitnessPoint -#[cfg(feature = "zk")] pub fn decrypt_gadget( composer: &mut Composer, secret_key: Witness, diff --git a/src/encryption/mod.rs b/circuits/src/encryption/mod.rs similarity index 88% rename from src/encryption/mod.rs rename to circuits/src/encryption/mod.rs index 8c4fd2e..300d79f 100644 --- a/src/encryption/mod.rs +++ b/circuits/src/encryption/mod.rs @@ -4,8 +4,5 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -/// AES symmetric cipher -pub mod aes; - /// ElGamal asymmetric cipher pub mod elgamal; diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs new file mode 100644 index 0000000..9c0f7bc --- /dev/null +++ b/circuits/src/lib.rs @@ -0,0 +1,17 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +//! Phoenix's circuits and gadgets. + +#![allow(non_snake_case)] +#![deny(missing_docs)] +#![no_std] + +/// Encryption algorithm +mod encryption; + +/// ElGamal asymmetric cipher +pub use encryption::elgamal; diff --git a/tests/gadgets.rs b/circuits/tests/elgamal.rs similarity index 81% rename from tests/gadgets.rs rename to circuits/tests/elgamal.rs index d302bf0..83e6ed8 100644 --- a/tests/gadgets.rs +++ b/circuits/tests/elgamal.rs @@ -5,12 +5,31 @@ // Copyright (c) DUSK NETWORK. All rights reserved. use dusk_jubjub::{JubJubAffine, JubJubScalar, GENERATOR_EXTENDED}; +use dusk_plonk::prelude::*; use ff::Field; +use phoenix_circuits::elgamal; +use phoenix_core::{PublicKey, SecretKey}; use rand_core::OsRng; -use phoenix_core::{elgamal, PublicKey, SecretKey}; +#[test] +fn test_elgamal_encrypt_and_decrypt() { + let sk = SecretKey::random(&mut OsRng); + let pk = PublicKey::from(&sk); -use dusk_plonk::prelude::*; + let message = GENERATOR_EXTENDED * JubJubScalar::from(1234u64); + + // Encrypt using a fresh random value 'r' + let r = JubJubScalar::random(&mut OsRng); + let (c1, c2) = elgamal::encrypt(pk.A(), &message, &r); + + // Assert decryption + let dec_message = elgamal::decrypt(sk.a(), &c1, &c2); + assert_eq!(message, dec_message); + + // Assert decryption using an incorrect key + let dec_message_wrong = elgamal::decrypt(sk.b(), &c1, &c2); + assert_ne!(message, dec_message_wrong); +} static LABEL: &[u8; 12] = b"dusk-network"; const CAPACITY: usize = 13; // capacity required for the setup diff --git a/CHANGELOG.md b/core/CHANGELOG.md similarity index 97% rename from CHANGELOG.md rename to core/CHANGELOG.md index 0468a65..57584d1 100644 --- a/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Restructure `Encryption` module. +- Move phoenix-core into a phoenix workspace [#171] + +### Removed + +- Remove 'encryption::elgamal' module as it has been added to the 'phoenix-circuits' lib in the same workspace [#171] ## [0.27.0] - 2024-04-24 @@ -294,9 +299,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Canonical implementation shielded by feature. -[#162]: https://github.com/dusk-network/phoenix-core/issues/162 +[#171]: https://github.com/dusk-network/phoenix-core/issues/171 [#166]: https://github.com/dusk-network/phoenix-core/issues/166 [#165]: https://github.com/dusk-network/phoenix-core/issues/165 +[#162]: https://github.com/dusk-network/phoenix-core/issues/162 [#156]: https://github.com/dusk-network/phoenix-core/issues/156 [#155]: https://github.com/dusk-network/phoenix-core/issues/155 [#152]: https://github.com/dusk-network/phoenix-core/issues/152 diff --git a/core/Cargo.toml b/core/Cargo.toml new file mode 100644 index 0000000..4446509 --- /dev/null +++ b/core/Cargo.toml @@ -0,0 +1,42 @@ +[package] +name = "phoenix-core" +version = "0.27.0" +authors = ["zer0 ", "Victor Lopez