Skip to content

Commit

Permalink
Add test of fingerprint() function (#1996)
Browse files Browse the repository at this point in the history
This will make it easier to verify refactorings like #1985.

---------

Co-authored-by: chriseth <[email protected]>
  • Loading branch information
georgwiese and chriseth authored Oct 30, 2024
1 parent 5a18f3e commit f3f8c60
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pipeline/tests/powdr_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ fn fp4() {
evaluate_function(&analyzed, "std::math::fp4::test::inverse", vec![]);
}

#[test]
fn fingerprint() {
let analyzed = std_analyzed::<GoldilocksField>();
evaluate_function(
&analyzed,
"std::protocols::fingerprint::test::test_fingerprint",
vec![],
);
}

#[test]
fn sort() {
let test_inputs = vec![
Expand Down
42 changes: 41 additions & 1 deletion std/protocols/fingerprint.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,44 @@ let<T: Add + Mul + FromLiteral> fingerprint: T[], Fp2<T> -> Fp2<T> = |expr_array
};

/// Maps [id, x_1, x_2, ..., x_n] to its Read-Solomon fingerprint, using a challenge alpha: $\sum_{i=1}^n alpha**{(n - i)} * x_i$
let<T: Add + Mul + FromLiteral> fingerprint_with_id: T, T[], Fp2<T> -> Fp2<T> = |id, expr_array, alpha| fingerprint([id] + expr_array, alpha);
let<T: Add + Mul + FromLiteral> fingerprint_with_id: T, T[], Fp2<T> -> Fp2<T> = |id, expr_array, alpha| fingerprint([id] + expr_array, alpha);

mod test {
use super::fingerprint;
use std::check::assert;
use std::math::fp2::Fp2;
use std::math::fp2::from_base;

/// Helper function to assert that the fingerprint of a tuple is equal to the expected value.
/// We are working on integers here, wrapping them as Fp2 elements.
let assert_fingerprint_equal: int[], int, int -> () = |tuple, challenge, expected| {
let result = fingerprint(tuple, from_base(challenge));
match result {
Fp2::Fp2(actual, should_be_zero) => {
assert(should_be_zero == 0, || "Returned an extension field element");
assert(expected == actual, || "expected != actual");
}
}
};

let test_fingerprint = || {
// A tuple t of size n with challenge x should be mapped to:
// t[0] * x**(n-1) + t[1] * x**(n-2) + ... + t[n-2] * x + t[n-1]

// For lists of length one, the fingerprint is the element itself
assert_fingerprint_equal([123], 0, 123);
assert_fingerprint_equal([123], 1234, 123);
assert_fingerprint_equal([123], -4, 123);

// For a list [a, b] of length two, the fingerprint is a * x + b
assert_fingerprint_equal([123, 456], 0, 456);
assert_fingerprint_equal([123, 456], 1, 123 + 456);
assert_fingerprint_equal([123, 456], 2, 123 * 2 + 456);
assert_fingerprint_equal([123, 456], -1, -123 + 456);

// For a list [a, b, c] of length three, the fingerprint is a * x**2 + b * x + c
assert_fingerprint_equal([123, 456, 789], 0, 789);
assert_fingerprint_equal([123, 456, 789], 1, 123 + 456 + 789);
assert_fingerprint_equal([123, 456, 789], 2, 123 * 4 + 456 * 2 + 789);
};
}

0 comments on commit f3f8c60

Please sign in to comment.