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

[FIX] m31 exp #96

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Cargo.lock
# Project data
notes.md

# Generated by SageMath
*.sage.py

# Programming env
.vscode/
perf.data
Expand Down
2 changes: 1 addition & 1 deletion arith/src/field/m31.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Field for M31 {
while !e.is_zero() {
let b = e & 1;
if b == 1 {
res *= self;
res *= t;
}
t = t * t;
e >>= 1;
Expand Down
14 changes: 13 additions & 1 deletion arith/src/tests/m31.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::Cursor;

use ark_std::test_rng;

use crate::{FieldSerde, M31x16, M31};
use crate::{Field, FieldSerde, M31x16, M31};

use super::{
field::{random_field_tests, random_inversion_tests},
Expand Down Expand Up @@ -32,3 +32,15 @@ fn test_custom_serde_vectorize_m31() {
let b = b.unwrap();
assert_eq!(a, b);
}

/// Compare to test vectors generated in SageMath
#[test]
fn test_vectors() {
// M31 inversion
let a = M31::from(3);
let a_inv = M31::from(1431655765);
assert_eq!(a_inv, a.inv().unwrap());
// M31 exponentiation
let a_pow_11 = M31::from(177147);
assert_eq!(a_pow_11, a.exp(11));
}
33 changes: 33 additions & 0 deletions arith/src/tests/m31_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,36 @@ fn test_field() {
random_extension_field_tests::<M31Ext3x16>("Simd M31 Ext3".to_string());
random_simd_field_tests::<M31Ext3x16>("Simd M31 Ext3".to_string());
}

/// Compare to test vectors generated in SageMath
#[test]
fn test_vectors() {
use crate::{Field, M31};
let a = M31Ext3 {
v: [M31::from(1), M31::from(2), M31::from(3)],
};
let b = M31Ext3 {
v: [M31::from(4), M31::from(5), M31::from(6)],
};
let expected_prod = M31Ext3 {
v: [M31::from(139), M31::from(103), M31::from(28)],
};
assert_eq!(expected_prod, a * b);

let expected_inv = M31Ext3 {
v: [
M31::from(1279570927),
M31::from(2027416670),
M31::from(696388467),
],
};
assert_eq!(expected_inv, a.inv().unwrap());
let a_pow_11 = M31Ext3 {
v: [
M31::from(2145691179),
M31::from(1848238717),
M31::from(1954563431),
],
};
assert_eq!(a_pow_11, a.exp(11));
}
24 changes: 24 additions & 0 deletions arith/src/tests/test_vectors.sage
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generate test vectors for the field implementations using SageMath
# Usage: sage test_vectors.sage

# M31 Field
p = 2**31 - 1
print("M31 Field")
print(f"p = {p}")
F = GF(p)
a = F(3)
print(f"a = {a}")
print(f"a^(-1) = {a^(-1)}")
print(f"a^(11) = {a^(11)}")

# Degree 3 extension
R.<x> = F[]
K.<a> = F.extension(x^3 - 5)
print("M31 Degree 3 Extension")
b = 1 + 2*a + 3*a^2
c = 4 + 5*a + 6*a^2
print(f"b = {b}")
print(f"c = {c}")
print(f"b*c = {b*c}")
print(f"b^(-1) = {b^(-1)}")
print(f"b^(11) = {b^(11)}")
Loading