From 18abe90e0e54c00fca19db56b3276cfdcd8e4269 Mon Sep 17 00:00:00 2001 From: "sm.wu" Date: Thu, 11 Jul 2024 21:03:18 +0800 Subject: [PATCH] clean up --- gkr/examples/keccak256.rs | 21 +- gkr/src/circuit/circuit_layout.rs | 5 +- gkr/src/circuit/circuit_witness.rs | 683 ++++++++++++++++++++++++++--- gkr/src/prover.rs | 12 +- gkr/src/prover/phase1.rs | 5 +- gkr/src/structs.rs | 1 - gkr/src/test/is_zero_gadget.rs | 9 +- gkr/src/verifier.rs | 1 - multilinear_extensions/src/mle.rs | 2 +- 9 files changed, 633 insertions(+), 106 deletions(-) diff --git a/gkr/examples/keccak256.rs b/gkr/examples/keccak256.rs index 73b660e74..90d4d4b66 100644 --- a/gkr/examples/keccak256.rs +++ b/gkr/examples/keccak256.rs @@ -8,11 +8,10 @@ use ff_ext::ExtensionField; use gkr::{ gadgets::keccak256::{keccak256_circuit, prove_keccak256, verify_keccak256}, structs::CircuitWitness, - util::ceil_log2, }; use goldilocks::GoldilocksExt2; use itertools::{izip, Itertools}; -use multilinear_extensions::mle::DenseMultilinearExtension; +use multilinear_extensions::mle::IntoMLE; use sumcheck::util::is_power_of_2; use tracing_flame::FlameLayer; use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Registry}; @@ -52,28 +51,14 @@ fn main() { vec![::BaseField::ZERO; 17 * 64], ] .into_iter() - .map(|mut wit_in| { - let next_pow_2 = ceil_log2(wit_in.len()); - wit_in.resize( - 1 << next_pow_2, - ::BaseField::ZERO, - ); - DenseMultilinearExtension::from_evaluations_vec(ceil_log2(wit_in.len()), wit_in) - }) + .map(|wit_in| wit_in.into_mle()) .collect(); let all_one = vec![ vec![::BaseField::ONE; 25 * 64], vec![::BaseField::ZERO; 17 * 64], ] .into_iter() - .map(|mut wit_in| { - let next_pow_2 = ceil_log2(wit_in.len()); - wit_in.resize( - 1 << next_pow_2, - ::BaseField::ZERO, - ); - DenseMultilinearExtension::from_evaluations_vec(ceil_log2(wit_in.len()), wit_in) - }) + .map(|wit_in| wit_in.into_mle()) .collect(); let mut witness = CircuitWitness::new(&circuit, Vec::new()); witness.add_instance(&circuit, all_zero); diff --git a/gkr/src/circuit/circuit_layout.rs b/gkr/src/circuit/circuit_layout.rs index 8e71bd4cb..f9e5728b2 100644 --- a/gkr/src/circuit/circuit_layout.rs +++ b/gkr/src/circuit/circuit_layout.rs @@ -282,10 +282,7 @@ impl Circuit { || circuit_builder.n_witness_out() == 1 && output_copy_to[0] != seg || !output_assert_const.is_empty() { - curr_sc_steps.extend([ - SumcheckStepType::OutputPhase1Step1, - SumcheckStepType::OutputPhase1Step2, - ]); + curr_sc_steps.extend([SumcheckStepType::OutputPhase1Step1]); } } else { let last_layer = &layers[(layer_id - 1) as usize]; diff --git a/gkr/src/circuit/circuit_witness.rs b/gkr/src/circuit/circuit_witness.rs index b31bee97e..66c22e0c8 100644 --- a/gkr/src/circuit/circuit_witness.rs +++ b/gkr/src/circuit/circuit_witness.rs @@ -5,7 +5,7 @@ use ff::Field; use ff_ext::ExtensionField; use itertools::{izip, Itertools}; use multilinear_extensions::{ - mle::{DenseMultilinearExtension, MultilinearExtension}, + mle::{DenseMultilinearExtension, IntoMLE, MultilinearExtension}, virtual_poly_v2::ArcMultilinearExtension, }; use simple_frontend::structs::{ChallengeConst, LayerId}; @@ -78,10 +78,7 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { } } - DenseMultilinearExtension::from_evaluations_vec( - ceil_log2(circuit.layers[n_layers - 1].size() * n_instances), - layer_wit, - ) + layer_wit.into_mle() }; for (layer_id, layer) in circuit.layers.iter().enumerate().rev().skip(1) { @@ -145,7 +142,8 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { current_layer_wit, ); } - let mut wits_out = vec![vec![]; circuit.n_witness_out]; + let mut wits_out = vec![DenseMultilinearExtension::default(); circuit.n_witness_out]; + let output_layer_wit = layer_wits[0].get_base_field_vec(); for instance_id in 0..n_instances { let last_layer_instance_start_index = instance_id * circuit.layers[0].size(); circuit @@ -153,16 +151,13 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { .iter() .enumerate() .for_each(|(wit_id, old_wire_ids)| { - let layer_wit = layer_wits[0].get_base_field_vec(); - let mut wit_out = old_wire_ids + let wit_out = old_wire_ids .iter() .map(|old_wire_id| { - layer_wit[last_layer_instance_start_index + *old_wire_id] + output_layer_wit[last_layer_instance_start_index + *old_wire_id] }) .collect_vec(); - let length = wit_out.len().next_power_of_two(); - wit_out.resize(length, E::BaseField::ZERO); - wits_out[wit_id].extend(wit_out); + wits_out[wit_id] = wit_out.into_mle(); }); // #[cfg(debug_assertions)] @@ -172,13 +167,6 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { // } // }); } - let wits_out = wits_out - .into_iter() - .map(|wit_out| { - let num_vars = ceil_log2(wit_out.len()); - DenseMultilinearExtension::from_evaluations_vec(num_vars, wit_out) - }) - .collect(); (layer_wits, wits_out) } @@ -225,10 +213,7 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { } } - DenseMultilinearExtension::from_evaluations_vec( - ceil_log2(circuit.layers[n_layers - 1].size() * n_instances), - layer_wit, - ) + layer_wit.into_mle() }; for (layer_id, layer) in circuit.layers.iter().enumerate().rev().skip(1) { @@ -292,7 +277,8 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { current_layer_wit, ); } - let mut wits_out = vec![vec![]; circuit.n_witness_out]; + let mut wits_out = vec![DenseMultilinearExtension::default(); circuit.n_witness_out]; + let output_layer_wit = layer_wits[0].get_base_field_vec(); for instance_id in 0..n_instances { let last_layer_instance_start_index = instance_id * circuit.layers[0].size(); circuit @@ -300,16 +286,13 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { .iter() .enumerate() .for_each(|(wit_id, old_wire_ids)| { - let layer_wit = layer_wits[0].get_base_field_vec(); - let mut wit_out = old_wire_ids + let wit_out = old_wire_ids .iter() .map(|old_wire_id| { - layer_wit[last_layer_instance_start_index + *old_wire_id] + output_layer_wit[last_layer_instance_start_index + *old_wire_id] }) .collect_vec(); - let length = wit_out.len().next_power_of_two(); - wit_out.resize(length, E::BaseField::ZERO); - wits_out[wit_id].extend(wit_out); + wits_out[wit_id] = wit_out.into_mle(); }); // #[cfg(debug_assertions)] @@ -319,13 +302,6 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { // } // }); } - let wits_out = wits_out - .into_iter() - .map(|wit_out| { - let num_vars = ceil_log2(wit_out.len()); - DenseMultilinearExtension::from_evaluations_vec(num_vars, wit_out) - }) - .collect(); (layer_wits, wits_out) } @@ -605,42 +581,623 @@ impl<'a, E: ExtensionField> CircuitWitness<'a, E> { } } -// impl CircuitWitnessV2 { -// pub fn layer_poly( -// &self, -// layer_id: LayerId, -// single_num_vars: usize, -// multi_threads_meta: (usize, usize), -// ) -> ArcDenseMultilinearExtension { -// self.layers[layer_id as usize] -// .instances -// .as_slice() -// .mle_with_meta( -// single_num_vars, -// self.instance_num_vars(), -// multi_threads_meta, -// ) -// } -// } - impl<'a, F: ExtensionField> Debug for CircuitWitness<'a, F> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "CircuitWitness {{")?; writeln!(f, " n_instances: {}", self.n_instances)?; writeln!(f, " layers: ")?; - // for (i, layer) in self.layers.iter().enumerate() { - // writeln!(f, " {}: {:?}", i, layer)?; - // } + for (i, layer) in self.layers.iter().enumerate() { + writeln!(f, " {}: {:?}", i, layer.evaluations())?; + } writeln!(f, " wires_in: ")?; for (i, wire) in self.witness_in.iter().enumerate() { - // TODO figure out how to print dyn trait - writeln!(f, " {}: {:?}", i, &wire.name())?; + writeln!(f, " {}: {:?}", i, &wire.evaluations())?; } writeln!(f, " wires_out: ")?; - // for (i, wire) in self.witness_out.iter().enumerate() { - // writeln!(f, " {}: {:?}", i, wire)?; - // } + for (i, wire) in self.witness_out.iter().enumerate() { + writeln!(f, " {}: {:?}", i, &wire.evaluations())?; + } writeln!(f, " challenges: {:?}", self.challenges)?; writeln!(f, "}}") } } + +// #[cfg(test)] +// mod test { +// use std::{collections::HashMap, ops::Neg}; + +// use ff::Field; +// use ff_ext::ExtensionField; +// use goldilocks::GoldilocksExt2; +// use itertools::Itertools; +// use simple_frontend::structs::{ChallengeConst, ChallengeId, CircuitBuilder, ConstantType}; + +// use crate::{ +// structs::{Circuit, CircuitWitness, LayerWitness}, +// utils::i64_to_field, +// }; + +// fn copy_and_paste_circuit() -> Circuit { +// let mut circuit_builder = CircuitBuilder::::new(); +// // Layer 3 +// let (_, input) = circuit_builder.create_witness_in(4); + +// // Layer 2 +// let mul_01 = circuit_builder.create_cell(); +// circuit_builder.mul2(mul_01, input[0], input[1], Ext::BaseField::ONE); + +// // Layer 1 +// let mul_012 = circuit_builder.create_cell(); +// circuit_builder.mul2(mul_012, mul_01, input[2], Ext::BaseField::ONE); + +// // Layer 0 +// let (_, mul_001123) = circuit_builder.create_witness_out(1); +// circuit_builder.mul3( +// mul_001123[0], +// mul_01, +// mul_012, +// input[3], +// Ext::BaseField::ONE, +// ); + +// circuit_builder.configure(); +// let circuit = Circuit::new(&circuit_builder); + +// circuit +// } + +// fn copy_and_paste_witness() -> ( +// Vec>, +// CircuitWitness, +// ) { +// // witness_in, single instance +// let inputs = vec![vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ]]; +// let witness_in = vec![LayerWitness { instances: inputs }]; + +// let layers = vec![ +// LayerWitness { +// instances: vec![vec![i64_to_field(175175)]], +// }, +// LayerWitness { +// instances: vec![vec![ +// i64_to_field(385), +// i64_to_field(35), +// i64_to_field(13), +// i64_to_field(0), // pad +// ]], +// }, +// LayerWitness { +// instances: vec![vec![i64_to_field(35), i64_to_field(11)]], +// }, +// LayerWitness { +// instances: vec![vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ]], +// }, +// ]; + +// let outputs = vec![vec![i64_to_field(175175)]]; +// let witness_out = vec![LayerWitness { instances: outputs }]; + +// ( +// witness_in.clone(), +// CircuitWitness { +// layers, +// witness_in, +// witness_out, +// n_instances: 1, +// challenges: HashMap::new(), +// }, +// ) +// } + +// fn paste_from_wit_in_circuit() -> Circuit { +// let mut circuit_builder = CircuitBuilder::::new(); + +// // Layer 2 +// let (_leaf_id1, leaves1) = circuit_builder.create_witness_in(3); +// let (_leaf_id2, leaves2) = circuit_builder.create_witness_in(3); +// // Unused input elements should also be in the circuit. +// let (_dummy_id, _) = circuit_builder.create_witness_in(3); +// let _ = circuit_builder.create_counter_in(1); +// let _ = circuit_builder.create_constant_in(2, 1); + +// // Layer 1 +// let (_, inners) = circuit_builder.create_witness_out(2); +// circuit_builder.mul2(inners[0], leaves1[0], leaves1[1], Ext::BaseField::ONE); +// circuit_builder.mul2(inners[1], leaves1[2], leaves2[0], Ext::BaseField::ONE); + +// // Layer 0 +// let (_, root) = circuit_builder.create_witness_out(1); +// circuit_builder.mul2(root[0], inners[0], inners[1], Ext::BaseField::ONE); + +// circuit_builder.configure(); +// let circuit = Circuit::new(&circuit_builder); +// circuit +// } + +// fn paste_from_wit_in_witness() -> ( +// Vec>, +// CircuitWitness, +// ) { +// // witness_in, single instance +// let leaves1 = vec![vec![i64_to_field(5), i64_to_field(7), i64_to_field(11)]]; +// let leaves2 = vec![vec![i64_to_field(13), i64_to_field(17), i64_to_field(19)]]; +// let dummy = vec![vec![i64_to_field(13), i64_to_field(17), i64_to_field(19)]]; +// let witness_in = vec![ +// LayerWitness { instances: leaves1 }, +// LayerWitness { instances: leaves2 }, +// LayerWitness { instances: dummy }, +// ]; + +// let layers = vec![ +// LayerWitness { +// instances: vec![vec![ +// i64_to_field(5005), +// i64_to_field(35), +// i64_to_field(143), +// i64_to_field(0), // pad +// ]], +// }, +// LayerWitness { +// instances: vec![vec![i64_to_field(35), i64_to_field(143)]], +// }, +// LayerWitness { +// instances: vec![vec![ +// i64_to_field(5), // leaves1 +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), // leaves2 +// i64_to_field(17), +// i64_to_field(19), +// i64_to_field(13), // dummy +// i64_to_field(17), +// i64_to_field(19), +// i64_to_field(0), // counter +// i64_to_field(1), +// i64_to_field(1), // constant +// i64_to_field(1), +// i64_to_field(0), // pad +// i64_to_field(0), +// i64_to_field(0), +// ]], +// }, +// ]; + +// let outputs1 = vec![vec![i64_to_field(35), i64_to_field(143)]]; +// let outputs2 = vec![vec![i64_to_field(5005)]]; +// let witness_out = vec![ +// LayerWitness { +// instances: outputs1, +// }, +// LayerWitness { +// instances: outputs2, +// }, +// ]; + +// ( +// witness_in.clone(), +// CircuitWitness { +// layers, +// witness_in, +// witness_out, +// n_instances: 1, +// challenges: HashMap::new(), +// }, +// ) +// } + +// fn copy_to_wit_out_circuit() -> Circuit { +// let mut circuit_builder = CircuitBuilder::::new(); +// // Layer 2 +// let (_, leaves) = circuit_builder.create_witness_in(4); + +// // Layer 1 +// let (_inner_id, inners) = circuit_builder.create_witness_out(2); +// circuit_builder.mul2(inners[0], leaves[0], leaves[1], Ext::BaseField::ONE); +// circuit_builder.mul2(inners[1], leaves[2], leaves[3], Ext::BaseField::ONE); + +// // Layer 0 +// let root = circuit_builder.create_cell(); +// circuit_builder.mul2(root, inners[0], inners[1], Ext::BaseField::ONE); +// circuit_builder.assert_const(root, 5005); + +// circuit_builder.configure(); +// let circuit = Circuit::new(&circuit_builder); + +// circuit +// } + +// fn copy_to_wit_out_witness() -> ( +// Vec>, +// CircuitWitness, +// ) { +// // witness_in, single instance +// let leaves = vec![vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ]]; +// let witness_in = vec![LayerWitness { instances: leaves }]; + +// let layers = vec![ +// LayerWitness { +// instances: vec![vec![ +// i64_to_field(5005), +// i64_to_field(35), +// i64_to_field(143), +// i64_to_field(0), // pad +// ]], +// }, +// LayerWitness { +// instances: vec![vec![i64_to_field(35), i64_to_field(143)]], +// }, +// LayerWitness { +// instances: vec![vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ]], +// }, +// ]; + +// let outputs = vec![vec![i64_to_field(35), i64_to_field(143)]]; +// let witness_out = vec![LayerWitness { instances: outputs }]; + +// ( +// witness_in.clone(), +// CircuitWitness { +// layers, +// witness_in, +// witness_out, +// n_instances: 1, +// challenges: HashMap::new(), +// }, +// ) +// } + +// fn copy_to_wit_out_witness_2() -> ( +// Vec>, +// CircuitWitness, +// ) { +// // witness_in, 2 instances +// let leaves = vec![ +// vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ], +// vec![ +// i64_to_field(5), +// i64_to_field(13), +// i64_to_field(11), +// i64_to_field(7), +// ], +// ]; +// let witness_in = vec![LayerWitness { instances: leaves }]; + +// let layers = vec![ +// LayerWitness { +// instances: vec![ +// vec![ +// i64_to_field(5005), +// i64_to_field(35), +// i64_to_field(143), +// i64_to_field(0), // pad +// ], +// vec![ +// i64_to_field(5005), +// i64_to_field(65), +// i64_to_field(77), +// i64_to_field(0), // pad +// ], +// ], +// }, +// LayerWitness { +// instances: vec![ +// vec![i64_to_field(35), i64_to_field(143)], +// vec![i64_to_field(65), i64_to_field(77)], +// ], +// }, +// LayerWitness { +// instances: vec![ +// vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ], +// vec![ +// i64_to_field(5), +// i64_to_field(13), +// i64_to_field(11), +// i64_to_field(7), +// ], +// ], +// }, +// ]; + +// let outputs = vec![ +// vec![i64_to_field(35), i64_to_field(143)], +// vec![i64_to_field(65), i64_to_field(77)], +// ]; +// let witness_out = vec![LayerWitness { instances: outputs }]; + +// ( +// witness_in.clone(), +// CircuitWitness { +// layers, +// witness_in, +// witness_out, +// n_instances: 2, +// challenges: HashMap::new(), +// }, +// ) +// } + +// fn rlc_circuit() -> Circuit { +// let mut circuit_builder = CircuitBuilder::::new(); +// // Layer 2 +// let (_, leaves) = circuit_builder.create_witness_in(4); + +// // Layer 1 +// let inners = circuit_builder.create_ext_cells(2); +// circuit_builder.rlc(&inners[0], &[leaves[0], leaves[1]], 0 as ChallengeId); +// circuit_builder.rlc(&inners[1], &[leaves[2], leaves[3]], 1 as ChallengeId); + +// // Layer 0 +// let (_root_id, roots) = circuit_builder.create_ext_witness_out(1); +// circuit_builder.mul2_ext(&roots[0], &inners[0], &inners[1], Ext::BaseField::ONE); + +// circuit_builder.configure(); +// let circuit = Circuit::new(&circuit_builder); + +// circuit +// } + +// fn rlc_witness_2() -> ( +// Vec>, +// CircuitWitness, +// Vec, +// ) +// where +// Ext: ExtensionField, +// { +// let challenges = vec![ +// Ext::from_bases(&[i64_to_field(31), i64_to_field(37)]), +// Ext::from_bases(&[i64_to_field(97), i64_to_field(23)]), +// ]; +// let challenge_pows = challenges +// .iter() +// .enumerate() +// .map(|(i, x)| { +// (0..3) +// .map(|j| { +// ( +// ChallengeConst { +// challenge: i as u8, +// exp: j as u64, +// }, +// x.pow(&[j as u64]), +// ) +// }) +// .collect_vec() +// }) +// .collect_vec(); + +// // witness_in, double instances +// let leaves = vec![ +// vec![ +// i64_to_field(5), +// i64_to_field(7), +// i64_to_field(11), +// i64_to_field(13), +// ], +// vec![ +// i64_to_field(5), +// i64_to_field(13), +// i64_to_field(11), +// i64_to_field(7), +// ], +// ]; +// let witness_in = vec![LayerWitness { +// instances: leaves.clone(), +// }]; + +// let inner00: Ext = challenge_pows[0][0].1 * (&leaves[0][0]) +// + challenge_pows[0][1].1 * (&leaves[0][1]) +// + challenge_pows[0][2].1; +// let inner01: Ext = challenge_pows[1][0].1 * (&leaves[0][2]) +// + challenge_pows[1][1].1 * (&leaves[0][3]) +// + challenge_pows[1][2].1; +// let inner10: Ext = challenge_pows[0][0].1 * (&leaves[1][0]) +// + challenge_pows[0][1].1 * (&leaves[1][1]) +// + challenge_pows[0][2].1; +// let inner11: Ext = challenge_pows[1][0].1 * (&leaves[1][2]) +// + challenge_pows[1][1].1 * (&leaves[1][3]) +// + challenge_pows[1][2].1; + +// let inners = vec![ +// [ +// inner00.clone().as_bases().to_vec(), +// inner01.clone().as_bases().to_vec(), +// ] +// .concat(), +// [ +// inner10.clone().as_bases().to_vec(), +// inner11.clone().as_bases().to_vec(), +// ] +// .concat(), +// ]; + +// let root_tmp0 = vec![ +// inners[0][0] * inners[0][2], +// inners[0][0] * inners[0][3], +// inners[0][1] * inners[0][2], +// inners[0][1] * inners[0][3], +// ]; +// let root_tmp1 = vec![ +// inners[1][0] * inners[1][2], +// inners[1][0] * inners[1][3], +// inners[1][1] * inners[1][2], +// inners[1][1] * inners[1][3], +// ]; +// let root_tmps = vec![root_tmp0, root_tmp1]; + +// let root0 = inner00 * inner01; +// let root1 = inner10 * inner11; +// let roots = vec![root0.as_bases().to_vec(), root1.as_bases().to_vec()]; + +// let layers = vec![ +// LayerWitness { +// instances: roots.clone(), +// }, +// LayerWitness { +// instances: root_tmps, +// }, +// LayerWitness { instances: inners }, +// LayerWitness { instances: leaves }, +// ]; + +// let outputs = roots; +// let witness_out = vec![LayerWitness { instances: outputs }]; + +// ( +// witness_in.clone(), +// CircuitWitness { +// layers, +// witness_in, +// witness_out, +// n_instances: 2, +// challenges: challenge_pows +// .iter() +// .flatten() +// .cloned() +// .map(|(k, v)| (k, v.as_bases().to_vec())) +// .collect::>(), +// }, +// challenges, +// ) +// } + +// #[test] +// fn test_add_instances() { +// let circuit = copy_and_paste_circuit::(); +// let (wits_in, expect_circuit_wits) = copy_and_paste_witness::(); + +// let mut circuit_wits = CircuitWitness::new(&circuit, vec![]); +// circuit_wits.add_instances(&circuit, wits_in, 1); + +// assert_eq!(circuit_wits, expect_circuit_wits); + +// let circuit = paste_from_wit_in_circuit::(); +// let (wits_in, expect_circuit_wits) = paste_from_wit_in_witness::(); + +// let mut circuit_wits = CircuitWitness::new(&circuit, vec![]); +// circuit_wits.add_instances(&circuit, wits_in, 1); + +// assert_eq!(circuit_wits, expect_circuit_wits); + +// let circuit = copy_to_wit_out_circuit::(); +// let (wits_in, expect_circuit_wits) = copy_to_wit_out_witness::(); + +// let mut circuit_wits = CircuitWitness::new(&circuit, vec![]); +// circuit_wits.add_instances(&circuit, wits_in, 1); + +// assert_eq!(circuit_wits, expect_circuit_wits); + +// let (wits_in, expect_circuit_wits) = copy_to_wit_out_witness_2::(); +// let mut circuit_wits = CircuitWitness::new(&circuit, vec![]); +// circuit_wits.add_instances(&circuit, wits_in, 2); + +// assert_eq!(circuit_wits, expect_circuit_wits); +// } + +// #[test] +// fn test_check_correctness() { +// let circuit = copy_to_wit_out_circuit::(); +// let (_wits_in, expect_circuit_wits) = copy_to_wit_out_witness_2::(); + +// expect_circuit_wits.check_correctness(&circuit); +// } + +// #[test] +// fn test_challenges() { +// let circuit = rlc_circuit::(); +// let (wits_in, expect_circuit_wits, challenges) = rlc_witness_2::(); +// let mut circuit_wits = CircuitWitness::new(&circuit, challenges); +// circuit_wits.add_instances(&circuit, wits_in, 2); + +// assert_eq!(circuit_wits, expect_circuit_wits); +// } + +// #[test] +// fn test_orphan_const_input() { +// // create circuit +// let mut circuit_builder = CircuitBuilder::::new(); + +// let (_, leaves) = circuit_builder.create_witness_in(3); +// let mul_0_1_res = circuit_builder.create_cell(); + +// // 2 * 3 = 6 +// circuit_builder.mul2( +// mul_0_1_res, +// leaves[0], +// leaves[1], +// ::BaseField::ONE, +// ); + +// let (_, out) = circuit_builder.create_witness_out(2); +// // like a bypass gate, passing 6 to output out[0] +// circuit_builder.add( +// out[0], +// mul_0_1_res, +// ::BaseField::ONE, +// ); + +// // assert const 2 +// circuit_builder.assert_const(leaves[2], 5); + +// // 5 + -5 = 0, put in out[1] +// circuit_builder.add( +// out[1], +// leaves[2], +// ::BaseField::ONE, +// ); +// circuit_builder.add_const( +// out[1], +// ::BaseField::from(5).neg(), // -5 +// ); + +// // assert out[1] == 0 +// circuit_builder.assert_const(out[1], 0); + +// circuit_builder.configure(); +// let circuit = Circuit::new(&circuit_builder); + +// let mut circuit_wits = CircuitWitness::new(&circuit, vec![]); +// let witness_in = vec![LayerWitness { +// instances: vec![vec![i64_to_field(2), i64_to_field(3), i64_to_field(5)]], +// }]; +// circuit_wits.add_instances(&circuit, witness_in, 1); + +// println!("circuit_wits {:?}", circuit_wits); +// let output_layer_witness = &circuit_wits.layers[0]; +// for gate in circuit.assert_consts.iter() { +// if let ConstantType::Field(constant) = gate.scalar { +// assert_eq!(output_layer_witness.instances[0][gate.idx_out], constant); +// } +// } +// } +// } diff --git a/gkr/src/prover.rs b/gkr/src/prover.rs index 8c8d14c1a..027902dad 100644 --- a/gkr/src/prover.rs +++ b/gkr/src/prover.rs @@ -12,8 +12,8 @@ use transcript::Transcript; use crate::{ entered_span, exit_span, structs::{ - Circuit, CircuitWitness, GKRInputClaims, IOPProof, IOPProverState, - IOPProverStepMessage, PointAndEval, SumcheckStepType, + Circuit, CircuitWitness, GKRInputClaims, IOPProof, IOPProverState, IOPProverStepMessage, + PointAndEval, SumcheckStepType, }, tracing_span, }; @@ -68,11 +68,7 @@ impl IOPProverState { .chain(vec![&dummy_step, &dummy_step]) .tuple_windows() .flat_map(|steps| match steps { - ( - SumcheckStepType::OutputPhase1Step1, - SumcheckStepType::OutputPhase1Step2, - _, - ) => { + (SumcheckStepType::OutputPhase1Step1, _, _) => { let alpha = transcript .get_and_append_challenge(b"combine subset evals") .elements; @@ -129,7 +125,7 @@ impl IOPProverState { sumcheck_prover_state, ); - vec![prover_msg, IOPProverStepMessage::default()] + vec![prover_msg] } (SumcheckStepType::Phase1Step1, _, _) => { let alpha = transcript diff --git a/gkr/src/prover/phase1.rs b/gkr/src/prover/phase1.rs index fcc23b533..5e6645c6b 100644 --- a/gkr/src/prover/phase1.rs +++ b/gkr/src/prover/phase1.rs @@ -14,8 +14,7 @@ use sumcheck::{entered_span, util::ceil_log2}; use crate::{ exit_span, structs::{ - Circuit, CircuitWitness, IOPProverState, IOPProverStepMessage, PointAndEval, - SumcheckProof, + Circuit, CircuitWitness, IOPProverState, IOPProverStepMessage, PointAndEval, SumcheckProof, }, utils::{tensor_product, MatrixMLERowFirst}, }; @@ -27,7 +26,7 @@ impl IOPProverState { /// f1^{(j)}(y) = layers[i](t || y) /// g1^{(j)}(y) = \alpha^j * eq(rt_j, t) * eq(ry_j, y) /// g1^{(j)}(y) = \alpha^j * eq(rt_j, t) * copy_to[j](ry_j, y) - #[tracing::instrument(skip_all, name = "build_phase1_step1_sumcheck_poly_v2")] + #[tracing::instrument(skip_all, name = "build_phase1_step1_sumcheck_poly")] pub(super) fn build_phase1_step1_sumcheck_poly<'a>( &self, layer_id: LayerId, diff --git a/gkr/src/structs.rs b/gkr/src/structs.rs index d7600162a..51ad58e48 100644 --- a/gkr/src/structs.rs +++ b/gkr/src/structs.rs @@ -119,7 +119,6 @@ pub struct GKRInputClaims { #[derive(Clone, Copy, Debug, PartialEq, Serialize)] pub(crate) enum SumcheckStepType { OutputPhase1Step1, - OutputPhase1Step2, Phase1Step1, Phase2Step1, Phase2Step2, diff --git a/gkr/src/test/is_zero_gadget.rs b/gkr/src/test/is_zero_gadget.rs index 77de395f7..7883d74a5 100644 --- a/gkr/src/test/is_zero_gadget.rs +++ b/gkr/src/test/is_zero_gadget.rs @@ -43,13 +43,8 @@ pub fn is_zero_gadget( #[test] fn test_gkr_circuit_is_zero_gadget_simple() { // input and output - let in_value: Vec<::BaseField> = - vec![::BaseField::from(5)]; - let in_inv: Vec<::BaseField> = vec![ - ::BaseField::from(5) - .invert() - .unwrap(), - ]; + let in_value = vec![Goldilocks::from(5)]; + let in_inv = vec![Goldilocks::from(5).invert().unwrap()]; let out_is_zero = Goldilocks::from(0); // build the circuit, only one cell for value, inv and value * inv etc diff --git a/gkr/src/verifier.rs b/gkr/src/verifier.rs index 16377efad..57a50cb62 100644 --- a/gkr/src/verifier.rs +++ b/gkr/src/verifier.rs @@ -57,7 +57,6 @@ impl IOPVerifierState { .verify_and_update_state_output_phase1_step1( circuit, step_proof, transcript, )?, - SumcheckStepType::OutputPhase1Step2 => Ok(())?, SumcheckStepType::Phase1Step1 => verifier_state .verify_and_update_state_phase1_step1(circuit, step_proof, transcript)?, SumcheckStepType::Phase2Step1 => verifier_state diff --git a/multilinear_extensions/src/mle.rs b/multilinear_extensions/src/mle.rs index 032d6f892..89f00f52b 100644 --- a/multilinear_extensions/src/mle.rs +++ b/multilinear_extensions/src/mle.rs @@ -84,7 +84,7 @@ pub trait IntoMLE: Sized { impl IntoMLE> for Vec { fn into_mle(mut self) -> DenseMultilinearExtension { let next_pow2 = ceil_log2(self.len()); - self.resize(next_pow2, E::BaseField::ZERO); + self.resize(1 << next_pow2, E::BaseField::ZERO); DenseMultilinearExtension::from_evaluations_vec(next_pow2, self) } }