Files
serai/coordinator/tributary-sdk/src/tests/block.rs
Luke Parker a141deaf36 Smash the singular Ciphersuite trait into multiple
This helps identify where the various functionalities are used, or rather, not
used. The `Ciphersuite` trait present in `patches/ciphersuite`, facilitating
the entire FCMP++ tree, only requires the markers _and_ canonical point
decoding. I've opened a PR to upstream such a trait into `group`
(https://github.com/zkcrypto/group/pull/68).

`WrappedGroup` is still justified for as long as `Group::generator` exists.
Moving `::generator()` to its own trait, on an independent structure (upstream)
would be massively appreciated. @tarcieri also wanted to update from
`fn generator()` to `const GENERATOR`, which would encourage further discussion
on https://github.com/zkcrypto/group/issues/32 and
https://github.com/zkcrypto/group/issues/45, which have been stagnant.

The `Id` trait is occasionally used yet really should be first off the chopping
block.

Finally, `WithPreferredHash` is only actually used around a third of the time,
which more than justifies it being a separate trait.

---

Updates `dalek_ff_group::Scalar` to directly re-export
`curve25519_dalek::Scalar`, as without issue. `dalek_ff_group::RistrettoPoint`
also could be replaced with an export of `curve25519_dalek::RistrettoPoint`,
yet the coordinator relies on how we implemented `Hash` on it for the hell of
it so it isn't worth it at this time. `dalek_ff_group::EdwardsPoint` can't be
replaced for an re-export of `curve25519_dalek::SubgroupPoint` as it doesn't
implement `zeroize`, `subtle` traits within a released, non-yanked version.
Relevance to https://github.com/serai-dex/serai/issues/201 and
https://github.com/dalek-cryptography/curve25519-dalek/issues/811#issuecomment-3247732746.

Also updates the `Ristretto` ciphersuite to prefer `Blake2b-512` over
`SHA2-512`. In order to maintain compliance with FROST's IETF standard,
`modular-frost` defines its own ciphersuite for Ristretto which still uses
`SHA2-512`.
2025-09-03 13:50:20 -04:00

140 lines
3.8 KiB
Rust

use std::{sync::Arc, io, collections::HashMap, fmt::Debug};
use blake2::{Digest, Blake2s256};
use dalek_ff_group::Ristretto;
use ciphersuite::{group::Group, *};
use schnorr::SchnorrSignature;
use serai_db::MemDb;
use tendermint::ext::Commit;
use crate::{
ReadWrite, BlockError, Block, Transaction,
tests::p2p::DummyP2p,
transaction::{TransactionError, Signed, TransactionKind, Transaction as TransactionTrait},
tendermint::{TendermintNetwork, Validators},
};
type N = TendermintNetwork<MemDb, NonceTransaction, DummyP2p>;
// A transaction solely defined by its nonce and a distinguisher (to allow creating distinct TXs
// sharing a nonce).
#[derive(Clone, PartialEq, Eq, Debug)]
struct NonceTransaction(u32, u8, Signed);
impl NonceTransaction {
fn new(nonce: u32, distinguisher: u8) -> Self {
NonceTransaction(
nonce,
distinguisher,
Signed {
signer: <Ristretto as WrappedGroup>::G::identity(),
nonce,
signature: SchnorrSignature::<Ristretto> {
R: <Ristretto as WrappedGroup>::G::identity(),
s: <Ristretto as WrappedGroup>::F::ZERO,
},
},
)
}
}
impl ReadWrite for NonceTransaction {
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let mut nonce = [0; 4];
reader.read_exact(&mut nonce)?;
let nonce = u32::from_le_bytes(nonce);
let mut distinguisher = [0];
reader.read_exact(&mut distinguisher)?;
Ok(NonceTransaction::new(nonce, distinguisher[0]))
}
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
writer.write_all(&self.0.to_le_bytes())?;
writer.write_all(&[self.1])
}
}
impl TransactionTrait for NonceTransaction {
fn kind(&self) -> TransactionKind {
TransactionKind::Signed(vec![], self.2.clone())
}
fn hash(&self) -> [u8; 32] {
Blake2s256::digest([self.0.to_le_bytes().as_ref(), &[self.1]].concat()).into()
}
fn verify(&self) -> Result<(), TransactionError> {
Ok(())
}
}
#[test]
fn empty_block() {
const GENESIS: [u8; 32] = [0xff; 32];
const LAST: [u8; 32] = [0x01; 32];
let validators = Arc::new(Validators::new(GENESIS, vec![]).unwrap());
let commit = |_: u64| -> Option<Commit<Arc<Validators>>> {
Some(Commit::<Arc<Validators>> { end_time: 0, validators: vec![], signature: vec![] })
};
let provided_or_unsigned_in_chain = |_: [u8; 32]| false;
Block::<NonceTransaction>::new(LAST, vec![], vec![])
.verify::<N, _>(
GENESIS,
LAST,
HashMap::new(),
&mut |_, _| None,
&validators,
commit,
provided_or_unsigned_in_chain,
false,
)
.unwrap();
}
#[test]
fn duplicate_nonces() {
const GENESIS: [u8; 32] = [0xff; 32];
const LAST: [u8; 32] = [0x01; 32];
let validators = Arc::new(Validators::new(GENESIS, vec![]).unwrap());
// Run once without duplicating a nonce, and once with, so that's confirmed to be the faulty
// component
for i in [1, 0] {
let mut mempool = vec![];
let mut insert = |tx: NonceTransaction| mempool.push(Transaction::Application(tx));
insert(NonceTransaction::new(0, 0));
insert(NonceTransaction::new(i, 1));
let commit = |_: u64| -> Option<Commit<Arc<Validators>>> {
Some(Commit::<Arc<Validators>> { end_time: 0, validators: vec![], signature: vec![] })
};
let provided_or_unsigned_in_chain = |_: [u8; 32]| false;
let mut last_nonce = 0;
let res = Block::new(LAST, vec![], mempool).verify::<N, _>(
GENESIS,
LAST,
HashMap::new(),
&mut |_, _| {
let res = last_nonce;
last_nonce += 1;
Some(res)
},
&validators,
commit,
provided_or_unsigned_in_chain,
false,
);
if i == 1 {
res.unwrap();
} else {
assert_eq!(res, Err(BlockError::TransactionError(TransactionError::InvalidNonce)));
}
}
}