Skip to content

Commit

Permalink
Add reveal groups of ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroitaki committed Nov 28, 2024
1 parent d974fb7 commit 5cf7128
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
32 changes: 32 additions & 0 deletions crates/core/src/transcript/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ impl<'a> TranscriptProofBuilder<'a> {
self.reveal(ranges, Direction::Sent)
}

/// Reveals multiple groups of ranges given in the sent transcript using the
/// default kind of commitment.
///
/// # Arguments
///
/// * `ranges_groups` - Groups of ranges to reveal.
pub fn reveal_sent_multi(
&mut self,
ranges_groups: &[&dyn ToRangeSet<usize>],
) -> Result<&mut Self, TranscriptProofBuilderError> {
for ranges in ranges_groups.iter() {
self.reveal(*ranges, Direction::Sent)?;

Check warning on line 295 in crates/core/src/transcript/proof.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/transcript/proof.rs#L290-L295

Added lines #L290 - L295 were not covered by tests
}
Ok(self)
}

Check warning on line 298 in crates/core/src/transcript/proof.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/transcript/proof.rs#L297-L298

Added lines #L297 - L298 were not covered by tests

/// Reveals the given ranges in the received transcript using the default
/// kind of commitment.
///
Expand All @@ -294,6 +310,22 @@ impl<'a> TranscriptProofBuilder<'a> {
self.reveal(ranges, Direction::Received)
}

/// Reveals multiple groups of ranges given in the received transcript using
/// the default kind of commitment.
///
/// # Arguments
///
/// * `ranges_groups` - Groups of ranges to reveal.
pub fn reveal_recv_multi(
&mut self,
ranges_groups: &[&dyn ToRangeSet<usize>],
) -> Result<&mut Self, TranscriptProofBuilderError> {
for ranges in ranges_groups.iter() {
self.reveal(*ranges, Direction::Received)?;

Check warning on line 324 in crates/core/src/transcript/proof.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/transcript/proof.rs#L319-L324

Added lines #L319 - L324 were not covered by tests
}
Ok(self)
}

Check warning on line 327 in crates/core/src/transcript/proof.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/transcript/proof.rs#L326-L327

Added lines #L326 - L327 were not covered by tests

/// Builds the transcript proof.
pub fn build(self) -> Result<TranscriptProof, TranscriptProofBuilderError> {
let encoding_proof = if !self.encoding_proof_idxs.is_empty() {
Expand Down
32 changes: 21 additions & 11 deletions crates/examples/attestation/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tlsn_examples::ExampleType;
use tlsn_formats::http::HttpTranscript;

use clap::Parser;
use utils::range::ToRangeSet;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -41,10 +42,11 @@ async fn create_presentation(example_type: &ExampleType) -> Result<(), Box<dyn s
let mut builder = secrets.transcript_proof_builder();

let request = &transcript.requests[0];
// Reveal the structure of the request without the headers or body.
builder.reveal_sent(&request.without_data())?;
// Reveal the request target.
builder.reveal_sent(&request.request.target)?;

// Reveal multiple parts of the request: (1) its structure without the headers
// or body, (2) the request target.
builder.reveal_sent_multi(&[&request.without_data(), &request.request.target])?;

// Reveal all headers except the values of User-Agent and Authorization.
for header in &request.headers {
if !(header
Expand All @@ -63,10 +65,16 @@ async fn create_presentation(example_type: &ExampleType) -> Result<(), Box<dyn s
}

// Reveal only parts of the response
// Use a vector to collect the ranges of all these parts before calling
// `reveal_recv_multi`
let mut recv_ranges: Vec<&dyn ToRangeSet<usize>> = Vec::new();

let response = &transcript.responses[0];
builder.reveal_recv(&response.without_data())?;
let response_without_data = &response.without_data();
recv_ranges.push(response_without_data);

for header in &response.headers {
builder.reveal_recv(header)?;
recv_ranges.push(header);
}

let content = &response.body.as_ref().unwrap().content;
Expand All @@ -75,19 +83,21 @@ async fn create_presentation(example_type: &ExampleType) -> Result<(), Box<dyn s
// For experimentation, reveal the entire response or just a selection
let reveal_all = false;
if reveal_all {
builder.reveal_recv(response)?;
recv_ranges.push(response);
} else {
builder.reveal_recv(json.get("id").unwrap())?;
builder.reveal_recv(json.get("information.name").unwrap())?;
builder.reveal_recv(json.get("meta.version").unwrap())?;
recv_ranges.push(json.get("id").unwrap());
recv_ranges.push(json.get("information.name").unwrap());
recv_ranges.push(json.get("meta.version").unwrap());
}
}
tlsn_formats::http::BodyContent::Unknown(span) => {
builder.reveal_recv(span)?;
recv_ranges.push(span);
}
_ => {}
}

builder.reveal_recv_multi(&recv_ranges)?;

let transcript_proof = builder.build()?;

// Use default crypto provider to build the presentation.
Expand Down

0 comments on commit 5cf7128

Please sign in to comment.