Skip to content

Commit e46bf85

Browse files
committed
add debug_asserts, fix comments, remove dead code, add tests
1 parent 07de24c commit e46bf85

3 files changed

Lines changed: 59 additions & 18 deletions

File tree

crypto/math/src/field/extensions_goldilocks.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ impl crate::traits::WriteBytes for FieldElement<Degree3GoldilocksExtensionField>
528528
const BYTE_LEN: usize = 24; // 3 × 8 bytes
529529
#[inline(always)]
530530
fn write_bytes_be(&self, buf: &mut [u8]) {
531+
debug_assert!(buf.len() >= 24);
531532
let components = self.value();
532533
components[0].write_bytes_be(&mut buf[0..8]);
533534
components[1].write_bytes_be(&mut buf[8..16]);
@@ -555,6 +556,30 @@ impl HasDefaultTranscript for Degree3GoldilocksExtensionField {
555556
}
556557
}
557558

559+
#[cfg(test)]
560+
mod tests {
561+
use super::*;
562+
use crate::traits::WriteBytes;
563+
564+
#[test]
565+
fn write_bytes_be_matches_as_bytes() {
566+
let cases = [
567+
FieldElement::<Degree3GoldilocksExtensionField>::zero(),
568+
FieldElement::<Degree3GoldilocksExtensionField>::one(),
569+
FieldElement::<Degree3GoldilocksExtensionField>::new([
570+
FpE::from(1u64),
571+
FpE::from(2u64),
572+
FpE::from(3u64),
573+
]),
574+
];
575+
for elem in &cases {
576+
let mut buf = [0u8; 24];
577+
elem.write_bytes_be(&mut buf);
578+
assert_eq!(&buf[..], elem.as_bytes().as_slice());
579+
}
580+
}
581+
}
582+
558583
// =====================================================
559584
// HELPER FUNCTIONS
560585
// =====================================================

crypto/math/src/field/goldilocks.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ impl crate::traits::WriteBytes for FieldElement<GoldilocksField> {
486486
const BYTE_LEN: usize = 8;
487487
#[inline(always)]
488488
fn write_bytes_be(&self, buf: &mut [u8]) {
489+
debug_assert!(buf.len() >= 8);
489490
buf[..8].copy_from_slice(&self.canonical_u64().to_be_bytes());
490491
}
491492
}
@@ -551,3 +552,32 @@ impl HasDefaultTranscript for GoldilocksField {
551552
}
552553
}
553554
}
555+
556+
#[cfg(test)]
557+
mod tests {
558+
use super::*;
559+
use crate::traits::WriteBytes;
560+
561+
#[test]
562+
fn write_bytes_be_matches_as_bytes() {
563+
let cases = [
564+
FieldElement::<GoldilocksField>::from(0u64),
565+
FieldElement::<GoldilocksField>::from(1u64),
566+
FieldElement::<GoldilocksField>::from(GOLDILOCKS_PRIME - 1),
567+
];
568+
for elem in &cases {
569+
let mut buf = [0u8; 8];
570+
elem.write_bytes_be(&mut buf);
571+
assert_eq!(&buf[..], elem.as_bytes().as_slice());
572+
}
573+
}
574+
575+
#[test]
576+
fn write_bytes_be_matches_as_bytes_noncanonical() {
577+
// Values stored as-is via from_raw are non-canonical (>= p) until serialized.
578+
let elem = FieldElement::<GoldilocksField>::from_raw(GOLDILOCKS_PRIME + 5);
579+
let mut buf = [0u8; 8];
580+
elem.write_bytes_be(&mut buf);
581+
assert_eq!(&buf[..], elem.as_bytes().as_slice());
582+
}
583+
}

crypto/stark/src/prover.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,6 @@ pub trait IsStarkProver<
358358
FieldElement<Field>: math::traits::WriteBytes,
359359
FieldElement<FieldExtension>: math::traits::WriteBytes,
360360
{
361-
/// Returns the Merkle tree and the commitment to the vectors `vectors`.
362-
fn batch_commit_extension(
363-
vectors: &[Vec<FieldElement<FieldExtension>>],
364-
) -> Option<(BatchedMerkleTree<FieldExtension>, Commitment)>
365-
where
366-
FieldElement<Field>: AsBytes + Sync + Send,
367-
FieldElement<FieldExtension>: AsBytes + Sync + Send,
368-
{
369-
let tree = BatchedMerkleTree::build(vectors)?;
370-
371-
let commitment = tree.root;
372-
Some((tree, commitment))
373-
}
374-
375361
/// Builds a Merkle tree commitment from column-major LDE evaluations with
376362
/// bit-reverse permutation, without cloning the full evaluation matrix.
377363
///
@@ -402,8 +388,8 @@ pub trait IsStarkProver<
402388
#[cfg(not(feature = "parallel"))]
403389
let iter = 0..num_rows;
404390

405-
// Zero per-element heap allocations: write bytes directly from columns
406-
// into a per-row buffer, then hash once.
391+
// One allocation per row (was one per field element): write all columns
392+
// into a single buffer, then hash once.
407393
let hashed_leaves: Vec<Commitment> = iter
408394
.map(|row_idx| {
409395
let br_idx = reverse_index(row_idx, num_rows as u64);
@@ -727,8 +713,8 @@ pub trait IsStarkProver<
727713

728714
let byte_len = <FieldElement<FieldExtension> as WriteBytes>::BYTE_LEN;
729715

730-
// Zero per-element allocations: write bytes directly from column data.
731-
// Each leaf = row_pair[2*i] ++ row_pair[2*i+1] after bit-reverse.
716+
// One allocation per leaf (was one per field element): write all parts
717+
// into a single buffer. Each leaf = row_pair[2*i] ++ row_pair[2*i+1] after bit-reverse.
732718
#[cfg(feature = "parallel")]
733719
let iter = (0..num_leaves).into_par_iter();
734720
#[cfg(not(feature = "parallel"))]

0 commit comments

Comments
 (0)