Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: refactored transcript traits and impls #648

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions plonk/src/circuit/plonk_verifier/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/// - poly_evals: zeta^n, zeta^n-1 and Lagrange evaluated at 1
/// - batch_proof: batched proof inputs
/// - non_native_field_info: aux information for non-native field
/// Output

Check warning on line 46 in plonk/src/circuit/plonk_verifier/gadgets.rs

View workflow job for this annotation

GitHub Actions / clippy

doc list item missing indentation

warning: doc list item missing indentation --> plonk/src/circuit/plonk_verifier/gadgets.rs:46:5 | 46 | /// Output | ^ | = help: if this is supposed to be its own paragraph, add a blank line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_lazy_continuation = note: `#[warn(clippy::doc_lazy_continuation)]` on by default help: indent this line | 46 | /// Output | ++
/// - scalar and bases prepared for MSM
/// - buffer info for u and v powers
pub(super) fn aggregate_poly_commitments_circuit<E, F>(
Expand Down Expand Up @@ -225,25 +225,25 @@
transcript_var.append_commitments_vars(b"witness_poly_comms", wires_poly_comms)?;
}

let beta = transcript_var.get_and_append_challenge_var::<E>(b"beta", circuit)?;
let gamma = transcript_var.get_and_append_challenge_var::<E>(b"gamma", circuit)?;
let beta = transcript_var.get_challenge_var::<E>(b"beta", circuit)?;
let gamma = transcript_var.get_challenge_var::<E>(b"gamma", circuit)?;
for prod_perm_poly_comm in batch_proof.prod_perm_poly_comms_vec.iter() {
transcript_var.append_commitment_var(b"perm_poly_comms", prod_perm_poly_comm)?;
}

let alpha = transcript_var.get_and_append_challenge_var::<E>(b"alpha", circuit)?;
let alpha = transcript_var.get_challenge_var::<E>(b"alpha", circuit)?;
transcript_var
.append_commitments_vars(b"quot_poly_comms", &batch_proof.split_quot_poly_comms)?;
let zeta = transcript_var.get_and_append_challenge_var::<E>(b"zeta", circuit)?;
let zeta = transcript_var.get_challenge_var::<E>(b"zeta", circuit)?;
for poly_evals in batch_proof.poly_evals_vec.iter() {
transcript_var.append_proof_evaluations_vars(circuit, poly_evals)?;
}

let v = transcript_var.get_and_append_challenge_var::<E>(b"v", circuit)?;
let v = transcript_var.get_challenge_var::<E>(b"v", circuit)?;
transcript_var.append_commitment_var(b"open_proof", &batch_proof.opening_proof)?;
transcript_var
.append_commitment_var(b"shifted_open_proof", &batch_proof.shifted_opening_proof)?;
let u = transcript_var.get_and_append_challenge_var::<E>(b"u", circuit)?;
let u = transcript_var.get_challenge_var::<E>(b"u", circuit)?;

// convert challenge vars into FpElemVars
let challenge_var = ChallengesVar {
Expand Down
27 changes: 8 additions & 19 deletions plonk/src/circuit/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ where
Ok(())
}

// Append a challenge variable to the transcript.
// For efficiency purpose, label is not used for rescue FS.
pub(crate) fn append_challenge_var(
&mut self,
_label: &'static [u8],
challenge_var: &Variable,
) -> Result<(), CircuitError> {
self.append_variable(_label, challenge_var)
}

// Append the proof evaluation to the transcript
pub(crate) fn append_proof_evaluations_vars(
&mut self,
Expand All @@ -171,7 +161,7 @@ where
// For efficiency purpose, label is not used for rescue FS.
// Note that this function currently only supports bls12-377
// curve due to its decomposition method.
pub(crate) fn get_and_append_challenge_var<E>(
pub(crate) fn get_challenge_var<E>(
&mut self,
_label: &'static [u8],
alxiong marked this conversation as resolved.
Show resolved Hide resolved
circuit: &mut PlonkCircuit<F>,
Expand All @@ -193,7 +183,7 @@ where
// This algorithm takes in 3 steps
// 1. state: [F: STATE_SIZE] = hash(state|transcript)
// 2. challenge = state[0] in Fr
// 3. transcript = vec![challenge]
// 3. transcript = vec![]
// ==================================

// step 1. state: [F: STATE_SIZE] = hash(state|transcript)
Expand All @@ -210,7 +200,6 @@ where
// finish and update the states
self.state_var.copy_from_slice(&res_var[0..STATE_SIZE]);
self.transcript_var = Vec::new();
self.append_challenge_var(_label, &challenge_var)?;

Ok(challenge_var)
}
Expand Down Expand Up @@ -267,10 +256,10 @@ mod tests {
.unwrap();
}

let challenge = transcript.get_and_append_challenge::<E>(label).unwrap();
let challenge = transcript.get_challenge::<E>(label).unwrap();

let challenge_var = transcript_var
.get_and_append_challenge_var::<E>(label, &mut circuit)
.get_challenge_var::<E>(label, &mut circuit)
.unwrap();

assert_eq!(
Expand Down Expand Up @@ -329,10 +318,10 @@ mod tests {
.append_vk_and_pub_input_vars::<E>(&mut circuit, &dummy_vk_var, &[])
.unwrap();

let challenge = transcript.get_and_append_challenge::<E>(label).unwrap();
let challenge = transcript.get_challenge::<E>(label).unwrap();

let challenge_var = transcript_var
.get_and_append_challenge_var::<E>(label, &mut circuit)
.get_challenge_var::<E>(label, &mut circuit)
.unwrap();

assert_eq!(
Expand Down Expand Up @@ -398,10 +387,10 @@ mod tests {
.append_vk_and_pub_input_vars::<E>(&mut circuit, &vk_var, &input_fp_elem_vars)
.unwrap();

let challenge = transcript.get_and_append_challenge::<E>(label).unwrap();
let challenge = transcript.get_challenge::<E>(label).unwrap();

let challenge_var = transcript_var
.get_and_append_challenge_var::<E>(label, &mut circuit)
.get_challenge_var::<E>(label, &mut circuit)
.unwrap();

assert_eq!(
Expand Down
3 changes: 3 additions & 0 deletions plonk/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub(crate) const EXTRA_TRANSCRIPT_MSG_LABEL: &[u8] = b"extra info";
pub(crate) const fn domain_size_ratio(n: usize, num_wire_types: usize) -> usize {
(num_wire_types * (n + 1) + 2) / n + 1
}

/// Keccak-256 have a 32 byte state size.
pub const KECCAK256_STATE_SIZE: usize = 32;
12 changes: 6 additions & 6 deletions plonk/src/proof_system/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ where
// Plookup: compute and interpolate the sorted concatenation of the (merged)
// lookup table and the (merged) witness values
if circuits.iter().any(|c| C::support_lookup(c)) {
challenges.tau = Some(transcript.get_and_append_challenge::<E>(b"tau")?);
challenges.tau = Some(transcript.get_challenge::<E>(b"tau")?);
} else {
challenges.tau = None;
}
Expand Down Expand Up @@ -284,8 +284,8 @@ where
}

// Round 2
challenges.beta = transcript.get_and_append_challenge::<E>(b"beta")?;
challenges.gamma = transcript.get_and_append_challenge::<E>(b"gamma")?;
challenges.beta = transcript.get_challenge::<E>(b"beta")?;
challenges.gamma = transcript.get_challenge::<E>(b"gamma")?;
let mut prod_perm_poly_comms_vec = vec![];
for i in 0..circuits.len() {
let (prod_perm_poly_comm, prod_perm_poly) =
Expand Down Expand Up @@ -318,7 +318,7 @@ where
}

// Round 3
challenges.alpha = transcript.get_and_append_challenge::<E>(b"alpha")?;
challenges.alpha = transcript.get_challenge::<E>(b"alpha")?;
let (split_quot_poly_comms, split_quot_polys) = prover.run_3rd_round(
prng,
&prove_keys[0].commit_key,
Expand All @@ -330,7 +330,7 @@ where
transcript.append_commitments(b"quot_poly_comms", &split_quot_poly_comms)?;

// Round 4
challenges.zeta = transcript.get_and_append_challenge::<E>(b"zeta")?;
challenges.zeta = transcript.get_challenge::<E>(b"zeta")?;
let mut poly_evals_vec = vec![];
for i in 0..circuits.len() {
let poly_evals = prover.compute_evaluations(
Expand Down Expand Up @@ -389,7 +389,7 @@ where
}

// Round 5
challenges.v = transcript.get_and_append_challenge::<E>(b"v")?;
challenges.v = transcript.get_challenge::<E>(b"v")?;
let (opening_proof, shifted_opening_proof) = prover.compute_opening_proofs(
&prove_keys[0].commit_key,
prove_keys,
Expand Down
18 changes: 9 additions & 9 deletions plonk/src/proof_system/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
/// - `Ai = [open_proof_i] + u_i * [shifted_open_proof_i]` and
/// - `Bi = eval_point_i * [open_proof_i] + u_i * next_eval_point_i *
/// [shifted_open_proof_i] + comm_i - eval_i * [1]1`.
/// By Schwartz-Zippel lemma, it's equivalent to check that for a random r:

Check warning on line 198 in plonk/src/proof_system/verifier.rs

View workflow job for this annotation

GitHub Actions / clippy

doc list item missing indentation

warning: doc list item missing indentation --> plonk/src/proof_system/verifier.rs:198:9 | 198 | /// By Schwartz-Zippel lemma, it's equivalent to check that for a random r: | ^ | = help: if this is supposed to be its own paragraph, add a blank line = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#doc_lazy_continuation help: indent this line | 198 | /// By Schwartz-Zippel lemma, it's equivalent to check that for a random r: | ++
/// - `e(A0 + ... + r^{m-1} * Am, [x]2) = e(B0 + ... + r^{m-1} * Bm, [1]2)`.
pub(crate) fn batch_verify_opening_proofs<T>(
open_key: &OpenKey<E>,
Expand All @@ -214,9 +214,9 @@
// protocol transcript. This approach is more secure as `r` depends not only
// on the proofs, but also the list of public inputs and verifying keys.
for pcs_info in pcs_infos {
transcript.append_challenge::<E>(b"u", &pcs_info.u)?;
transcript.append_field::<E>(b"u", &pcs_info.u)?;
}
transcript.get_and_append_challenge::<E>(b"r")?
transcript.get_challenge::<E>(b"r")?
};

// Compute A := A0 + r * A1 + ... + r^{m-1} * Am
Expand Down Expand Up @@ -287,7 +287,7 @@
transcript.append_commitments(b"witness_poly_comms", wires_poly_comms)?;
}
let tau = if verify_keys.iter().any(|vk| vk.plookup_vk.is_some()) {
Some(transcript.get_and_append_challenge::<E>(b"tau")?)
Some(transcript.get_challenge::<E>(b"tau")?)
} else {
None
};
Expand All @@ -298,8 +298,8 @@
}
}

let beta = transcript.get_and_append_challenge::<E>(b"beta")?;
let gamma = transcript.get_and_append_challenge::<E>(b"gamma")?;
let beta = transcript.get_challenge::<E>(b"beta")?;
let gamma = transcript.get_challenge::<E>(b"gamma")?;
for prod_perm_poly_comm in batch_proof.prod_perm_poly_comms_vec.iter() {
transcript.append_commitment(b"perm_poly_comms", prod_perm_poly_comm)?;
}
Expand All @@ -310,9 +310,9 @@
}
}

let alpha = transcript.get_and_append_challenge::<E>(b"alpha")?;
let alpha = transcript.get_challenge::<E>(b"alpha")?;
transcript.append_commitments(b"quot_poly_comms", &batch_proof.split_quot_poly_comms)?;
let zeta = transcript.get_and_append_challenge::<E>(b"zeta")?;
let zeta = transcript.get_challenge::<E>(b"zeta")?;
for poly_evals in batch_proof.poly_evals_vec.iter() {
transcript.append_proof_evaluations::<E>(poly_evals)?;
}
Expand All @@ -322,10 +322,10 @@
}
}

let v = transcript.get_and_append_challenge::<E>(b"v")?;
let v = transcript.get_challenge::<E>(b"v")?;
transcript.append_commitment(b"open_proof", &batch_proof.opening_proof)?;
transcript.append_commitment(b"shifted_open_proof", &batch_proof.shifted_opening_proof)?;
let u = transcript.get_and_append_challenge::<E>(b"u")?;
let u = transcript.get_challenge::<E>(b"u")?;
Ok(Challenges {
tau,
alpha,
Expand Down
9 changes: 5 additions & 4 deletions plonk/src/testing_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#![allow(missing_docs)]

use crate::{
constants::KECCAK256_STATE_SIZE,
errors::PlonkError,
lagrange::LagrangeCoeffs,
proof_system::{
Expand Down Expand Up @@ -379,12 +380,12 @@ where
/// exposing the internal states for testing purposes
impl SolidityTranscript {
/// Create a new transcript from specific internal states.
pub fn from_internal(transcript: Vec<u8>) -> Self {
Self { transcript }
pub fn from_internal(state: [u8; KECCAK256_STATE_SIZE], transcript: Vec<u8>) -> Self {
Self { state, transcript }
}

/// Returns the internal states
pub fn internal(&self) -> Vec<u8> {
self.transcript.clone()
pub fn internal(&self) -> ([u8; KECCAK256_STATE_SIZE], Vec<u8>) {
(self.state.clone(), self.transcript.clone())
}
}
Loading
Loading