-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61ff3a8
commit e90159f
Showing
37 changed files
with
1,544 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "tlsn-authdecode-single-range" | ||
authors = ["TLSNotary Team"] | ||
description = "A convenience type for using AuthDecode with data contained in a single range of bytes" | ||
keywords = ["tls", "mpc", "2pc"] | ||
categories = ["cryptography"] | ||
license = "MIT OR Apache-2.0" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "authdecode_single_range" | ||
|
||
[dependencies] | ||
tlsn-authdecode-core = { workspace = true } | ||
tlsn-core = { workspace = true, features = ["use_poseidon_halo2"] } | ||
mpz-circuits = { workspace = true } | ||
mpz-garble-core = { workspace = true } | ||
mpz-core = { workspace = true } | ||
|
||
getset = "0.1.2" | ||
serde = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
//! A convenience type for using AuthDecode with the halo2 backend for a single byterange of a TLS | ||
//! transcript data. The length of the data must not exceed the maximum chunk size allowed by the | ||
//! backend. | ||
use core::ops::Range; | ||
use getset::Getters; | ||
use mpz_circuits::types::ValueType; | ||
use mpz_core::{utils::blake3, Block}; | ||
use mpz_garble_core::ChaChaEncoder; | ||
use serde::{Deserialize, Serialize}; | ||
use tlsn_core::transcript::{Direction, RX_TRANSCRIPT_ID, TX_TRANSCRIPT_ID}; | ||
|
||
use authdecode_core::{ | ||
backend::halo2::CHUNK_SIZE, | ||
encodings::{Encoding, EncodingProvider, EncodingProviderError, FullEncodings}, | ||
id::{Id, IdCollection}, | ||
SSP, | ||
}; | ||
|
||
#[derive(Clone, PartialEq, Serialize, Deserialize, Getters)] | ||
/// A single byterange of data with the corresponding direction. The data is treated as a big-endian | ||
/// byte string with MSB0 bit ordering. | ||
pub struct SingleRange { | ||
/// The direction in which the data was transmitted. | ||
#[getset(get = "pub")] | ||
direction: Direction, | ||
/// A range of bytes. | ||
#[getset(get = "pub")] | ||
range: Range<usize>, | ||
} | ||
|
||
impl SingleRange { | ||
/// Creates a new `SingleRange`. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the range length exceeds the maximim allowed length. | ||
pub fn new(direction: Direction, range: &Range<usize>) -> Self { | ||
assert!(range.len() <= CHUNK_SIZE); | ||
|
||
Self { | ||
direction, | ||
range: range.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for SingleRange { | ||
fn default() -> Self { | ||
Self { | ||
direction: Direction::Sent, | ||
range: Range::default(), | ||
} | ||
} | ||
} | ||
|
||
impl IdCollection for SingleRange { | ||
fn drain_front(&mut self, count: usize) -> Self { | ||
assert!(count % 8 == 0); | ||
assert!(count <= CHUNK_SIZE * 8); | ||
// We will never need to drain since the collection spans a single chunk. | ||
self.clone() | ||
} | ||
|
||
fn id(&self, _index: usize) -> Id { | ||
unimplemented!() | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.range.len() * 8 | ||
} | ||
|
||
fn new_from_iter<I: IntoIterator<Item = Self>>(_iter: I) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
/// An encoder of a TLS transcript. | ||
pub struct TranscriptEncoder { | ||
encoder: ChaChaEncoder, | ||
} | ||
|
||
impl TranscriptEncoder { | ||
/// Creates a new encoder from the `seed`. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `seed` - The seed to create the encoder from. | ||
pub fn new(seed: [u8; 32]) -> Self { | ||
Self { | ||
encoder: ChaChaEncoder::new(seed), | ||
} | ||
} | ||
|
||
/// Encodes a byte at the given position and direction in the transcript. | ||
fn encode_byte(&self, dir: Direction, pos: usize) -> Vec<[Encoding; 2]> { | ||
let id = match dir { | ||
Direction::Sent => TX_TRANSCRIPT_ID, | ||
Direction::Received => RX_TRANSCRIPT_ID, | ||
}; | ||
|
||
let id_hash = blake3(format!("{}/{}", id, pos).as_bytes()); | ||
let id = u64::from_be_bytes(id_hash[..8].try_into().unwrap()); | ||
|
||
let mut encodings = <ChaChaEncoder as mpz_garble_core::Encoder>::encode_by_type( | ||
&self.encoder, | ||
id, | ||
&ValueType::U8, | ||
) | ||
.iter_blocks() | ||
.map(|blocks| { | ||
// Hash the encodings to break the correlation and truncate them. | ||
[ | ||
Encoding::new( | ||
blake3(&Block::to_bytes(blocks[0]))[0..SSP / 8] | ||
.try_into() | ||
.unwrap(), | ||
false, | ||
), | ||
Encoding::new( | ||
blake3(&Block::to_bytes(blocks[1]))[0..SSP / 8] | ||
.try_into() | ||
.unwrap(), | ||
true, | ||
), | ||
] | ||
}) | ||
.collect::<Vec<_>>(); | ||
// Reverse byte encodings to MSB0. | ||
encodings.reverse(); | ||
encodings | ||
} | ||
} | ||
|
||
impl EncodingProvider<SingleRange> for TranscriptEncoder { | ||
fn get_by_ids( | ||
&self, | ||
ids: &SingleRange, | ||
) -> Result<FullEncodings<SingleRange>, EncodingProviderError> { | ||
let mut full_encoding = Vec::with_capacity(ids.range().len() * 8); | ||
|
||
for pos in ids.range().clone() { | ||
full_encoding.extend(self.encode_byte(*ids.direction(), pos)); | ||
} | ||
|
||
Ok(FullEncodings::new(full_encoding, ids.clone())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.