Skip to content

Commit

Permalink
removed FieldElement::zeroed_vector() function
Browse files Browse the repository at this point in the history
  • Loading branch information
irakliyk committed May 9, 2024
1 parent 7995ec2 commit db88592
Show file tree
Hide file tree
Showing 17 changed files with 16 additions and 197 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.9.0 (2024-05-09)
- [BREAKING] removed `group_vector_elements()` utility function (#282).
- [BREAKING] removed `FieldElement::zeroed_vector()` function (#282).

## 0.8.4 (2024-03-28) - `math` crate only
* Added more to/from conversions for `f64` field (#268).
Expand Down
4 changes: 2 additions & 2 deletions air/src/air/lagrange/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<E: FieldElement> LagrangeKernelTransitionConstraints<E> {
// HELPERS
// ---------------------------------------------------------------------------------------------

/// Evaluates the transition constraints' numerators over the specificed Lagrange kernel
/// Evaluates the transition constraints' numerators over the specified Lagrange kernel
/// evaluation frame.
fn evaluate_numerators<F>(
&self,
Expand All @@ -122,7 +122,7 @@ impl<E: FieldElement> LagrangeKernelTransitionConstraints<E> {
E: ExtensionOf<F>,
{
let log2_trace_len = lagrange_kernel_column_frame.num_rows() - 1;
let mut transition_evals = E::zeroed_vector(log2_trace_len);
let mut transition_evals = vec![E::ZERO; log2_trace_len];

let c = lagrange_kernel_column_frame.inner();
let v = c.len() - 1;
Expand Down
4 changes: 2 additions & 2 deletions air/src/air/transition/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl<E: FieldElement> EvaluationFrame<E> {
pub fn new(num_columns: usize) -> Self {
assert!(num_columns > 0, "number of columns must be greater than zero");
EvaluationFrame {
current: E::zeroed_vector(num_columns),
next: E::zeroed_vector(num_columns),
current: vec![E::ZERO; num_columns],
next: vec![E::ZERO; num_columns],
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/src/rescue/rescue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CYCLE_LENGTH: usize = 16;
/// Implementation of Rescue hash function with a 4 element state and 14 rounds. Accepts a
/// 2-element input, and returns a 2-element digest.
pub fn hash(value: [BaseElement; 2], result: &mut [BaseElement]) {
let mut state = BaseElement::zeroed_vector(STATE_WIDTH);
let mut state = [BaseElement::ZERO; STATE_WIDTH];
state[..2].copy_from_slice(&value);
for i in 0..NUM_ROUNDS {
apply_round(&mut state, i);
Expand Down
43 changes: 1 addition & 42 deletions math/src/field/extensions/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
// LICENSE file in the root directory of this source tree.

use super::{ExtensibleField, ExtensionOf, FieldElement};
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloc::string::{String, ToString};
use core::{
fmt,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
Expand Down Expand Up @@ -45,22 +42,6 @@ impl<B: ExtensibleField<3>> CubeExtension<B> {
<B as ExtensibleField<3>>::is_supported()
}

/// Converts a vector of base elements into a vector of elements in a cubic extension field
/// by fusing three adjacent base elements together. The output vector is one-third the length
/// of the source vector.
fn base_to_cubic_vector(source: Vec<B>) -> Vec<Self> {
debug_assert!(
source.len() % Self::EXTENSION_DEGREE == 0,
"source vector length must be divisible by three, but was {}",
source.len()
);
let mut v = core::mem::ManuallyDrop::new(source);
let p = v.as_mut_ptr();
let len = v.len() / Self::EXTENSION_DEGREE;
let cap = v.capacity() / Self::EXTENSION_DEGREE;
unsafe { Vec::from_raw_parts(p as *mut Self, len, cap) }
}

/// Returns an array of base field elements comprising this extension field element.
///
/// The order of abase elements in the returned array is the same as the order in which
Expand Down Expand Up @@ -182,16 +163,6 @@ impl<B: ExtensibleField<3>> FieldElement for CubeExtension<B> {

Ok(slice::from_raw_parts(p as *const Self, len))
}

// UTILITIES
// --------------------------------------------------------------------------------------------

fn zeroed_vector(n: usize) -> Vec<Self> {
// get three times the number of base elements and re-interpret them as cubic field
// elements
let result = B::zeroed_vector(n * Self::EXTENSION_DEGREE);
Self::base_to_cubic_vector(result)
}
}

impl<B: ExtensibleField<3>> ExtensionOf<B> for CubeExtension<B> {
Expand Down Expand Up @@ -441,18 +412,6 @@ mod tests {
assert_eq!(expected, r1 - r2);
}

// INITIALIZATION
// --------------------------------------------------------------------------------------------

#[test]
fn zeroed_vector() {
let result = CubeExtension::<BaseElement>::zeroed_vector(4);
assert_eq!(4, result.len());
for element in result.into_iter() {
assert_eq!(CubeExtension::<BaseElement>::ZERO, element);
}
}

// SERIALIZATION / DESERIALIZATION
// --------------------------------------------------------------------------------------------

Expand Down
42 changes: 1 addition & 41 deletions math/src/field/extensions/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
// LICENSE file in the root directory of this source tree.

use super::{ExtensibleField, ExtensionOf, FieldElement};
use alloc::{
string::{String, ToString},
vec::Vec,
};
use alloc::string::{String, ToString};
use core::{
fmt,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
Expand Down Expand Up @@ -45,22 +42,6 @@ impl<B: ExtensibleField<2>> QuadExtension<B> {
<B as ExtensibleField<2>>::is_supported()
}

/// Converts a vector of base elements into a vector of elements in a quadratic extension
/// field by fusing two adjacent base elements together. The output vector is half the length
/// of the source vector.
fn base_to_quad_vector(source: Vec<B>) -> Vec<Self> {
debug_assert!(
source.len() % Self::EXTENSION_DEGREE == 0,
"source vector length must be divisible by two, but was {}",
source.len()
);
let mut v = core::mem::ManuallyDrop::new(source);
let p = v.as_mut_ptr();
let len = v.len() / Self::EXTENSION_DEGREE;
let cap = v.capacity() / Self::EXTENSION_DEGREE;
unsafe { Vec::from_raw_parts(p as *mut Self, len, cap) }
}

/// Returns an array of base field elements comprising this extension field element.
///
/// The order of abase elements in the returned array is the same as the order in which
Expand Down Expand Up @@ -178,15 +159,6 @@ impl<B: ExtensibleField<2>> FieldElement for QuadExtension<B> {

Ok(slice::from_raw_parts(p as *const Self, len))
}

// UTILITIES
// --------------------------------------------------------------------------------------------

fn zeroed_vector(n: usize) -> Vec<Self> {
// get twice the number of base elements, and re-interpret them as quad field elements
let result = B::zeroed_vector(n * Self::EXTENSION_DEGREE);
Self::base_to_quad_vector(result)
}
}

impl<B: ExtensibleField<2>> ExtensionOf<B> for QuadExtension<B> {
Expand Down Expand Up @@ -433,18 +405,6 @@ mod tests {
assert_eq!(expected, r1 - r2);
}

// INITIALIZATION
// --------------------------------------------------------------------------------------------

#[test]
fn zeroed_vector() {
let result = QuadExtension::<BaseElement>::zeroed_vector(4);
assert_eq!(4, result.len());
for element in result.into_iter() {
assert_eq!(QuadExtension::<BaseElement>::ZERO, element);
}
}

// SERIALIZATION / DESERIALIZATION
// --------------------------------------------------------------------------------------------

Expand Down
19 changes: 0 additions & 19 deletions math/src/field/f128/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,6 @@ impl FieldElement for BaseElement {

Ok(slice::from_raw_parts(p as *const Self, len))
}

// UTILITIES
// --------------------------------------------------------------------------------------------

fn zeroed_vector(n: usize) -> Vec<Self> {
// this uses a specialized vector initialization code which requests zero-filled memory
// from the OS; unfortunately, this works only for built-in types and we can't use
// Self::ZERO here as much less efficient initialization procedure will be invoked.
// We also use u128 to make sure the memory is aligned correctly for our element size.
debug_assert_eq!(Self::ELEMENT_BYTES, mem::size_of::<u128>());
let result = vec![0u128; n];

// translate a zero-filled vector of u128s into a vector of base field elements
let mut v = core::mem::ManuallyDrop::new(result);
let p = v.as_mut_ptr();
let len = v.len();
let cap = v.capacity();
unsafe { Vec::from_raw_parts(p as *mut Self, len, cap) }
}
}

impl StarkField for BaseElement {
Expand Down
12 changes: 0 additions & 12 deletions math/src/field/f128/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,6 @@ fn read_elements_from() {
}
}

// INITIALIZATION
// ================================================================================================

#[test]
fn zeroed_vector() {
let result = BaseElement::zeroed_vector(4);
assert_eq!(4, result.len());
for element in result.into_iter() {
assert_eq!(BaseElement::ZERO, element);
}
}

// HELPER FUNCTIONS
// ================================================================================================

Expand Down
18 changes: 0 additions & 18 deletions math/src/field/f62/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,6 @@ impl FieldElement for BaseElement {

Ok(slice::from_raw_parts(p as *const Self, len))
}

// UTILITIES
// --------------------------------------------------------------------------------------------

fn zeroed_vector(n: usize) -> Vec<Self> {
// this uses a specialized vector initialization code which requests zero-filled memory
// from the OS; unfortunately, this works only for built-in types and we can't use
// Self::ZERO here as much less efficient initialization procedure will be invoked.
// We also use u64 to make sure the memory is aligned correctly for our element size.
let result = vec![0u64; n];

// translate a zero-filled vector of u64s into a vector of base field elements
let mut v = core::mem::ManuallyDrop::new(result);
let p = v.as_mut_ptr();
let len = v.len();
let cap = v.capacity();
unsafe { Vec::from_raw_parts(p as *mut Self, len, cap) }
}
}

impl StarkField for BaseElement {
Expand Down
12 changes: 0 additions & 12 deletions math/src/field/f62/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,6 @@ fn bytes_as_elements() {
assert!(matches!(result, Err(DeserializationError::InvalidValue(_))));
}

// INITIALIZATION
// ------------------------------------------------------------------------------------------------

#[test]
fn zeroed_vector() {
let result = BaseElement::zeroed_vector(4);
assert_eq!(4, result.len());
for element in result.into_iter() {
assert_eq!(BaseElement::ZERO, element);
}
}

// RANDOMIZED TESTS
// ================================================================================================

Expand Down
18 changes: 0 additions & 18 deletions math/src/field/f64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,24 +232,6 @@ impl FieldElement for BaseElement {

Ok(slice::from_raw_parts(p as *const Self, len))
}

// UTILITIES
// --------------------------------------------------------------------------------------------

fn zeroed_vector(n: usize) -> Vec<Self> {
// this uses a specialized vector initialization code which requests zero-filled memory
// from the OS; unfortunately, this works only for built-in types and we can't use
// Self::ZERO here as much less efficient initialization procedure will be invoked.
// We also use u64 to make sure the memory is aligned correctly for our element size.
let result = vec![0u64; n];

// translate a zero-filled vector of u64s into a vector of base field elements
let mut v = core::mem::ManuallyDrop::new(result);
let p = v.as_mut_ptr();
let len = v.len();
let cap = v.capacity();
unsafe { Vec::from_raw_parts(p as *mut Self, len, cap) }
}
}

impl StarkField for BaseElement {
Expand Down
12 changes: 0 additions & 12 deletions math/src/field/f64/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,6 @@ fn bytes_as_elements() {
assert!(matches!(result, Err(DeserializationError::InvalidValue(_))));
}

// INITIALIZATION
// ------------------------------------------------------------------------------------------------

#[test]
fn zeroed_vector() {
let result = BaseElement::zeroed_vector(4);
assert_eq!(4, result.len());
for element in result.into_iter() {
assert_eq!(BaseElement::ZERO, element);
}
}

// QUADRATIC EXTENSION
// ------------------------------------------------------------------------------------------------
#[test]
Expand Down
10 changes: 0 additions & 10 deletions math/src/field/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,6 @@ pub trait FieldElement:
/// This function is unsafe because it does not check whether underlying bytes represent valid
/// field elements according to their internal representation.
unsafe fn bytes_as_elements(bytes: &[u8]) -> Result<&[Self], DeserializationError>;

// UTILITIES
// --------------------------------------------------------------------------------------------

/// Returns a vector of length `n` initialized with all ZERO elements.
///
/// Specialized implementations of this function may be faster than the generic implementation.
fn zeroed_vector(n: usize) -> Vec<Self> {
vec![Self::ZERO; n]
}
}

// STARK FIELD
Expand Down
6 changes: 3 additions & 3 deletions math/src/polynom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ where
let denominators: Vec<E> = numerators.iter().zip(xs).map(|(e, &x)| eval(e, x)).collect();
let denominators = batch_inversion(&denominators);

let mut result = E::zeroed_vector(xs.len());
let mut result = vec![E::ZERO; xs.len()];
for i in 0..xs.len() {
let y_slice = ys[i] * denominators[i];
for (j, res) in result.iter_mut().enumerate() {
Expand Down Expand Up @@ -320,7 +320,7 @@ where
E: FieldElement,
{
let result_len = a.len() + b.len() - 1;
let mut result = E::zeroed_vector(result_len);
let mut result = vec![E::ZERO; result_len];
for i in 0..a.len() {
for j in 0..b.len() {
let s = a[i] * b[j];
Expand Down Expand Up @@ -409,7 +409,7 @@ where
assert!(b[0] != E::ZERO, "cannot divide polynomial by zero");
}

let mut result = E::zeroed_vector(apos - bpos + 1);
let mut result = vec![E::ZERO; apos - bpos + 1];
for i in (0..result.len()).rev() {
let quot = a[apos] / b[bpos];
result[i] = quot;
Expand Down
4 changes: 2 additions & 2 deletions prover/src/composer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ impl<E: FieldElement> DeepCompositionPoly<E> {
let next_z = self.z * g;

// combine trace polynomials into 2 composition polynomials T'(x) and T''(x)
let mut t1_composition = E::zeroed_vector(trace_length);
let mut t2_composition = E::zeroed_vector(trace_length);
let mut t1_composition = vec![E::ZERO; trace_length];
let mut t2_composition = vec![E::ZERO; trace_length];

// index of a trace polynomial; we declare it here so that we can maintain index continuity
// across all trace segments
Expand Down
2 changes: 1 addition & 1 deletion prover/src/constraints/evaluation_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<'a, E: FieldElement> ConstraintEvaluationTable<'a, E> {
/// combines the results into a single column.
pub fn combine(self) -> Vec<E> {
// allocate memory for the combined polynomial
let mut combined_poly = E::zeroed_vector(self.num_rows());
let mut combined_poly = vec![E::ZERO; self.num_rows()];

// iterate over all columns of the constraint evaluation table, divide each column
// by the evaluations of its corresponding divisor, and add all resulting evaluations
Expand Down
4 changes: 2 additions & 2 deletions verifier/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ pub fn evaluate_constraints<A: Air, E: FieldElement<BaseField = A::BaseField>>(
.collect::<Vec<_>>();

// evaluate transition constraints for the main trace segment
let mut t_evaluations1 = E::zeroed_vector(t_constraints.num_main_constraints());
let mut t_evaluations1 = vec![E::ZERO; t_constraints.num_main_constraints()];
air.evaluate_transition(main_trace_frame, &periodic_values, &mut t_evaluations1);

// evaluate transition constraints for the auxiliary trace segment (if any)
let mut t_evaluations2 = E::zeroed_vector(t_constraints.num_aux_constraints());
let mut t_evaluations2 = vec![E::ZERO; t_constraints.num_aux_constraints()];
if let Some(aux_trace_frame) = aux_trace_frame {
let aux_rand_elements =
aux_rand_elements.expect("expected aux rand elements to be present");
Expand Down

0 comments on commit db88592

Please sign in to comment.