Skip to content

Commit

Permalink
Strip down allocation in serialization of R1CSProof (#298)
Browse files Browse the repository at this point in the history
Add `InnerProductProof::to_bytes_iter`.

This semantically equivalent to `proof.to_bytes().into_iter()` except:

1. It borrows the proof;
2. It doesn't allocate
  • Loading branch information
AnthonyMikh authored and oleganza committed Oct 11, 2019
1 parent 426c87a commit b1edf95
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/inner_product_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ impl InnerProductProof {
buf
}

/// Converts the proof into a byte iterator over serialized view of the proof.
/// The layout of the inner product proof is:
/// * \\(n\\) pairs of compressed Ristretto points \\(L_0, R_0 \dots, L_{n-1}, R_{n-1}\\),
/// * two scalars \\(a, b\\).
#[inline]
pub(crate) fn to_bytes_iter(&self) -> impl Iterator<Item = u8> + '_ {
self.L_vec
.iter()
.zip(self.R_vec.iter())
.flat_map(|(l, r)| l.as_bytes().iter().chain(r.as_bytes()))
.chain(self.a.as_bytes())
.chain(self.b.as_bytes())
.copied()
}

/// Deserializes the proof from a byte slice.
/// Returns an error in the following cases:
/// * the slice does not have \\(2n+2\\) 32-byte elements,
Expand Down
3 changes: 1 addition & 2 deletions src/r1cs/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ impl R1CSProof {
buf.extend_from_slice(self.t_x.as_bytes());
buf.extend_from_slice(self.t_x_blinding.as_bytes());
buf.extend_from_slice(self.e_blinding.as_bytes());
// XXX this costs an extra alloc
buf.extend_from_slice(self.ipp_proof.to_bytes().as_slice());
buf.extend(self.ipp_proof.to_bytes_iter());
buf
}

Expand Down
3 changes: 1 addition & 2 deletions src/range_proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ impl RangeProof {
buf.extend_from_slice(self.t_x.as_bytes());
buf.extend_from_slice(self.t_x_blinding.as_bytes());
buf.extend_from_slice(self.e_blinding.as_bytes());
// XXX this costs an extra alloc
buf.extend_from_slice(self.ipp_proof.to_bytes().as_slice());
buf.extend(self.ipp_proof.to_bytes_iter());
buf
}

Expand Down

0 comments on commit b1edf95

Please sign in to comment.