TritonVM / tasm-lib

A collection of functions written in Triton VM assembly (tasm)
Apache License 2.0
11 stars 2 forks source link

Flaky test `hashing::algebraic_hasher::sample_scalars_static_length_kmalloc::tests::verify_agreement_with_tip5_sample_scalars` #132

Closed Sword-Smith closed 2 months ago

Sword-Smith commented 2 months ago
thread 'hashing::algebraic_hasher::sample_scalars_static_length_kmalloc::tests::verify_agreement_with_tip5_sample_scalars' panicked at tasm-lib/src/library.rs:135:34:
attempt to subtract with overflow
thread 'hashing::algebraic_hasher::sample_scalars_static_length_kmalloc::tests::verify_agreement_with_tip5_sample_scalars' panicked at tasm-lib/src/library.rs:135:34:
attempt to subtract with overflow
thread 'hashing::algebraic_hasher::sample_scalars_static_length_kmalloc::tests::verify_agreement_with_tip5_sample_scalars' panicked at tasm-lib/src/library.rs:135:34:
attempt to subtract with overflow
thread 'hashing::algebraic_hasher::sample_scalars_static_length_kmalloc::tests::verify_agreement_with_tip5_sample_scalars' panicked at tasm-lib/src/library.rs:135:34:
attempt to subtract with overflow
thread 'hashing::algebraic_hasher::sample_scalars_static_length_kmalloc::tests::verify_agreement_with_tip5_sample_scalars' panicked at tasm-lib/src/hashing/algebraic_hasher/sample_scalars_static_length_kmalloc.rs:183:5:
Test failed: attempt to subtract with overflow.
minimal failing input: input = _VerifyAgreementWithTip5SampleScalarsArgs {
    num_elements_to_sample: 0,
    extra_capacity: 0,
    sponge: Tip5 {
        state: [
            BFieldElement(
                0,
            ),
            BFieldElement(
Sword-Smith commented 2 months ago

This test crashes. Narrows down the problem.

#[test]
    fn verify_agreement_with_tip5_sample_scalars_zero_zero() {
        let snippet = SampleScalarsStaticLengthKMalloc {
            num_elements_to_sample: 0,
            extra_capacity: 0,
        };
        let init_stack = snippet.init_stack_for_isolated_run();
        let mut sponge: Tip5 = Sponge::init();
        let tasm = tasm_final_state(
            &ShadowedProcedure::new(snippet),
            &init_stack,
            &[],
            NonDeterminism::default(),
            &Some(sponge.clone()),
        );

        let scalar_pointer = snippet.k_malloc_address_isolated_run();
        let read_scalar = |i| array_get(scalar_pointer, i, &tasm.ram, EXTENSION_DEGREE);

        let scalars_from_tip5 = sponge.sample_scalars(0);
        for (i, expected_scalar) in scalars_from_tip5.into_iter().enumerate() {
            assert_eq!(expected_scalar.coefficients.to_vec(), read_scalar(i));
        }
    }
Sword-Smith commented 2 months ago

The problem happens in kmalloc because it's called with an argument value of 0. That probably should be allowed TBH. Let's just add an assert that kmalloc is not called with an argument of 0.

    pub fn kmalloc(&mut self, num_words: u32) -> BFieldElement {
        let address = STATIC_MEMORY_FIRST_ADDRESS
            - bfe!(self.num_allocated_words)
            - BFieldElement::new(num_words as u64 - 1);
        self.num_allocated_words = self
            .num_allocated_words
            .checked_add(num_words)
            .expect("Cannot allocate more that u32::MAX words through `kmalloc`.");

        address
    }