2023-07-01 08:53:46 -04:00
|
|
|
use transcript::{Transcript, RecommendedTranscript};
|
2025-08-20 04:50:37 -04:00
|
|
|
use dalek_ff_group::Ristretto;
|
|
|
|
|
use ciphersuite::{group::GroupEncoding, Ciphersuite};
|
2023-07-01 08:53:46 -04:00
|
|
|
|
2023-11-25 04:01:11 -05:00
|
|
|
use borsh::{BorshSerialize, BorshDeserialize};
|
2023-07-01 08:53:46 -04:00
|
|
|
|
2025-09-02 00:32:06 -04:00
|
|
|
use serai_primitives::network_id::ExternalNetworkId;
|
2023-07-01 08:53:46 -04:00
|
|
|
|
2023-11-26 11:22:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, BorshSerialize, BorshDeserialize)]
|
2023-07-01 08:53:46 -04:00
|
|
|
pub enum Service {
|
2024-10-07 05:16:11 +03:00
|
|
|
Processor(ExternalNetworkId),
|
2023-07-01 08:53:46 -04:00
|
|
|
Coordinator,
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-26 11:22:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
2023-07-01 08:53:46 -04:00
|
|
|
pub struct QueuedMessage {
|
|
|
|
|
pub from: Service,
|
2023-07-17 15:49:15 -04:00
|
|
|
pub id: u64,
|
2023-07-01 08:53:46 -04:00
|
|
|
pub msg: Vec<u8>,
|
|
|
|
|
pub sig: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-26 11:22:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
2023-07-01 08:53:46 -04:00
|
|
|
pub struct Metadata {
|
|
|
|
|
pub from: Service,
|
|
|
|
|
pub to: Service,
|
2023-07-16 20:32:04 -04:00
|
|
|
pub intent: Vec<u8>,
|
2023-07-01 08:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
2023-11-26 11:22:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
|
|
|
|
pub enum MessageQueueRequest {
|
|
|
|
|
Queue { meta: Metadata, msg: Vec<u8>, sig: Vec<u8> },
|
|
|
|
|
Next { from: Service, to: Service },
|
|
|
|
|
Ack { from: Service, to: Service, id: u64, sig: Vec<u8> },
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-01 08:53:46 -04:00
|
|
|
pub fn message_challenge(
|
2023-07-17 17:40:34 -04:00
|
|
|
from: Service,
|
|
|
|
|
from_key: <Ristretto as Ciphersuite>::G,
|
2023-07-16 20:32:04 -04:00
|
|
|
to: Service,
|
|
|
|
|
intent: &[u8],
|
2023-07-01 08:53:46 -04:00
|
|
|
msg: &[u8],
|
|
|
|
|
nonce: <Ristretto as Ciphersuite>::G,
|
|
|
|
|
) -> <Ristretto as Ciphersuite>::F {
|
2023-07-17 17:40:34 -04:00
|
|
|
let mut transcript = RecommendedTranscript::new(b"Serai Message Queue v0.1 Message");
|
2023-07-16 20:32:04 -04:00
|
|
|
transcript.domain_separate(b"metadata");
|
2023-11-25 04:01:11 -05:00
|
|
|
transcript.append_message(b"from", borsh::to_vec(&from).unwrap());
|
2023-07-17 17:40:34 -04:00
|
|
|
transcript.append_message(b"from_key", from_key.to_bytes());
|
2023-11-25 04:01:11 -05:00
|
|
|
transcript.append_message(b"to", borsh::to_vec(&to).unwrap());
|
2023-07-16 20:32:04 -04:00
|
|
|
transcript.append_message(b"intent", intent);
|
|
|
|
|
transcript.domain_separate(b"message");
|
2023-07-01 08:53:46 -04:00
|
|
|
transcript.append_message(b"msg", msg);
|
|
|
|
|
transcript.domain_separate(b"signature");
|
|
|
|
|
transcript.append_message(b"nonce", nonce.to_bytes());
|
Replace `Ciphersuite::hash_to_F`
The prior-present `Ciphersuite::hash_to_F` was a sin. Implementations took a
DST, yet were not require to securely handle it. It was also biased towards the
requirements of `modular-frost` as `ciphersuite` was originally written all
those years ago, when `modular-frost` had needs exceeding what `ff`, `group`
satisfied.
Now, the hash is bound to produce an output which can be converted to a scalar
with `ff::FromUniformBytes`. A new `hash_to_F`, which accepts a single argument
of the value to hash (removing the potential to insecurely handle the DST by
removing the DST entirely). Due to `digest` yielding a `GenericArray`, yet
`FromUniformBytes` taking a `const usize`, the `ciphersuite` crate now defines
a `FromUniformBytes` trait taking an array (then implemented for all satisfiers
of `ff::FromUniformBytes`). In order to get the array type from the
`GenericArray`, the output of the hash, `digest` is updated to the `0.11`
release candidate which moves to `flexible-array` which solves that problem.
The existing, specific `hash_to_F` functions have been moved to `modular-frost`
as necessary.
`flexible-array` itself is patched to a fork due to
https://github.com/RustCrypto/hybrid-array/issues/131.
2025-08-29 05:04:03 -04:00
|
|
|
<Ristretto as Ciphersuite>::hash_to_F(&transcript.challenge(b"challenge"))
|
2023-07-17 17:40:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ack_challenge(
|
2023-09-27 12:20:57 -04:00
|
|
|
to: Service,
|
|
|
|
|
to_key: <Ristretto as Ciphersuite>::G,
|
2023-07-17 17:40:34 -04:00
|
|
|
from: Service,
|
|
|
|
|
id: u64,
|
|
|
|
|
nonce: <Ristretto as Ciphersuite>::G,
|
|
|
|
|
) -> <Ristretto as Ciphersuite>::F {
|
2023-12-17 02:06:51 -05:00
|
|
|
let mut transcript = RecommendedTranscript::new(b"Serai Message Queue v0.1 Acknowledgement");
|
2023-07-17 17:40:34 -04:00
|
|
|
transcript.domain_separate(b"metadata");
|
2023-11-25 04:01:11 -05:00
|
|
|
transcript.append_message(b"to", borsh::to_vec(&to).unwrap());
|
2023-09-27 12:20:57 -04:00
|
|
|
transcript.append_message(b"to_key", to_key.to_bytes());
|
2023-11-25 04:01:11 -05:00
|
|
|
transcript.append_message(b"from", borsh::to_vec(&from).unwrap());
|
2023-07-17 17:40:34 -04:00
|
|
|
transcript.domain_separate(b"message");
|
|
|
|
|
transcript.append_message(b"id", id.to_le_bytes());
|
|
|
|
|
transcript.domain_separate(b"signature");
|
|
|
|
|
transcript.append_message(b"nonce", nonce.to_bytes());
|
Replace `Ciphersuite::hash_to_F`
The prior-present `Ciphersuite::hash_to_F` was a sin. Implementations took a
DST, yet were not require to securely handle it. It was also biased towards the
requirements of `modular-frost` as `ciphersuite` was originally written all
those years ago, when `modular-frost` had needs exceeding what `ff`, `group`
satisfied.
Now, the hash is bound to produce an output which can be converted to a scalar
with `ff::FromUniformBytes`. A new `hash_to_F`, which accepts a single argument
of the value to hash (removing the potential to insecurely handle the DST by
removing the DST entirely). Due to `digest` yielding a `GenericArray`, yet
`FromUniformBytes` taking a `const usize`, the `ciphersuite` crate now defines
a `FromUniformBytes` trait taking an array (then implemented for all satisfiers
of `ff::FromUniformBytes`). In order to get the array type from the
`GenericArray`, the output of the hash, `digest` is updated to the `0.11`
release candidate which moves to `flexible-array` which solves that problem.
The existing, specific `hash_to_F` functions have been moved to `modular-frost`
as necessary.
`flexible-array` itself is patched to a fork due to
https://github.com/RustCrypto/hybrid-array/issues/131.
2025-08-29 05:04:03 -04:00
|
|
|
<Ristretto as Ciphersuite>::hash_to_F(&transcript.challenge(b"challenge"))
|
2023-07-01 08:53:46 -04:00
|
|
|
}
|