From d86ee32cf0e85ffeea42fd3f336c4285a66a6e42 Mon Sep 17 00:00:00 2001 From: ibmp33 <2285673866@qq.com> Date: Mon, 4 Dec 2023 23:17:27 +0800 Subject: [PATCH 1/7] chore: avx2 acceleration --- starky/src/arch/x86_64/avx2_poseidon_gl.rs | 148 ++++++++++++++------- 1 file changed, 97 insertions(+), 51 deletions(-) diff --git a/starky/src/arch/x86_64/avx2_poseidon_gl.rs b/starky/src/arch/x86_64/avx2_poseidon_gl.rs index 81a8c525..19319ab6 100644 --- a/starky/src/arch/x86_64/avx2_poseidon_gl.rs +++ b/starky/src/arch/x86_64/avx2_poseidon_gl.rs @@ -59,16 +59,16 @@ impl Default for Poseidon { } } -#[inline] +#[inline(always)] unsafe fn spmv_avx_4x12( r: &mut Avx2GoldilocksField, st0: Avx2GoldilocksField, st1: Avx2GoldilocksField, st2: Avx2GoldilocksField, - m: Vec, + m: &[FrRepr], ) { - let m = Avx2GoldilocksField::pack_slice(&m); - *r = (st0 * m[0]) + (st1 * m[1]) + (st2 * m[2]) + let m = Avx2GoldilocksField::pack_slice(m); + *r = (st0 * m[0]) + (st1 * m[1]) + (st2 * m[2]); } impl Poseidon { @@ -118,9 +118,9 @@ impl Poseidon { st0: &mut Avx2GoldilocksField, st1: &mut Avx2GoldilocksField, st2: &mut Avx2GoldilocksField, - c: Vec, + c: &[FrRepr], ) { - let c = Avx2GoldilocksField::pack_slice(&c); + let c = Avx2GoldilocksField::pack_slice(c); *st0 = *st0 + c[0]; *st1 = *st1 + c[1]; *st2 = *st2 + c[2]; @@ -132,9 +132,9 @@ impl Poseidon { st1: &mut Avx2GoldilocksField, st2: &mut Avx2GoldilocksField, s0: Avx2GoldilocksField, - s: Vec, + s: &[FrRepr], ) { - let s = Avx2GoldilocksField::pack_slice(&s); + let s = Avx2GoldilocksField::pack_slice(s); *st0 = *st0 + s[0] * s0; *st1 = *st1 + s[1] * s0; *st2 = *st2 + s[2] * s0; @@ -145,36 +145,36 @@ impl Poseidon { st0: &mut Avx2GoldilocksField, st1: &mut Avx2GoldilocksField, st2: &mut Avx2GoldilocksField, - p: Vec, + p: &[FrRepr], ) { let mut tmp0 = Avx2GoldilocksField::ZEROS; let mut tmp1 = Avx2GoldilocksField::ZEROS; let mut tmp2 = Avx2GoldilocksField::ZEROS; - Self::mmult_avx_4x12(&mut tmp0, *st0, *st1, *st2, p[0..48].to_vec()); - Self::mmult_avx_4x12(&mut tmp1, *st0, *st1, *st2, p[48..96].to_vec()); - Self::mmult_avx_4x12(&mut tmp2, *st0, *st1, *st2, p[96..144].to_vec()); + Self::mmult_avx_4x12(&mut tmp0, *st0, *st1, *st2, &p[0..48]); + Self::mmult_avx_4x12(&mut tmp1, *st0, *st1, *st2, &p[48..96]); + Self::mmult_avx_4x12(&mut tmp2, *st0, *st1, *st2, &p[96..144]); *st0 = tmp0; *st1 = tmp1; *st2 = tmp2; } // Dense matrix-vector product - #[inline] + #[inline(always)] unsafe fn mmult_avx_4x12( tmp: &mut Avx2GoldilocksField, st0: Avx2GoldilocksField, st1: Avx2GoldilocksField, st2: Avx2GoldilocksField, - m: Vec, + m: &[FrRepr], ) { let mut r0 = Avx2GoldilocksField::ZEROS; let mut r1 = Avx2GoldilocksField::ZEROS; let mut r2 = Avx2GoldilocksField::ZEROS; let mut r3 = Avx2GoldilocksField::ZEROS; - spmv_avx_4x12(&mut r0, st0, st1, st2, m[0..12].to_vec()); - spmv_avx_4x12(&mut r1, st0, st1, st2, m[12..24].to_vec()); - spmv_avx_4x12(&mut r2, st0, st1, st2, m[24..36].to_vec()); - spmv_avx_4x12(&mut r3, st0, st1, st2, m[36..48].to_vec()); + spmv_avx_4x12(&mut r0, st0, st1, st2, &m[0..12]); + spmv_avx_4x12(&mut r1, st0, st1, st2, &m[12..24]); + spmv_avx_4x12(&mut r2, st0, st1, st2, &m[24..36]); + spmv_avx_4x12(&mut r3, st0, st1, st2, &m[36..48]); // Transpose: transform de 4x4 matrix stored in rows r0...r3 to the columns c0...c3 let t0 = _mm256_permute2f128_si256(r0.get(), r2.get(), 0b00100000); let t1 = _mm256_permute2f128_si256(r1.get(), r3.get(), 0b00100000); @@ -205,36 +205,36 @@ impl Poseidon { st0: &mut Avx2GoldilocksField, st1: &mut Avx2GoldilocksField, st2: &mut Avx2GoldilocksField, - m: Vec, + m: &[FrRepr], ) { let mut tmp0 = Avx2GoldilocksField::ZEROS; let mut tmp1 = Avx2GoldilocksField::ZEROS; let mut tmp2 = Avx2GoldilocksField::ZEROS; - Self::mmult_avx_4x12_8(&mut tmp0, *st0, *st1, *st2, m[0..48].to_vec()); - Self::mmult_avx_4x12_8(&mut tmp1, *st0, *st1, *st2, m[48..96].to_vec()); - Self::mmult_avx_4x12_8(&mut tmp2, *st0, *st1, *st2, m[96..144].to_vec()); + Self::mmult_avx_4x12_8(&mut tmp0, *st0, *st1, *st2, &m[0..48]); + Self::mmult_avx_4x12_8(&mut tmp1, *st0, *st1, *st2, &m[48..96]); + Self::mmult_avx_4x12_8(&mut tmp2, *st0, *st1, *st2, &m[96..144]); *st0 = tmp0; *st1 = tmp1; *st2 = tmp2; } // Dense matrix-vector product - #[inline] + #[inline(always)] unsafe fn mmult_avx_4x12_8( tmp: &mut Avx2GoldilocksField, st0: Avx2GoldilocksField, st1: Avx2GoldilocksField, st2: Avx2GoldilocksField, - m: Vec, + m: &[FrRepr], ) { let mut r0 = Avx2GoldilocksField::ZEROS; let mut r1 = Avx2GoldilocksField::ZEROS; let mut r2 = Avx2GoldilocksField::ZEROS; let mut r3 = Avx2GoldilocksField::ZEROS; - Self::spmv_avx_4x12_8(&mut r0, st0, st1, st2, m[0..12].to_vec()); - Self::spmv_avx_4x12_8(&mut r1, st0, st1, st2, m[12..24].to_vec()); - Self::spmv_avx_4x12_8(&mut r2, st0, st1, st2, m[24..36].to_vec()); - Self::spmv_avx_4x12_8(&mut r3, st0, st1, st2, m[36..48].to_vec()); + Self::spmv_avx_4x12_8(&mut r0, st0, st1, st2, &m[0..12]); + Self::spmv_avx_4x12_8(&mut r1, st0, st1, st2, &m[12..24]); + Self::spmv_avx_4x12_8(&mut r2, st0, st1, st2, &m[24..36]); + Self::spmv_avx_4x12_8(&mut r3, st0, st1, st2, &m[36..48]); // Transpose: transform de 4x4 matrix stored in rows r0...r3 to the columns c0...c3 let t0 = _mm256_permute2f128_si256(r0.get(), r2.get(), 0b00100000); let t1 = _mm256_permute2f128_si256(r1.get(), r3.get(), 0b00100000); @@ -260,13 +260,13 @@ impl Poseidon { *tmp = c0 + c1 + c2 + c3; } - #[inline] + #[inline(always)] unsafe fn spmv_avx_4x12_8( r: &mut Avx2GoldilocksField, st0: Avx2GoldilocksField, st1: Avx2GoldilocksField, st2: Avx2GoldilocksField, - m: Vec, + m: &[FrRepr], ) { let m = Avx2GoldilocksField::pack_slice(&m); let mut c0_h = Avx2GoldilocksField::ZEROS; @@ -283,7 +283,7 @@ impl Poseidon { *r = Avx2GoldilocksField::reduce(c_h.get(), c_l.get()) } - #[inline] + #[inline(always)] unsafe fn mult_avx_72( c_h: &mut Avx2GoldilocksField, c_l: &mut Avx2GoldilocksField, @@ -354,20 +354,20 @@ impl Poseidon { let mut st1 = st[1]; let mut st2 = st[2]; - Self::add_avx(&mut st0, &mut st1, &mut st2, (&C[0..12]).to_vec()); + Self::add_avx(&mut st0, &mut st1, &mut st2, &C[0..12]); for r in 0..(n_rounds_f / 2 - 1) { Self::pow7_triple(&mut st0, &mut st1, &mut st2); Self::add_avx( &mut st0, &mut st1, &mut st2, - (&C[(r + 1) * 12..((r + 1) * 12 + 12)]).to_vec(), + &C[(r + 1) * 12..((r + 1) * 12 + 12)], ); - Self::mmult_avx_8(&mut st0, &mut st1, &mut st2, (&M[0..144]).to_vec()); + Self::mmult_avx_8(&mut st0, &mut st1, &mut st2, &M[0..144]); } Self::pow7_triple(&mut st0, &mut st1, &mut st2); - Self::add_avx(&mut st0, &mut st1, &mut st2, (&C[48..60]).to_vec()); - Self::mmult_avx(&mut st0, &mut st1, &mut st2, (&P[0..144]).to_vec()); + Self::add_avx(&mut st0, &mut st1, &mut st2, &C[48..60]); + Self::mmult_avx(&mut st0, &mut st1, &mut st2, &P[0..144]); for r in 0..n_rounds_p { let st0_slice = st0.as_slice_mut(); @@ -382,13 +382,7 @@ impl Poseidon { st0_slice[0] = _st0.as_slice_mut()[0]; let mut tmp = Avx2GoldilocksField::ZEROS; - spmv_avx_4x12( - &mut tmp, - st0, - st1, - st2, - S[12 * 2 * r..(12 * 2 * r + 12)].to_vec(), - ); + spmv_avx_4x12(&mut tmp, st0, st1, st2, &S[12 * 2 * r..(12 * 2 * r + 12)]); let tmp_slice = tmp.as_slice_mut(); let sum = FGL::from_repr(tmp_slice[0]).unwrap() + FGL::from_repr(tmp_slice[1]).unwrap() @@ -409,7 +403,7 @@ impl Poseidon { &mut st1, &mut st2, *s0, - (&S[(12 * (2 * r + 1))..(12 * (2 * r + 2))]).to_vec(), + &S[(12 * (2 * r + 1))..(12 * (2 * r + 2))], ); let st0_slice = st0.as_slice_mut(); @@ -422,14 +416,13 @@ impl Poseidon { &mut st0, &mut st1, &mut st2, - (&C[((n_rounds_f / 2 + 1) * t + n_rounds_p + r * t) - ..((n_rounds_f / 2 + 1) * t + n_rounds_p + r * t + 12)]) - .to_vec(), + &C[((n_rounds_f / 2 + 1) * t + n_rounds_p + r * t) + ..((n_rounds_f / 2 + 1) * t + n_rounds_p + r * t + 12)], ); - Self::mmult_avx_8(&mut st0, &mut st1, &mut st2, (&M[0..144]).to_vec()); + Self::mmult_avx_8(&mut st0, &mut st1, &mut st2, &M[0..144]); } Self::pow7_triple(&mut st0, &mut st1, &mut st2); - Self::mmult_avx(&mut st0, &mut st1, &mut st2, (&M[0..144]).to_vec()); + Self::mmult_avx(&mut st0, &mut st1, &mut st2, &M[0..144]); let st0_slice = st0.as_slice(); @@ -447,13 +440,19 @@ mod tests { use algebraic::packed::PackedField; use plonky::field_gl::Fr as FGL; use plonky::PrimeField; + use std::time::{Duration, Instant}; #[test] fn test_poseidon_opt_hash_all_0_avx() { let poseidon = Poseidon::new(); let input = vec![FGL::ZERO; 8]; let state = vec![FGL::ZERO; 4]; + + let start = Instant::now(); let res = poseidon.hash(&input, &state, 4).unwrap(); + let hash_avx2_duration = start.elapsed(); + println!("hash_avx2_duration_0: {:?}", hash_avx2_duration); + let expected = vec![ FGL::from(0x3c18a9786cb0b359u64), FGL::from(0xc4055e3364a246c3u64), @@ -468,7 +467,11 @@ mod tests { let poseidon = Poseidon::new(); let input = (0u64..8).map(FGL::from).collect::>(); let state = (8u64..12).map(FGL::from).collect::>(); + let start = Instant::now(); let res = poseidon.hash(&input, &state, 4).unwrap(); + let hash_avx2_duration = start.elapsed(); + println!("hash_avx2_duration_1: {:?}", hash_avx2_duration); + let expected = vec![ FGL::from(0xd64e1e3efc5b8e9eu64), FGL::from(0x53666633020aaa47u64), @@ -484,7 +487,11 @@ mod tests { let init = FGL::ZERO - FGL::ONE; let input = vec![init; 8]; let state = vec![init; 4]; + let start = Instant::now(); let res = poseidon.hash(&input, &state, 4).unwrap(); + let hash_avx2_duration = start.elapsed(); + println!("hash_avx2_duration_2: {:?}", hash_avx2_duration); + let expected = vec![ FGL::from(0xbe0085cfc57a8357u64), FGL::from(0xd95af71847d05c09u64), @@ -494,6 +501,45 @@ mod tests { assert_eq!(res, expected); } + #[test] + fn test_poseidon_opt_hash_1_11_avx_average() { + let poseidon = Poseidon::new(); + let input = (0u64..8).map(FGL::from).collect::>(); + let state = (8u64..12).map(FGL::from).collect::>(); + + let mut total_duration = Duration::new(0, 0); + let iterations = 100; + + for _ in 0..iterations { + let start = Instant::now(); + let _res = poseidon.hash(&input, &state, 4).unwrap(); + total_duration += start.elapsed(); + } + + let average_duration = total_duration / iterations; + println!("Average hash_avx2_duration_1: {:?}", average_duration); + } + + #[test] + fn test_poseidon_opt_hash_all_neg_1_avx_average() { + let poseidon = Poseidon::new(); + let init = FGL::ZERO - FGL::ONE; + let input = vec![init; 8]; + let state = vec![init; 4]; + + let mut total_duration = Duration::new(0, 0); + let iterations = 100; + + for _ in 0..iterations { + let start = Instant::now(); + let _res = poseidon.hash(&input, &state, 4).unwrap(); + total_duration += start.elapsed(); + } + + let average_duration = total_duration / iterations; + println!("Average hash_avx2_duration_2: {:?}", average_duration); + } + #[test] fn test_spmv_avx_4x12() { let mut out = Avx2GoldilocksField::ZEROS; @@ -516,9 +562,9 @@ mod tests { FrRepr([18446744069414584320]), ]); - let in12 = vec![FrRepr([18446744069414584320]); 12]; + let in12 = [FrRepr([18446744069414584320]); 12]; unsafe { - spmv_avx_4x12(&mut out, *in0, *in1, *in2, in12); + spmv_avx_4x12(&mut out, *in0, *in1, *in2, &in12); }; let tmp_slice = out.as_slice_mut(); let _sum = FGL::from_repr(tmp_slice[0]).unwrap() From 667c0ea4734e7af5c670834de7daf46ac0d2af7b Mon Sep 17 00:00:00 2001 From: ibmp33 <2285673866@qq.com> Date: Mon, 4 Dec 2023 23:58:36 +0800 Subject: [PATCH 2/7] chore: avx2 acceleration --- starky/src/arch/x86_64/avx2_poseidon_gl.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/starky/src/arch/x86_64/avx2_poseidon_gl.rs b/starky/src/arch/x86_64/avx2_poseidon_gl.rs index 19319ab6..184e1879 100644 --- a/starky/src/arch/x86_64/avx2_poseidon_gl.rs +++ b/starky/src/arch/x86_64/avx2_poseidon_gl.rs @@ -451,7 +451,7 @@ mod tests { let start = Instant::now(); let res = poseidon.hash(&input, &state, 4).unwrap(); let hash_avx2_duration = start.elapsed(); - println!("hash_avx2_duration_0: {:?}", hash_avx2_duration); + log::debug!("hash_avx2_duration_0: {:?}", hash_avx2_duration); let expected = vec![ FGL::from(0x3c18a9786cb0b359u64), @@ -470,7 +470,7 @@ mod tests { let start = Instant::now(); let res = poseidon.hash(&input, &state, 4).unwrap(); let hash_avx2_duration = start.elapsed(); - println!("hash_avx2_duration_1: {:?}", hash_avx2_duration); + log::debug!("hash_avx2_duration_1: {:?}", hash_avx2_duration); let expected = vec![ FGL::from(0xd64e1e3efc5b8e9eu64), @@ -490,7 +490,7 @@ mod tests { let start = Instant::now(); let res = poseidon.hash(&input, &state, 4).unwrap(); let hash_avx2_duration = start.elapsed(); - println!("hash_avx2_duration_2: {:?}", hash_avx2_duration); + log::debug!("hash_avx2_duration_2: {:?}", hash_avx2_duration); let expected = vec![ FGL::from(0xbe0085cfc57a8357u64), @@ -517,7 +517,7 @@ mod tests { } let average_duration = total_duration / iterations; - println!("Average hash_avx2_duration_1: {:?}", average_duration); + log::debug!("Average hash_avx2_duration_1: {:?}", average_duration); } #[test] @@ -537,7 +537,7 @@ mod tests { } let average_duration = total_duration / iterations; - println!("Average hash_avx2_duration_2: {:?}", average_duration); + log::debug!("Average hash_avx2_duration_2: {:?}", average_duration); } #[test] From f8e131c7757cebcef92fa51798127eb8f4028844 Mon Sep 17 00:00:00 2001 From: eigmax Date: Tue, 5 Dec 2023 00:39:54 +0800 Subject: [PATCH 3/7] chore: fix clippy --- starky/src/compressor12/compressor12_setup.rs | 4 ++-- starky/src/compressor12/plonk_setup.rs | 2 +- starky/src/fft.rs | 4 ++-- starky/src/fri.rs | 4 ++-- starky/src/interpreter.rs | 2 +- starky/src/polutils.rs | 2 +- starky/src/poseidon_bls12381.rs | 6 +++--- starky/src/poseidon_bn128.rs | 10 +++++----- starky/src/poseidon_opt.rs | 7 +------ 9 files changed, 18 insertions(+), 23 deletions(-) diff --git a/starky/src/compressor12/compressor12_setup.rs b/starky/src/compressor12/compressor12_setup.rs index 66159b08..e9ce1499 100644 --- a/starky/src/compressor12/compressor12_setup.rs +++ b/starky/src/compressor12/compressor12_setup.rs @@ -49,8 +49,8 @@ pub fn setup( // construct and save ExecFile: plonk additions + sMap -> BigUint64Array pub(super) fn write_exec_file( exec_file: &str, - adds: &Vec, - s_map: &Vec>, + adds: &[PlonkAdd], + s_map: &[Vec], ) -> Result<()> { let adds_len = adds.len(); let s_map_row_len = s_map.len(); diff --git a/starky/src/compressor12/plonk_setup.rs b/starky/src/compressor12/plonk_setup.rs index ffec19cd..b83ddc6e 100644 --- a/starky/src/compressor12/plonk_setup.rs +++ b/starky/src/compressor12/plonk_setup.rs @@ -56,7 +56,7 @@ pub(crate) struct NormalPlonkInfo { } impl NormalPlonkInfo { - pub(crate) fn new(plonk_constrains: &Vec) -> Self { + pub(crate) fn new(plonk_constrains: &[PlonkGate]) -> Self { let mut uses: BTreeMap = BTreeMap::new(); let plonk_constrains_len = plonk_constrains.len(); for (i, c) in plonk_constrains.iter().enumerate() { diff --git a/starky/src/fft.rs b/starky/src/fft.rs index f0659d0c..e81018e4 100644 --- a/starky/src/fft.rs +++ b/starky/src/fft.rs @@ -36,9 +36,9 @@ impl FFT { } } - pub fn fft(&mut self, p: &Vec) -> Vec { + pub fn fft(&mut self, p: &[F]) -> Vec { if p.len() <= 1 { - return p.clone(); + return p.to_owned(); } let bits = log2_any(p.len() - 1) + 1; self.set_roots(bits); diff --git a/starky/src/fri.rs b/starky/src/fri.rs index 1b6e7c2a..1bd554b1 100644 --- a/starky/src/fri.rs +++ b/starky/src/fri.rs @@ -266,7 +266,7 @@ impl FRI { } } -fn get_transposed_buffer(pol: &Vec, transpose_bits: usize) -> Vec { +fn get_transposed_buffer(pol: &[F], transpose_bits: usize) -> Vec { let n = pol.len(); let w = 1 << transpose_bits; let h = n / w; @@ -292,7 +292,7 @@ fn get3(arr: &[FGL], idx: usize) -> F { } // TODO: Support F5G -fn split3(arr: &Vec) -> Vec { +fn split3(arr: &[FGL]) -> Vec { let mut res: Vec = Vec::new(); for i in (0..arr.len()).step_by(3) { res.push(F::from_vec(vec![arr[i], arr[i + 1], arr[i + 2]])); diff --git a/starky/src/interpreter.rs b/starky/src/interpreter.rs index 3a8bdc15..738e42ba 100644 --- a/starky/src/interpreter.rs +++ b/starky/src/interpreter.rs @@ -198,7 +198,7 @@ impl fmt::Display for Block { pub fn compile_code( ctx: &StarkContext, starkinfo: &StarkInfo, - code: &Vec
, + code: &[Section], dom: &str, ret: bool, ) -> Block { diff --git a/starky/src/polutils.rs b/starky/src/polutils.rs index 0ef53a73..bec9745b 100644 --- a/starky/src/polutils.rs +++ b/starky/src/polutils.rs @@ -10,7 +10,7 @@ pub fn pol_mul_axi(p: &mut Vec, init: F, acc: &F) { } } -pub fn eval_pol(p: &Vec, x: &F) -> F { +pub fn eval_pol(p: &[F], x: &F) -> F { if p.is_empty() { return F::ZERO; } diff --git a/starky/src/poseidon_bls12381.rs b/starky/src/poseidon_bls12381.rs index b3065416..b5ff33de 100644 --- a/starky/src/poseidon_bls12381.rs +++ b/starky/src/poseidon_bls12381.rs @@ -94,7 +94,7 @@ impl Poseidon { } } - pub fn mix(&self, state: &Vec, m: &[Vec]) -> Vec { + pub fn mix(&self, state: &[Fr], m: &[Vec]) -> Vec { let mut new_state: Vec = Vec::new(); for i in 0..state.len() { new_state.push(Fr::zero()); @@ -114,11 +114,11 @@ impl Poseidon { Ok(result[0]) } - pub fn hash_ex(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + pub fn hash_ex(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { self.hash_inner(inp, init_state, out) } - fn hash_inner(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + fn hash_inner(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { if inp.is_empty() || inp.len() > POSEIDON_BLS12381_CONSTANTS.n_rounds_p.len() { return Err(format!( "Wrong inputs length {} > {}", diff --git a/starky/src/poseidon_bn128.rs b/starky/src/poseidon_bn128.rs index 3d110b3a..11f1ae9c 100644 --- a/starky/src/poseidon_bn128.rs +++ b/starky/src/poseidon_bn128.rs @@ -70,7 +70,7 @@ impl Poseidon { pub fn new() -> Poseidon { Self {} } - pub fn ark(&self, state: &mut Vec, c: &[Fr], it: usize) { + pub fn ark(&self, state: &mut [Fr], c: &[Fr], it: usize) { for i in 0..state.len() { state[i].add_assign(&c[it + i]); } @@ -94,7 +94,7 @@ impl Poseidon { } } - pub fn mix(&self, state: &Vec, m: &[Vec]) -> Vec { + pub fn mix(&self, state: &[Fr], m: &[Vec]) -> Vec { let mut new_state: Vec = Vec::new(); for i in 0..state.len() { new_state.push(Fr::zero()); @@ -109,16 +109,16 @@ impl Poseidon { /// Hash function /// init_state would be Fr::zero() initially - pub fn hash(&self, inp: &Vec, init_state: &Fr) -> Result { + pub fn hash(&self, inp: &[Fr], init_state: &Fr) -> Result { let result = self.hash_inner(inp, init_state, 1)?; Ok(result[0]) } - pub fn hash_ex(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + pub fn hash_ex(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { self.hash_inner(inp, init_state, out) } - fn hash_inner(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + fn hash_inner(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { if inp.is_empty() || inp.len() > POSEIDON_BN128_CONSTANTS.n_rounds_p.len() { return Err(format!( "Wrong inputs length {} > {}", diff --git a/starky/src/poseidon_opt.rs b/starky/src/poseidon_opt.rs index 4d336725..1ec2fdfa 100644 --- a/starky/src/poseidon_opt.rs +++ b/starky/src/poseidon_opt.rs @@ -79,12 +79,7 @@ impl Poseidon { self.hash_inner(inp, init_state, out) } - fn hash_inner( - &self, - inp: &Vec, - init_state: &[FGL], - out: usize, - ) -> Result, String> { + fn hash_inner(&self, inp: &[FGL], init_state: &[FGL], out: usize) -> Result, String> { if inp.len() != 8 { return Err(format!("Wrong inputs length {} != 8", inp.len(),)); } From e859f1589f94ed691c994e3e0b57e8c143995c34 Mon Sep 17 00:00:00 2001 From: eigmax Date: Tue, 5 Dec 2023 00:48:23 +0800 Subject: [PATCH 4/7] chore: fix clippy --- starky/src/fft.rs | 2 +- starky/src/helper.rs | 2 +- starky/src/poseidon_bls12381.rs | 6 +++--- starky/src/poseidon_bn128.rs | 14 +++++++------- starky/src/poseidon_bn128_opt.rs | 6 +++--- starky/src/poseidon_opt.rs | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/starky/src/fft.rs b/starky/src/fft.rs index e81018e4..72af5a18 100644 --- a/starky/src/fft.rs +++ b/starky/src/fft.rs @@ -71,7 +71,7 @@ impl FFT { buff } - pub fn ifft(&mut self, p: &Vec) -> Vec { + pub fn ifft(&mut self, p: &[F]) -> Vec { let q = self.fft(p); let n = p.len(); let n2inv = F::from(p.len()).inv(); diff --git a/starky/src/helper.rs b/starky/src/helper.rs index 16a0821d..0161277d 100644 --- a/starky/src/helper.rs +++ b/starky/src/helper.rs @@ -86,7 +86,7 @@ pub fn biguint_bls12381_to_fr(f: &BigUint) -> Fr_bls12381 { } use std::fmt::{Debug, Display}; -pub fn pretty_print_array(cols: &Vec) -> String { +pub fn pretty_print_array(cols: &[T]) -> String { let mut msg = String::new(); writeln!(&mut msg, "array size: {}", cols.len()).unwrap(); writeln!(&mut msg, "[").unwrap(); diff --git a/starky/src/poseidon_bls12381.rs b/starky/src/poseidon_bls12381.rs index b5ff33de..81d7e4d1 100644 --- a/starky/src/poseidon_bls12381.rs +++ b/starky/src/poseidon_bls12381.rs @@ -70,7 +70,7 @@ impl Poseidon { pub fn new() -> Poseidon { Self {} } - pub fn ark(&self, state: &mut Vec, c: &[Fr], it: usize) { + pub fn ark(&self, state: &mut [Fr], c: &[Fr], it: usize) { for i in 0..state.len() { state[i].add_assign(&c[it + i]); } @@ -84,7 +84,7 @@ impl Poseidon { x.mul_assign(&aux); } - pub fn sbox(&self, n_rounds_f: usize, n_rounds_p: usize, state: &mut Vec, i: usize) { + pub fn sbox(&self, n_rounds_f: usize, n_rounds_p: usize, state: &mut [Fr], i: usize) { if i < n_rounds_f / 2 || i >= n_rounds_f / 2 + n_rounds_p { for x in state { Self::pow5(x); @@ -109,7 +109,7 @@ impl Poseidon { /// Hash function /// init_state would be Fr::zero() initially - pub fn hash(&self, inp: &Vec, init_state: &Fr) -> Result { + pub fn hash(&self, inp: &[Fr], init_state: &Fr) -> Result { let result = self.hash_inner(inp, init_state, 1)?; Ok(result[0]) } diff --git a/starky/src/poseidon_bn128.rs b/starky/src/poseidon_bn128.rs index 11f1ae9c..60214987 100644 --- a/starky/src/poseidon_bn128.rs +++ b/starky/src/poseidon_bn128.rs @@ -182,43 +182,43 @@ mod tests { let b6: Fr = Fr::from_str("6").unwrap(); let is = Fr::zero(); - let h = poseidon.hash(&vec![b1], &is).unwrap(); + let h = poseidon.hash(&[b1], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x29176100eaa962bdc1fe6c654d6a3c130e96a4d1168b33848b897dc502820133)" // "18586133768512220936620570745912940619677854269274689475585506675881198879027" ); - let h = poseidon.hash(&vec![b1, b2], &is).unwrap(); + let h = poseidon.hash(&[b1, b2], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x115cc0f5e7d690413df64c6b9662e9cf2a3617f2743245519e19607a4417189a)" // "7853200120776062878684798364095072458815029376092732009249414926327459813530" ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x024058dd1e168f34bac462b6fffe58fd69982807e9884c1c6148182319cee427)" // "1018317224307729531995786483840663576608797660851238720571059489595066344487" ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x21e82f465e00a15965e97a44fe3c30f3bf5279d8bf37d4e65765b6c2550f42a1)" // "15336558801450556532856248569924170992202208561737609669134139141992924267169" ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x0cd93f1bab9e8c9166ef00f2a1b0e1d66d6a4145e596abe0526247747cc71214)" // "5811595552068139067952687508729883632420015185677766880877743348592482390548" ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x1b1caddfc5ea47e09bb445a7447eb9694b8d1b75a97fff58e884398c6b22825a)" // "12263118664590987767234828103155242843640892839966517009184493198782366909018" ); - let h = poseidon.hash(&vec![b1, b2, b3, b4, b5, b6], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b3, b4, b5, b6], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x2d1a03850084442813c8ebf094dea47538490a68b05f2239134a4cca2f6302e1)" // "20400040500897583745843009878988256314335038853985262692600694741116813247201" diff --git a/starky/src/poseidon_bn128_opt.rs b/starky/src/poseidon_bn128_opt.rs index 0e57f571..fb4bc9c7 100644 --- a/starky/src/poseidon_bn128_opt.rs +++ b/starky/src/poseidon_bn128_opt.rs @@ -89,16 +89,16 @@ impl Poseidon { /// Hash function /// init_state would be Fr::zero() initially - pub fn hash(&self, inp: &Vec, init_state: &Fr) -> Result { + pub fn hash(&self, inp: &[Fr], init_state: &Fr) -> Result { let result = self.hash_inner(inp, init_state, 1)?; Ok(result[0]) } - pub fn hash_ex(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + pub fn hash_ex(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { self.hash_inner(inp, init_state, out) } - fn hash_inner(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + fn hash_inner(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { if inp.is_empty() || inp.len() > POSEIDON_BN128_CONSTANTS_OPT.n_rounds_p.len() { return Err(format!( "Wrong inputs length {} > {}", diff --git a/starky/src/poseidon_opt.rs b/starky/src/poseidon_opt.rs index 1ec2fdfa..502bf129 100644 --- a/starky/src/poseidon_opt.rs +++ b/starky/src/poseidon_opt.rs @@ -75,7 +75,7 @@ impl Poseidon { x.mul_assign(&aux); } - pub fn hash(&self, inp: &Vec, init_state: &[FGL], out: usize) -> Result, String> { + pub fn hash(&self, inp: &[FGL], init_state: &[FGL], out: usize) -> Result, String> { self.hash_inner(inp, init_state, out) } From 78c1fc82eb18d169e0e869670df370b155d50545 Mon Sep 17 00:00:00 2001 From: eigmax Date: Tue, 5 Dec 2023 00:51:52 +0800 Subject: [PATCH 5/7] chore: fix clippy --- starky/src/polutils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/starky/src/polutils.rs b/starky/src/polutils.rs index bec9745b..6e456c7f 100644 --- a/starky/src/polutils.rs +++ b/starky/src/polutils.rs @@ -2,7 +2,7 @@ use crate::constant::SHIFT; use crate::fft::FFT; use crate::traits::FieldExtension; -pub fn pol_mul_axi(p: &mut Vec, init: F, acc: &F) { +pub fn pol_mul_axi(p: &mut [F], init: F, acc: &F) { let mut r = init; for pi in p { *pi *= r; @@ -22,7 +22,7 @@ pub fn eval_pol(p: &[F], x: &F) -> F { } #[allow(dead_code)] -pub fn extend_pol(p: &Vec, extend_bits: usize) -> Vec { +pub fn extend_pol(p: &[F], extend_bits: usize) -> Vec { let mut standard_fft = FFT::new(); let mut res = standard_fft.ifft(p); pol_mul_axi(&mut res, F::ONE, &F::from(*SHIFT)); From 1ce59050c120ff903cdbc8cc95a5b7d4289b3d33 Mon Sep 17 00:00:00 2001 From: eigmax Date: Tue, 5 Dec 2023 00:55:59 +0800 Subject: [PATCH 6/7] chore: fix clippy --- starky/src/poseidon_bls12381_opt.rs | 22 +++++++++++----------- starky/src/poseidon_bn128_opt.rs | 14 +++++++------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/starky/src/poseidon_bls12381_opt.rs b/starky/src/poseidon_bls12381_opt.rs index 0a97931a..52df3b41 100644 --- a/starky/src/poseidon_bls12381_opt.rs +++ b/starky/src/poseidon_bls12381_opt.rs @@ -90,7 +90,7 @@ impl Poseidon { /// Hash function /// init_state would be Fr::zero() initially - pub fn hash(&self, inp: &Vec, init_state: &Fr) -> Result { + pub fn hash(&self, inp: &[Fr], init_state: &Fr) -> Result { let result = self.hash_inner(inp, init_state, 2)?; // Return the second element of the result based on Neptune project's specifications. // ------------------------------------------------------- @@ -100,11 +100,11 @@ impl Poseidon { Ok(result[1]) } - pub fn hash_ex(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + pub fn hash_ex(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { self.hash_inner(inp, init_state, out) } - fn hash_inner(&self, inp: &Vec, init_state: &Fr, out: usize) -> Result, String> { + fn hash_inner(&self, inp: &[Fr], init_state: &Fr, out: usize) -> Result, String> { if inp.is_empty() || inp.len() > POSEIDON_BLS12381_CONSTANTS_OPT.n_rounds_p.len() { return Err(format!( "Wrong inputs length {} > {}", @@ -256,49 +256,49 @@ mod tests { let b4: Fr = Fr::from_str("4").unwrap(); let is = Fr::zero(); - let h = poseidon.hash(&vec![b1], &is).unwrap(); + let h = poseidon.hash(&[b1], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x164efff6c8a32ef98836c868f8c8dedcbe3068d16ba6098f282a6d185edb551f)" //10090463338479474364654416042385169859560025017303585988626920959727361545503 ); - let h = poseidon.hash(&vec![b1, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x59220c0fc5748e83c141c7bb8dae0a2bd5bbb227c778ede87296ba07960ec3d8)" //40315999570263005229566068098191840653718756303362127561954793579940120806360 ); - let h = poseidon.hash(&vec![b1, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x73584296b068384db6028b55d995108518d4483ab177197274effe979b91526e)" //52171919706604857662228147548523676303297329614804576829062159794914391577198 ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x385acd94e53a8c6f981809c2201582beceaec12250200f1e75ba93e6cf5ec736)" //25489954628706771422434337159093356230875147553184381182493646336226215511862 ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x023dd8aecc0967c0588754eebd39af39bdae2bbf4195fee1208613c909aaa29b)" //1013898857847217674473086247177895055941699630695530588118970595082884522651 ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x19c96d726da9e3df4e5d0da19f324f7bf376dc7bf97efbf37082473f7fa24af8)" //11663712849936763722275869035629160480859126086041635677673535448082509089528 ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x0cb7b1761b9abe661847a10701c6eae7c631ff580c5b7f3ac2f8be1088d22bba)" //5752311989137819405955540762621381412568871047030860382530977484434251590586 ); - let h = poseidon.hash(&vec![b1, b2, b3, b4], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b3, b4], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x6f5f297b0ab0d1e7400501b9bdd4c3be2fe676b6a05deb845143b87355167a8d)" //50374862952696036512232585533148559412665642735378685892656796916864806976141 diff --git a/starky/src/poseidon_bn128_opt.rs b/starky/src/poseidon_bn128_opt.rs index fb4bc9c7..47d15c6b 100644 --- a/starky/src/poseidon_bn128_opt.rs +++ b/starky/src/poseidon_bn128_opt.rs @@ -252,43 +252,43 @@ mod tests { let b6: Fr = Fr::from_str("6").unwrap(); let is = Fr::zero(); - let h = poseidon.hash(&vec![b1], &is).unwrap(); + let h = poseidon.hash(&[b1], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x29176100eaa962bdc1fe6c654d6a3c130e96a4d1168b33848b897dc502820133)" // "18586133768512220936620570745912940619677854269274689475585506675881198879027" ); - let h = poseidon.hash(&vec![b1, b2], &is).unwrap(); + let h = poseidon.hash(&[b1, b2], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x115cc0f5e7d690413df64c6b9662e9cf2a3617f2743245519e19607a4417189a)" // "7853200120776062878684798364095072458815029376092732009249414926327459813530" ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x024058dd1e168f34bac462b6fffe58fd69982807e9884c1c6148182319cee427)" // "1018317224307729531995786483840663576608797660851238720571059489595066344487" ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x21e82f465e00a15965e97a44fe3c30f3bf5279d8bf37d4e65765b6c2550f42a1)" // "15336558801450556532856248569924170992202208561737609669134139141992924267169" ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x0cd93f1bab9e8c9166ef00f2a1b0e1d66d6a4145e596abe0526247747cc71214)" // "5811595552068139067952687508729883632420015185677766880877743348592482390548" ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x1b1caddfc5ea47e09bb445a7447eb9694b8d1b75a97fff58e884398c6b22825a)" // "12263118664590987767234828103155242843640892839966517009184493198782366909018" ); - let h = poseidon.hash(&vec![b1, b2, b3, b4, b5, b6], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b3, b4, b5, b6], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x2d1a03850084442813c8ebf094dea47538490a68b05f2239134a4cca2f6302e1)" // "20400040500897583745843009878988256314335038853985262692600694741116813247201" From c5f800dd446e317806444cef3e669e9c9bc3b400 Mon Sep 17 00:00:00 2001 From: eigmax Date: Tue, 5 Dec 2023 00:59:47 +0800 Subject: [PATCH 7/7] chore: fix clippy --- starky/src/linearhash_bls12381.rs | 2 +- starky/src/linearhash_bn128.rs | 2 +- starky/src/poseidon_bls12381.rs | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/starky/src/linearhash_bls12381.rs b/starky/src/linearhash_bls12381.rs index ab56f1b8..7c425946 100644 --- a/starky/src/linearhash_bls12381.rs +++ b/starky/src/linearhash_bls12381.rs @@ -131,7 +131,7 @@ impl LinearHashBLS12381 { // hash on each 16 for i in (0..tmp_buf.len()).step_by(16) { let in_sz = std::cmp::min(16, tmp_buf.len() - i); - digest = self.h.hash(&tmp_buf[i..(i + in_sz)].to_vec(), &digest)?; + digest = self.h.hash(&tmp_buf[i..(i + in_sz)], &digest)?; } Ok(ElementDigest::<4>::from_scalar(&digest)) diff --git a/starky/src/linearhash_bn128.rs b/starky/src/linearhash_bn128.rs index c1d8ff47..0992fc6e 100644 --- a/starky/src/linearhash_bn128.rs +++ b/starky/src/linearhash_bn128.rs @@ -132,7 +132,7 @@ impl LinearHashBN128 { // hash on each 16 for i in (0..tmp_buf.len()).step_by(16) { let in_sz = std::cmp::min(16, tmp_buf.len() - i); - digest = self.h.hash(&tmp_buf[i..(i + in_sz)].to_vec(), &digest)?; + digest = self.h.hash(&tmp_buf[i..(i + in_sz)], &digest)?; } Ok(ElementDigest::<4>::from_scalar(&digest)) diff --git a/starky/src/poseidon_bls12381.rs b/starky/src/poseidon_bls12381.rs index 81d7e4d1..3bb8fc67 100644 --- a/starky/src/poseidon_bls12381.rs +++ b/starky/src/poseidon_bls12381.rs @@ -180,43 +180,43 @@ mod tests { let b4: Fr = Fr::from_str("4").unwrap(); let is = Fr::zero(); - let h = poseidon.hash(&vec![b1], &is).unwrap(); + let h = poseidon.hash(&[b1], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x49a66f6b01dbc6440d1a5f920e027b94429916f2c821a920cf6203ad3de56cea)" ); - let h = poseidon.hash(&vec![b1, b2], &is).unwrap(); + let h = poseidon.hash(&[b1, b2], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x28ce19420fc246a05553ad1e8c98f5c9d67166be2c18e9e4cb4b4e317dd2a78a)" ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x38a4dfeeb62c8ddc28f907fff9658ad10495c587433646531f57d7741c372226)" ); - let h = poseidon.hash(&vec![b1, b2, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x4a300ef0358077f7277ded221e20d5967013c62a653bee2db65e162b6143321c)" ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x631d5456b4cf350c218dfb3c8de41ae9d05ede09f46be5982eed8d3f1d6c7c2a)" ); - let h = poseidon.hash(&vec![b3, b4, b0, b0, b0, b0], &is).unwrap(); + let h = poseidon.hash(&[b3, b4, b0, b0, b0, b0], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x4946ee12005b4c8f9fc919b4768499cfeccb4d35168c1db4509eae7ea5055483)" ); - let h = poseidon.hash(&vec![b1, b2, b3, b4], &is).unwrap(); + let h = poseidon.hash(&[b1, b2, b3, b4], &is).unwrap(); assert_eq!( h.to_string(), "Fr(0x2a918b9c9f9bd7bb509331c81e297b5707f6fc7393dcee1b13901a0b22202e18)"