Skip to content

Commit

Permalink
Tie SessionId to SessionParameters::Digest
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Oct 24, 2024
1 parent cda1d26 commit b483374
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `MessageBundle` is not generic anymore. ([#36])
- `ProcessedArtifact` is now also generic on `SessionParameters`. ([#37])
- Added a `Test` prefix to `testing::Signer`/`Verifier`/`Signature`/`Hasher` and renamed `TestingSessionParams` to `TestSessionParams`. ([#40])
- `SessionId::new()` renamed to `from_seed()`. ([#41])
- `FirstRound::new()` takes a `&[u8]` instead of a `SessionId` object. ([#41])


### Added
Expand All @@ -22,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#36]: https://github.com/entropyxyz/manul/pull/36
[#37]: https://github.com/entropyxyz/manul/pull/37
[#40]: https://github.com/entropyxyz/manul/pull/40
[#41]: https://github.com/entropyxyz/manul/pull/41


## [0.0.1] - 2024-10-12
Expand Down
2 changes: 1 addition & 1 deletion examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for Round1<
type Inputs = Inputs<Id>;
fn new(
_rng: &mut impl CryptoRngCore,
_session_id: &SessionId,
_shared_randomness: &[u8],
id: Id,
inputs: Self::Inputs,
) -> Result<Self, LocalError> {
Expand Down
8 changes: 3 additions & 5 deletions examples/src/simple_malicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use alloc::collections::{BTreeMap, BTreeSet};
use core::fmt::Debug;

use manul::{
protocol::{
Artifact, DirectMessage, FinalizeError, FinalizeOutcome, FirstRound, LocalError, Payload, Round, SessionId,
},
protocol::{Artifact, DirectMessage, FinalizeError, FinalizeOutcome, FirstRound, LocalError, Payload, Round},
session::signature::Keypair,
testing::{round_override, run_sync, RoundOverride, RoundWrapper, TestSessionParams, TestSigner, TestVerifier},
};
Expand Down Expand Up @@ -46,11 +44,11 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for Malicio
type Inputs = MaliciousInputs<Id>;
fn new(
rng: &mut impl CryptoRngCore,
session_id: &SessionId,
shared_randomness: &[u8],
id: Id,
inputs: Self::Inputs,
) -> Result<Self, LocalError> {
let round = Round1::new(rng, session_id, id, inputs.inputs)?;
let round = Round1::new(rng, shared_randomness, id, inputs.inputs)?;
Ok(Self {
round,
behavior: inputs.behavior,
Expand Down
2 changes: 1 addition & 1 deletion examples/tests/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ async fn async_run() {
.iter()
.map(|signer| signer.verifying_key())
.collect::<BTreeSet<_>>();
let session_id = SessionId::random(&mut OsRng);
let session_id = SessionId::random::<TestSessionParams>(&mut OsRng);

// Create 4 `Session`s
let sessions = signers
Expand Down
4 changes: 2 additions & 2 deletions manul/benches/empty_rounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use manul::{
Artifact, DeserializationError, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, FirstRound,
LocalError, Payload, Protocol, ProtocolError, ProtocolValidationError, ReceiveError, Round, RoundId,
},
session::{signature::Keypair, SessionId, SessionOutcome},
session::{signature::Keypair, SessionOutcome},
testing::{run_sync, TestSessionParams, TestSigner, TestVerifier},
};
use rand_core::{CryptoRngCore, OsRng};
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<Id: 'static + Debug + Clone + Ord + Send + Sync> FirstRound<Id> for EmptyRo
type Inputs = Inputs<Id>;
fn new(
_rng: &mut impl CryptoRngCore,
_session_id: &SessionId,
_shared_randomness: &[u8],
_id: Id,
inputs: Self::Inputs,
) -> Result<Self, LocalError> {
Expand Down
1 change: 0 additions & 1 deletion manul/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ mod errors;
mod object_safe;
mod round;

pub use crate::session::SessionId;
pub use errors::{
DeserializationError, DirectMessageError, EchoBroadcastError, FinalizeError, LocalError, MessageValidationError,
ProtocolValidationError, ReceiveError, RemoteError,
Expand Down
3 changes: 1 addition & 2 deletions manul/src/protocol/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use super::{
},
object_safe::{ObjectSafeRound, ObjectSafeRoundWrapper},
};
use crate::session::SessionId;

/// Possible successful outcomes of [`Round::finalize`].
#[derive(Debug)]
Expand Down Expand Up @@ -347,7 +346,7 @@ pub trait FirstRound<Id: 'static>: Round<Id> + Sized {
/// `id` is the ID of this node.
fn new(
rng: &mut impl CryptoRngCore,
session_id: &SessionId,
shared_randomness: &[u8],
id: Id,
inputs: Self::Inputs,
) -> Result<Self, LocalError>;
Expand Down
37 changes: 28 additions & 9 deletions manul/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::fmt::Debug;
use digest::Digest;
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
use serde_encoded_bytes::{Base64, SliceLike};
use serde_encoded_bytes::{Hex, SliceLike};
use signature::{DigestVerifier, Keypair, RandomizedDigestSigner};
use tracing::{debug, trace};

Expand Down Expand Up @@ -52,7 +52,7 @@ pub trait SessionParameters {

/// A session identifier shared between the parties.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct SessionId(#[serde(with = "SliceLike::<Base64>")] Box<[u8]>);
pub struct SessionId(#[serde(with = "SliceLike::<Hex>")] Box<[u8]>);

/// A session ID.
///
Expand All @@ -61,15 +61,34 @@ pub struct SessionId(#[serde(with = "SliceLike::<Base64>")] Box<[u8]>);
/// Must be created uniquely for each session execution, otherwise there is a danger of replay attacks.
impl SessionId {
/// Creates a random session identifier.
pub fn random(rng: &mut impl CryptoRngCore) -> Self {
let mut buffer = [0u8; 256];
///
/// **Warning:** this should generally be used for testing; creating a random session ID in a centralized way
/// usually defeats the purpose of having a distributed protocol.
#[cfg(any(test, feature = "testing"))]
pub fn random<SP: SessionParameters>(rng: &mut impl CryptoRngCore) -> Self {
let mut buffer = digest::Output::<SP::Digest>::default();
rng.fill_bytes(&mut buffer);
Self(buffer.into())
Self(buffer.as_ref().into())
}

/// Creates a session identifier from the given bytestring.
pub fn new(bytes: &[u8]) -> Self {
Self(bytes.into())
/// Creates a session identifier deterministically from the given bytestring.
///
/// Every node executing a session must be given the same session ID.
///
/// **Warning:** make sure the bytestring you provide will not be reused within your application,
/// and cannot be predicted in advance.
/// Session ID collisions will affect error attribution and evidence verification.
///
/// In a blockchain setting, it may be some combination of the current block hash with the public parameters
/// (identities of the parties, hash of the inputs).
pub fn from_seed<SP: SessionParameters>(bytes: &[u8]) -> Self {
Self(
SP::Digest::new_with_prefix(b"SessionId")
.chain_update(bytes)
.finalize()
.as_ref()
.into(),
)
}
}

Expand Down Expand Up @@ -125,7 +144,7 @@ where
let verifier = signer.verifying_key();
let first_round = Box::new(ObjectSafeRoundWrapper::new(R::new(
rng,
&session_id,
session_id.as_ref(),
verifier.clone(),
inputs,
)?));
Expand Down
2 changes: 1 addition & 1 deletion manul/src/testing/run_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ where
R: 'static + FirstRound<SP::Verifier>,
SP: 'static + SessionParameters + Debug,
{
let session_id = SessionId::random(rng);
let session_id = SessionId::random::<SP>(rng);

let mut messages = Vec::new();
let mut states = BTreeMap::new();
Expand Down

0 comments on commit b483374

Please sign in to comment.