Skip to content

Commit 7b927ec

Browse files
authored
Add PIL extension field unit test (#2431)
1 parent 927a87b commit 7b927ec

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

std/math/extension_field.asm

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,66 @@ let<T> from_array: T[] -> Ext<T> = |arr| match len(arr) {
8484
_ => panic("Expected 1, 2, or 4")
8585
};
8686

87+
let eq_ext: Ext<fe>, Ext<fe> -> bool = |a, b| match (a, b) {
88+
(Ext::Fp(aa), Ext::Fp(bb)) => aa == bb,
89+
(Ext::Fp2(aa), Ext::Fp2(bb)) => std::math::fp2::eq_ext(aa, bb),
90+
(Ext::Fp4(aa), Ext::Fp4(bb)) => std::math::fp4::eq_ext(aa, bb),
91+
_ => panic("Operands have different types")
92+
};
93+
8794
let constrain_eq_ext: Ext<expr>, Ext<expr> -> Constr[] = |a, b| match (a, b) {
8895
(Ext::Fp(aa), Ext::Fp(bb)) => [aa = bb],
8996
(Ext::Fp2(aa), Ext::Fp2(bb)) => std::math::fp2::constrain_eq_ext(aa, bb),
9097
(Ext::Fp4(aa), Ext::Fp4(bb)) => std::math::fp4::constrain_eq_ext(aa, bb),
9198
_ => panic("Operands have different types")
92-
};
99+
};
100+
101+
102+
mod test {
103+
use super::Ext;
104+
use super::from_base;
105+
use super::add_ext;
106+
use super::sub_ext;
107+
use super::mul_ext;
108+
use super::inv_ext;
109+
use super::eq_ext;
110+
use super::from_array;
111+
use super::unpack_ext_array;
112+
use std::check::assert;
113+
use std::array;
114+
use std::math::fp2::Fp2;
115+
use std::math::fp4::Fp4;
116+
117+
let run_test = query |a, x| {
118+
let dimensions = array::len(unpack_ext_array(x));
119+
// Don't use from_base, because then it depends on the field on which we run.
120+
let a_ext = from_array([a] + array::new(dimensions - 1, |i| 0));
121+
122+
// Assert that (a + x) * x / x - x == a
123+
assert(
124+
eq_ext(
125+
sub_ext(
126+
mul_ext(
127+
mul_ext(
128+
add_ext(
129+
a_ext,
130+
x
131+
),
132+
x
133+
),
134+
inv_ext(x)
135+
),
136+
x
137+
),
138+
a_ext
139+
),
140+
|| "(a + x) * x / x - x != a"
141+
);
142+
};
143+
144+
let test_all_extension_fields = query || {
145+
run_test(42, Ext::Fp(123));
146+
run_test(42, Ext::Fp2(Fp2::Fp2(123, 456)));
147+
run_test(42, Ext::Fp4(Fp4::Fp4(123, 456, 789, 150)));
148+
};
149+
}

0 commit comments

Comments
 (0)