Skip to content

Commit

Permalink
refactor: move symmetric module to penumbra-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Jul 6, 2023
1 parent 116af9b commit f79b25d
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/core/component/dex/src/swap/ciphertext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;

use penumbra_keys::keys::OutgoingViewingKey;
use penumbra_shielded_pool::{note, PayloadKey};
use penumbra_keys::{keys::OutgoingViewingKey, PayloadKey};
use penumbra_shielded_pool::note;

use super::{SwapPlaintext, SWAP_CIPHERTEXT_BYTES, SWAP_LEN_BYTES};

Expand Down
4 changes: 2 additions & 2 deletions crates/core/component/dex/src/swap/plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use rand_core::{CryptoRng, RngCore};

use decaf377_ka as ka;
use penumbra_asset::{asset, Value, ValueVar};
use penumbra_keys::{keys::OutgoingViewingKey, Address, AddressVar};
use penumbra_keys::{keys::OutgoingViewingKey, Address, AddressVar, PayloadKey};
use penumbra_num::{Amount, AmountVar};
use penumbra_shielded_pool::{Note, PayloadKey, Rseed};
use penumbra_shielded_pool::{Note, Rseed};
use penumbra_tct::r1cs::StateCommitmentVar;

use crate::{BatchSwapOutputData, TradingPair, TradingPairVar};
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/governance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ penumbra-proof-params = { path = "../../../crypto/proof-params", default-feature
penumbra-crypto = { path = "../../crypto", default-features = false }
penumbra-sct = { path = "../sct", default-features = false }
penumbra-component = { path = "../component", optional = true }
penumbra-shielded-pool = { path = "../shielded-pool", optional = true }
penumbra-shielded-pool = { path = "../shielded-pool", default-features = false }
penumbra-chain = { path = "../chain", default-features = false }
penumbra-asset = { path = "../../../core/asset", default-features = false }
penumbra-keys = { path = "../../../core/keys", default-features = false }
Expand Down
2 changes: 0 additions & 2 deletions crates/core/component/shielded-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ pub mod state_key;
pub mod note;
mod note_payload;
pub mod rseed;
pub mod symmetric;

pub use note::{Note, NoteCiphertext, NoteView};
pub use note_payload::NotePayload;
pub use rseed::Rseed;
pub use symmetric::PayloadKey;

pub mod nullifier_derivation;
pub mod output;
Expand Down
6 changes: 2 additions & 4 deletions crates/core/component/shielded-pool/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use once_cell::sync::Lazy;
use penumbra_chain::genesis::Allocation;
use penumbra_keys::{
keys::{Diversifier, FullViewingKey, IncomingViewingKey, OutgoingViewingKey},
symmetric::{OutgoingCipherKey, OvkWrappedKey, PayloadKey, PayloadKind},
Address, AddressView,
};
use penumbra_proto::core::crypto::v1alpha1 as pb;
Expand All @@ -24,10 +25,7 @@ pub use penumbra_tct::StateCommitment;
use penumbra_asset::{asset, balance, Value, ValueView};
use penumbra_num::Amount;

use crate::{
symmetric::{OutgoingCipherKey, OvkWrappedKey, PayloadKey, PayloadKind},
NotePayload, Rseed,
};
use crate::{NotePayload, Rseed};

pub const NOTE_LEN_BYTES: usize = 160;
pub const NOTE_CIPHERTEXT_BYTES: usize = 176;
Expand Down
6 changes: 2 additions & 4 deletions crates/core/component/shielded-pool/src/output/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use anyhow::{Context, Error};
use bytes::Bytes;
use penumbra_asset::balance;
use penumbra_crypto::{EffectHash, EffectingData, FieldExt};
use penumbra_keys::symmetric::{OvkWrappedKey, WrappedMemoKey};
use penumbra_proto::{
core::crypto::v1alpha1 as pbc, core::transaction::v1alpha1 as pb, DomainType, TypeUrl,
};

use crate::{
symmetric::{OvkWrappedKey, WrappedMemoKey},
NotePayload, OutputProof,
};
use crate::{NotePayload, OutputProof};

#[derive(Clone, Debug)]
pub struct Output {
Expand Down
12 changes: 7 additions & 5 deletions crates/core/component/shielded-pool/src/output/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use penumbra_asset::{Balance, Value, STAKING_TOKEN_ASSET_ID};
use penumbra_crypto::{ka, FieldExt, Fq, Fr};
use penumbra_keys::{
keys::{IncomingViewingKey, OutgoingViewingKey},
Address,
symmetric::WrappedMemoKey,
Address, PayloadKey,
};
use penumbra_proto::{core::transaction::v1alpha1 as pb, DomainType, TypeUrl};
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};

use super::{Body, Output, OutputProof};
use crate::{symmetric::WrappedMemoKey, Note, PayloadKey, Rseed};
use crate::{Note, Rseed};

/// A planned [`Output`](Output).
#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -173,12 +174,13 @@ impl TryFrom<pb::OutputPlan> for OutputPlan {
mod test {
use super::OutputPlan;
use penumbra_asset::Value;
use penumbra_keys::keys::{SeedPhrase, SpendKey};
use penumbra_keys::{
keys::{SeedPhrase, SpendKey},
PayloadKey,
};
use penumbra_proof_params::OUTPUT_PROOF_VERIFICATION_KEY;
use rand_core::OsRng;

use crate::PayloadKey;

#[test]
/// Check that a valid output proof passes the `penumbra_crypto` integrity checks successfully.
/// This test serves to anchor how an `OutputPlan` prepares its `OutputProof`, in particular
Expand Down
3 changes: 2 additions & 1 deletion crates/core/component/shielded-pool/src/output/view.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use penumbra_keys::PayloadKey;
use penumbra_proto::{core::transaction::v1alpha1 as pbt, DomainType, TypeUrl};
use serde::{Deserialize, Serialize};

use crate::NoteView;
use crate::Output;
use crate::{NoteView, PayloadKey};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "pbt::OutputView", into = "pbt::OutputView")]
Expand Down
1 change: 1 addition & 0 deletions crates/core/keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ decaf377-ka = { path = "../../crypto/decaf377-ka/" }
decaf377-fmd = { path = "../../crypto/decaf377-fmd/" }
penumbra-proto = { path = "../../proto/" }
penumbra-tct = { path = "../../crypto/tct/", features = ["r1cs"] }
penumbra-asset = { path = "../../core/asset" }

# Git deps
decaf377 = {version = "0.4", features = ["r1cs"] }
Expand Down
2 changes: 2 additions & 0 deletions crates/core/keys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use decaf377_rdsa as rdsa;
pub mod address;
pub mod keys;
pub mod prf;
pub mod symmetric;

pub use address::{Address, AddressVar, AddressView};
pub use keys::FullViewingKey;
pub use symmetric::PayloadKey;

fn fmt_hex<T: AsRef<[u8]>>(data: T, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", hex::encode(data))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::keys::{IncomingViewingKey, OutgoingViewingKey};
use anyhow::{anyhow, Result};
use chacha20poly1305::{
aead::{Aead, NewAead},
Expand All @@ -6,12 +7,10 @@ use chacha20poly1305::{
use decaf377::FieldExt;
use decaf377_ka as ka;
use penumbra_asset::balance;
use penumbra_keys::keys::{IncomingViewingKey, OutgoingViewingKey};
use penumbra_proto::core::transaction::v1alpha1::{self as pb};
use penumbra_tct::StateCommitment;
use rand::{CryptoRng, RngCore};

use crate::note;

pub const PAYLOAD_KEY_LEN_BYTES: usize = 32;
pub const OVK_WRAPPED_LEN_BYTES: usize = 48;
pub const MEMOKEY_WRAPPED_LEN_BYTES: usize = 48;
Expand All @@ -29,7 +28,7 @@ pub enum PayloadKind {
}

impl PayloadKind {
pub(crate) fn nonce(&self, commitment: Option<note::StateCommitment>) -> [u8; 12] {
pub(crate) fn nonce(&self, commitment: Option<StateCommitment>) -> [u8; 12] {
match self {
Self::Note => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Self::MemoKey => [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand Down Expand Up @@ -103,7 +102,7 @@ impl PayloadKey {
}

/// Use Blake2b-256 to derive an encryption key from the OVK and public fields for swaps.
pub fn derive_swap(ovk: &OutgoingViewingKey, cm: note::StateCommitment) -> Self {
pub fn derive_swap(ovk: &OutgoingViewingKey, cm: StateCommitment) -> Self {
let cm_bytes: [u8; 32] = cm.into();

let mut kdf_params = blake2b_simd::Params::new();
Expand All @@ -118,7 +117,7 @@ impl PayloadKey {
}

/// Encrypt a swap using the `PayloadKey`.
pub fn encrypt_swap(&self, plaintext: Vec<u8>, commitment: note::StateCommitment) -> Vec<u8> {
pub fn encrypt_swap(&self, plaintext: Vec<u8>, commitment: StateCommitment) -> Vec<u8> {
let cipher = ChaCha20Poly1305::new(&self.0);
let nonce_bytes = PayloadKind::Swap.nonce(Some(commitment));
let nonce = Nonce::from_slice(&nonce_bytes);
Expand All @@ -132,7 +131,7 @@ impl PayloadKey {
pub fn decrypt_swap(
&self,
ciphertext: Vec<u8>,
commitment: note::StateCommitment,
commitment: StateCommitment,
) -> Result<Vec<u8>> {
let cipher = ChaCha20Poly1305::new(&self.0);

Expand Down Expand Up @@ -194,10 +193,10 @@ pub struct OutgoingCipherKey(Key);

impl OutgoingCipherKey {
/// Use Blake2b-256 to derive an encryption key `ock` from the OVK and public fields.
pub(crate) fn derive(
pub fn derive(
ovk: &OutgoingViewingKey,
cv: balance::Commitment,
cm: note::StateCommitment,
cm: StateCommitment,
epk: &ka::Public,
) -> Self {
let cv_bytes: [u8; 32] = cv.into();
Expand Down
4 changes: 2 additions & 2 deletions crates/core/transaction/src/effect_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use penumbra_dex::{
swap, swap_claim, TradingPair,
};
use penumbra_fee::Fee;
use penumbra_keys::FullViewingKey;
use penumbra_keys::{FullViewingKey, PayloadKey};
use penumbra_proto::DomainType;
use penumbra_shielded_pool::{NotePayload, PayloadKey};
use penumbra_shielded_pool::NotePayload;
use penumbra_stake::{Delegate, Undelegate, UndelegateClaimBody};

use crate::{
Expand Down
10 changes: 5 additions & 5 deletions crates/core/transaction/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use rand_core::OsRng;

use decaf377_ka as ka;
use penumbra_asset::balance;
use penumbra_keys::{keys::OutgoingViewingKey, Address};
use penumbra_proto::core::transaction::v1alpha1 as pbt;
use penumbra_shielded_pool::{
note,
use penumbra_keys::{
keys::OutgoingViewingKey,
symmetric::{OvkWrappedKey, PayloadKey, PayloadKind, WrappedMemoKey},
Note,
Address,
};
use penumbra_proto::core::transaction::v1alpha1 as pbt;
use penumbra_shielded_pool::{note, Note};

pub const MEMO_CIPHERTEXT_LEN_BYTES: usize = 528;

Expand Down
3 changes: 1 addition & 2 deletions crates/core/transaction/src/plan/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use anyhow::{Context, Result};
use ark_ff::Zero;
use decaf377::Fr;
use decaf377_rdsa as rdsa;
use penumbra_keys::FullViewingKey;
use penumbra_shielded_pool::symmetric::PayloadKey;
use penumbra_keys::{symmetric::PayloadKey, FullViewingKey};
use rand_core::{CryptoRng, RngCore};

use super::TransactionPlan;
Expand Down
3 changes: 1 addition & 2 deletions crates/core/transaction/src/plan/memo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Context;
use penumbra_keys::Address;
use penumbra_keys::{symmetric::PayloadKey, Address};
use penumbra_proto::{core::transaction::v1alpha1 as pb, DomainType, TypeUrl};
use penumbra_shielded_pool::symmetric::PayloadKey;

use rand::{CryptoRng, RngCore};

Expand Down
4 changes: 2 additions & 2 deletions crates/core/transaction/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ use penumbra_dex::{
};
use penumbra_fee::Fee;
use penumbra_ibc::IbcAction;
use penumbra_keys::FullViewingKey;
use penumbra_keys::{FullViewingKey, PayloadKey};
use penumbra_proto::{core::transaction::v1alpha1 as pbt, DomainType, Message, TypeUrl};
use penumbra_sct::Nullifier;
use penumbra_shielded_pool::{Note, Output, PayloadKey, Spend};
use penumbra_shielded_pool::{Note, Output, Spend};
use penumbra_stake::{Delegate, Undelegate, UndelegateClaim};
use penumbra_tct as tct;
use penumbra_tct::StateCommitment;
Expand Down
4 changes: 2 additions & 2 deletions crates/core/transaction/src/view/transaction_perspective.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use penumbra_asset::asset;
use penumbra_keys::AddressView;
use penumbra_keys::{AddressView, PayloadKey};
use penumbra_proto::core::transaction::v1alpha1::{
self as pb, NullifierWithNote, PayloadKeyWithCommitment,
};
use penumbra_sct::Nullifier;
use penumbra_shielded_pool::{note, Note, NoteView, PayloadKey};
use penumbra_shielded_pool::{note, Note, NoteView};

use std::collections::BTreeMap;

Expand Down

0 comments on commit f79b25d

Please sign in to comment.