Skip to content

Commit

Permalink
crypto/encoding: swap dependency direction
Browse files Browse the repository at this point in the history
  • Loading branch information
emturner committed Jun 26, 2024
1 parent c086e19 commit 93f3fc3
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 167 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ members = [
"tezos-encoding-derive",
]

[workspace.dependencies.tezos_data_encoding]
version = "0.5.2"
path = "./tezos-encoding"
default-features = false

[workspace.dependencies.tezos_data_encoding_derive]
version = "0.5.2"
path = "./tezos-encoding-derive"
default-features = false

[workspace.dependencies.tezos_crypto_rs]
version = "0.5.2"
path = "./crypto"
default-features = false

[workspace.dependencies.nom]
nom = "7.1"

[profile.fuzz]
inherits = "release"
opt-level = 3
Expand Down
3 changes: 3 additions & 0 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ blst = { version = "0.3.12", optional = true }

proptest = { version = "1.1", optional = true }

tezos_data_encoding.workspace = true
nom.workspace = true

[dev-dependencies]
serde_json = "1.0"

Expand Down
6 changes: 5 additions & 1 deletion crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;
use zeroize::Zeroize;

mod encoding;

const CRYPTO_KEY_SIZE: usize = 32;

mod prefix_bytes {
Expand Down Expand Up @@ -339,7 +341,9 @@ unknown_sig!(Secp256k1Signature);
unknown_sig!(P256Signature);

/// Note: see Tezos ocaml lib_crypto/base58.ml
#[derive(Debug, Copy, Clone, PartialEq, Eq, strum_macros::AsRefStr)]
#[derive(
Debug, Copy, Clone, PartialEq, Eq, strum_macros::AsRefStr, strum_macros::IntoStaticStr,
)]
pub enum HashType {
// "\087\082\000" (* Net(15) *)
ChainId,
Expand Down
140 changes: 140 additions & 0 deletions crypto/src/hash/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) SimpleStaking, Viable Systems, Nomadic Labs and Tezedge Contributors
// SPDX-CopyrightText: 2022-2024 TriliTech <[email protected]>
//
// SPDX-License-Identifier: MIT

use super::*;
use tezos_data_encoding::enc::{BinResult, BinWriter};
use tezos_data_encoding::encoding::{Encoding, HasEncoding};
use tezos_data_encoding::nom::{NomReader, NomResult};

macro_rules! encode_hash {
($hash_name:ty) => {
impl BinWriter for $hash_name {
fn bin_write(&self, out: &mut Vec<u8>) -> BinResult {
use tezos_data_encoding::enc::*;

put_bytes(self.as_ref(), out);
Ok(())
}
}
};
}

encode_hash!(ChainId);
encode_hash!(BlockHash);
encode_hash!(BlockMetadataHash);
encode_hash!(BlockPayloadHash);
encode_hash!(OperationHash);
encode_hash!(OperationListListHash);
encode_hash!(OperationMetadataHash);
encode_hash!(OperationMetadataListListHash);
encode_hash!(ContextHash);
encode_hash!(ProtocolHash);
encode_hash!(ContractKt1Hash);
encode_hash!(ContractTz1Hash);
encode_hash!(ContractTz2Hash);
encode_hash!(ContractTz3Hash);
encode_hash!(ContractTz4Hash);
encode_hash!(CryptoboxPublicKeyHash);
encode_hash!(PublicKeyEd25519);
encode_hash!(PublicKeySecp256k1);
encode_hash!(PublicKeyP256);
encode_hash!(PublicKeyBls);
encode_hash!(SecretKeyEd25519);
encode_hash!(SecretKeyBls);
encode_hash!(UnknownSignature);
encode_hash!(Ed25519Signature);
encode_hash!(Secp256k1Signature);
encode_hash!(P256Signature);
encode_hash!(BlsSignature);
encode_hash!(NonceHash);
encode_hash!(SmartRollupHash);

macro_rules! hash_nom_reader {
($hash_name:ident) => {
impl<'a> NomReader<'a> for $hash_name {
#[inline(always)]
fn nom_read(input: &[u8]) -> NomResult<Self> {
use nom::{bytes::complete::take, combinator::map};

map(take(Self::hash_size()), |bytes| {
Self::try_from_bytes(bytes).unwrap()
})(input)
}
}
};
}

hash_nom_reader!(ChainId);
hash_nom_reader!(BlockHash);
hash_nom_reader!(BlockMetadataHash);
hash_nom_reader!(BlockPayloadHash);
hash_nom_reader!(OperationHash);
hash_nom_reader!(OperationListListHash);
hash_nom_reader!(OperationMetadataHash);
hash_nom_reader!(OperationMetadataListListHash);
hash_nom_reader!(ContextHash);
hash_nom_reader!(ProtocolHash);
hash_nom_reader!(ContractKt1Hash);
hash_nom_reader!(ContractTz1Hash);
hash_nom_reader!(ContractTz2Hash);
hash_nom_reader!(ContractTz3Hash);
hash_nom_reader!(ContractTz4Hash);
hash_nom_reader!(CryptoboxPublicKeyHash);
hash_nom_reader!(PublicKeyEd25519);
hash_nom_reader!(PublicKeySecp256k1);
hash_nom_reader!(PublicKeyP256);
hash_nom_reader!(PublicKeyBls);
hash_nom_reader!(SecretKeyEd25519);
hash_nom_reader!(SecretKeyBls);
hash_nom_reader!(UnknownSignature);
hash_nom_reader!(Ed25519Signature);
hash_nom_reader!(Secp256k1Signature);
hash_nom_reader!(P256Signature);
hash_nom_reader!(BlsSignature);
hash_nom_reader!(NonceHash);
hash_nom_reader!(SmartRollupHash);

macro_rules! hash_has_encoding {
($hash_name:ident, $enc_ref_name:ident) => {
impl HasEncoding for $hash_name {
fn encoding() -> Encoding {
Encoding::Hash($hash_name::hash_type().into())
}
}
};
}

hash_has_encoding!(ChainId, CHAIN_ID);
hash_has_encoding!(BlockHash, BLOCK_HASH);
hash_has_encoding!(BlockMetadataHash, BLOCK_METADATA_HASH);
hash_has_encoding!(BlockPayloadHash, BLOCK_PAYLOAD_HASH);
hash_has_encoding!(OperationHash, OPERATION_HASH);
hash_has_encoding!(OperationListListHash, OPERATION_LIST_LIST_HASH);
hash_has_encoding!(OperationMetadataHash, OPERATION_METADATA_HASH);
hash_has_encoding!(
OperationMetadataListListHash,
OPERATION_METADATA_LIST_LIST_HASH
);
hash_has_encoding!(ContextHash, CONTEXT_HASH);
hash_has_encoding!(ProtocolHash, PROTOCOL_HASH);
hash_has_encoding!(ContractKt1Hash, CONTRACT_KT1HASH);
hash_has_encoding!(ContractTz1Hash, CONTRACT_TZ1HASH);
hash_has_encoding!(ContractTz2Hash, CONTRACT_TZ2HASH);
hash_has_encoding!(ContractTz3Hash, CONTRACT_TZ3HASH);
hash_has_encoding!(ContractTz4Hash, CONTRACT_TZ4HASH);
hash_has_encoding!(CryptoboxPublicKeyHash, CRYPTOBOX_PUBLIC_KEY_HASH);
hash_has_encoding!(PublicKeyEd25519, PUBLIC_KEY_ED25519);
hash_has_encoding!(PublicKeySecp256k1, PUBLIC_KEY_SECP256K1);
hash_has_encoding!(PublicKeyP256, PUBLIC_KEY_P256);
hash_has_encoding!(PublicKeyBls, PUBLIC_KEY_BLS);
hash_has_encoding!(SecretKeyEd25519, SECRET_KEY_ED25519);
hash_has_encoding!(SecretKeyBls, SECRET_KEY_BLS);
hash_has_encoding!(UnknownSignature, UNKNOWN_SIGNATURE);
hash_has_encoding!(Ed25519Signature, ED25519_SIGNATURE_HASH);
hash_has_encoding!(Secp256k1Signature, SECP256K1_SIGNATURE_HASH);
hash_has_encoding!(P256Signature, P256_SIGNATURE_HASH);
hash_has_encoding!(BlsSignature, BLS_SIGNATURE_HASH);
hash_has_encoding!(NonceHash, NONCE_HASH);
hash_has_encoding!(SmartRollupHash, SMART_ROLLUP_HASH);
35 changes: 35 additions & 0 deletions crypto/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ use crate::hash::{
BlsSignature, Ed25519Signature, FromBytesError, HashTrait, HashType, P256Signature,
Secp256k1Signature, UnknownSignature,
};
use nom::Err;
use serde::{Deserialize, Serialize};
use tezos_data_encoding::enc::{BinResult, BinWriter};
use tezos_data_encoding::encoding::{Encoding, HasEncoding};
use tezos_data_encoding::nom::error::BoundedEncodingKind;
use tezos_data_encoding::nom::error::DecodeError;
use tezos_data_encoding::nom::{NomReader, NomResult};
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -201,6 +207,35 @@ impl TryFrom<Signature> for UnknownSignature {
}
}

impl BinWriter for Signature {
fn bin_write(&self, out: &mut Vec<u8>) -> BinResult {
use tezos_data_encoding::enc::*;

dynamic(bytes)(self, out)
}
}
impl HasEncoding for Signature {
fn encoding() -> Encoding {
Encoding::Custom
}
}

impl<'a> NomReader<'a> for Signature {
fn nom_read(input: &'a [u8]) -> NomResult<'a, Self> {
use tezos_data_encoding::nom::*;

let (rest, v) = dynamic(bytes)(input)?;
if let Ok(v) = Self::try_from(v) {
Ok((rest, v))
} else {
Err(Err::Error(DecodeError::limit(
input,
BoundedEncodingKind::Signature,
)))
}
}
}

#[cfg(test)]
mod test {
#[test]
Expand Down
7 changes: 1 addition & 6 deletions tezos-encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ hex = "0.4"
num-bigint = { version = "0.4", default-features = false }
num-traits = "0.2.8"
serde = { version = "1.0", features = ["derive"] }
nom = "7.1"
nom.workspace = true
bitvec = "1.0"
lazy_static = "1.4"

[dependencies.tezos_crypto_rs]
path = "../crypto"
version = "0.5.2"
default-features = false

[dependencies.tezos_data_encoding_derive]
path = "../tezos-encoding-derive"
version = "0.5.2"
Expand Down
47 changes: 0 additions & 47 deletions tezos-encoding/src/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,53 +318,6 @@ mod integers {

pub use integers::*;

macro_rules! encode_hash {
($hash_name:ty) => {
impl BinWriter for $hash_name {
fn bin_write(&self, out: &mut Vec<u8>) -> BinResult {
put_bytes(self.as_ref(), out);
Ok(())
}
}
};
}

encode_hash!(crypto::hash::ChainId);
encode_hash!(crypto::hash::BlockHash);
encode_hash!(crypto::hash::BlockMetadataHash);
encode_hash!(crypto::hash::BlockPayloadHash);
encode_hash!(crypto::hash::OperationHash);
encode_hash!(crypto::hash::OperationListListHash);
encode_hash!(crypto::hash::OperationMetadataHash);
encode_hash!(crypto::hash::OperationMetadataListListHash);
encode_hash!(crypto::hash::ContextHash);
encode_hash!(crypto::hash::ProtocolHash);
encode_hash!(crypto::hash::ContractKt1Hash);
encode_hash!(crypto::hash::ContractTz1Hash);
encode_hash!(crypto::hash::ContractTz2Hash);
encode_hash!(crypto::hash::ContractTz3Hash);
encode_hash!(crypto::hash::ContractTz4Hash);
encode_hash!(crypto::hash::CryptoboxPublicKeyHash);
encode_hash!(crypto::hash::PublicKeyEd25519);
encode_hash!(crypto::hash::PublicKeySecp256k1);
encode_hash!(crypto::hash::PublicKeyP256);
encode_hash!(crypto::hash::PublicKeyBls);
encode_hash!(crypto::hash::SecretKeyEd25519);
encode_hash!(crypto::hash::SecretKeyBls);
encode_hash!(crypto::hash::UnknownSignature);
encode_hash!(crypto::hash::Ed25519Signature);
encode_hash!(crypto::hash::Secp256k1Signature);
encode_hash!(crypto::hash::P256Signature);
encode_hash!(crypto::hash::BlsSignature);
encode_hash!(crypto::hash::NonceHash);
encode_hash!(crypto::hash::SmartRollupHash);

impl BinWriter for crypto::signature::Signature {
fn bin_write(&self, out: &mut Vec<u8>) -> BinResult {
dynamic(bytes)(self, out)
}
}

impl BinWriter for Mutez {
fn bin_write(&self, out: &mut Vec<u8>) -> BinResult {
n_bignum(self.0.magnitude(), out)
Expand Down
Loading

0 comments on commit 93f3fc3

Please sign in to comment.