From d599a5361ac3d028a35989250d9c924b8b505241 Mon Sep 17 00:00:00 2001 From: Hanting Zhang Date: Wed, 29 Nov 2023 11:23:26 -0800 Subject: [PATCH] add suggestions --- src/lib.rs | 32 +++++++++++++++++--------------- src/r1cs/mod.rs | 4 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8d9542722..e204bfa12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -271,10 +271,11 @@ where } } -/// A resource sink for [`RecursiveSNARK`] +/// A resource buffer for [`RecursiveSNARK`] for storing scratch values that are computed by `prove_step`, +/// which allows the reuse of memory allocations and avoids unnecessary new allocations in the critical section. #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(bound = "")] -pub struct ResourceSink { +pub struct ResourceBuffer { l_w: Option>, l_u: Option>, @@ -304,8 +305,10 @@ where l_w_secondary: R1CSWitness, l_u_secondary: R1CSInstance, - sink_primary: ResourceSink, - sink_secondary: ResourceSink, + /// Buffer for memory needed by the primary fold-step + buffer_primary: ResourceBuffer, + /// Buffer for memory needed by the secondary fold-step + buffer_secondary: ResourceBuffer, i: usize, zi_primary: Vec, @@ -421,7 +424,7 @@ where .collect::::Scalar>, NovaError>>() .expect("Nova error synthesis"); - let sink_primary = ResourceSink { + let buffer_primary = ResourceBuffer { l_w: None, l_u: None, ABC_Z_1: R1CSResult::default(r1cs_primary), @@ -429,7 +432,7 @@ where T: r1cs::default_T(r1cs_primary), }; - let sink_secondary = ResourceSink { + let buffer_secondary = ResourceBuffer { l_w: None, l_u: None, ABC_Z_1: R1CSResult::default(r1cs_secondary), @@ -447,8 +450,8 @@ where l_w_secondary, l_u_secondary, - sink_primary, - sink_secondary, + buffer_primary, + buffer_secondary, i: 0, zi_primary, zi_secondary, @@ -474,7 +477,6 @@ where // save the inputs before proceeding to the `i+1`th step let r_U_primary_i = self.r_U_primary.clone(); let r_U_secondary_i = self.r_U_secondary.clone(); - // let l_u_primary_i = self.l_u_primary.clone(); let l_u_secondary_i = self.l_u_secondary.clone(); // fold the secondary circuit's instance @@ -487,9 +489,9 @@ where &mut self.r_W_secondary, &self.l_u_secondary, &self.l_w_secondary, - &mut self.sink_secondary.T, - &mut self.sink_secondary.ABC_Z_1, - &mut self.sink_secondary.ABC_Z_2, + &mut self.buffer_secondary.T, + &mut self.buffer_secondary.ABC_Z_1, + &mut self.buffer_secondary.ABC_Z_2, ) .expect("Unable to fold secondary"); @@ -533,9 +535,9 @@ where &mut self.r_W_primary, &l_u_primary, &l_w_primary, - &mut self.sink_primary.T, - &mut self.sink_primary.ABC_Z_1, - &mut self.sink_primary.ABC_Z_2, + &mut self.buffer_primary.T, + &mut self.buffer_primary.ABC_Z_1, + &mut self.buffer_primary.ABC_Z_2, ) .expect("Unable to fold primary"); diff --git a/src/r1cs/mod.rs b/src/r1cs/mod.rs index 2b387ae1c..657d73676 100644 --- a/src/r1cs/mod.rs +++ b/src/r1cs/mod.rs @@ -414,8 +414,8 @@ impl R1CSShape { .map(|i| { let AZ_1_circ_BZ_2 = AZ_1[i] * BZ_2[i]; let AZ_2_circ_BZ_1 = AZ_2[i] * BZ_1[i]; - let u_1_cdot_CZ_2 = U1.u * CZ_2[i]; - AZ_1_circ_BZ_2 + AZ_2_circ_BZ_1 - u_1_cdot_CZ_2 - CZ_1[i] + let u_1_cdot_Cz_2_plus_Cz_1 = U1.u * CZ_2[i] + CZ_1[i]; + AZ_1_circ_BZ_2 + AZ_2_circ_BZ_1 - u_1_cdot_Cz_2_plus_Cz_1 }) .collect_into_vec(T) });