Skip to content

Commit

Permalink
implement some TryFrom and From traits for ProofStream and Proof
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Jun 8, 2023
1 parent f8dd813 commit fdf04f9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
4 changes: 2 additions & 2 deletions triton-vm/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,10 @@ mod triton_xfri_tests {

fri.prove(&codeword, &mut prover_proof_stream);

let proof = prover_proof_stream.to_proof();
let proof = prover_proof_stream.into();

let mut verifier_proof_stream: ProofStream<ProofItem, H> =
ProofStream::from_proof(&proof).unwrap();
ProofStream::try_from(&proof).unwrap();

assert_eq!(prover_proof_stream.len(), verifier_proof_stream.len());
for (prover_item, verifier_item) in prover_proof_stream
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/proof_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ mod proof_item_typed_tests {
proof_stream.enqueue(&ProofItem::FriResponse(fri_response.clone()), false);
fs.push(proof_stream.sponge_state.state);

let proof = proof_stream.to_proof();
let proof = proof_stream.into();

let mut proof_stream_ =
ProofStream::<ProofItem, H>::from_proof(&proof).expect("invalid parsing of proof");
ProofStream::<ProofItem, H>::try_from(&proof).expect("invalid parsing of proof");

let mut fs_ = vec![];
fs_.push(proof_stream_.sponge_state.state);
Expand Down
50 changes: 36 additions & 14 deletions triton-vm/src/proof_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,10 @@ where

/// The number of field elements required to encode the proof.
pub fn transcript_length(&self) -> usize {
let Proof(b_field_elements) = self.to_proof();
let Proof(b_field_elements) = self.into();
b_field_elements.len()
}

/// Convert the proof stream, _i.e._, the transcript, into a Proof.
pub fn to_proof(&self) -> Proof {
Proof(self.encode())
}

/// Convert the proof into a proof stream for the verifier.
pub fn from_proof(proof: &Proof) -> Result<Self> {
let proof_stream = *ProofStream::decode(&proof.0)?;
Ok(proof_stream)
}

fn encode_and_pad_item(item: &Item) -> Vec<BFieldElement> {
let encoding = item.encode();
let last_chunk_len = (encoding.len() + 1) % H::RATE;
Expand Down Expand Up @@ -188,6 +177,39 @@ where
}
}

impl<Item, H> TryFrom<&Proof> for ProofStream<Item, H>
where
Item: Clone + BFieldCodec,
H: AlgebraicHasher,
{
type Error = anyhow::Error;

fn try_from(proof: &Proof) -> Result<Self> {
let proof_stream = *ProofStream::decode(&proof.0)?;
Ok(proof_stream)
}
}

impl<Item, H> From<&ProofStream<Item, H>> for Proof
where
Item: Clone + BFieldCodec,
H: AlgebraicHasher,
{
fn from(proof_stream: &ProofStream<Item, H>) -> Self {
Proof(proof_stream.encode())
}
}

impl<Item, H> From<ProofStream<Item, H>> for Proof
where
Item: Clone + BFieldCodec,
H: AlgebraicHasher,
{
fn from(proof_stream: ProofStream<Item, H>) -> Self {
(&proof_stream).into()
}
}

#[cfg(test)]
mod proof_stream_typed_tests {
use anyhow::bail;
Expand Down Expand Up @@ -270,10 +292,10 @@ mod proof_stream_typed_tests {
proof_stream.enqueue(&TestItem::ManyB(manyb2.clone()), true);
let fs4 = proof_stream.sponge_state.state;

let proof = proof_stream.to_proof();
let proof = proof_stream.into();

let mut proof_stream: ProofStream<TestItem, H> =
ProofStream::from_proof(&proof).expect("invalid parsing of proof");
ProofStream::try_from(&proof).expect("invalid parsing of proof");

let fs1_ = proof_stream.sponge_state.state;
match proof_stream.dequeue(false).expect("can't dequeue item") {
Expand Down
4 changes: 2 additions & 2 deletions triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl Stark {
println!("Created proof containing {transcript_length} B-field elements ({kib} kiB).");
}

proof_stream.to_proof()
proof_stream.into()
}

/// Compute the upper bound to use for the maximum degree the quotients given the length of the
Expand Down Expand Up @@ -577,7 +577,7 @@ impl Stark {
prof_stop!(maybe_profiler, "derive additional parameters");

prof_start!(maybe_profiler, "deserialize");
let mut proof_stream = StarkProofStream::from_proof(proof)?;
let mut proof_stream = StarkProofStream::try_from(proof)?;
prof_stop!(maybe_profiler, "deserialize");

prof_start!(maybe_profiler, "Fiat-Shamir 1", "hash");
Expand Down

0 comments on commit fdf04f9

Please sign in to comment.