Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify batch_ops() #1492

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions core/src/mast/node/basic_block_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use core::fmt;

use miden_crypto::{hash::rpo::RpoDigest, Felt, ZERO};
use miden_formatting::prettier::PrettyPrint;
use winter_utils::flatten_slice_elements;

use crate::{
chiplets::hasher, mast::MastForestError, Decorator, DecoratorIterator, DecoratorList, Operation,
Expand Down Expand Up @@ -110,7 +109,7 @@ impl BasicBlockNode {
digest: RpoDigest,
) -> Self {
assert!(!operations.is_empty());
let (op_batches, _) = batch_ops(operations);
let op_batches = batch_ops(operations);
Self { op_batches, digest, decorators }
}
}
Expand Down Expand Up @@ -315,22 +314,20 @@ impl<'a> Iterator for OperationOrDecoratorIterator<'a> {
/// Groups the provided operations into batches and computes the hash of the block.
fn batch_and_hash_ops(ops: Vec<Operation>) -> (Vec<OpBatch>, RpoDigest) {
// Group the operations into batches.
let (batches, batch_groups) = batch_ops(ops);
let batches = batch_ops(ops);

// Compute the hash of all operation groups.
let op_groups = &flatten_slice_elements(&batch_groups);
let hash = hasher::hash_elements(op_groups);
let op_groups: Vec<Felt> = batches.iter().flat_map(|batch| batch.groups).collect();
let hash = hasher::hash_elements(&op_groups);

(batches, hash)
}

/// Groups the provided operations into batches as described in the docs for this module (i.e.,
/// up to 9 operations per group, and 8 groups per batch).
/// Returns a list of operation batches and a list of operation groups.
fn batch_ops(ops: Vec<Operation>) -> (Vec<OpBatch>, Vec<[Felt; BATCH_SIZE]>) {
/// Groups the provided operations into batches as described in the docs for this module (i.e., up
/// to 9 operations per group, and 8 groups per batch).
fn batch_ops(ops: Vec<Operation>) -> Vec<OpBatch> {
let mut batches = Vec::<OpBatch>::new();
let mut batch_acc = OpBatchAccumulator::new();
let mut batch_groups = Vec::<[Felt; BATCH_SIZE]>::new();

for op in ops {
// If the operation cannot be accepted into the current accumulator, add the contents of
Expand All @@ -339,7 +336,6 @@ fn batch_ops(ops: Vec<Operation>) -> (Vec<OpBatch>, Vec<[Felt; BATCH_SIZE]>) {
let batch = batch_acc.into_batch();
batch_acc = OpBatchAccumulator::new();

batch_groups.push(*batch.groups());
batches.push(batch);
}

Expand All @@ -350,10 +346,10 @@ fn batch_ops(ops: Vec<Operation>) -> (Vec<OpBatch>, Vec<[Felt; BATCH_SIZE]>) {
// Make sure we finished processing the last batch.
if !batch_acc.is_empty() {
let batch = batch_acc.into_batch();
batch_groups.push(*batch.groups());
batches.push(batch);
}
(batches, batch_groups)

batches
}

/// Checks if a given decorators list is valid (only checked in debug mode)
Expand Down
Loading