Skip to content

Commit

Permalink
refactor: use EMPTY_WORD, ZERO and ONE
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Aug 31, 2023
1 parent 3a51a58 commit 3e6239e
Show file tree
Hide file tree
Showing 61 changed files with 456 additions and 493 deletions.
4 changes: 2 additions & 2 deletions air/benches/enforce_stack_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use miden_air::{
Felt, FieldElement,
};
use std::time::Duration;
use vm_core::Operation;
use vm_core::{Operation, ZERO};

fn enforce_stack_constraint(c: &mut Criterion) {
let mut group = c.benchmark_group("enforce_stack_constraint");
Expand All @@ -27,7 +27,7 @@ fn enforce_stack_constraint(c: &mut Criterion) {
frame.current_mut()[STACK_TRACE_OFFSET] = Felt::new(89u64);
frame.next_mut()[STACK_TRACE_OFFSET] = Felt::new(89u64).inv();

let mut result = [Felt::ZERO; NUM_CONSTRAINTS];
let mut result = [ZERO; NUM_CONSTRAINTS];

let frame = generate_evaluation_frame(36);
bench.iter(|| {
Expand Down
31 changes: 7 additions & 24 deletions air/src/constraints/chiplets/bitwise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
BITWISE_OUTPUT_COL_IDX, BITWISE_PREV_OUTPUT_COL_IDX, BITWISE_SELECTOR_COL_IDX,
},
utils::{are_equal, binary_not, is_binary, is_zero, EvaluationResult},
ONE, ZERO,
};
use winter_air::TransitionConstraintDegree;

Expand Down Expand Up @@ -413,27 +414,9 @@ pub fn agg_bits<E: FieldElement>(row: &[E], start_idx: usize) -> E {

// CYCLE MASKS
// ================================================================================================
pub const BITWISE_K0_MASK: [Felt; OP_CYCLE_LEN] = [
Felt::ONE,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
];

pub const BITWISE_K1_MASK: [Felt; OP_CYCLE_LEN] = [
Felt::ONE,
Felt::ONE,
Felt::ONE,
Felt::ONE,
Felt::ONE,
Felt::ONE,
Felt::ONE,
Felt::ZERO,
];
pub const BITWISE_K0_MASK: [Felt; OP_CYCLE_LEN] = [ONE, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO];

pub const BITWISE_K1_MASK: [Felt; OP_CYCLE_LEN] = [ONE, ONE, ONE, ONE, ONE, ONE, ONE, ZERO];

// TEST HELPERS
// ================================================================================================
Expand All @@ -442,8 +425,8 @@ pub const BITWISE_K1_MASK: [Felt; OP_CYCLE_LEN] = [
#[cfg(test)]
fn get_periodic_values(cycle_row: usize) -> [Felt; 2] {
match cycle_row {
0 => [Felt::ONE, Felt::ONE],
8 => [Felt::ZERO, Felt::ZERO],
_ => [Felt::ZERO, Felt::ONE],
0 => [ONE, ONE],
8 => [ZERO, ZERO],
_ => [ZERO, ONE],
}
}
70 changes: 35 additions & 35 deletions air/src/constraints/chiplets/bitwise/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{
enforce_constraints, get_periodic_values, EvaluationFrame, BITWISE_A_COL_IDX,
BITWISE_A_COL_RANGE, BITWISE_B_COL_IDX, BITWISE_B_COL_RANGE, BITWISE_OUTPUT_COL_IDX,
BITWISE_PREV_OUTPUT_COL_IDX, BITWISE_SELECTOR_COL_IDX, NUM_CONSTRAINTS, NUM_DECOMP_BITS,
OP_CYCLE_LEN,
BITWISE_PREV_OUTPUT_COL_IDX, BITWISE_SELECTOR_COL_IDX, NUM_CONSTRAINTS, NUM_DECOMP_BITS, ONE,
OP_CYCLE_LEN, ZERO,
};
use crate::{
trace::{
Expand All @@ -12,7 +12,7 @@ use crate::{
},
TRACE_WIDTH,
},
Felt, FieldElement,
Felt,
};
use rand_utils::rand_value;

Expand All @@ -25,7 +25,7 @@ use proptest::prelude::*;
/// specify the operation change within a cycle.
#[test]
fn test_bitwise_change_ops_fail() {
let expected = [Felt::ZERO; NUM_CONSTRAINTS];
let expected = [ZERO; NUM_CONSTRAINTS];

let a = rand_value::<u32>();
let b = rand_value::<u32>();
Expand All @@ -48,28 +48,28 @@ fn output_aggregation_and() {
let cycle_row = 0;

// create a valid test frame manually
let mut current = vec![Felt::ZERO; TRACE_WIDTH];
let mut next = vec![Felt::ZERO; TRACE_WIDTH];
let mut current = vec![ZERO; TRACE_WIDTH];
let mut next = vec![ZERO; TRACE_WIDTH];

let current_bitwise = [
// selector
BITWISE_AND,
// a
Felt::ONE,
ONE,
// b
Felt::new(9),
// decomposition of a
Felt::ONE,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
ONE,
ZERO,
ZERO,
ZERO,
// decomposition of b
Felt::ONE,
Felt::ZERO,
Felt::ZERO,
Felt::ONE,
ONE,
ZERO,
ZERO,
ONE,
// previous output
Felt::ZERO,
ZERO,
// assert a false output
Felt::new(1337),
];
Expand All @@ -82,15 +82,15 @@ fn output_aggregation_and() {
// b
Felt::new(157),
// decomposition of a
Felt::ONE,
Felt::ONE,
Felt::ZERO,
Felt::ZERO,
ONE,
ONE,
ZERO,
ZERO,
// decomposition of b
Felt::ONE,
Felt::ZERO,
Felt::ONE,
Felt::ONE,
ONE,
ZERO,
ONE,
ONE,
// previous output
Felt::new(1337),
// output
Expand All @@ -104,7 +104,7 @@ fn output_aggregation_and() {
let result = get_constraint_evaluation(frame, cycle_row);

// expect a failure for the output aggregation constraint (the last one)
assert_ne!(Felt::ZERO, result[NUM_CONSTRAINTS - 1]);
assert_ne!(ZERO, result[NUM_CONSTRAINTS - 1]);
}

// RANDOMIZED TESTS
Expand All @@ -115,7 +115,7 @@ proptest! {
/// compute the bitwise AND operation.
#[test]
fn test_bitwise_and(a in any::<u32>(), b in any::<u32>(), cycle_row in 0..(OP_CYCLE_LEN - 1)) {
let expected = [Felt::ZERO; NUM_CONSTRAINTS];
let expected = [ZERO; NUM_CONSTRAINTS];
let frame = get_test_frame(BITWISE_AND, a, b, cycle_row);
let result = get_constraint_evaluation(frame, cycle_row);
assert_eq!(expected, result);
Expand All @@ -125,7 +125,7 @@ proptest! {
/// compute the bitwise XOR operation.
#[test]
fn test_bitwise_xor(a in any::<u32>(), b in any::<u32>(), cycle_row in 0..(OP_CYCLE_LEN - 1)) {
let expected = [Felt::ZERO; NUM_CONSTRAINTS];
let expected = [ZERO; NUM_CONSTRAINTS];
let frame = get_test_frame(BITWISE_XOR, a, b, cycle_row);
let result = get_constraint_evaluation(frame, cycle_row);
assert_eq!(expected, result);
Expand All @@ -139,9 +139,9 @@ proptest! {
/// specified row.
fn get_constraint_evaluation(frame: EvaluationFrame<Felt>, row: usize) -> [Felt; NUM_CONSTRAINTS] {
let periodic_values = get_periodic_values(row);
let mut result = [Felt::ZERO; NUM_CONSTRAINTS];
let mut result = [ZERO; NUM_CONSTRAINTS];

enforce_constraints(&frame, &periodic_values, &mut result, Felt::ONE);
enforce_constraints(&frame, &periodic_values, &mut result, ONE);

result
}
Expand All @@ -165,8 +165,8 @@ pub fn get_test_frame(
);

// Initialize the rows.
let mut current = vec![Felt::ZERO; TRACE_WIDTH];
let mut next = vec![Felt::ZERO; TRACE_WIDTH];
let mut current = vec![ZERO; TRACE_WIDTH];
let mut next = vec![ZERO; TRACE_WIDTH];

// Set the operation selectors.
current[BITWISE_SELECTOR_COL_IDX] = operation;
Expand All @@ -183,7 +183,7 @@ pub fn get_test_frame(

// Set the previous output.
let output_prev = if cycle_row_num == 0 {
Felt::ZERO
ZERO
} else {
Felt::new((result >> previous_shift) as u64)
};
Expand Down Expand Up @@ -217,8 +217,8 @@ pub fn get_test_frame_with_two_ops(
);

// Initialize the rows.
let mut current = vec![Felt::ZERO; TRACE_WIDTH];
let mut next = vec![Felt::ZERO; TRACE_WIDTH];
let mut current = vec![ZERO; TRACE_WIDTH];
let mut next = vec![ZERO; TRACE_WIDTH];

// Set the operation selector.
current[BITWISE_SELECTOR_COL_IDX] = op_current;
Expand All @@ -235,7 +235,7 @@ pub fn get_test_frame_with_two_ops(

// Set the previous output.
let output_prev = if cycle_row_num == 0 {
Felt::ZERO
ZERO
} else {
Felt::new((result_op_current >> previous_shift) as u64)
};
Expand Down
40 changes: 8 additions & 32 deletions air/src/constraints/chiplets/hasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::trace::chiplets::{
},
HASHER_NODE_INDEX_COL_IDX, HASHER_SELECTOR_COL_RANGE, HASHER_STATE_COL_RANGE,
};
use crate::utils::{are_equal, binary_not, is_binary, EvaluationResult};
use crate::{
utils::{are_equal, binary_not, is_binary, EvaluationResult},
ONE, ZERO,
};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -517,40 +520,13 @@ impl<E: FieldElement> EvaluationFrameExt<E> for &EvaluationFrame<E> {
// ================================================================================================

/// Periodic column mask used to indicate the last row of a cycle.
pub const HASH_K0_MASK: [Felt; HASH_CYCLE_LEN] = [
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ONE,
];
pub const HASH_K0_MASK: [Felt; HASH_CYCLE_LEN] = [ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ONE];

/// Periodic column mask used to indicate when the next row will be the last row of a cycle.
pub const HASH_K1_MASK: [Felt; HASH_CYCLE_LEN] = [
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ONE,
Felt::ZERO,
];
pub const HASH_K1_MASK: [Felt; HASH_CYCLE_LEN] = [ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ONE, ZERO];

/// Periodic column mask used to identify the first row of a cycle.
pub const HASH_K2_MASK: [Felt; HASH_CYCLE_LEN] = [
Felt::ONE,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
Felt::ZERO,
];
pub const HASH_K2_MASK: [Felt; HASH_CYCLE_LEN] = [ONE, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO, ZERO];

// ROUND CONSTANTS
// ================================================================================================
Expand All @@ -559,7 +535,7 @@ pub const HASH_K2_MASK: [Felt; HASH_CYCLE_LEN] = [
pub fn get_round_constants() -> Vec<Vec<Felt>> {
let mut constants = Vec::new();
for _ in 0..(STATE_WIDTH * 2) {
constants.push(vec![Felt::ZERO; HASH_CYCLE_LEN]);
constants.push(vec![ZERO; HASH_CYCLE_LEN]);
}

#[allow(clippy::needless_range_loop)]
Expand Down
30 changes: 15 additions & 15 deletions air/src/constraints/chiplets/hasher/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{
enforce_constraints, Hasher, HASHER_NODE_INDEX_COL_IDX, HASHER_SELECTOR_COL_RANGE,
HASHER_STATE_COL_RANGE, NUM_CONSTRAINTS,
HASHER_STATE_COL_RANGE, NUM_CONSTRAINTS, ONE, ZERO,
};
use crate::{
trace::chiplets::hasher::{Selectors, LINEAR_HASH, STATE_WIDTH},
Felt, FieldElement, TRACE_WIDTH,
Felt, TRACE_WIDTH,
};
use rand_utils::rand_array;
use vm_core::{chiplets::hasher::apply_round, utils::collections::Vec};
Expand All @@ -17,10 +17,10 @@ use winter_air::EvaluationFrame;
/// eight, and applies a round of the VM's native hash function.
#[test]
fn hash_round() {
let expected = [Felt::ZERO; NUM_CONSTRAINTS];
let expected = [ZERO; NUM_CONSTRAINTS];

let cycle_row_num: usize = 3;
let current_selectors = [Felt::ZERO, LINEAR_HASH[1], LINEAR_HASH[2]];
let current_selectors = [ZERO, LINEAR_HASH[1], LINEAR_HASH[2]];
let next_selectors = current_selectors;

let frame = get_test_hashing_frame(current_selectors, next_selectors, cycle_row_num);
Expand All @@ -37,10 +37,10 @@ fn get_constraint_evaluation(
frame: EvaluationFrame<Felt>,
cycle_row_num: usize,
) -> [Felt; NUM_CONSTRAINTS] {
let mut result = [Felt::ZERO; NUM_CONSTRAINTS];
let mut result = [ZERO; NUM_CONSTRAINTS];
let periodic_values = get_test_periodic_values(cycle_row_num);

enforce_constraints(&frame, &periodic_values, &mut result, Felt::ONE);
enforce_constraints(&frame, &periodic_values, &mut result, ONE);

result
}
Expand All @@ -49,15 +49,15 @@ fn get_constraint_evaluation(
fn get_test_periodic_values(cycle_row: usize) -> Vec<Felt> {
// Set the periodic column values.
let mut periodic_values = match cycle_row {
0 => vec![Felt::ZERO, Felt::ZERO, Felt::ONE],
7 => vec![Felt::ZERO, Felt::ONE, Felt::ZERO],
8 => vec![Felt::ONE, Felt::ZERO, Felt::ZERO],
_ => vec![Felt::ZERO, Felt::ZERO, Felt::ZERO],
0 => vec![ZERO, ZERO, ONE],
7 => vec![ZERO, ONE, ZERO],
8 => vec![ONE, ZERO, ZERO],
_ => vec![ZERO, ZERO, ZERO],
};

// Add the RPO round constants for the first 7 rows of the cycle, or pad with zeros.
if cycle_row == 7 {
periodic_values.resize(periodic_values.len() + STATE_WIDTH * 2, Felt::ZERO);
periodic_values.resize(periodic_values.len() + STATE_WIDTH * 2, ZERO);
} else {
periodic_values.extend_from_slice(&Hasher::ARK1[cycle_row]);
periodic_values.extend_from_slice(&Hasher::ARK2[cycle_row]);
Expand All @@ -71,8 +71,8 @@ fn get_test_hashing_frame(
next_selectors: Selectors,
cycle_row_num: usize,
) -> EvaluationFrame<Felt> {
let mut current = vec![Felt::ZERO; TRACE_WIDTH];
let mut next = vec![Felt::ZERO; TRACE_WIDTH];
let mut current = vec![ZERO; TRACE_WIDTH];
let mut next = vec![ZERO; TRACE_WIDTH];

// Set the selectors for the hash operation.
current[HASHER_SELECTOR_COL_RANGE].copy_from_slice(&current_selectors);
Expand All @@ -87,8 +87,8 @@ fn get_test_hashing_frame(
next[HASHER_STATE_COL_RANGE].copy_from_slice(&state);

// Set the node index values to zero for hash computations.
current[HASHER_NODE_INDEX_COL_IDX] = Felt::ZERO;
next[HASHER_NODE_INDEX_COL_IDX] = Felt::ZERO;
current[HASHER_NODE_INDEX_COL_IDX] = ZERO;
next[HASHER_NODE_INDEX_COL_IDX] = ZERO;

EvaluationFrame::from_rows(current, next)
}
Loading

0 comments on commit 3e6239e

Please sign in to comment.