Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ceremony Utilities #213

Merged
merged 22 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .config/hakari.toml

This file was deleted.

1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ Before we can merge this PR, please make sure that all the following items have
- [ ] Added **one** line describing your change in [`CHANGELOG.md`](https://github.com/manta-network/manta-rs/blob/main/CHANGELOG.md) and added the appropriate `changelog` label to the PR.
- [ ] Re-reviewed `Files changed` in the GitHub PR explorer.
- [ ] Checked that changes and commits conform to the standards outlined in [`CONTRIBUTING.md`](https://github.com/manta-network/manta-rs/blob/main/CONTRIBUTING.md).
- [ ] Ran `cargo hakari generate` to update the `workspace-hack` system
15 changes: 5 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ env:
RUSTFLAGS: -D warnings
RUST_BACKTRACE: full
jobs:
workspace-hack-check:
name: Workspace Hack Check
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: rustup update nightly && rustup default nightly
- run: cargo install cargo-hakari
- run: cargo hakari manage-deps --dry-run
format:
name: Format
runs-on: ubuntu-latest
Expand All @@ -38,7 +29,6 @@ jobs:
- uses: actions/checkout@v3
- run: rustup update nightly && rustup default nightly
- run: cargo install cargo-sort
- run: cargo sort workspace-hack
- run: cargo sort --workspace --check
docs:
name: Build Documentation
Expand All @@ -62,6 +52,11 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }} && rustup component add clippy
- run: cargo install cargo-hakari
- run: cargo hakari init workspace-hack --yes
- run: cargo hakari generate
- run: cargo hakari manage-deps --yes
- run: cargo hakari verify
- run: cargo install cargo-hack
- run: cargo hack clippy --workspace --feature-powerset
- run: cargo hack clippy --workspace --feature-powerset --bins
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]
### Added
- [\#213](https://github.com/Manta-Network/manta-rs/pull/197) Add Ceremony Utilities
- [\#206](https://github.com/Manta-Network/manta-rs/pull/206) Move Poseidon sage script to test the hardcoded round constant values.
- [\#172](https://github.com/Manta-Network/manta-rs/pull/172) Add abstract Phase 2 for Groth16 trusted setup
- [\#196](https://github.com/Manta-Network/manta-rs/pull/172) Add fixed base scalar multiplication using precomputed bases
Expand Down
6 changes: 1 addition & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ We use pull-request templates to standardize the PR process. See the [`PULL_REQU

### CI Pipeline

When writing a new PR, the Continuous Integration (CI) system will trigger linting and tests to run on every commit. See the [`.github/workflows/ci.yml`](./.github/workflows/ci.yml) for more detail on this workflow and see the [`workspace-hack`](./workspace-hack/) directory for dependency upgrade issues.

#### Updating the Dependency List

When a dependency on `Cargo.toml` needs to be updated, the CI for a PR will fail because `cargo hakari generate --diff` will return with error code `1`. In this case, the `cargo hakari generate` command should be run on a local machine and the updates pushed to the relevant development branch. Be sure to install `cargo-hakari` with `cargo install cargo-hakari`, just as in the [CI workflow](./../.github/workflows/ci.yml).
When writing a new PR, the Continuous Integration (CI) system will trigger linting and tests to run on every commit. See the [`.github/workflows/ci.yml`](./.github/workflows/ci.yml) for more detail on this workflow.

## Style Guide

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["manta-*", "workspace-hack"]
members = ["manta-*"]
exclude = ["forks/cocoon"]

[patch.crates-io]
Expand Down
1 change: 0 additions & 1 deletion manta-accounting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ manta-util = { path = "../manta-util", default-features = false, features = ["al
parking_lot = { version = "0.12.0", optional = true, default-features = false }
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
statrs = { version = "0.15.0", optional = true, default-features = false }
workspace-hack = { version = "0.1.0", path = "../workspace-hack" }

[dev-dependencies]
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom"] }
1 change: 0 additions & 1 deletion manta-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ manta-pay = { path = "../manta-pay", default-features = false, features = ["grot
wasm-bindgen = { version = "0.2.82", default-features = false }
wasm-bindgen-test = { version = "0.3.30", default-features = false }
web-sys = { version = "0.3.59", default-features = false, features = ["console"] }
workspace-hack = { version = "0.1.0", path = "../workspace-hack" }

[dev-dependencies]
criterion = { version = "0.3.4", default-features = false }
20 changes: 15 additions & 5 deletions manta-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ arkworks = [
"ark-serialize",
]

# Dalek Cryptography Backend
dalek = [
"ed25519-dalek",
]

# Enable `getrandom` Entropy Source
getrandom = ["rand_core/getrandom"]

# Serde
serde = ["manta-util/serde-alloc", "manta-util/serde-array"]
# Serde Serialization
serde = [
"manta-util/serde-alloc",
"manta-util/serde-array",
"rand_chacha?/serde1"
]

# Standard Library
std = ["manta-util/std"]
std = ["manta-util/std", "rand_chacha?/std"]

# Testing Frameworks
test = []
Expand All @@ -51,12 +60,13 @@ ark-ec = { version = "0.3.0", optional = true, default-features = false }
ark-ff = { version = "0.3.0", optional = true, default-features = false }
ark-r1cs-std = { version = "0.3.1", optional = true, default-features = false }
ark-relations = { version = "0.3.0", optional = true, default-features = false }
ark-serialize = { version = "0.3.0", optional = true, default-features = false }
ark-serialize = { version = "0.3.0", optional = true, default-features = false, features = ["derive"] }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
ed25519-dalek = { version = "1.0.1", optional = true, default-features = false, features = ["u64_backend"] }
manta-util = { path = "../manta-util", default-features = false, features = ["alloc"] }
rand = { version = "0.8.4", optional = true, default-features = false, features = ["alloc"] }
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
rand_core = { version = "0.6.3", default-features = false }
workspace-hack = { version = "0.1.0", path = "../workspace-hack" }

[dev-dependencies]
ark-bn254 = { version = "0.3.0", default-features = false, features = ["scalar_field"] }
Expand Down
138 changes: 138 additions & 0 deletions manta-crypto/src/dalek/ed25519.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2019-2022 Manta Network.
// This file is part of manta-rs.
//
// manta-rs is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// manta-rs is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

//! Dalek Cryptography `ed25519` Backend

use crate::{
rand::{CryptoRng, Rand, RngCore},
signature::{
MessageType, RandomnessType, Sign, SignatureType, SigningKeyType, Verify, VerifyingKeyType,
},
};
use core::marker::PhantomData;
use manta_util::AsBytes;

pub use ed25519_dalek::*;

/// Converts `bytes` into a [`SecretKey`].
#[inline]
pub fn secret_key_from_bytes(bytes: [u8; SECRET_KEY_LENGTH]) -> SecretKey {
match SecretKey::from_bytes(&bytes) {
Ok(secret_key) => secret_key,
_ => {
unreachable!("We are guaranteed the correct number of bytes from `SECRET_KEY_LENGTH`.")
}
}
}

/// Clones the `secret_key` by serializing and then deserializing.
#[inline]
pub fn clone_secret_key(secret_key: &SecretKey) -> SecretKey {
secret_key_from_bytes(secret_key.to_bytes())
}

/// Generates a [`Keypair`] from `secret_key`.
#[inline]
pub fn keypair(secret_key: &SecretKey) -> Keypair {
Keypair {
public: secret_key.into(),
secret: clone_secret_key(secret_key),
}
}

/// Generates a [`SecretKey`] from `rng`.
#[inline]
pub fn generate_secret_key<R>(rng: &mut R) -> SecretKey
where
R: CryptoRng + RngCore,
{
secret_key_from_bytes(rng.gen())
}

/// Generates a [`Keypair`] from `rng`.
#[inline]
pub fn generate_keypair<R>(rng: &mut R) -> Keypair
where
R: CryptoRng + RngCore,
{
let secret_key = generate_secret_key(rng);
Keypair {
public: (&secret_key).into(),
secret: secret_key,
}
}

/// Edwards Curve Signature Scheme for the `Curve25519` Elliptic Curve
#[derive(derivative::Derivative)]
#[derivative(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Ed25519<M>(PhantomData<M>);

impl<M> MessageType for Ed25519<M> {
type Message = M;
}

impl<M> RandomnessType for Ed25519<M> {
/// The `ed25519_dalek` crate provides randomness internally so we set it as `()` here.
type Randomness = ();
}

impl<M> SignatureType for Ed25519<M> {
type Signature = Signature;
}

impl<M> SigningKeyType for Ed25519<M> {
type SigningKey = SecretKey;
}

impl<M> VerifyingKeyType for Ed25519<M> {
type VerifyingKey = PublicKey;
}

impl<M> Sign for Ed25519<M>
where
M: AsBytes,
{
#[inline]
fn sign(
&self,
signing_key: &Self::SigningKey,
randomness: &Self::Randomness,
message: &Self::Message,
compiler: &mut (),
) -> Self::Signature {
let _ = (randomness, compiler);
keypair(signing_key).sign(&message.as_bytes())
}
}

impl<M> Verify for Ed25519<M>
where
M: AsBytes,
{
type Verification = Result<(), SignatureError>;

#[inline]
fn verify(
&self,
verifying_key: &Self::VerifyingKey,
message: &Self::Message,
signature: &Self::Signature,
compiler: &mut (),
) -> Self::Verification {
let _ = compiler;
verifying_key.verify(&message.as_bytes(), signature)
}
}
19 changes: 19 additions & 0 deletions manta-crypto/src/dalek/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2019-2022 Manta Network.
// This file is part of manta-rs.
//
// manta-rs is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// manta-rs is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

//! Dalek Cryptography Backend

pub mod ed25519;
4 changes: 4 additions & 0 deletions manta-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ pub mod signature;
#[cfg(feature = "arkworks")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "arkworks")))]
pub mod arkworks;

#[cfg(feature = "dalek")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "dalek")))]
pub mod dalek;
4 changes: 4 additions & 0 deletions manta-crypto/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ use manta_util::serde::{Deserialize, Serialize};

pub use rand_core::{block, CryptoRng, Error, RngCore, SeedableRng};

#[cfg(feature = "rand_chacha")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "rand_chacha")))]
pub use rand_chacha::*;

#[cfg(feature = "getrandom")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
#[doc(inline)]
Expand Down
61 changes: 61 additions & 0 deletions manta-crypto/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
//!
//! See the [`correctness`](test::correctness) test for more.

use core::{fmt::Debug, hash::Hash};

#[cfg(feature = "serde")]
use manta_util::serde::{Deserialize, Serialize};

pub mod convert;

/// Signing Key
Expand Down Expand Up @@ -116,6 +121,62 @@ where
/// Randomness Type
pub type Randomness<T> = <T as RandomnessType>::Randomness;

/// Signed Message
#[cfg_attr(
feature = "serde",
derive(Deserialize, Serialize),
serde(crate = "manta_util::serde", deny_unknown_fields)
)]
#[derive(derivative::Derivative)]
#[derivative(
Clone(bound = "T::Signature: Clone, T::Message: Clone"),
Copy(bound = "T::Signature: Copy, T::Message: Copy"),
Debug(bound = "T::Signature: Debug, T::Message: Debug"),
Default(bound = "T::Signature: Default, T::Message: Default"),
Eq(bound = "T::Signature: Eq, T::Message: Eq"),
Hash(bound = "T::Signature: Hash, T::Message: Hash"),
PartialEq(bound = "T::Signature: PartialEq, T::Message: PartialEq")
)]
pub struct SignedMessage<T>
where
T: MessageType + SignatureType,
{
/// Signature
pub signature: T::Signature,

/// Message
pub message: T::Message,
}

impl<T> SignedMessage<T>
where
T: MessageType + SignatureType,
{
/// Generates a new [`SignedMessage`] by signing `message` with `signing_key`.
#[inline]
pub fn new<COM>(
parameters: &T,
signing_key: &T::SigningKey,
randomness: &T::Randomness,
message: T::Message,
compiler: &mut COM,
) -> Self
where
T: Sign<COM>,
{
Self::new_unchecked(
parameters.sign(signing_key, randomness, &message, compiler),
message,
)
}

/// Builds a new [`SignedMessage`] without checking that `signature` is valid over `message`.
#[inline]
pub fn new_unchecked(signature: T::Signature, message: T::Message) -> Self {
Self { signature, message }
}
}

/// Signature Verifying Key Derivation Function
pub trait Derive<COM = ()>: SigningKeyType + VerifyingKeyType {
/// Derives the verifying key from `signing_key`.
Expand Down
1 change: 0 additions & 1 deletion manta-parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ std = ["anyhow?/std"]
anyhow = { version = "1.0.62", optional = true, default-features = false }
attohttpc = { version = "0.19.1", optional = true }
blake3 = { version = "1.3.1", default-features = false }
workspace-hack = { version = "0.1.0", path = "../workspace-hack" }

[dev-dependencies]
git2 = { version = "0.15.0", default-features = false }
Expand Down
Loading