From 5a379f15e7ea89a3bbdad7e882214ae7cea77442 Mon Sep 17 00:00:00 2001 From: Jakub Nabaglo Date: Wed, 29 Dec 2021 16:56:38 -0800 Subject: [PATCH] Rename PackedField constants ZERO -> ZEROS, ONE -> ONES (#408) --- field/src/arch/x86_64/avx2_goldilocks_field.rs | 10 +++++----- field/src/fft.rs | 2 +- field/src/packed_field.rs | 8 ++++---- plonky2/src/gates/arithmetic_u32.rs | 8 ++++---- plonky2/src/gates/assert_le.rs | 13 ++++++------- plonky2/src/gates/comparison.rs | 17 ++++++++--------- plonky2/src/gates/exponentiation.rs | 8 ++++---- plonky2/src/gates/gmimc.rs | 6 +++--- plonky2/src/gates/random_access.rs | 6 +++--- plonky2/src/gates/subtraction_u32.rs | 8 ++++---- plonky2/src/gates/switch.rs | 4 ++-- plonky2/src/plonk/plonk_common.rs | 2 +- plonky2/src/util/strided_view.rs | 2 +- 13 files changed, 46 insertions(+), 48 deletions(-) diff --git a/field/src/arch/x86_64/avx2_goldilocks_field.rs b/field/src/arch/x86_64/avx2_goldilocks_field.rs index db6ab2b0..61eb26ac 100644 --- a/field/src/arch/x86_64/avx2_goldilocks_field.rs +++ b/field/src/arch/x86_64/avx2_goldilocks_field.rs @@ -73,7 +73,7 @@ impl Debug for Avx2GoldilocksField { impl Default for Avx2GoldilocksField { #[inline] fn default() -> Self { - Self::ZERO + Self::ZEROS } } @@ -142,7 +142,7 @@ impl Neg for Avx2GoldilocksField { impl Product for Avx2GoldilocksField { #[inline] fn product>(iter: I) -> Self { - iter.reduce(|x, y| x * y).unwrap_or(Self::ONE) + iter.reduce(|x, y| x * y).unwrap_or(Self::ONES) } } @@ -151,8 +151,8 @@ unsafe impl PackedField for Avx2GoldilocksField { type Scalar = GoldilocksField; - const ZERO: Self = Self([::ZERO; 4]); - const ONE: Self = Self([::ONE; 4]); + const ZEROS: Self = Self([GoldilocksField::ZERO; 4]); + const ONES: Self = Self([GoldilocksField::ONE; 4]); #[inline] fn from_arr(arr: [Self::Scalar; Self::WIDTH]) -> Self { @@ -238,7 +238,7 @@ impl SubAssign for Avx2GoldilocksField { impl Sum for Avx2GoldilocksField { #[inline] fn sum>(iter: I) -> Self { - iter.reduce(|x, y| x + y).unwrap_or(Self::ZERO) + iter.reduce(|x, y| x + y).unwrap_or(Self::ZEROS) } } diff --git a/field/src/fft.rs b/field/src/fft.rs index 14112dfc..8428d3fb 100644 --- a/field/src/fft.rs +++ b/field/src/fft.rs @@ -121,7 +121,7 @@ fn fft_classic_simd( let half_m = 1 << lg_half_m; // Set omega to root_table[lg_half_m][0..half_m] but repeated. - let mut omega = P::ZERO; + let mut omega = P::default(); for (j, omega_j) in omega.as_slice_mut().iter_mut().enumerate() { *omega_j = root_table[lg_half_m][j % half_m]; } diff --git a/field/src/packed_field.rs b/field/src/packed_field.rs index 091dbc97..813cbeba 100644 --- a/field/src/packed_field.rs +++ b/field/src/packed_field.rs @@ -41,8 +41,8 @@ where type Scalar: Field; const WIDTH: usize; - const ZERO: Self; - const ONE: Self; + const ZEROS: Self; + const ONES: Self; fn square(&self) -> Self { *self * *self @@ -103,8 +103,8 @@ unsafe impl PackedField for F { type Scalar = Self; const WIDTH: usize = 1; - const ZERO: Self = ::ZERO; - const ONE: Self = ::ONE; + const ZEROS: Self = F::ZERO; + const ONES: Self = F::ONE; fn square(&self) -> Self { ::square(self) diff --git a/plonky2/src/gates/arithmetic_u32.rs b/plonky2/src/gates/arithmetic_u32.rs index baf30ea8..1d4a834c 100644 --- a/plonky2/src/gates/arithmetic_u32.rs +++ b/plonky2/src/gates/arithmetic_u32.rs @@ -100,8 +100,8 @@ impl, const D: usize> Gate for U32ArithmeticG constraints.push(combined_output - computed_output); - let mut combined_low_limbs = ::ZERO; - let mut combined_high_limbs = ::ZERO; + let mut combined_low_limbs = F::Extension::ZERO; + let mut combined_high_limbs = F::Extension::ZERO; let midpoint = Self::num_limbs() / 2; let base = F::Extension::from_canonical_u64(1u64 << Self::limb_bits()); for j in (0..Self::num_limbs()).rev() { @@ -255,8 +255,8 @@ impl, const D: usize> PackedEvaluableBase yield_constr.one(combined_output - computed_output); - let mut combined_low_limbs = P::ZERO; - let mut combined_high_limbs = P::ZERO; + let mut combined_low_limbs = P::ZEROS; + let mut combined_high_limbs = P::ZEROS; let midpoint = Self::num_limbs() / 2; let base = F::from_canonical_u64(1u64 << Self::limb_bits()); for j in (0..Self::num_limbs()).rev() { diff --git a/plonky2/src/gates/assert_le.rs b/plonky2/src/gates/assert_le.rs index 2f7adb40..6a99acd9 100644 --- a/plonky2/src/gates/assert_le.rs +++ b/plonky2/src/gates/assert_le.rs @@ -116,7 +116,7 @@ impl, const D: usize> Gate for AssertLessThan let chunk_size = 1 << self.chunk_bits(); - let mut most_significant_diff_so_far = ::ZERO; + let mut most_significant_diff_so_far = F::Extension::ZERO; for i in 0..self.num_chunks { // Range-check the chunks to be less than `chunk_size`. @@ -134,15 +134,14 @@ impl, const D: usize> Gate for AssertLessThan let chunks_equal = vars.local_wires[self.wire_chunks_equal(i)]; // Two constraints to assert that `chunks_equal` is valid. - constraints - .push(difference * equality_dummy - (::ONE - chunks_equal)); + constraints.push(difference * equality_dummy - (F::Extension::ONE - chunks_equal)); constraints.push(chunks_equal * difference); // Update `most_significant_diff_so_far`. let intermediate_value = vars.local_wires[self.wire_intermediate_value(i)]; constraints.push(intermediate_value - chunks_equal * most_significant_diff_so_far); most_significant_diff_so_far = - intermediate_value + (::ONE - chunks_equal) * difference; + intermediate_value + (F::Extension::ONE - chunks_equal) * difference; } let most_significant_diff = vars.local_wires[self.wire_most_significant_diff()]; @@ -314,7 +313,7 @@ impl, const D: usize> PackedEvaluableBase let chunk_size = 1 << self.chunk_bits(); - let mut most_significant_diff_so_far = P::ZERO; + let mut most_significant_diff_so_far = P::ZEROS; for i in 0..self.num_chunks { // Range-check the chunks to be less than `chunk_size`. @@ -332,14 +331,14 @@ impl, const D: usize> PackedEvaluableBase let chunks_equal = vars.local_wires[self.wire_chunks_equal(i)]; // Two constraints to assert that `chunks_equal` is valid. - yield_constr.one(difference * equality_dummy - (P::ONE - chunks_equal)); + yield_constr.one(difference * equality_dummy - (P::ONES - chunks_equal)); yield_constr.one(chunks_equal * difference); // Update `most_significant_diff_so_far`. let intermediate_value = vars.local_wires[self.wire_intermediate_value(i)]; yield_constr.one(intermediate_value - chunks_equal * most_significant_diff_so_far); most_significant_diff_so_far = - intermediate_value + (P::ONE - chunks_equal) * difference; + intermediate_value + (P::ONES - chunks_equal) * difference; } let most_significant_diff = vars.local_wires[self.wire_most_significant_diff()]; diff --git a/plonky2/src/gates/comparison.rs b/plonky2/src/gates/comparison.rs index d8238295..9a119b1d 100644 --- a/plonky2/src/gates/comparison.rs +++ b/plonky2/src/gates/comparison.rs @@ -123,7 +123,7 @@ impl, const D: usize> Gate for ComparisonGate let chunk_size = 1 << self.chunk_bits(); - let mut most_significant_diff_so_far = ::ZERO; + let mut most_significant_diff_so_far = F::Extension::ZERO; for i in 0..self.num_chunks { // Range-check the chunks to be less than `chunk_size`. @@ -141,15 +141,14 @@ impl, const D: usize> Gate for ComparisonGate let chunks_equal = vars.local_wires[self.wire_chunks_equal(i)]; // Two constraints to assert that `chunks_equal` is valid. - constraints - .push(difference * equality_dummy - (::ONE - chunks_equal)); + constraints.push(difference * equality_dummy - (F::Extension::ONE - chunks_equal)); constraints.push(chunks_equal * difference); // Update `most_significant_diff_so_far`. let intermediate_value = vars.local_wires[self.wire_intermediate_value(i)]; constraints.push(intermediate_value - chunks_equal * most_significant_diff_so_far); most_significant_diff_so_far = - intermediate_value + (::ONE - chunks_equal) * difference; + intermediate_value + (F::Extension::ONE - chunks_equal) * difference; } let most_significant_diff = vars.local_wires[self.wire_most_significant_diff()]; @@ -161,7 +160,7 @@ impl, const D: usize> Gate for ComparisonGate // Range-check the bits. for &bit in &most_significant_diff_bits { - constraints.push(bit * (::ONE - bit)); + constraints.push(bit * (F::Extension::ONE - bit)); } let bits_combined = reduce_with_powers(&most_significant_diff_bits, F::Extension::TWO); @@ -347,7 +346,7 @@ impl, const D: usize> PackedEvaluableBase let chunk_size = 1 << self.chunk_bits(); - let mut most_significant_diff_so_far = P::ZERO; + let mut most_significant_diff_so_far = P::ZEROS; for i in 0..self.num_chunks { // Range-check the chunks to be less than `chunk_size`. @@ -365,14 +364,14 @@ impl, const D: usize> PackedEvaluableBase let chunks_equal = vars.local_wires[self.wire_chunks_equal(i)]; // Two constraints to assert that `chunks_equal` is valid. - yield_constr.one(difference * equality_dummy - (P::ONE - chunks_equal)); + yield_constr.one(difference * equality_dummy - (P::ONES - chunks_equal)); yield_constr.one(chunks_equal * difference); // Update `most_significant_diff_so_far`. let intermediate_value = vars.local_wires[self.wire_intermediate_value(i)]; yield_constr.one(intermediate_value - chunks_equal * most_significant_diff_so_far); most_significant_diff_so_far = - intermediate_value + (P::ONE - chunks_equal) * difference; + intermediate_value + (P::ONES - chunks_equal) * difference; } let most_significant_diff = vars.local_wires[self.wire_most_significant_diff()]; @@ -384,7 +383,7 @@ impl, const D: usize> PackedEvaluableBase // Range-check the bits. for &bit in &most_significant_diff_bits { - yield_constr.one(bit * (P::ONE - bit)); + yield_constr.one(bit * (P::ONES - bit)); } let bits_combined = reduce_with_powers(&most_significant_diff_bits, F::TWO); diff --git a/plonky2/src/gates/exponentiation.rs b/plonky2/src/gates/exponentiation.rs index b70b3577..d9628c4f 100644 --- a/plonky2/src/gates/exponentiation.rs +++ b/plonky2/src/gates/exponentiation.rs @@ -88,7 +88,7 @@ impl, const D: usize> Gate for Exponentiation for i in 0..self.num_power_bits { let prev_intermediate_value = if i == 0 { - ::ONE + F::Extension::ONE } else { ::square(&intermediate_values[i - 1]) }; @@ -96,7 +96,7 @@ impl, const D: usize> Gate for Exponentiation // power_bits is in LE order, but we accumulate in BE order. let cur_bit = power_bits[self.num_power_bits - i - 1]; - let not_cur_bit = ::ONE - cur_bit; + let not_cur_bit = F::Extension::ONE - cur_bit; let computed_intermediate_value = prev_intermediate_value * (cur_bit * base + not_cur_bit); constraints.push(computed_intermediate_value - intermediate_values[i]); @@ -210,7 +210,7 @@ impl, const D: usize> PackedEvaluableBase for i in 0..self.num_power_bits { let prev_intermediate_value = if i == 0 { - P::ONE + P::ONES } else { intermediate_values[i - 1].square() }; @@ -218,7 +218,7 @@ impl, const D: usize> PackedEvaluableBase // power_bits is in LE order, but we accumulate in BE order. let cur_bit = power_bits[self.num_power_bits - i - 1]; - let not_cur_bit = P::ONE - cur_bit; + let not_cur_bit = P::ONES - cur_bit; let computed_intermediate_value = prev_intermediate_value * (cur_bit * base + not_cur_bit); yield_constr.one(computed_intermediate_value - intermediate_values[i]); diff --git a/plonky2/src/gates/gmimc.rs b/plonky2/src/gates/gmimc.rs index 7f59271a..fdfb8673 100644 --- a/plonky2/src/gates/gmimc.rs +++ b/plonky2/src/gates/gmimc.rs @@ -81,7 +81,7 @@ impl + GMiMC, const D: usize, const WIDTH: u // Assert that `swap` is binary. let swap = vars.local_wires[Self::WIRE_SWAP]; - constraints.push(swap * (swap - ::ONE)); + constraints.push(swap * (swap - F::Extension::ONE)); let mut state = Vec::with_capacity(12); for i in 0..4 { @@ -100,7 +100,7 @@ impl + GMiMC, const D: usize, const WIDTH: u // Value that is implicitly added to each element. // See https://affine.group/2020/02/starkware-challenge - let mut addition_buffer = ::ZERO; + let mut addition_buffer = F::Extension::ZERO; for r in 0..gmimc::NUM_ROUNDS { let active = r % WIDTH; @@ -245,7 +245,7 @@ impl + GMiMC, const D: usize, const WIDTH: u // Value that is implicitly added to each element. // See https://affine.group/2020/02/starkware-challenge - let mut addition_buffer = P::ZERO; + let mut addition_buffer = P::ZEROS; for r in 0..gmimc::NUM_ROUNDS { let active = r % WIDTH; diff --git a/plonky2/src/gates/random_access.rs b/plonky2/src/gates/random_access.rs index c266bd3c..77359a19 100644 --- a/plonky2/src/gates/random_access.rs +++ b/plonky2/src/gates/random_access.rs @@ -105,14 +105,14 @@ impl, const D: usize> Gate for RandomAccessGa // Assert that each bit wire value is indeed boolean. for &b in &bits { - constraints.push(b * (b - ::ONE)); + constraints.push(b * (b - F::Extension::ONE)); } // Assert that the binary decomposition was correct. let reconstructed_index = bits .iter() .rev() - .fold(::ZERO, |acc, &b| acc.double() + b); + .fold(F::Extension::ZERO, |acc, &b| acc.double() + b); constraints.push(reconstructed_index - access_index); // Repeatedly fold the list, selecting the left or right item from each pair based on @@ -254,7 +254,7 @@ impl, const D: usize> PackedEvaluableBase } // Assert that the binary decomposition was correct. - let reconstructed_index = bits.iter().rev().fold(P::ZERO, |acc, &b| acc + acc + b); + let reconstructed_index = bits.iter().rev().fold(P::ZEROS, |acc, &b| acc + acc + b); yield_constr.one(reconstructed_index - access_index); // Repeatedly fold the list, selecting the left or right item from each pair based on diff --git a/plonky2/src/gates/subtraction_u32.rs b/plonky2/src/gates/subtraction_u32.rs index 3ae9105b..fa817ce4 100644 --- a/plonky2/src/gates/subtraction_u32.rs +++ b/plonky2/src/gates/subtraction_u32.rs @@ -100,7 +100,7 @@ impl, const D: usize> Gate for U32Subtraction constraints.push(output_result - (result_initial + base * output_borrow)); // Range-check output_result to be at most 32 bits. - let mut combined_limbs = ::ZERO; + let mut combined_limbs = F::Extension::ZERO; let limb_base = F::Extension::from_canonical_u64(1u64 << Self::limb_bits()); for j in (0..Self::num_limbs()).rev() { let this_limb = vars.local_wires[self.wire_ith_output_jth_limb(i, j)]; @@ -115,7 +115,7 @@ impl, const D: usize> Gate for U32Subtraction constraints.push(combined_limbs - output_result); // Range-check output_borrow to be one bit. - constraints.push(output_borrow * (::ONE - output_borrow)); + constraints.push(output_borrow * (F::Extension::ONE - output_borrow)); } constraints @@ -243,7 +243,7 @@ impl, const D: usize> PackedEvaluableBase yield_constr.one(output_result - (result_initial + output_borrow * base)); // Range-check output_result to be at most 32 bits. - let mut combined_limbs = P::ZERO; + let mut combined_limbs = P::ZEROS; let limb_base = F::from_canonical_u64(1u64 << Self::limb_bits()); for j in (0..Self::num_limbs()).rev() { let this_limb = vars.local_wires[self.wire_ith_output_jth_limb(i, j)]; @@ -258,7 +258,7 @@ impl, const D: usize> PackedEvaluableBase yield_constr.one(combined_limbs - output_result); // Range-check output_borrow to be one bit. - yield_constr.one(output_borrow * (P::ONE - output_borrow)); + yield_constr.one(output_borrow * (P::ONES - output_borrow)); } } } diff --git a/plonky2/src/gates/switch.rs b/plonky2/src/gates/switch.rs index 9315ab4f..c5271a60 100644 --- a/plonky2/src/gates/switch.rs +++ b/plonky2/src/gates/switch.rs @@ -83,7 +83,7 @@ impl, const D: usize> Gate for SwitchGate::ONE - switch_bool; + let not_switch = F::Extension::ONE - switch_bool; for e in 0..self.chunk_size { let first_input = vars.local_wires[self.wire_first_input(c, e)]; @@ -197,7 +197,7 @@ impl, const D: usize> PackedEvaluableBase for ) { for c in 0..self.num_copies { let switch_bool = vars.local_wires[self.wire_switch_bool(c)]; - let not_switch = P::ONE - switch_bool; + let not_switch = P::ONES - switch_bool; for e in 0..self.chunk_size { let first_input = vars.local_wires[self.wire_first_input(c, e)]; diff --git a/plonky2/src/plonk/plonk_common.rs b/plonky2/src/plonk/plonk_common.rs index 34e50f33..5b8119aa 100644 --- a/plonky2/src/plonk/plonk_common.rs +++ b/plonky2/src/plonk/plonk_common.rs @@ -167,7 +167,7 @@ pub(crate) fn reduce_with_powers<'a, P: PackedField, T: IntoIterator TryInto<[P; N]> for PackedStridedView<'_, P type Error = TryFromPackedStridedViewError; fn try_into(self) -> Result<[P; N], Self::Error> { if N == self.len() { - let mut res = [P::ZERO; N]; + let mut res = [P::default(); N]; for i in 0..N { res[i] = *self.get(i).unwrap(); }