Skip to content

Commit

Permalink
use half-aggregation for tm messages (#346)
Browse files Browse the repository at this point in the history
* 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:

serde-rs/serde#2538 - Binary blob discussion

This includes several reports of various workflows being broken.

serde-rs/serde#2538 (comment)

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.

serde-rs/serde#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.

serde-rs/serde#2530 - Usage via Bazel broken

serde-rs/serde#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>
  • Loading branch information
akildemir and kayabaNerve committed Aug 21, 2023
1 parent f161135 commit e319762
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 18 deletions.
55 changes: 45 additions & 10 deletions coordinator/tributary/src/tendermint/mod.rs
Expand Up @@ -18,7 +18,10 @@ use ciphersuite::{
},
Ciphersuite, Ristretto,
};
use schnorr::SchnorrSignature;
use schnorr::{
SchnorrSignature,
aggregate::{SchnorrAggregator, SchnorrAggregate},
};

use serai_db::Db;

Expand Down Expand Up @@ -46,6 +49,8 @@ use crate::{
pub mod tx;
use tx::{TendermintTx, VoteSignature};

const DST: &[u8] = b"Tributary Tendermint Commit Aggregator";

fn challenge(
genesis: [u8; 32],
key: [u8; 32],
Expand Down Expand Up @@ -159,8 +164,7 @@ impl Validators {
impl SignatureScheme for Validators {
type ValidatorId = [u8; 32];
type Signature = [u8; 64];
// TODO: Use half-aggregation.
type AggregateSignature = Vec<[u8; 64]>;
type AggregateSignature = Vec<u8>;
type Signer = Arc<Signer>;

#[must_use]
Expand All @@ -177,8 +181,23 @@ impl SignatureScheme for Validators {
actual_sig.verify(validator_point, challenge(self.genesis, validator, &sig[.. 32], msg))
}

fn aggregate(sigs: &[Self::Signature]) -> Self::AggregateSignature {
sigs.to_vec()
fn aggregate(
&self,
validators: &[Self::ValidatorId],
msg: &[u8],
sigs: &[Self::Signature],
) -> Self::AggregateSignature {
assert_eq!(validators.len(), sigs.len());

let mut aggregator = SchnorrAggregator::<Ristretto>::new(DST);
for (key, sig) in validators.iter().zip(sigs) {
let actual_sig = SchnorrSignature::<Ristretto>::read::<&[u8]>(&mut sig.as_ref()).unwrap();
let challenge = challenge(self.genesis, *key, actual_sig.R.to_bytes().as_ref(), msg);
aggregator.aggregate(challenge, actual_sig);
}

let aggregate = aggregator.complete().unwrap();
aggregate.serialize()
}

#[must_use]
Expand All @@ -188,12 +207,28 @@ impl SignatureScheme for Validators {
msg: &[u8],
sig: &Self::AggregateSignature,
) -> bool {
for (signer, sig) in signers.iter().zip(sig.iter()) {
if !self.verify(*signer, msg, sig) {
return false;
}
let Ok(aggregate) = SchnorrAggregate::<Ristretto>::read::<&[u8]>(&mut sig.as_slice()) else {
return false;
};

if signers.len() != aggregate.Rs().len() {
return false;
}
true

let mut challenges = vec![];
for (key, nonce) in signers.iter().zip(aggregate.Rs()) {
challenges.push(challenge(self.genesis, *key, nonce.to_bytes().as_ref(), msg));
}

aggregate.verify(
DST,
signers
.iter()
.zip(challenges)
.map(|(s, c)| (<Ristretto as Ciphersuite>::read_G(&mut s.as_slice()).unwrap(), c))
.collect::<Vec<_>>()
.as_slice(),
)
}
}

Expand Down
8 changes: 7 additions & 1 deletion coordinator/tributary/src/tendermint/tx.rs
Expand Up @@ -172,7 +172,13 @@ impl Transaction for TendermintTx {
let signature = &vote.sig.signature;
<Ristretto as Ciphersuite>::F::from_bytes_mod_order_wide(
&Blake2b512::digest(
[genesis.as_ref(), &self.hash(), signature.R.to_bytes().as_ref()].concat(),
[
b"Tributary Slash Vote",
genesis.as_ref(),
&self.hash(),
signature.R.to_bytes().as_ref(),
]
.concat(),
)
.into(),
)
Expand Down
8 changes: 7 additions & 1 deletion coordinator/tributary/src/transaction.rs
Expand Up @@ -126,7 +126,13 @@ pub trait Transaction: 'static + Send + Sync + Clone + Eq + Debug + ReadWrite {
TransactionKind::Signed(Signed { signature, .. }) => {
<Ristretto as Ciphersuite>::F::from_bytes_mod_order_wide(
&Blake2b512::digest(
[genesis.as_ref(), &self.hash(), signature.R.to_bytes().as_ref()].concat(),
[
b"Tributary Signed Transaction",
genesis.as_ref(),
&self.hash(),
signature.R.to_bytes().as_ref(),
]
.concat(),
)
.into(),
)
Expand Down
17 changes: 14 additions & 3 deletions coordinator/tributary/tendermint/src/ext.rs
Expand Up @@ -81,7 +81,13 @@ pub trait SignatureScheme: Send + Sync + Clone {
fn verify(&self, validator: Self::ValidatorId, msg: &[u8], sig: &Self::Signature) -> bool;

/// Aggregate signatures.
fn aggregate(sigs: &[Self::Signature]) -> Self::AggregateSignature;
/// It may panic if corrupted data passed in.
fn aggregate(
&self,
validators: &[Self::ValidatorId],
msg: &[u8],
sigs: &[Self::Signature],
) -> Self::AggregateSignature;
/// Verify an aggregate signature for the list of signers.
#[must_use]
fn verify_aggregate(
Expand All @@ -102,8 +108,13 @@ impl<S: SignatureScheme> SignatureScheme for Arc<S> {
self.as_ref().verify(validator, msg, sig)
}

fn aggregate(sigs: &[Self::Signature]) -> Self::AggregateSignature {
S::aggregate(sigs)
fn aggregate(
&self,
validators: &[Self::ValidatorId],
msg: &[u8],
sigs: &[Self::Signature],
) -> Self::AggregateSignature {
self.as_ref().aggregate(validators, msg, sigs)
}

#[must_use]
Expand Down
8 changes: 6 additions & 2 deletions coordinator/tributary/tendermint/src/lib.rs
Expand Up @@ -469,10 +469,14 @@ impl<N: Network + 'static> TendermintMachine<N> {
}
}

let commit_msg = commit_msg(
self.block.end_time[&self.block.round().number].canonical(),
block.id().as_ref(),
);
let commit = Commit {
end_time: self.block.end_time[&msg.round].canonical(),
validators,
signature: N::SignatureScheme::aggregate(&sigs),
validators: validators.clone(),
signature: self.network.signature_scheme().aggregate(&validators, &commit_msg, &sigs),
};
debug_assert!(self.network.verify_commit(block.id(), &commit));

Expand Down
7 changes: 6 additions & 1 deletion coordinator/tributary/tendermint/tests/ext.rs
Expand Up @@ -49,7 +49,12 @@ impl SignatureScheme for TestSignatureScheme {
(sig[.. 2] == validator.to_le_bytes()) && (sig[2 ..] == [msg, &[0; 30]].concat()[.. 30])
}

fn aggregate(sigs: &[[u8; 32]]) -> Vec<[u8; 32]> {
fn aggregate(
&self,
_: &[Self::ValidatorId],
_: &[u8],
sigs: &[Self::Signature],
) -> Self::AggregateSignature {
sigs.to_vec()
}

Expand Down
5 changes: 5 additions & 0 deletions crypto/schnorr/src/aggregate.rs
Expand Up @@ -111,6 +111,11 @@ impl<C: Ciphersuite> SchnorrAggregate<C> {
buf
}

#[allow(non_snake_case)]
pub fn Rs(&self) -> &[C::G] {
self.Rs.as_slice()
}

/// Perform signature verification.
///
/// Challenges must be properly crafted, which means being binding to the public key, nonce, and
Expand Down

0 comments on commit e319762

Please sign in to comment.