From c5e03649626c20300a49e56219da0c31e01314b1 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Wed, 23 Dec 2020 16:20:27 -0700 Subject: [PATCH] Remove the Read/Write type parameters from Transcript{Read,Write}. --- benches/plonk.rs | 2 +- examples/performance_model.rs | 2 +- src/plonk.rs | 2 +- src/plonk/lookup/prover.rs | 12 ++------ src/plonk/lookup/verifier.rs | 11 ++----- src/plonk/permutation/prover.rs | 5 ++-- src/plonk/permutation/verifier.rs | 5 ++-- src/plonk/prover.rs | 8 +---- src/plonk/vanishing/prover.rs | 6 ++-- src/plonk/vanishing/verifier.rs | 5 ++-- src/plonk/verifier.rs | 3 +- src/poly/commitment/prover.rs | 4 +-- src/poly/commitment/verifier.rs | 4 +-- src/poly/multiopen/prover.rs | 4 +-- src/poly/multiopen/verifier.rs | 5 +--- src/transcript.rs | 49 +++++++++++++++---------------- 16 files changed, 49 insertions(+), 78 deletions(-) diff --git a/benches/plonk.rs b/benches/plonk.rs index 6712db2..8c61570 100644 --- a/benches/plonk.rs +++ b/benches/plonk.rs @@ -6,7 +6,7 @@ use halo2::arithmetic::FieldExt; use halo2::pasta::{EqAffine, Fp, Fq}; use halo2::plonk::*; use halo2::poly::commitment::Params; -use halo2::transcript::{DummyHashRead, DummyHashWrite, TranscriptRead, TranscriptWrite}; +use halo2::transcript::{DummyHashRead, DummyHashWrite}; use std::marker::PhantomData; diff --git a/examples/performance_model.rs b/examples/performance_model.rs index 2408899..0b87170 100644 --- a/examples/performance_model.rs +++ b/examples/performance_model.rs @@ -4,7 +4,7 @@ use halo2::{ pasta::{EqAffine, Fp, Fq}, plonk::*, poly::commitment::{Blind, Params}, - transcript::{DummyHashRead, DummyHashWrite, TranscriptRead, TranscriptWrite}, + transcript::{DummyHashRead, DummyHashWrite}, }; use std::marker::PhantomData; diff --git a/src/plonk.rs b/src/plonk.rs index 222471e..e87e09e 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -105,7 +105,7 @@ fn test_proving() { use crate::arithmetic::{Curve, FieldExt}; use crate::pasta::{EqAffine, Fp, Fq}; use crate::poly::commitment::{Blind, Params}; - use crate::transcript::{DummyHashRead, DummyHashWrite, TranscriptRead, TranscriptWrite}; + use crate::transcript::{DummyHashRead, DummyHashWrite}; use circuit::{Advice, Column, Fixed}; use std::marker::PhantomData; const K: u32 = 5; diff --git a/src/plonk/lookup/prover.rs b/src/plonk/lookup/prover.rs index 3e452ac..82e7921 100644 --- a/src/plonk/lookup/prover.rs +++ b/src/plonk/lookup/prover.rs @@ -13,7 +13,6 @@ use crate::{ transcript::TranscriptWrite, }; use ff::Field; -use std::io::Write; use std::{collections::BTreeMap, iter}; #[derive(Debug)] @@ -73,12 +72,7 @@ impl Argument { /// - constructs Permuted struct using permuted_input_value = A', and /// permuted_table_column = S'. /// The Permuted struct is used to update the Lookup, and is then returned. - pub(in crate::plonk) fn commit_permuted< - 'a, - C: CurveAffine, - W: Write, - T: TranscriptWrite, - >( + pub(in crate::plonk) fn commit_permuted<'a, C: CurveAffine, T: TranscriptWrite>( &self, pk: &ProvingKey, params: &Params, @@ -195,7 +189,7 @@ impl<'a, C: CurveAffine> Permuted<'a, C> { /// grand product polynomial over the lookup. The grand product polynomial /// is used to populate the Product struct. The Product struct is /// added to the Lookup and finally returned by the method. - pub(in crate::plonk) fn commit_product>( + pub(in crate::plonk) fn commit_product>( self, pk: &ProvingKey, params: &Params, @@ -443,7 +437,7 @@ impl<'a, C: CurveAffine> Committed<'a, C> { } impl Constructed { - pub(in crate::plonk) fn evaluate>( + pub(in crate::plonk) fn evaluate>( self, pk: &ProvingKey, x: ChallengeX, diff --git a/src/plonk/lookup/verifier.rs b/src/plonk/lookup/verifier.rs index 23a5c68..09be0ac 100644 --- a/src/plonk/lookup/verifier.rs +++ b/src/plonk/lookup/verifier.rs @@ -9,7 +9,6 @@ use crate::{ transcript::TranscriptRead, }; use ff::Field; -use std::io::Read; pub struct PermutationCommitments { permuted_input_commitment: C, @@ -32,11 +31,7 @@ pub struct Evaluated { } impl Argument { - pub(in crate::plonk) fn absorb_permuted_commitments< - C: CurveAffine, - R: Read, - T: TranscriptRead, - >( + pub(in crate::plonk) fn absorb_permuted_commitments>( &self, transcript: &mut T, ) -> Result, Error> { @@ -55,7 +50,7 @@ impl Argument { } impl PermutationCommitments { - pub(in crate::plonk) fn absorb_product_commitment>( + pub(in crate::plonk) fn absorb_product_commitment>( self, transcript: &mut T, ) -> Result, Error> { @@ -72,7 +67,7 @@ impl PermutationCommitments { } impl Committed { - pub(crate) fn evaluate>( + pub(crate) fn evaluate>( self, transcript: &mut T, ) -> Result, Error> { diff --git a/src/plonk/permutation/prover.rs b/src/plonk/permutation/prover.rs index 2c218fa..7d5fd7a 100644 --- a/src/plonk/permutation/prover.rs +++ b/src/plonk/permutation/prover.rs @@ -1,5 +1,4 @@ use ff::Field; -use std::io::Write; use std::iter; use super::{Argument, ProvingKey}; @@ -34,7 +33,7 @@ pub(crate) struct Evaluated { } impl Argument { - pub(in crate::plonk) fn commit>( + pub(in crate::plonk) fn commit>( &self, params: &Params, pk: &plonk::ProvingKey, @@ -237,7 +236,7 @@ impl super::ProvingKey { } impl Constructed { - pub(in crate::plonk) fn evaluate>( + pub(in crate::plonk) fn evaluate>( self, pk: &plonk::ProvingKey, pkey: &ProvingKey, diff --git a/src/plonk/permutation/verifier.rs b/src/plonk/permutation/verifier.rs index 0bfe7b5..38205f4 100644 --- a/src/plonk/permutation/verifier.rs +++ b/src/plonk/permutation/verifier.rs @@ -1,5 +1,4 @@ use ff::Field; -use std::io::Read; use std::iter; use super::{Argument, VerifyingKey}; @@ -22,7 +21,7 @@ pub struct Evaluated { } impl Argument { - pub(crate) fn absorb_product_commitment>( + pub(crate) fn absorb_product_commitment>( &self, transcript: &mut T, ) -> Result, Error> { @@ -37,7 +36,7 @@ impl Argument { } impl Committed { - pub(crate) fn evaluate>( + pub(crate) fn evaluate>( self, vkey: &VerifyingKey, transcript: &mut T, diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 5f5429f..365ae55 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -1,5 +1,4 @@ use ff::Field; -use std::io::Write; use std::iter; use super::{ @@ -18,12 +17,7 @@ use crate::transcript::TranscriptWrite; /// This creates a proof for the provided `circuit` when given the public /// parameters `params` and the proving key [`ProvingKey`] that was /// generated previously for the same circuit. -pub fn create_proof< - C: CurveAffine, - W: Write, - T: TranscriptWrite, - ConcreteCircuit: Circuit, ->( +pub fn create_proof, ConcreteCircuit: Circuit>( params: &Params, pk: &ProvingKey, circuit: &ConcreteCircuit, diff --git a/src/plonk/vanishing/prover.rs b/src/plonk/vanishing/prover.rs index b50176f..6f182ac 100644 --- a/src/plonk/vanishing/prover.rs +++ b/src/plonk/vanishing/prover.rs @@ -1,5 +1,3 @@ -use std::io::Write; - use super::Argument; use crate::{ arithmetic::{eval_polynomial, Curve, CurveAffine, FieldExt}, @@ -23,7 +21,7 @@ pub(in crate::plonk) struct Evaluated { } impl Argument { - pub(in crate::plonk) fn construct>( + pub(in crate::plonk) fn construct>( params: &Params, domain: &EvaluationDomain, expressions: impl Iterator>, @@ -69,7 +67,7 @@ impl Argument { } impl Constructed { - pub(in crate::plonk) fn evaluate>( + pub(in crate::plonk) fn evaluate>( self, x: ChallengeX, transcript: &mut T, diff --git a/src/plonk/vanishing/verifier.rs b/src/plonk/vanishing/verifier.rs index 90cfb9e..1641e15 100644 --- a/src/plonk/vanishing/verifier.rs +++ b/src/plonk/vanishing/verifier.rs @@ -1,5 +1,4 @@ use ff::Field; -use std::io::Read; use crate::{ arithmetic::CurveAffine, @@ -20,7 +19,7 @@ pub struct Evaluated { } impl Argument { - pub(in crate::plonk) fn absorb_commitments>( + pub(in crate::plonk) fn absorb_commitments>( vk: &VerifyingKey, transcript: &mut T, ) -> Result, Error> { @@ -33,7 +32,7 @@ impl Argument { } impl Committed { - pub(in crate::plonk) fn evaluate>( + pub(in crate::plonk) fn evaluate>( self, transcript: &mut T, ) -> Result, Error> { diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index 4526500..02b4c7c 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -1,5 +1,4 @@ use ff::Field; -use std::io::Read; use std::iter; use super::{ @@ -14,7 +13,7 @@ use crate::poly::{ use crate::transcript::{read_n_points, read_n_scalars, TranscriptRead}; /// Returns a boolean indicating whether or not the proof is valid -pub fn verify_proof<'a, C: CurveAffine, R: Read, T: TranscriptRead>( +pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( params: &'a Params, vk: &VerifyingKey, msm: MSM<'a, C>, diff --git a/src/poly/commitment/prover.rs b/src/poly/commitment/prover.rs index bbe1445..12afe08 100644 --- a/src/poly/commitment/prover.rs +++ b/src/poly/commitment/prover.rs @@ -7,7 +7,7 @@ use crate::arithmetic::{ CurveAffine, FieldExt, }; use crate::transcript::{Challenge, ChallengeScalar, TranscriptWrite}; -use std::io::{self, Write}; +use std::io; /// Create a polynomial commitment opening proof for the polynomial defined /// by the coefficients `px`, the blinding factor `blind` used for the @@ -22,7 +22,7 @@ use std::io::{self, Write}; /// opening v, and the point x. It's probably also nice for the transcript /// to have seen the elliptic curve description and the SRS, if you want to /// be rigorous. -pub fn create_proof>( +pub fn create_proof>( params: &Params, transcript: &mut T, px: &Polynomial, diff --git a/src/poly/commitment/verifier.rs b/src/poly/commitment/verifier.rs index 970765f..0adf9ba 100644 --- a/src/poly/commitment/verifier.rs +++ b/src/poly/commitment/verifier.rs @@ -6,8 +6,6 @@ use crate::transcript::{Challenge, ChallengeScalar, TranscriptRead}; use crate::arithmetic::{best_multiexp, Curve, CurveAffine, FieldExt}; -use std::io::Read; - /// A guard returned by the verifier #[derive(Debug, Clone)] pub struct Guard<'a, C: CurveAffine> { @@ -67,7 +65,7 @@ impl<'a, C: CurveAffine> Guard<'a, C> { /// Checks to see if an [`Proof`] is valid given the current `transcript`, and a /// point `x` that the polynomial commitment `P` opens purportedly to the value /// `v`. The provided `msm` should evaluate to the commitment `P` being opened. -pub fn verify_proof<'a, C: CurveAffine, R: Read, T: TranscriptRead>( +pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( params: &'a Params, mut msm: MSM<'a, C>, transcript: &mut T, diff --git a/src/poly/multiopen/prover.rs b/src/poly/multiopen/prover.rs index 6244e20..26be711 100644 --- a/src/poly/multiopen/prover.rs +++ b/src/poly/multiopen/prover.rs @@ -13,7 +13,7 @@ use crate::arithmetic::{ use crate::transcript::TranscriptWrite; use ff::Field; -use std::io::{self, Write}; +use std::io; use std::marker::PhantomData; #[derive(Debug, Clone)] @@ -25,7 +25,7 @@ struct CommitmentData { } /// Create a multi-opening proof -pub fn create_proof<'a, I, C: CurveAffine, W: Write, T: TranscriptWrite>( +pub fn create_proof<'a, I, C: CurveAffine, T: TranscriptWrite>( params: &Params, transcript: &mut T, queries: I, diff --git a/src/poly/multiopen/verifier.rs b/src/poly/multiopen/verifier.rs index 9202bdd..dea3cf0 100644 --- a/src/poly/multiopen/verifier.rs +++ b/src/poly/multiopen/verifier.rs @@ -10,9 +10,6 @@ use super::{ }; use crate::arithmetic::{eval_polynomial, lagrange_interpolate, CurveAffine, FieldExt}; use crate::transcript::TranscriptRead; - -use std::io::Read; - #[derive(Debug, Clone)] struct CommitmentData { set_index: usize, @@ -21,7 +18,7 @@ struct CommitmentData { } /// Verify a multi-opening proof -pub fn verify_proof<'b, 'a: 'b, I, C: CurveAffine, R: Read, T: TranscriptRead>( +pub fn verify_proof<'b, 'a: 'b, I, C: CurveAffine, T: TranscriptRead>( params: &'a Params, transcript: &mut T, queries: I, diff --git a/src/transcript.rs b/src/transcript.rs index 16a1162..537556c 100644 --- a/src/transcript.rs +++ b/src/transcript.rs @@ -21,10 +21,7 @@ pub trait Transcript { /// Transcript view from the perspective of a verifier that has access to an /// input stream of data from the prover to the verifier. -pub trait TranscriptRead: Transcript { - /// Initialize the transcript with a key and an input stream. - fn init(reader: R, key: C::Base) -> Self; - +pub trait TranscriptRead: Transcript { /// Read a curve point from the prover. fn read_point(&mut self) -> io::Result; @@ -34,12 +31,9 @@ pub trait TranscriptRead: Transcript { /// Transcript view from the perspective of a prover that has access to an /// output stream of messages from the prover to the verifier. -pub trait TranscriptWrite: Transcript { +pub trait TranscriptWrite: Transcript { /// Forked transcript that does not write to the proof structure. - type ForkedTranscript: TranscriptWrite; - - /// Initialize the transcript with a key and an output stream. - fn init(writer: W, key: C::Base) -> Self; + type ForkedTranscript: TranscriptWrite; /// Write a curve point to the proof and the transcript. fn write_point(&mut self, point: C) -> io::Result<()>; @@ -50,10 +44,6 @@ pub trait TranscriptWrite: Transcript { /// Fork the transcript, creating a variant of this `TranscriptWrite` which /// does not output anything to the writer. fn fork(&self) -> Self::ForkedTranscript; - - /// Return the writer to conclude the interaction and take possession of the - /// proof. - fn finalize(self) -> W; } /// This is just a simple (and completely broken) transcript reader @@ -67,8 +57,9 @@ pub struct DummyHashRead { reader: R, } -impl TranscriptRead for DummyHashRead { - fn init(reader: R, key: C::Base) -> Self { +impl DummyHashRead { + /// Initialize a transcript given an input buffer and a key. + pub fn init(reader: R, key: C::Base) -> Self { DummyHashRead { base_state: key + &C::Base::from_u64(1013), scalar_state: C::Scalar::from_u64(1013), @@ -76,7 +67,9 @@ impl TranscriptRead for DummyHashRead { reader, } } +} +impl TranscriptRead for DummyHashRead { fn read_point(&mut self) -> io::Result { let mut compressed = [0u8; 32]; self.reader.read_exact(&mut compressed[..])?; @@ -149,10 +142,9 @@ pub struct DummyHashWrite { writer: W, } -impl TranscriptWrite for DummyHashWrite { - type ForkedTranscript = DummyHashWrite; - - fn init(writer: W, key: C::Base) -> Self { +impl DummyHashWrite { + /// Initialize a transcript given an output buffer and a key. + pub fn init(writer: W, key: C::Base) -> Self { DummyHashWrite { base_state: key + &C::Base::from_u64(1013), scalar_state: C::Scalar::from_u64(1013), @@ -160,6 +152,17 @@ impl TranscriptWrite for DummyHashWrite { writer, } } + + /// Conclude the interaction and return the output buffer (writer). + pub fn finalize(self) -> W { + // TODO: handle outstanding scalars? + self.writer + } +} + +impl TranscriptWrite for DummyHashWrite { + type ForkedTranscript = DummyHashWrite; + fn write_point(&mut self, point: C) -> io::Result<()> { self.common_point(point)?; let compressed = point.to_bytes(); @@ -180,10 +183,6 @@ impl TranscriptWrite for DummyHashWrite { writer: io::sink(), } } - fn finalize(self) -> W { - // TODO: handle outstanding scalars? - self.writer - } } impl Transcript for DummyHashWrite { @@ -285,7 +284,7 @@ impl Deref for ChallengeScalar { } } -pub(crate) fn read_n_points>( +pub(crate) fn read_n_points>( transcript: &mut T, n: usize, ) -> io::Result> { @@ -296,7 +295,7 @@ pub(crate) fn read_n_points>( Ok(v) } -pub(crate) fn read_n_scalars>( +pub(crate) fn read_n_scalars>( transcript: &mut T, n: usize, ) -> io::Result> {