From 152b65f887df5542f3a89bcd3e8f82eb34dea069 Mon Sep 17 00:00:00 2001 From: Tianyu Zhang Date: Fri, 26 Jul 2024 15:16:07 -0500 Subject: [PATCH] solve conflicts --- .../multivariate/multilinear/sparse.rs | 2 +- zkp/src/piop/zq_to_rq.rs | 94 ++++++++++--------- zkp/tests/test_zq_to_rq.rs | 91 +++++++++--------- 3 files changed, 101 insertions(+), 86 deletions(-) diff --git a/algebra/src/polynomial/multivariate/multilinear/sparse.rs b/algebra/src/polynomial/multivariate/multilinear/sparse.rs index 4beaad21..fd4c5d96 100644 --- a/algebra/src/polynomial/multivariate/multilinear/sparse.rs +++ b/algebra/src/polynomial/multivariate/multilinear/sparse.rs @@ -54,7 +54,7 @@ impl SparsePolynomial { /// Transform sparse representation into dense representation #[inline] pub fn to_dense(&self) -> DenseMultilinearExtension { - let mut evaluations = vec![F::ZERO; 1 << self.num_vars]; + let mut evaluations = vec![F::zero(); 1 << self.num_vars]; self.evaluations.iter().for_each(|(idx, item)| { evaluations[*idx] = *item; }); diff --git a/zkp/src/piop/zq_to_rq.rs b/zkp/src/piop/zq_to_rq.rs index 6ce838cf..98818545 100644 --- a/zkp/src/piop/zq_to_rq.rs +++ b/zkp/src/piop/zq_to_rq.rs @@ -39,8 +39,8 @@ use crate::utils::eval_identity_function; use crate::sumcheck::MLSumcheck; use crate::utils::gen_identity_evaluations; use algebra::{ - AsFrom, DenseMultilinearExtension, Field, ListOfProductsOfPolynomials, MultilinearExtension, - PolynomialInfo, SparsePolynomial, + AsFrom, DecomposableField, DenseMultilinearExtension, Field, ListOfProductsOfPolynomials, + MultilinearExtension, PolynomialInfo, SparsePolynomial, }; use rand::{RngCore, SeedableRng}; use rand_chacha::ChaCha12Rng; @@ -107,7 +107,7 @@ pub struct TransformZqtoRQInstanceInfo { pub decomposed_bits_info: DecomposedBitsInfo, } -impl TransformZqtoRQInstance { +impl TransformZqtoRQInstance { /// Extract the information of addition in Zq for verification #[inline] pub fn info(&self) -> TransformZqtoRQInstanceInfo { @@ -208,12 +208,12 @@ impl TransformZqtoRQ { let mut product = Vec::with_capacity(3); let mut op_coefficient = Vec::with_capacity(3); product.push(Rc::new(gen_identity_evaluations(u))); - op_coefficient.push((F::ONE, F::ZERO)); + op_coefficient.push((F::one(), F::zero())); product.push(Rc::clone(&transform_instance.k)); - op_coefficient.push((F::ONE, F::ZERO)); + op_coefficient.push((F::one(), F::zero())); product.push(Rc::clone(&transform_instance.k)); - op_coefficient.push((-F::ONE, F::ONE)); - poly.add_product_with_linear_op(product, &op_coefficient, F::ONE); + op_coefficient.push((-F::one(), F::one())); + poly.add_product_with_linear_op(product, &op_coefficient, F::one()); let first_sumcheck_proof = MLSumcheck::prove_as_subprotocol(fs_rng, &poly) .expect("sumcheck for transformation from Zq to RQ failed"); @@ -224,20 +224,20 @@ impl TransformZqtoRQ { let mut product = Vec::with_capacity(3); let mut op_coefficient = Vec::with_capacity(3); product.push(Rc::new(gen_identity_evaluations(u))); - op_coefficient.push((F::ONE, F::ZERO)); + op_coefficient.push((F::one(), F::zero())); product.push(Rc::clone(&transform_instance.r)); - op_coefficient.push((F::ONE, F::ONE)); + op_coefficient.push((F::one(), F::one())); product.push(Rc::clone(&transform_instance.k)); - op_coefficient.push((-(F::ONE + F::ONE), F::ONE)); - poly.add_product_with_linear_op(product, &op_coefficient, F::ONE); + op_coefficient.push((-(F::one() + F::one()), F::one())); + poly.add_product_with_linear_op(product, &op_coefficient, F::one()); let mut product = Vec::with_capacity(2); let mut op_coefficient = Vec::with_capacity(2); product.push(Rc::new(gen_identity_evaluations(u))); - op_coefficient.push((F::ONE, F::ZERO)); + op_coefficient.push((F::one(), F::zero())); product.push(Rc::clone(&transform_instance.s)); - op_coefficient.push((-F::ONE, F::ZERO)); - poly.add_product_with_linear_op(product, &op_coefficient, F::ONE); + op_coefficient.push((-F::one(), F::zero())); + poly.add_product_with_linear_op(product, &op_coefficient, F::one()); let second_sumcheck_proof = MLSumcheck::prove_as_subprotocol(fs_rng, &poly) .expect("sumcheck for transformation from Zq to RQ failed"); @@ -247,7 +247,7 @@ impl TransformZqtoRQ { // construct c_u let eq_u = gen_identity_evaluations(u).evaluations; - let mut c_u_evaluations = vec![F::ZERO; transform_instance.n]; + let mut c_u_evaluations = vec![F::zero(); transform_instance.n]; transform_instance .c .iter() @@ -264,7 +264,7 @@ impl TransformZqtoRQ { // construct t let t_evaluations = (1..=transform_instance.n) - .map(|i| F::new(F::Value::as_from(i as u32))) + .map(|i| F::new(F::Value::as_from(i as f64))) .collect(); let t = Rc::new(DenseMultilinearExtension::from_evaluations_vec( c_num_vars, @@ -275,10 +275,10 @@ impl TransformZqtoRQ { let mut product = Vec::with_capacity(2); let mut op_coefficient = Vec::with_capacity(2); product.push(Rc::clone(&c_u)); - op_coefficient.push((F::ONE, F::ZERO)); + op_coefficient.push((F::one(), F::zero())); product.push(Rc::clone(&t)); - op_coefficient.push((F::ONE, F::ZERO)); - poly.add_product_with_linear_op(product, &op_coefficient, F::ONE); + op_coefficient.push((F::one(), F::zero())); + poly.add_product_with_linear_op(product, &op_coefficient, F::one()); let third_sumcheck_proof = MLSumcheck::prove_as_subprotocol(fs_rng, &poly) .expect("sumcheck for transformation from Zq to RQ failed"); @@ -329,13 +329,21 @@ impl TransformZqtoRQ { num_variables: decomposed_bits_info.num_vars, }; - let first_subclaim = - MLSumcheck::verify_as_subprotocol(fs_rng, &poly_info, F::ZERO, &proof.sumcheck_msgs[0]) - .expect("sumcheck protocol for transformation from Zq to RQ failed"); + let first_subclaim = MLSumcheck::verify_as_subprotocol( + fs_rng, + &poly_info, + F::zero(), + &proof.sumcheck_msgs[0], + ) + .expect("sumcheck protocol for transformation from Zq to RQ failed"); - let second_subclaim = - MLSumcheck::verify_as_subprotocol(fs_rng, &poly_info, F::ZERO, &proof.sumcheck_msgs[1]) - .expect("sumcheck protocol for transformation from Zq to RQ failed"); + let second_subclaim = MLSumcheck::verify_as_subprotocol( + fs_rng, + &poly_info, + F::zero(), + &proof.sumcheck_msgs[1], + ) + .expect("sumcheck protocol for transformation from Zq to RQ failed"); let poly_info = PolynomialInfo { max_multiplicands: 2, @@ -381,7 +389,7 @@ impl TransformZqtoRQSubclaim { k: &DenseMultilinearExtension, r: &[Rc>], s: &DenseMultilinearExtension, - r_bits: &[Vec>>], + r_bits: &[&Vec>>], u: &[F], info: &TransformZqtoRQInstanceInfo, ) -> bool { @@ -398,7 +406,7 @@ impl TransformZqtoRQSubclaim { // check 2: subclaim for sumcheck, i.e. eq(u, point) * k(point) * (1 - k(point)) = 0 let eval_k = k.evaluate(&self.sumcheck_points[0]); - if eval_identity_function(u, &self.sumcheck_points[0]) * eval_k * (F::ONE - eval_k) + if eval_identity_function(u, &self.sumcheck_points[0]) * eval_k * (F::one() - eval_k) != self.sumcheck_expected_evaluations[0] { return false; @@ -406,8 +414,8 @@ impl TransformZqtoRQSubclaim { // check 3: subclaim for sumcheck, i.e. eq(u, point) * ((r(point) + 1) * (1 - 2 * k(point)) - s(point)) = 0 if eval_identity_function(u, &self.sumcheck_points[1]) - * ((r[0].evaluate(&self.sumcheck_points[1]) + F::ONE) - * (F::ONE - (F::ONE + F::ONE) * k.evaluate(&self.sumcheck_points[1])) + * ((r[0].evaluate(&self.sumcheck_points[1]) + F::one()) + * (F::one() - (F::one() + F::one()) * k.evaluate(&self.sumcheck_points[1])) - s.evaluate(&self.sumcheck_points[1])) != self.sumcheck_expected_evaluations[1] { @@ -417,7 +425,7 @@ impl TransformZqtoRQSubclaim { // check 4: subclaim for sumcheck, i.e. c(u, point) * t(point) = s(u) let eval_c_u = c_dense.evaluate(&[&self.sumcheck_points[2], u].concat()); let t_evaluations = (1..=info.n) - .map(|i| F::new(F::Value::as_from(i as u32))) + .map(|i| F::new(F::Value::as_from(i as f64))) .collect(); let t = Rc::new(DenseMultilinearExtension::from_evaluations_vec( info.n.ilog(2) as usize, @@ -429,10 +437,11 @@ impl TransformZqtoRQSubclaim { } // check 5: (2n/q) * a(u) = k(u) * n + r(u) - let n = F::new(F::Value::as_from(info.n as u32)); - let n_divied_by_q = F::new(F::Value::as_from((info.n / q) as u32)); + let n = F::new(F::Value::as_from(info.n as f64)); + let n_divied_by_q = F::new(F::Value::as_from((info.n / q) as f64)); - (F::ONE + F::ONE) * n_divied_by_q * a.evaluate(u) == n * k.evaluate(u) + r[0].evaluate(u) + (F::one() + F::one()) * n_divied_by_q * a.evaluate(u) + == n * k.evaluate(u) + r[0].evaluate(u) } /// verify the sumcliam @@ -450,7 +459,7 @@ impl TransformZqtoRQSubclaim { k: &DenseMultilinearExtension, r: &[Rc>], s: &DenseMultilinearExtension, - r_bits: &[Vec>>], + r_bits: &[&Vec>>], u: &[F], info: &TransformZqtoRQInstanceInfo, ) -> bool { @@ -467,7 +476,7 @@ impl TransformZqtoRQSubclaim { // check 2: subclaim for sumcheck, i.e. eq(u, point) * k(point) * (1 - k(point)) = 0 let eval_k = k.evaluate(&self.sumcheck_points[0]); - if eval_identity_function(u, &self.sumcheck_points[0]) * eval_k * (F::ONE - eval_k) + if eval_identity_function(u, &self.sumcheck_points[0]) * eval_k * (F::one() - eval_k) != self.sumcheck_expected_evaluations[0] { return false; @@ -475,8 +484,8 @@ impl TransformZqtoRQSubclaim { // check 3: subclaim for sumcheck, i.e. eq(u, point) * ((r(point) + 1) * (1 - 2 * k(point)) - s(point)) = 0 if eval_identity_function(u, &self.sumcheck_points[1]) - * ((r[0].evaluate(&self.sumcheck_points[1]) + F::ONE) - * (F::ONE - (F::ONE + F::ONE) * k.evaluate(&self.sumcheck_points[1])) + * ((r[0].evaluate(&self.sumcheck_points[1]) + F::one()) + * (F::one() - (F::one() + F::one()) * k.evaluate(&self.sumcheck_points[1])) - s.evaluate(&self.sumcheck_points[1])) != self.sumcheck_expected_evaluations[1] { @@ -486,7 +495,7 @@ impl TransformZqtoRQSubclaim { // check 4: subclaim for sumcheck, i.e. c(u, point) * t(point) = s(u) let eq_u = gen_identity_evaluations(u); let eq_v = gen_identity_evaluations(&self.sumcheck_points[2]); - let mut eval_c_u = F::ZERO; + let mut eval_c_u = F::zero(); c_sparse.iter().enumerate().for_each(|(x_idx, c)| { assert_eq!(c.evaluations.len(), 1); let (y_idx, c_val) = c.evaluations[0]; @@ -494,7 +503,7 @@ impl TransformZqtoRQSubclaim { }); let t_evaluations = (1..=info.n) - .map(|i| F::new(F::Value::as_from(i as u32))) + .map(|i| F::new(F::Value::as_from(i as f64))) .collect(); let t = Rc::new(DenseMultilinearExtension::from_evaluations_vec( info.n.ilog(2) as usize, @@ -506,9 +515,10 @@ impl TransformZqtoRQSubclaim { } // check 5: (2n/q) * a(u) = k(u) * n + r(u) - let n = F::new(F::Value::as_from(info.n as u32)); - let n_divied_by_q = F::new(F::Value::as_from((info.n / q) as u32)); + let n = F::new(F::Value::as_from(info.n as f64)); + let n_divied_by_q = F::new(F::Value::as_from((info.n / q) as f64)); - (F::ONE + F::ONE) * n_divied_by_q * a.evaluate(u) == n * k.evaluate(u) + r[0].evaluate(u) + (F::one() + F::one()) * n_divied_by_q * a.evaluate(u) + == n * k.evaluate(u) + r[0].evaluate(u) } } diff --git a/zkp/tests/test_zq_to_rq.rs b/zkp/tests/test_zq_to_rq.rs index 2f254b3d..9b139a44 100644 --- a/zkp/tests/test_zq_to_rq.rs +++ b/zkp/tests/test_zq_to_rq.rs @@ -1,18 +1,19 @@ use algebra::{ - derive::{Field, Prime, NTT}, - Basis, DenseMultilinearExtension, Field, FieldUniformSampler, SparsePolynomial, + derive::*, Basis, DecomposableField, DenseMultilinearExtension, Field, FieldUniformSampler, + SparsePolynomial, }; +use num_traits::{One, Zero}; use rand::prelude::*; use rand_distr::Distribution; use std::rc::Rc; use std::vec; use zkp::piop::zq_to_rq::{TransformZqtoRQ, TransformZqtoRQInstance}; -#[derive(Field, Prime, NTT)] +#[derive(Field, Prime, DecomposableField)] #[modulus = 132120577] pub struct Fp32(u32); -#[derive(Field)] +#[derive(Field, DecomposableField)] #[modulus = 512] pub struct Fq(u32); @@ -80,19 +81,19 @@ fn test_trivial_zq_to_rq() { let c = vec![ Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(0, FF::ONE)], + vec![(0, FF::one())], )), Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(6, FF::ONE)], + vec![(6, FF::one())], )), Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(2, -FF::ONE)], + vec![(2, -FF::one())], )), Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(6, -FF::ONE)], + vec![(6, -FF::one())], )), ]; @@ -105,7 +106,8 @@ fn test_trivial_zq_to_rq() { 0, 0, 0, 0, 0, 0, p-1, 0), )); - let r_bits = vec![r.get_decomposed_mles(base_len, bits_len)]; + let tmp = r.get_decomposed_mles(base_len, bits_len); + let r_bits = vec![&tmp]; let instance = TransformZqtoRQInstance::from_vec( q, c.clone(), @@ -142,7 +144,7 @@ fn test_random_zq_to_rq() { let uniform_fp = >::new(); let num_vars = 10; let q = FF::new(Fq::MODULUS_VALUE); - let c_num_vars = q.get().ilog2(); + let c_num_vars = q.value() as usize; let base_len: u32 = 3; let base: FF = FF::new(1 << base_len); let bits_len: u32 = >::new(base_len).decompose_len() as u32; @@ -159,30 +161,30 @@ fn test_random_zq_to_rq() { let mut c_dense_matrix = Vec::new(); a_over_fq.iter().for_each(|x| { - let mut x = FF::new(x.get()); + let mut x = FF::new(x.value() as u32); a.push(x); x = FF::new(2) * x; if x >= q { - k.push(FF::ONE); + k.push(FF::one()); r.push(x - q); - s.push(-(x - q + FF::ONE)); + s.push(-(x - q + FF::one())); c.push(Rc::new(SparsePolynomial::from_evaluations_vec( - c_num_vars as usize, - vec![((x - q).get() as usize, -FF::ONE)], + c_num_vars, + vec![((x - q).value() as usize, -FF::one())], ))); - let mut c_dense_row = vec![FF::ZERO; q.get() as usize]; - c_dense_row[(x - q).get() as usize] = -FF::ONE; + let mut c_dense_row = vec![FF::zero(); q.value() as usize]; + c_dense_row[(x - q).value() as usize] = -FF::one(); c_dense_matrix.extend(c_dense_row); } else { - k.push(FF::ZERO); + k.push(FF::zero()); r.push(x); - s.push(x + FF::ONE); + s.push(x + FF::one()); c.push(Rc::new(SparsePolynomial::from_evaluations_vec( - c_num_vars as usize, - vec![(x.get() as usize, FF::ONE)], + c_num_vars, + vec![(x.value() as usize, FF::one())], ))); - let mut c_dense_row = vec![FF::ZERO; q.get() as usize]; - c_dense_row[x.get() as usize] = FF::ONE; + let mut c_dense_row = vec![FF::zero(); q.value() as usize]; + c_dense_row[x.value() as usize] = FF::one(); c_dense_matrix.extend(c_dense_row); } }); @@ -196,13 +198,14 @@ fn test_random_zq_to_rq() { let s: Rc> = Rc::new(DenseMultilinearExtension::from_evaluations_vec(num_vars, s)); let c_dense = Rc::new(DenseMultilinearExtension::from_evaluations_vec( - num_vars + c_num_vars as usize, + num_vars + c_num_vars, c_dense_matrix, )); - let r_bits: Vec<_> = vec![r.get_decomposed_mles(base_len, bits_len)]; + let tmp = r.get_decomposed_mles(base_len, bits_len); + let r_bits: Vec<_> = vec![&tmp]; let instance = TransformZqtoRQInstance::::from_vec( - q.get() as usize, + q.value() as usize, c.clone(), a.clone(), &k, @@ -218,7 +221,7 @@ fn test_random_zq_to_rq() { let subclaim = TransformZqtoRQ::verify(&proof, &info.decomposed_bits_info, c_num_vars as usize); assert!(subclaim.verify_subclaim( - q.get() as usize, + q.value() as usize, a, &c_dense, k.as_ref(), @@ -265,23 +268,24 @@ fn test_trivial_zq_to_rq_without_oracle() { let c_sparse = vec![ Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(0, FF::ONE)], + vec![(0, FF::one())], )), Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(6, FF::ONE)], + vec![(6, FF::one())], )), Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(2, -FF::ONE)], + vec![(2, -FF::one())], )), Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars, - vec![(6, -FF::ONE)], + vec![(6, -FF::one())], )), ]; - let r_bits = vec![r.get_decomposed_mles(base_len, bits_len)]; + let tmp = r.get_decomposed_mles(base_len, bits_len); + let r_bits = vec![&tmp]; let instance = TransformZqtoRQInstance::from_vec( q, c_sparse.clone(), @@ -318,7 +322,7 @@ fn test_random_zq_to_rq_without_oracle() { let uniform_fp = >::new(); let num_vars = 10; let q = FF::new(Fq::MODULUS_VALUE); - let c_num_vars = q.get().ilog2(); + let c_num_vars = q.value() as usize; let base_len: u32 = 3; let base: FF = FF::new(1 << base_len); let bits_len: u32 = >::new(base_len).decompose_len() as u32; @@ -334,24 +338,24 @@ fn test_random_zq_to_rq_without_oracle() { let mut c_sparse = Vec::new(); a_over_fq.iter().for_each(|x| { - let mut x = FF::new(x.get()); + let mut x = FF::new(x.value() as u32); a.push(x); x = FF::new(2) * x; if x >= q { - k.push(FF::ONE); + k.push(FF::one()); r.push(x - q); - s.push(-(x - q + FF::ONE)); + s.push(-(x - q + FF::one())); c_sparse.push(Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars as usize, - vec![((x - q).get() as usize, -FF::ONE)], + vec![((x - q).value() as usize, -FF::one())], ))); } else { - k.push(FF::ZERO); + k.push(FF::zero()); r.push(x); - s.push(x + FF::ONE); + s.push(x + FF::one()); c_sparse.push(Rc::new(SparsePolynomial::from_evaluations_vec( c_num_vars as usize, - vec![(x.get() as usize, FF::ONE)], + vec![(x.value() as usize, FF::one())], ))); } }); @@ -365,9 +369,10 @@ fn test_random_zq_to_rq_without_oracle() { let s: Rc> = Rc::new(DenseMultilinearExtension::from_evaluations_vec(num_vars, s)); - let r_bits: Vec<_> = vec![r.get_decomposed_mles(base_len, bits_len)]; + let tmp = r.get_decomposed_mles(base_len, bits_len); + let r_bits: Vec<_> = vec![&tmp]; let instance = TransformZqtoRQInstance::::from_vec( - q.get() as usize, + q.value() as usize, c_sparse.clone(), a.clone(), &k, @@ -383,7 +388,7 @@ fn test_random_zq_to_rq_without_oracle() { let subclaim = TransformZqtoRQ::verify(&proof, &info.decomposed_bits_info, c_num_vars as usize); assert!(subclaim.verify_subclaim_without_oracle( - q.get() as usize, + q.value() as usize, a, &c_sparse, k.as_ref(),