Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: MPC-TLS upgrade #698

Merged
merged 22 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(core): decouple encoder from mpz (#692)
* WIP: Adding new encoding logic...

* feat: add new encoder

* add feedback

* rename conversions
  • Loading branch information
th4s authored and sinui0 committed Feb 25, 2025
commit df903b78a2a91288579234530aa40e1d199df729
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ http = { version = "1.1" }
http-body-util = { version = "0.1" }
hyper = { version = "1.1" }
hyper-util = { version = "0.1" }
itybity = { version = "0.2" }
k256 = { version = "0.13" }
log = { version = "0.4" }
once_cell = { version = "1.19" }
Expand Down
2 changes: 2 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ opaque-debug = { workspace = true }
p256 = { workspace = true, features = ["serde"] }
rand = { workspace = true }
rand_core = { workspace = true }
rand_chacha = { workspace = true }
rs_merkle = { workspace = true, features = ["serde"] }
rstest = { workspace = true, optional = true }
serde = { workspace = true }
Expand All @@ -38,6 +39,7 @@ thiserror = { workspace = true }
tiny-keccak = { version = "2.0", features = ["keccak"] }
web-time = { workspace = true }
webpki-roots = { workspace = true }
itybity = { workspace = true }

[dev-dependencies]
bincode = { workspace = true }
Expand Down
25 changes: 13 additions & 12 deletions crates/core/src/attestation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
request::Request,
serialize::CanonicalSerialize,
signing::SignatureAlgId,
transcript::encoding::EncoderSecret,
CryptoProvider,
};

Expand All @@ -25,7 +26,7 @@ pub struct Sign {
server_ephemeral_key: Option<ServerEphemKey>,
cert_commitment: ServerCertCommitment,
encoding_commitment_root: Option<TypedHash>,
encoding_seed: Option<Vec<u8>>,
encoder_secret: Option<EncoderSecret>,
}

/// An attestation builder.
Expand Down Expand Up @@ -91,7 +92,7 @@ impl<'a> AttestationBuilder<'a, Accept> {
server_ephemeral_key: None,
cert_commitment,
encoding_commitment_root,
encoding_seed: None,
encoder_secret: None,
},
})
}
Expand All @@ -110,9 +111,9 @@ impl AttestationBuilder<'_, Sign> {
self
}

/// Sets the encoding seed.
pub fn encoding_seed(&mut self, seed: Vec<u8>) -> &mut Self {
self.state.encoding_seed = Some(seed);
/// Sets the encoder secret.
pub fn encoder_secret(&mut self, secret: EncoderSecret) -> &mut Self {
self.state.encoder_secret = Some(secret);
self
}

Expand All @@ -125,7 +126,7 @@ impl AttestationBuilder<'_, Sign> {
server_ephemeral_key,
cert_commitment,
encoding_commitment_root,
encoding_seed,
encoder_secret,
} = self.state;

let hasher = provider.hash.get(&hash_alg).map_err(|_| {
Expand All @@ -144,14 +145,14 @@ impl AttestationBuilder<'_, Sign> {
})?;

let encoding_commitment = if let Some(root) = encoding_commitment_root {
let Some(seed) = encoding_seed else {
let Some(secret) = encoder_secret else {
return Err(AttestationBuilderError::new(
ErrorKind::Field,
"encoding commitment requested but seed was not set",
"encoding commitment requested but encoder_secret was not set",
));
};

Some(EncodingCommitment { root, seed })
Some(EncodingCommitment { root, secret })
} else {
None
};
Expand Down Expand Up @@ -246,7 +247,7 @@ mod test {
use crate::{
connection::{HandshakeData, HandshakeDataV1_2},
fixtures::{
encoder_seed, encoding_provider, request_fixture, ConnectionFixture, RequestFixture,
encoder_secret, encoding_provider, request_fixture, ConnectionFixture, RequestFixture,
},
hash::Blake3,
transcript::Transcript,
Expand Down Expand Up @@ -435,7 +436,7 @@ mod test {

attestation_builder
.connection_info(connection_info)
.encoding_seed(encoder_seed().to_vec());
.encoder_secret(encoder_secret());

let err = attestation_builder.build(crypto_provider).err().unwrap();
assert!(matches!(err.kind, ErrorKind::Field));
Expand Down Expand Up @@ -471,7 +472,7 @@ mod test {

attestation_builder
.server_ephemeral_key(server_ephemeral_key)
.encoding_seed(encoder_seed().to_vec());
.encoder_secret(encoder_secret());

let err = attestation_builder.build(crypto_provider).err().unwrap();
assert!(matches!(err.kind, ErrorKind::Field));
Expand Down
30 changes: 22 additions & 8 deletions crates/core/src/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

mod provider;

pub use provider::ChaChaProvider;
pub use provider::FixtureEncodingProvider;

use hex::FromHex;
use p256::ecdsa::SigningKey;
Expand All @@ -17,7 +17,7 @@ use crate::{
request::{Request, RequestConfig},
signing::SignatureAlgId,
transcript::{
encoding::{EncodingProvider, EncodingTree},
encoding::{EncoderSecret, EncodingProvider, EncodingTree},
Transcript, TranscriptCommitConfigBuilder,
},
CryptoProvider,
Expand Down Expand Up @@ -131,12 +131,26 @@ impl ConnectionFixture {

/// Returns an encoding provider fixture.
pub fn encoding_provider(tx: &[u8], rx: &[u8]) -> impl EncodingProvider {
ChaChaProvider::new(encoder_seed(), Transcript::new(tx, rx))
let secret = encoder_secret();
FixtureEncodingProvider::new(&secret, Transcript::new(tx, rx))
}

/// Returns an encoder seed fixture.
pub fn encoder_seed() -> [u8; 32] {
[0u8; 32]
/// Seed fixture.
const SEED: [u8; 32] = [0; 32];

/// Delta fixture.
const DELTA: [u8; 16] = [1; 16];

/// Returns an encoder secret fixture.
pub fn encoder_secret() -> EncoderSecret {
EncoderSecret::new(SEED, DELTA)
}

/// Returns a tampered encoder secret fixture.
pub fn encoder_secret_tampered_seed() -> EncoderSecret {
let mut seed = SEED;
seed[0] += 1;
EncoderSecret::new(seed, DELTA)
}

/// Returns a notary signing key fixture.
Expand Down Expand Up @@ -205,7 +219,7 @@ pub fn attestation_fixture(
request: Request,
connection: ConnectionFixture,
signature_alg: SignatureAlgId,
encoding_seed: Vec<u8>,
secret: EncoderSecret,
) -> Attestation {
let ConnectionFixture {
connection_info,
Expand Down Expand Up @@ -237,7 +251,7 @@ pub fn attestation_fixture(
attestation_builder
.connection_info(connection_info)
.server_ephemeral_key(server_ephemeral_key)
.encoding_seed(encoding_seed);
.encoder_secret(secret);

attestation_builder.build(&provider).unwrap()
}
20 changes: 9 additions & 11 deletions crates/core/src/fixtures/provider.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
use mpz_garble_core::ChaChaEncoder;

use crate::transcript::{
encoding::{Encoder, EncodingProvider},
encoding::{new_encoder, Encoder, EncoderSecret, EncodingProvider},
Direction, Idx, Transcript,
};

/// A ChaCha encoding provider fixture.
pub struct ChaChaProvider {
encoder: ChaChaEncoder,
/// A encoding provider fixture.
pub struct FixtureEncodingProvider {
encoder: Box<dyn Encoder>,
transcript: Transcript,
}

impl ChaChaProvider {
/// Creates a new ChaCha encoding provider.
pub(crate) fn new(seed: [u8; 32], transcript: Transcript) -> Self {
impl FixtureEncodingProvider {
/// Creates a new encoding provider fixture.
pub(crate) fn new(secret: &EncoderSecret, transcript: Transcript) -> Self {
Self {
encoder: ChaChaEncoder::new(seed),
encoder: Box::new(new_encoder(secret)),
transcript,
}
}
}

impl EncodingProvider for ChaChaProvider {
impl EncodingProvider for FixtureEncodingProvider {
fn provide_encoding(&self, direction: Direction, idx: &Idx) -> Option<Vec<u8>> {
let seq = self.transcript.get(direction, idx)?;
Some(self.encoder.encode_subsequence(direction, &seq))
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod test {
use crate::{
connection::{ServerCertOpening, TranscriptLength},
fixtures::{
attestation_fixture, encoder_seed, encoding_provider, request_fixture,
attestation_fixture, encoder_secret, encoding_provider, request_fixture,
ConnectionFixture, RequestFixture,
},
hash::{Blake3, Hash, HashAlgId},
Expand All @@ -122,7 +122,7 @@ mod test {
request.clone(),
connection,
SignatureAlgId::SECP256K1,
encoder_seed().to_vec(),
encoder_secret(),
);

assert!(request.validate(&attestation).is_ok())
Expand All @@ -144,7 +144,7 @@ mod test {
request.clone(),
connection,
SignatureAlgId::SECP256K1,
encoder_seed().to_vec(),
encoder_secret(),
);

request.signature_alg = SignatureAlgId::SECP256R1;
Expand All @@ -169,7 +169,7 @@ mod test {
request.clone(),
connection,
SignatureAlgId::SECP256K1,
encoder_seed().to_vec(),
encoder_secret(),
);

request.hash_alg = HashAlgId::SHA256;
Expand All @@ -194,7 +194,7 @@ mod test {
request.clone(),
connection,
SignatureAlgId::SECP256K1,
encoder_seed().to_vec(),
encoder_secret(),
);

let ConnectionFixture {
Expand Down Expand Up @@ -229,7 +229,7 @@ mod test {
request.clone(),
connection,
SignatureAlgId::SECP256K1,
encoder_seed().to_vec(),
encoder_secret(),
);

request.encoding_commitment_root = Some(TypedHash {
Expand Down
16 changes: 0 additions & 16 deletions crates/core/src/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ pub use proof::{
TranscriptProof, TranscriptProofBuilder, TranscriptProofBuilderError, TranscriptProofError,
};

/// Sent data transcript ID.
pub static TX_TRANSCRIPT_ID: &str = "tx";
/// Received data transcript ID.
pub static RX_TRANSCRIPT_ID: &str = "rx";

/// A transcript contains all the data communicated over a TLS connection.
#[derive(Clone, Serialize, Deserialize)]
pub struct Transcript {
Expand Down Expand Up @@ -588,17 +583,6 @@ impl Subsequence {
#[error("invalid subsequence: {0}")]
pub struct InvalidSubsequence(&'static str);

/// Returns the value ID for each byte in the provided range set.
#[doc(hidden)]
pub fn get_value_ids(direction: Direction, idx: &Idx) -> impl Iterator<Item = String> + '_ {
let id = match direction {
Direction::Sent => TX_TRANSCRIPT_ID,
Direction::Received => RX_TRANSCRIPT_ID,
};

idx.iter().map(move |idx| format!("{}/{}", id, idx))
}

mod validation {
use super::*;

Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/transcript/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod proof;
mod provider;
mod tree;

pub use encoder::EncoderSecret;
pub(crate) use encoder::{new_encoder, Encoder};
pub use proof::{EncodingProof, EncodingProofError};
pub use provider::EncodingProvider;
Expand All @@ -32,7 +33,7 @@ pub struct EncodingCommitment {
/// Merkle root of the encoding commitments.
pub root: TypedHash,
/// Seed used to generate the encodings.
pub seed: Vec<u8>,
pub secret: EncoderSecret,
}

impl_domain_separator!(EncodingCommitment);
Loading