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

Add std::math::fp4::square_ext #1990

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions std/math/fp4.asm
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ let<T: Add + FromLiteral + Mul> mul_ext: Fp4<T>, Fp4<T> -> Fp4<T> = |a, b| match
)
};

/// Extension field squaring
/// This implementation yields a shorter expression than `mul_ext(a, a)` and should be preferred
/// when squaring expressions many times.
let<T: Add + FromLiteral + Mul> square_ext: Fp4<T> -> Fp4<T> = |a| match a {
Fp4::Fp4(a0, a1, a2, a3) => Fp4::Fp4(
a0 * a0 + 11 * (2 * a1 * a3 + a2 * a2),
2 * (a0 * a1 + 11 * a2 * a3),
2 * a0 * a2 + a1 * a1 + 11 * (a3 * a3),
2 * (a0 * a3 + a1 * a2)
)
};

/// Inversion for an Fp4 element
/// The inverse of (a0, a1, a2, a3) is a point (b0, b1, b2, b3) such that:
/// (a0 + a1 * x + a2 * x^2 + a3 * x^3) (b0 + b1 * x + b2 * x^2 + b3 * x^3) = 1 (mod x^4 - 11)
Expand Down Expand Up @@ -141,6 +153,7 @@ mod test {
use super::add_ext;
use super::sub_ext;
use super::mul_ext;
use super::square_ext;
use super::inv_ext;
use super::eq_ext;
use std::check::assert;
Expand Down Expand Up @@ -197,6 +210,18 @@ mod test {
test_mul(Fp4::Fp4(-1, -2, -3, -4), Fp4::Fp4(-3, 4, 4, 5), Fp4::Fp4(-415, -339, -223, -13));
};

let test_square = || {
// Tests consistency with mul_ext
let test_square = |a| assert(eq_ext(mul_ext(a, a), square_ext(a)), || "Wrong squaring result");

test_square(from_base(0));
test_square(from_base(1));
test_square(from_base(2));
test_square(Fp4::Fp4(1, 1, 1, 1));
test_square(Fp4::Fp4(123, 1234, 12, 12345));
test_square(Fp4::Fp4(-1, -2, -3, -4));
};

let test_inverse = || {
let test_elements = [
from_base(1),
Expand Down