2023-04-12 00:24:27 +00:00
|
|
|
use core::fmt::Debug;
|
2023-04-14 00:35:55 +00:00
|
|
|
use std::{io, collections::HashMap};
|
2023-04-11 17:42:18 +00:00
|
|
|
|
2023-07-14 18:05:12 +00:00
|
|
|
use zeroize::Zeroize;
|
2023-04-11 17:42:18 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2023-04-11 23:03:36 +00:00
|
|
|
use blake2::{Digest, Blake2b512};
|
|
|
|
|
2023-07-14 18:05:12 +00:00
|
|
|
use ciphersuite::{
|
|
|
|
group::{Group, GroupEncoding},
|
|
|
|
Ciphersuite, Ristretto,
|
|
|
|
};
|
2023-04-11 17:42:18 +00:00
|
|
|
use schnorr::SchnorrSignature;
|
|
|
|
|
2023-04-14 00:35:55 +00:00
|
|
|
use crate::{TRANSACTION_SIZE_LIMIT, ReadWrite};
|
2023-04-11 17:42:18 +00:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Error)]
|
|
|
|
pub enum TransactionError {
|
2023-04-14 00:35:55 +00:00
|
|
|
/// Transaction exceeded the size limit.
|
2023-04-20 10:27:00 +00:00
|
|
|
#[error("transaction is too large")]
|
2023-04-14 00:35:55 +00:00
|
|
|
TooLargeTransaction,
|
2023-04-20 10:27:00 +00:00
|
|
|
/// Transaction's signer isn't a participant.
|
2023-04-12 22:04:28 +00:00
|
|
|
#[error("invalid signer")]
|
|
|
|
InvalidSigner,
|
2023-04-20 10:27:00 +00:00
|
|
|
/// Transaction's nonce isn't the prior nonce plus one.
|
2023-04-12 22:04:28 +00:00
|
|
|
#[error("invalid nonce")]
|
|
|
|
InvalidNonce,
|
2023-04-20 10:27:00 +00:00
|
|
|
/// Transaction's signature is invalid.
|
2023-04-12 22:04:28 +00:00
|
|
|
#[error("invalid signature")]
|
|
|
|
InvalidSignature,
|
2023-04-20 10:27:00 +00:00
|
|
|
/// Transaction's content is invalid.
|
|
|
|
#[error("transaction content is invalid")]
|
|
|
|
InvalidContent,
|
2023-10-15 01:50:11 +00:00
|
|
|
/// Transaction's signer has too many transactions in the mempool.
|
|
|
|
#[error("signer has too many transactions in the mempool")]
|
|
|
|
TooManyInMempool,
|
|
|
|
/// Provided Transaction added to mempool.
|
|
|
|
#[error("provided transaction added to mempool")]
|
|
|
|
ProvidedAddedToMempool,
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 23:03:36 +00:00
|
|
|
/// Data for a signed transaction.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Signed {
|
|
|
|
pub signer: <Ristretto as Ciphersuite>::G,
|
|
|
|
pub nonce: u32,
|
|
|
|
pub signature: SchnorrSignature<Ristretto>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadWrite for Signed {
|
|
|
|
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
|
|
|
|
let signer = Ristretto::read_G(reader)?;
|
|
|
|
|
|
|
|
let mut nonce = [0; 4];
|
|
|
|
reader.read_exact(&mut nonce)?;
|
|
|
|
let nonce = u32::from_le_bytes(nonce);
|
2023-04-12 16:15:38 +00:00
|
|
|
if nonce >= (u32::MAX - 1) {
|
2023-11-19 23:01:13 +00:00
|
|
|
Err(io::Error::other("nonce exceeded limit"))?;
|
2023-04-12 16:15:38 +00:00
|
|
|
}
|
2023-04-11 23:03:36 +00:00
|
|
|
|
2023-07-14 18:05:12 +00:00
|
|
|
let mut signature = SchnorrSignature::<Ristretto>::read(reader)?;
|
|
|
|
if signature.R.is_identity().into() {
|
|
|
|
// Anyone malicious could remove this and try to find zero signatures
|
|
|
|
// We should never produce zero signatures though meaning this should never come up
|
|
|
|
// If it does somehow come up, this is a decent courtesy
|
|
|
|
signature.zeroize();
|
2023-11-19 23:01:13 +00:00
|
|
|
Err(io::Error::other("signature nonce was identity"))?;
|
2023-07-14 18:05:12 +00:00
|
|
|
}
|
2023-04-11 23:03:36 +00:00
|
|
|
|
|
|
|
Ok(Signed { signer, nonce, signature })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
|
2023-07-14 18:05:12 +00:00
|
|
|
// This is either an invalid signature or a private key leak
|
|
|
|
if self.signature.R.is_identity().into() {
|
2023-11-19 23:01:13 +00:00
|
|
|
Err(io::Error::other("signature nonce was identity"))?;
|
2023-07-14 18:05:12 +00:00
|
|
|
}
|
2023-04-11 23:03:36 +00:00
|
|
|
writer.write_all(&self.signer.to_bytes())?;
|
|
|
|
writer.write_all(&self.nonce.to_le_bytes())?;
|
|
|
|
self.signature.write(writer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
2023-04-11 17:42:18 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2023-04-12 13:38:20 +00:00
|
|
|
pub enum TransactionKind<'a> {
|
2023-10-13 23:45:47 +00:00
|
|
|
/// This transaction should be provided by every validator, in an exact order.
|
2023-04-14 00:35:55 +00:00
|
|
|
///
|
2023-04-20 11:30:49 +00:00
|
|
|
/// The contained static string names the orderer to use. This allows two distinct provided
|
|
|
|
/// transaction kinds, without a synchronized order, to be ordered within their own kind without
|
|
|
|
/// requiring ordering with each other.
|
|
|
|
///
|
2023-04-14 00:35:55 +00:00
|
|
|
/// The only malleability is in when this transaction appears on chain. The block producer will
|
|
|
|
/// include it when they have it. Block verification will fail for validators without it.
|
|
|
|
///
|
2023-10-13 23:45:47 +00:00
|
|
|
/// If a supermajority of validators produce a commit for a block with a provided transaction
|
|
|
|
/// which isn't locally held, the block will be added to the local chain. When the transaction is
|
|
|
|
/// locally provided, it will be compared for correctness to the on-chain version
|
2023-04-20 11:30:49 +00:00
|
|
|
Provided(&'static str),
|
2023-04-11 17:42:18 +00:00
|
|
|
|
|
|
|
/// An unsigned transaction, only able to be included by the block producer.
|
Slash malevolent validators (#294)
* add slash tx
* ignore unsigned tx replays
* verify that provided evidence is valid
* fix clippy + fmt
* move application tx handling to another module
* partially handle the tendermint txs
* fix pr comments
* support unsigned app txs
* add slash target to the votes
* enforce provided, unsigned, signed tx ordering within a block
* bug fixes
* add unit test for tendermint txs
* bug fixes
* update tests for tendermint txs
* add tx ordering test
* tidy up tx ordering test
* cargo +nightly fmt
* Misc fixes from rebasing
* Finish resolving clippy
* Remove sha3 from tendermint-machine
* Resolve a DoS in SlashEvidence's read
Also moves Evidence from Vec<Message> to (Message, Option<Message>). That
should meet all requirements while being a bit safer.
* Make lazy_static a dev-depend for tributary
* Various small tweaks
One use of sort was inefficient, sorting unsigned || signed when unsigned was
already properly sorted. Given how the unsigned TXs were given a nonce of 0, an
unstable sort may swap places with an unsigned TX and a signed TX with a nonce
of 0 (leading to a faulty block).
The extra protection added here sorts signed, then concats.
* Fix Tributary tests I broke, start review on tendermint/tx.rs
* Finish reviewing everything outside tests and empty_signature
* Remove empty_signature
empty_signature led to corrupted local state histories. Unfortunately, the API
is only sane with a signature.
We now use the actual signature, which risks creating a signature over a
malicious message if we have ever have an invariant producing malicious
messages. Prior, we only signed the message after the local machine confirmed
it was okay per the local view of consensus.
This is tolerated/preferred over a corrupt state history since production of
such messages is already an invariant. TODOs are added to make handling of this
theoretical invariant further robust.
* Remove async_sequential for tokio::test
There was no competition for resources forcing them to be run sequentially.
* Modify block order test to be statistically significant without multiple runs
* Clean tests
---------
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
2023-08-21 04:28:23 +00:00
|
|
|
///
|
|
|
|
/// Once an Unsigned transaction is included on-chain, it may not be included again. In order to
|
|
|
|
/// have multiple Unsigned transactions with the same values included on-chain, some distinct
|
|
|
|
/// nonce must be included in order to cause a distinct hash.
|
2023-04-11 17:42:18 +00:00
|
|
|
Unsigned,
|
|
|
|
|
|
|
|
/// A signed transaction.
|
2023-04-12 13:38:20 +00:00
|
|
|
Signed(&'a Signed),
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|
|
|
|
|
Slash malevolent validators (#294)
* add slash tx
* ignore unsigned tx replays
* verify that provided evidence is valid
* fix clippy + fmt
* move application tx handling to another module
* partially handle the tendermint txs
* fix pr comments
* support unsigned app txs
* add slash target to the votes
* enforce provided, unsigned, signed tx ordering within a block
* bug fixes
* add unit test for tendermint txs
* bug fixes
* update tests for tendermint txs
* add tx ordering test
* tidy up tx ordering test
* cargo +nightly fmt
* Misc fixes from rebasing
* Finish resolving clippy
* Remove sha3 from tendermint-machine
* Resolve a DoS in SlashEvidence's read
Also moves Evidence from Vec<Message> to (Message, Option<Message>). That
should meet all requirements while being a bit safer.
* Make lazy_static a dev-depend for tributary
* Various small tweaks
One use of sort was inefficient, sorting unsigned || signed when unsigned was
already properly sorted. Given how the unsigned TXs were given a nonce of 0, an
unstable sort may swap places with an unsigned TX and a signed TX with a nonce
of 0 (leading to a faulty block).
The extra protection added here sorts signed, then concats.
* Fix Tributary tests I broke, start review on tendermint/tx.rs
* Finish reviewing everything outside tests and empty_signature
* Remove empty_signature
empty_signature led to corrupted local state histories. Unfortunately, the API
is only sane with a signature.
We now use the actual signature, which risks creating a signature over a
malicious message if we have ever have an invariant producing malicious
messages. Prior, we only signed the message after the local machine confirmed
it was okay per the local view of consensus.
This is tolerated/preferred over a corrupt state history since production of
such messages is already an invariant. TODOs are added to make handling of this
theoretical invariant further robust.
* Remove async_sequential for tokio::test
There was no competition for resources forcing them to be run sequentially.
* Modify block order test to be statistically significant without multiple runs
* Clean tests
---------
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
2023-08-21 04:28:23 +00:00
|
|
|
// TODO: Should this be renamed TransactionTrait now that a literal Transaction exists?
|
|
|
|
// Or should the literal Transaction be renamed to Event?
|
2023-04-13 22:43:03 +00:00
|
|
|
pub trait Transaction: 'static + Send + Sync + Clone + Eq + Debug + ReadWrite {
|
2023-04-12 15:13:48 +00:00
|
|
|
/// Return what type of transaction this is.
|
2023-04-12 13:38:20 +00:00
|
|
|
fn kind(&self) -> TransactionKind<'_>;
|
2023-04-12 15:13:48 +00:00
|
|
|
|
2023-04-12 00:24:27 +00:00
|
|
|
/// Return the hash of this transaction.
|
|
|
|
///
|
|
|
|
/// The hash must NOT commit to the signature.
|
2023-04-11 17:42:18 +00:00
|
|
|
fn hash(&self) -> [u8; 32];
|
|
|
|
|
2023-04-12 15:13:48 +00:00
|
|
|
/// Perform transaction-specific verification.
|
2023-04-11 17:42:18 +00:00
|
|
|
fn verify(&self) -> Result<(), TransactionError>;
|
2023-04-11 23:03:36 +00:00
|
|
|
|
2023-04-12 15:13:48 +00:00
|
|
|
/// Obtain the challenge for this transaction's signature.
|
|
|
|
///
|
|
|
|
/// Do not override this unless you know what you're doing.
|
2023-06-08 10:38:25 +00:00
|
|
|
///
|
|
|
|
/// Panics if called on non-signed transactions.
|
2023-04-11 23:03:36 +00:00
|
|
|
fn sig_hash(&self, genesis: [u8; 32]) -> <Ristretto as Ciphersuite>::F {
|
2023-06-08 10:38:25 +00:00
|
|
|
match self.kind() {
|
|
|
|
TransactionKind::Signed(Signed { signature, .. }) => {
|
|
|
|
<Ristretto as Ciphersuite>::F::from_bytes_mod_order_wide(
|
|
|
|
&Blake2b512::digest(
|
use half-aggregation for tm messages (#346)
* dalek 4.0
* cargo update
Moves to a version of Substrate which uses curve25519-dalek 4.0 (not a rc).
Doesn't yet update the repo to curve25519-dalek 4.0 (as a branch does) due
to the official schnorrkel using a conflicting curve25519-dalek. This would
prevent installation of frost-schnorrkel without a patch.
* use half-aggregation for tm messages
* fmt
* fix pr comments
* cargo update
Achieves three notable updates.
1) Resolves RUSTSEC-2022-0093 by updating libp2p-identity.
2) Removes 3 old rand crates via updating ed25519-dalek (a dependency of
libp2p-identity).
3) Sets serde_derive to 1.0.171 via updating to time 0.3.26 which pins at up to
1.0.171.
The last one is the most important. The former two are niceties.
serde_derive, since 1.0.171, ships a non-reproducible binary blob in what's a
complete compromise of supply chain security. This is done in order to reduce
compile times, yet also for the maintainer of serde (dtolnay) to leverage
serde's position as the 8th most downloaded crate to attempt to force changes
to the Rust build pipeline.
While dtolnay's contributions to Rust are respectable, being behind syn, quote,
and proc-macro2 (the top three crates by downloads), along with thiserror,
anyhow, async-trait, and more (I believe also being part of the Rust project),
they have unfortunately decided to refuse to listen to the community on this
issue (or even engage with counter-commentary). Given their political agenda
they seem to try to be accomplishing with force, I'd go as far as to call their
actions terroristic (as they're using the threat of the binary blob as
justification for cargo to ship 'proper' support for binary blobs).
This is arguably representative of dtolnay's past work on watt. watt was a wasm
interpreter to execute a pre-compiled proc macro. This would save the compile
time of proc macros, yet sandbox it so a full binary did not have to be run.
Unfortunately, watt (while decreasing compile times) fails to be a valid
solution to supply chain security (without massive ecosystem changes). It never
implemented reproducible builds for its wasm blobs, and a malicious wasm blob
could still fundamentally compromise a project. The only solution for an end
user to achieve a secure pipeline would be to locally build the project,
verifying the blob aligns, yet doing so would negate all advantages of the
blob.
dtolnay also seems to be giving up their role as a FOSS maintainer given that
serde no longer works in several environments. While FOSS maintainers are not
required to never implement breaking changes, the version number is still 1.0.
While FOSS maintainers are not required to follow semver, releasing a very
notable breaking change *without a new version number* in an ecosystem which
*does follow semver*, then refusing to acknowledge bugs as bugs with their work
does meet my personal definition of "not actively maintaining their existing
work". Maintenance would be to fix bugs, not introduce and ignore.
For now, serde > 1.0.171 has been banned. In the future, we may host a fork
without the blobs (yet with the patches). It may be necessary to ban all of
dtolnay's maintained crates, if they continue to force their agenda as such,
yet I hope this may be resolved within the next week or so.
Sources:
https://github.com/serde-rs/serde/issues/2538 - Binary blob discussion
This includes several reports of various workflows being broken.
https://github.com/serde-rs/serde/issues/2538#issuecomment-1682519944
dtolnay commenting that security should be resolved via Rust toolchain edits,
not via their own work being secure. This is why I say they're trying to
leverage serde in a political game.
https://github.com/serde-rs/serde/issues/2526 - Usage via git broken
dtolnay explicitly asks the submitting user if they'd be willing to advocate
for changes to Rust rather than actually fix the issue they created. This is
further political arm wrestling.
https://github.com/serde-rs/serde/issues/2530 - Usage via Bazel broken
https://github.com/serde-rs/serde/issues/2575 - Unverifiable binary blob
https://github.com/dtolnay/watt - dtolnay's prior work on precompilation
* add Rs() api to SchnorrAggregate
* Correct serai-processor-tests to dalek 4
* fmt + deny
* Slash malevolent validators (#294)
* add slash tx
* ignore unsigned tx replays
* verify that provided evidence is valid
* fix clippy + fmt
* move application tx handling to another module
* partially handle the tendermint txs
* fix pr comments
* support unsigned app txs
* add slash target to the votes
* enforce provided, unsigned, signed tx ordering within a block
* bug fixes
* add unit test for tendermint txs
* bug fixes
* update tests for tendermint txs
* add tx ordering test
* tidy up tx ordering test
* cargo +nightly fmt
* Misc fixes from rebasing
* Finish resolving clippy
* Remove sha3 from tendermint-machine
* Resolve a DoS in SlashEvidence's read
Also moves Evidence from Vec<Message> to (Message, Option<Message>). That
should meet all requirements while being a bit safer.
* Make lazy_static a dev-depend for tributary
* Various small tweaks
One use of sort was inefficient, sorting unsigned || signed when unsigned was
already properly sorted. Given how the unsigned TXs were given a nonce of 0, an
unstable sort may swap places with an unsigned TX and a signed TX with a nonce
of 0 (leading to a faulty block).
The extra protection added here sorts signed, then concats.
* Fix Tributary tests I broke, start review on tendermint/tx.rs
* Finish reviewing everything outside tests and empty_signature
* Remove empty_signature
empty_signature led to corrupted local state histories. Unfortunately, the API
is only sane with a signature.
We now use the actual signature, which risks creating a signature over a
malicious message if we have ever have an invariant producing malicious
messages. Prior, we only signed the message after the local machine confirmed
it was okay per the local view of consensus.
This is tolerated/preferred over a corrupt state history since production of
such messages is already an invariant. TODOs are added to make handling of this
theoretical invariant further robust.
* Remove async_sequential for tokio::test
There was no competition for resources forcing them to be run sequentially.
* Modify block order test to be statistically significant without multiple runs
* Clean tests
---------
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
* Add DSTs to Tributary TX sig_hash functions
Prevents conflicts with other systems/other parts of the Tributary.
---------
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
2023-08-21 05:22:00 +00:00
|
|
|
[
|
|
|
|
b"Tributary Signed Transaction",
|
|
|
|
genesis.as_ref(),
|
|
|
|
&self.hash(),
|
|
|
|
signature.R.to_bytes().as_ref(),
|
|
|
|
]
|
|
|
|
.concat(),
|
2023-06-08 10:38:25 +00:00
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => panic!("sig_hash called on non-signed transaction"),
|
|
|
|
}
|
2023-04-11 23:03:36 +00:00
|
|
|
}
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 16:42:23 +00:00
|
|
|
// This will only cause mutations when the transaction is valid
|
2023-04-11 17:42:18 +00:00
|
|
|
pub(crate) fn verify_transaction<T: Transaction>(
|
|
|
|
tx: &T,
|
2023-04-11 23:03:36 +00:00
|
|
|
genesis: [u8; 32],
|
2023-04-11 17:42:18 +00:00
|
|
|
next_nonces: &mut HashMap<<Ristretto as Ciphersuite>::G, u32>,
|
|
|
|
) -> Result<(), TransactionError> {
|
2023-04-14 00:35:55 +00:00
|
|
|
if tx.serialize().len() > TRANSACTION_SIZE_LIMIT {
|
|
|
|
Err(TransactionError::TooLargeTransaction)?;
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:15:38 +00:00
|
|
|
tx.verify()?;
|
|
|
|
|
2023-04-11 17:42:18 +00:00
|
|
|
match tx.kind() {
|
2023-04-20 11:30:49 +00:00
|
|
|
TransactionKind::Provided(_) => {}
|
2023-04-11 17:42:18 +00:00
|
|
|
TransactionKind::Unsigned => {}
|
2023-04-11 23:03:36 +00:00
|
|
|
TransactionKind::Signed(Signed { signer, nonce, signature }) => {
|
2023-04-12 16:42:23 +00:00
|
|
|
if let Some(next_nonce) = next_nonces.get(signer) {
|
|
|
|
if nonce != next_nonce {
|
2023-04-12 22:04:28 +00:00
|
|
|
Err(TransactionError::InvalidNonce)?;
|
2023-04-12 16:42:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not a participant
|
2023-04-12 22:04:28 +00:00
|
|
|
Err(TransactionError::InvalidSigner)?;
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use Schnorr half-aggregation and a batch verification here
|
2023-04-12 13:38:20 +00:00
|
|
|
if !signature.verify(*signer, tx.sig_hash(genesis)) {
|
2023-04-12 22:04:28 +00:00
|
|
|
Err(TransactionError::InvalidSignature)?;
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|
2023-04-12 16:15:38 +00:00
|
|
|
|
|
|
|
next_nonces.insert(*signer, nonce + 1);
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:15:38 +00:00
|
|
|
Ok(())
|
2023-04-11 17:42:18 +00:00
|
|
|
}
|