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 22, 2024
1 parent 5cfc6c1 commit c90c85f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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, Signer, TestingSessionParams, Verifier},
};
Expand Down Expand Up @@ -45,11 +43,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.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,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::<TestingSessionParams>(&mut OsRng);
let sessions = signers
.into_iter()
.map(|signer| {
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, Signer, TestingSessionParams, Verifier},
};
use rand_core::{CryptoRngCore, OsRng};
Expand Down Expand Up @@ -77,7 +77,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`].
pub enum FinalizeOutcome<Id, P: Protocol> {
Expand Down Expand Up @@ -343,7 +342,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
24 changes: 15 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;

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,21 @@ 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];
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.
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 @@ -123,7 +129,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 @@ -97,7 +97,7 @@ where
R: 'static + FirstRound<SP::Verifier>,
SP: 'static + SessionParameters,
{
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 c90c85f

Please sign in to comment.