aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-06-24 08:46:01 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-06-24 08:46:01 +0200
commit9747206e7a0a08f6dc8c92730f0780e1b66cb23e (patch)
tree2b85dfdabcb39ebeea50e7eb551d8cb66335ac8f
parent4b5936586e570b125b1c68d04b1479262af9c98b (diff)
Implement Polynomial Extension Fields
Notably, only `Fp[X]/1+x^2`, or `Fp2` is tested at the moment. More tests for `Fp12` coming right up!
-rw-r--r--docs/journal/pages/field-extension.md27
-rw-r--r--lib/field.ml38
-rw-r--r--lib/fields.ml216
-rw-r--r--lib/fields.mli80
-rw-r--r--test/test_fp.ml104
-rw-r--r--test/test_fp2.ml141
-rw-r--r--test/test_plonkers.ml3
7 files changed, 509 insertions, 100 deletions
diff --git a/docs/journal/pages/field-extension.md b/docs/journal/pages/field-extension.md
index 6494df5..f1ff95d 100644
--- a/docs/journal/pages/field-extension.md
+++ b/docs/journal/pages/field-extension.md
@@ -1,11 +1,34 @@
> [Index](../README.md)
-# Title
+# Polynomial Extension Rings
-Body.
+The other kind of field that is important for elliptic curve pairings are polynomial extension fields. These are just polynomial quotient rings over the curve's field modulo some irreducible monic polynomial (chosen as a parameter of the extension field). This concept is a _little_ complicated, and the Wikipedia article linked in the resources section does a decent job at explainng it (at least better than I can, as I lack a lot of the jargon required).
+
+To try an explain it better, lets take the polynomial extension field of degree 2 used in BN254 pairings. This is defined as the polynomial quotient ring `Fp[x] / (1 + x^2)`, or `Fp2` for brevity, where `Fp[x]` are polynomials over the field `Fp`. With a slightly more concrete example, imagine the polynomials `a + bx` and `c + dx` belonging to `Fp2` where `a`, `b`, `c`, `d` are elements of `Fp` and `x` is some indeterminate also belonging to `Fp`. For this field, addition and multiplication are just definied to be the addition and multiplication modulo `1 + x^2` respectively of the polynomials.
+
+- Addition: `(a + bx) + (c + dx) = (a + c) + (b + d)x`. Since we don't create any `x^2` terms, this is already non-divisible by `1 + x^2`. Addition in these polynomial extension fields are, therefore, very straight forward.
+- Multiplication: `(a + bx) * (c + dx) = ac + adx + bcx + bdx^2`. This must be taken modulo `1 + x^2`, so `ac + adx + bcx + bdx^2 =~ ac + adx + bcx + bdx^2 - bd(1 + x^2) =~ (ac - bd) + (ad + bc)x`.
+
+Additionally, in code we can represent the elements of `Fp2` as a tuple of two `Fp` elements `Fp.t * Fp.t` where `(a, b)` represents `a + bx`.
+
+This same concept can be extended to polynomial extension fields of any degree, and in particular a polynomial extension field of degree 12 is also used for elliptic curve pairings (but with a different irreducible monic polynomial):
+
+- Addition: this can be trivially extended to component-wise adding the coefficients of the polynomials together.
+- Multiplication: this is a little more complicated, but the general idea is:
+ 1. You first multiply out the polynomials and get a result of degree `2d - 1` where `d` is the degree of the polynomial extension field.
+ 2. Starting from the highest degree term, you subtract multiples of the irreducible polynomial until you have a polynomial of degree `d - 1` or less. Since the irreducible polynomial monic, in each iteration you subtract the component-wise multiplication of the `i`th degree term from the polynomial computed in step 1.
+
+Computing additive and multiplicative inverses are done by:
+
+- Additive inverse: this is just the component-wise additive inverse of the polynomial coeficients: `-(a + bx) = -a + (-b)x`.
+- Multiplicative inverse: this is a little more complicated, but can be done using the extended euclidean algorithm and Bézout's identity.
+
+Furthermore, the same principles apply for the elliptic curve pairings for the BLS12-381 curve, but with different parameterization.
# Resources
- [Polynomial Ring](https://en.wikipedia.org/wiki/Polynomial_ring)
+- [Extended Euclidean Algorithm](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Simple_algebraic_field_extensions)
+- [Euclidean Division](https://en.wikipedia.org/wiki/Polynomial_greatest_common_divisor#Euclidean_division)
> [Index](../README.md)
diff --git a/lib/field.ml b/lib/field.ml
index 4b6408f..8840f5b 100644
--- a/lib/field.ml
+++ b/lib/field.ml
@@ -1,5 +1,6 @@
(** Finite field. *)
+(** Module signature for a finite field. *)
module type S = sig
type t
(** A type representing an element in the finite field *)
@@ -18,8 +19,8 @@ module type S = sig
[a]. *)
val inv : t -> t
- (** [inv a] is the multiplicative inverse of [a]. [mul a (inv a) = 1] for all
- non-zero [a]. Can raise [Divide_by_zero]. *)
+ (** [inv a] is the multiplicative inverse of [a]. [mul a (inv a) = one] for
+ all non-zero [a]. Furthermore, we define [inv zero = zero]. *)
val add : t -> t -> t
(** [add a b] is the sum of [a] plus [b] *)
@@ -33,7 +34,7 @@ module type S = sig
val div : t -> t -> t
(** [div a b] is the field division of [a] by [b]. It is equivalent to
- [mul a (inv b)]. Can raise [Divide_by_zero]. *)
+ [mul a (inv b)]. Furthermore, we define [div a zero = zero]. *)
val ( ~- ) : t -> t
(** [neg]. *)
@@ -53,3 +54,34 @@ module type S = sig
val ( / ) : t -> t -> t
(** [div]. *)
end
+
+(** Construct a finite field from its definition. *)
+module Make (F : sig
+ type t
+
+ val zero : t
+ val one : t
+ val equal : t -> t -> bool
+ val neg : t -> t
+ val inv : t -> t
+ val add : t -> t -> t
+ val mul : t -> t -> t
+end) : S with type t = F.t = struct
+ type t = F.t
+
+ let zero = F.zero
+ let one = F.one
+ let equal = F.equal
+ let neg = F.neg
+ let inv = F.inv
+ let add = F.add
+ let sub a b = add a (neg b)
+ let mul = F.mul
+ let div a b = mul a (inv b)
+ let ( ~- ) = neg
+ let ( ~/ ) = inv
+ let ( + ) = add
+ let ( - ) = sub
+ let ( * ) = mul
+ let ( / ) = div
+end
diff --git a/lib/fields.ml b/lib/fields.ml
index 2cdbbda..e9a6514 100644
--- a/lib/fields.ml
+++ b/lib/fields.ml
@@ -1,42 +1,182 @@
-let p =
- Z.of_string
- "21888242871839275222246405745257275088696311157297823662689037894645226208583"
-
-let p_minus_2 = Z.(p - ~$2)
-let mod_p x = Z.erem x p
-
-module Fq = struct
- type t = Z.t
-
- let zero = Z.zero
- let one = Z.one
- let equal = Z.equal
- let neg a = Z.neg a |> mod_p
- let inv a = Z.powm a p_minus_2 p
- let add a b = Z.add a b |> mod_p
- let sub a b = add a (neg b)
- let mul a b = Z.mul a b |> mod_p
- let div a b = mul a (inv b)
- let ( ~- ) = neg
- let ( ~/ ) = inv
- let ( + ) = add
- let ( - ) = sub
- let ( * ) = mul
- let ( / ) = div
+module Fp = struct
+ let p =
+ Z.of_string
+ "21888242871839275222246405745257275088696311157297823662689037894645226208583"
+
+ let p_minus_2 = Z.(p - ~$2)
+ let mod_p x = Z.erem x p
+
+ include Field.Make (struct
+ type t = Z.t
+
+ let zero = Z.zero
+ let one = Z.one
+ let equal = Z.equal
+ let neg a = Z.neg a |> mod_p
+ let inv a = Z.powm a p_minus_2 p
+ let add a b = Z.add a b |> mod_p
+ let mul a b = Z.mul a b |> mod_p
+ end)
+
let of_string s = Z.of_string s |> mod_p
let to_string = Z.to_string
end
-module ExtensionField =
-functor
- (Ext : sig
- type t
- end)
- ->
- struct
- type t = Ext.t
- end
-
-module Fq2 = ExtensionField (struct
- type t = Fq.t * Fq.t
-end)
+module FpN (R : sig
+ val p : Fp.t array
+end) =
+struct
+ let degree = Array.length R.p
+
+ include Field.Make (struct
+ type t = Fp.t array
+
+ let zero = Array.make degree Fp.zero
+ let one = Array.init degree (function 0 -> Fp.one | _ -> Fp.zero)
+ let equal = Array.for_all2 Fp.equal
+ let neg a = Array.map Fp.neg a
+ let add a b = Array.map2 Fp.add a b
+
+ let mul a b =
+ let r = Array.make ((degree * 2) - 1) Fp.zero in
+ for i = 0 to degree - 1 do
+ for j = 0 to degree - 1 do
+ r.(i + j) <- Fp.add r.(i + j) (Fp.mul a.(i) b.(j))
+ done
+ done;
+ for i = Array.length r - 1 downto degree do
+ let q = r.(i) in
+ if not (Fp.equal q Fp.zero) then
+ for j = 0 to degree - 1 do
+ let j' = i - degree + j in
+ r.(j') <- Fp.sub r.(j') (Fp.mul q R.p.(j))
+ done
+ else ()
+ done;
+ Array.sub r 0 degree
+
+ let inv a =
+ let deg a =
+ let rec deg' = function
+ | 0 -> 0
+ | i -> if Fp.equal a.(i) Fp.zero then deg' (i - 1) else i
+ in
+ deg' (Array.length a - 1)
+ in
+ let ediv a b =
+ let da = deg a in
+ let db = deg b in
+ let q = Array.make (da + 1) Fp.zero in
+ let r = Array.sub a 0 (da + 1) in
+ let c = b.(db) in
+ for i = da - db downto 0 do
+ let qd = Fp.div r.(db + i) c in
+ q.(i) <- qd;
+ for j = 0 to db do
+ r.(i + j) <- Fp.sub r.(i + j) (Fp.mul qd b.(j))
+ done
+ done;
+ q
+ in
+ let submulq a b q =
+ let r = Array.copy a in
+ for i = 0 to deg b do
+ for j = 0 to deg q do
+ r.(i + j) <- Fp.sub r.(i + j) (Fp.mul b.(i) q.(j))
+ done
+ done;
+ r
+ in
+ let t = ref (Array.append zero [| Fp.zero |]) in
+ let newt = ref (Array.append one [| Fp.zero |]) in
+ let r = ref (Array.append R.p [| Fp.one |]) in
+ let newr = ref (Array.append a [| Fp.zero |]) in
+ while Array.for_all Fp.(equal zero) !newr = false do
+ let q = ediv !r !newr in
+ let newr' = submulq !r !newr q in
+ let newt' = submulq !t !newt q in
+ r := !newr;
+ newr := newr';
+ t := !newt;
+ newt := newt'
+ done;
+ let a0 = Fp.inv !r.(0) in
+ Array.map (Fp.mul a0) (Array.sub !t 0 degree)
+ end)
+
+ let of_fp a = Array.init degree (function 0 -> a | _ -> Fp.zero)
+
+ let of_string s =
+ let a =
+ Scanf.sscanf s "(%[^)])" (String.split_on_char ',')
+ |> List.map String.trim |> List.map Fp.of_string |> Array.of_list
+ in
+ if Array.length a = Array.length R.p then a
+ else invalid_arg "wrong number of elements"
+
+ let to_string a =
+ Array.map Fp.to_string a |> Array.to_list |> String.concat ", "
+ |> Printf.sprintf "(%s)"
+end
+
+module Fp2 = struct
+ include FpN (struct
+ let p = [| Fp.one; Fp.zero |]
+ end)
+
+ type coeffs = Fp.t * Fp.t
+
+ let of_coeffs (c0, c1) = [| c0; c1 |]
+ let to_coeffs a = (a.(0), a.(1))
+end
+
+module Fp12 = struct
+ include FpN (struct
+ let p =
+ [|
+ Fp.of_string "82";
+ Fp.zero;
+ Fp.zero;
+ Fp.zero;
+ Fp.zero;
+ Fp.zero;
+ Fp.of_string "-18";
+ Fp.zero;
+ Fp.zero;
+ Fp.zero;
+ Fp.zero;
+ Fp.zero;
+ |]
+ end)
+
+ type coeffs =
+ Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+
+ let of_coeffs (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11) =
+ [| c0; c1; c2; c3; c4; c5; c6; c7; c8; c9; c10; c11 |]
+
+ let to_coeffs a =
+ ( a.(0),
+ a.(1),
+ a.(2),
+ a.(3),
+ a.(4),
+ a.(5),
+ a.(6),
+ a.(7),
+ a.(8),
+ a.(9),
+ a.(10),
+ a.(11) )
+end
diff --git a/lib/fields.mli b/lib/fields.mli
index 3fc15dc..4ecad01 100644
--- a/lib/fields.mli
+++ b/lib/fields.mli
@@ -1,6 +1,6 @@
(** Finite fields used by the BN254 curve and pairings *)
-module Fq : sig
+module Fp : sig
include Field.S
val of_string : string -> t
@@ -11,24 +11,60 @@ module Fq : sig
end
(* The prime field over which the BN254 curve is defined. *)
-module Fq2 : Field.S with type t = Fq.t * Fq.t
-(* The extension field of degree 2 over [Fq]. *)
-
-(*
- module Fq12 :
- S
- with type t =
- Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- * Fq.t
- (* The extension field of degree 12 over [Fq]. *)
-*)
+module Fp2 : sig
+ include Field.S
+
+ type coeffs = Fp.t * Fp.t
+ (** Polynomial coefficients of the extension field. *)
+
+ val of_fp : Fp.t -> t
+ (** [of_fp a] returns maps a field element to the extension field. *)
+
+ val of_string : string -> t
+ (** [of_string s] returns the field element represented by [s]. *)
+
+ val to_string : t -> string
+ (** [to_string a] returns the string representation of [a]. *)
+
+ val of_coeffs : coeffs -> t
+ (** [of_coeffs c] is the field element with polynomial coefficients [c]. *)
+
+ val to_coeffs : t -> coeffs
+ (** [to_coeffs a] are the polynomial coefficients of [a]. *)
+end
+(* The polynomial field extension of degree 2 for BN254 pairings. *)
+
+module Fp12 : sig
+ include Field.S
+
+ type coeffs =
+ Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ * Fp.t
+ (** Polynomial coefficients of the extension field. *)
+
+ val of_fp : Fp.t -> t
+ (** [of_fp a] returns maps a field element to the extension field. *)
+
+ val of_string : string -> t
+ (** [of_string s] returns the field element represented by [s]. *)
+
+ val to_string : t -> string
+ (** [to_string a] returns the string representation of [a]. *)
+
+ val of_coeffs : coeffs -> t
+ (** [of_coeffs c] is the field element with polynomial coefficients [c]. *)
+
+ val to_coeffs : t -> coeffs
+ (** [to_coeffs a] are the polynomial coefficients of [a]. *)
+end
+(* The polynomial field extension of degree 12 for BN254 pairings. *)
diff --git a/test/test_fp.ml b/test/test_fp.ml
index 2189c7d..ca8f9e4 100644
--- a/test/test_fp.ml
+++ b/test/test_fp.ml
@@ -1,99 +1,134 @@
-module Fq = Plonkers.Fields.Fq
+module Fp = Plonkers.Fields.Fp
-let fq = Testable.make Fq.to_string Fq.equal
-let f = Fq.of_string
+let fp = Testable.make Fp.to_string Fp.equal
+let f = Fp.of_string
let test_add () =
- Alcotest.(check fq)
+ Alcotest.(check fp)
"equals"
(f
"5034212870056682811541563993093442956024207074167934414259669251040086104221")
- (Fq.add
+ (Fp.add
(f
"3019612232293951348432769707427639303474751231631017441050730061639747788964")
(f
"2014600637762731463108794285665803652549455842536916973208939189400338315257"));
- Alcotest.(check fq)
- "equals" Fq.zero
- (Fq.add
+ Alcotest.(check fp)
+ "equals" Fp.zero
+ (Fp.add
(f
"21888242871839275222246405745257275088696311157297823662689037894645226208582")
- Fq.one);
- Alcotest.(check fq)
+ Fp.one);
+ Alcotest.(check fp)
"equals"
(f
"7363893478117210948657131929839491162862302555393162961985689652909776739235")
- (Fq.add
+ (Fp.add
(f
"7363893478117210948657131929839491162862302555393162961985689652909776739235")
- Fq.zero)
+ Fp.zero)
let test_sub () =
- Alcotest.(check fq)
+ Alcotest.(check fp)
"equals"
(f
"1647997538064335793847192302859723563372189463772006638527106196094592175035")
- (Fq.sub
+ (Fp.sub
(f
"17522329235179667504196475280824254606128390327458085261948007699233694805172")
(f
"15874331697115331710349282977964531042756200863686078623420901503139102630137"));
- Alcotest.(check fq)
+ Alcotest.(check fp)
"equals"
(f
"21888242871839275222246405745257275088696311157297823662689037894645226208582")
- (Fq.sub Fq.zero Fq.one);
- Alcotest.(check fq)
+ (Fp.sub Fp.zero Fp.one);
+ Alcotest.(check fp)
"equals"
(f
"1550460075629904631277637708383660261580971272511193857451434052003108082807")
- (Fq.add
+ (Fp.add
(f
"1550460075629904631277637708383660261580971272511193857451434052003108082807")
- Fq.zero)
+ Fp.zero)
let test_mul () =
- Alcotest.(check fq)
+ Alcotest.(check fp)
"equals"
(f
"20539703399960180369319660518633290048071922000666914740466234074847854697757")
- (Fq.mul
+ (Fp.mul
(f
"16652515618518207323315643285221741056245295412987863357779458647516886387587")
(f
"15388577223719168739739036375931130294575142146897880107473273404618844312776"));
- Alcotest.(check fq)
- "equals" Fq.zero
- (Fq.mul
+ Alcotest.(check fp)
+ "equals" Fp.zero
+ (Fp.mul
(f
"10935302729977221140784701859408855206669147633587285404353277660075789680922")
- Fq.zero)
+ Fp.zero)
let test_div () =
- Alcotest.(check fq)
+ Alcotest.(check fp)
+ "equals" Fp.zero
+ (Fp.div
+ (f
+ "18116615978164648621644052928628515074324534642679951563606417178540650964031")
+ Fp.zero);
+ Alcotest.(check fp)
"equals"
(f
"7363953525538778156567393633860492517850968441297156550375719598579598367322")
- (Fq.div
+ (Fp.div
(f
"7363953525538778156567393633860492517850968441297156550375719598579598367322")
- Fq.one)
+ Fp.one);
+ Alcotest.(check fp)
+ "equals"
+ (f
+ "7790294916104464264789490491726462498925424319049857352064036320409088886798")
+ (Fp.div
+ (f
+ "4322946717978085483115008694110144245819367528797529184678953776094386598796")
+ (f
+ "14108716032053518963412902325315866377355891041801503021777950559570482464634"))
+
+let test_neg () =
+ Alcotest.(check fp)
+ "equals"
+ (f
+ "21888242871839275222246405745257275088696311157297823662689037894645226208565")
+ (Fp.neg (f "18"));
+ Alcotest.(check fp)
+ "equals"
+ (f
+ "21888242871839275222246405745257275088696311157297823662689037894645226208565")
+ (f "-18");
+ Alcotest.(check fp)
+ "equals" Fp.zero
+ (let a =
+ f
+ "17892529277404961739509303065517565646363806323806017705817770169891693557191"
+ in
+ Fp.add a (Fp.neg a))
let test_inv () =
- Alcotest.(check fq)
+ Alcotest.(check fp) "equals" Fp.zero (Fp.inv Fp.zero);
+ Alcotest.(check fp)
"equals"
(f
"14812345552713361047837749161990489217154524375614120373819285291721752372748")
- (Fq.inv
+ (Fp.inv
(f
"1160181877204925047836599448201084379181489517022839288462621953652233427695"));
- Alcotest.(check fq)
- "equals" Fq.one
+ Alcotest.(check fp)
+ "equals" Fp.one
(let a =
f
"17876831255806984619368587498426968568242044248616647019388897055879503636463"
in
- Fq.mul a (Fq.inv a))
+ Fp.mul a (Fp.inv a))
let tests =
Alcotest.
@@ -101,6 +136,7 @@ let tests =
test_case "Addition" `Quick test_add;
test_case "Subtraction" `Quick test_sub;
test_case "Multiplication" `Quick test_mul;
- test_case "Division" `Quick test_mul;
+ test_case "Division" `Quick test_div;
+ test_case "Additive Inverse" `Quick test_neg;
test_case "Multiplicative Inverse" `Quick test_inv;
]
diff --git a/test/test_fp2.ml b/test/test_fp2.ml
new file mode 100644
index 0000000..f62748e
--- /dev/null
+++ b/test/test_fp2.ml
@@ -0,0 +1,141 @@
+module Fp = Plonkers.Fields.Fp
+module Fp2 = Plonkers.Fields.Fp2
+
+let fp2 = Testable.make Fp2.to_string Fp2.equal
+let f a b = Fp2.of_coeffs Fp.(of_string a, of_string b)
+
+let test_strconv () =
+ let a =
+ f
+ "10107500267655288533860407941350647620522758874612469022754124115798347851255"
+ "4272490765421360074221748579109824733865223751122700688342039244846082709245"
+ in
+ Alcotest.(check string)
+ "equals"
+ "(10107500267655288533860407941350647620522758874612469022754124115798347851255, \
+ 4272490765421360074221748579109824733865223751122700688342039244846082709245)"
+ (Fp2.to_string a);
+ Alcotest.(check fp2)
+ "equals" a
+ (Fp2.of_string
+ "(10107500267655288533860407941350647620522758874612469022754124115798347851255, \
+ 4272490765421360074221748579109824733865223751122700688342039244846082709245)")
+
+let test_add () =
+ Alcotest.(check fp2)
+ "equals"
+ (f
+ "2863935846698150146497381873035561276727058128903565528503671733559822376250"
+ "14401928050079391824950970163870648129396875197878295136665664481048699468020")
+ (Fp2.add
+ (f
+ "14324043627524728108674506634258156009885262437468906348349536322248493484364"
+ "18029279639026325195296840766401583521358381131411036066373305410616713471095")
+ (f
+ "10428135091012697260069280984034680355538106848732482842843173305956555100469"
+ "18260891282892341851900535142726339696734805223765082732981396965077212205508"))
+
+let test_sub () =
+ Alcotest.(check fp2)
+ "equals"
+ (f
+ "15120500054601708444564040485468985282448658084430827774523947688586748960053"
+ "9100764362236999328461500289225313390908509119277966267226462796263537316381")
+ (Fp2.sub
+ (f
+ "14293698145849566696549634796503386029017218475989635574509430094951523306687"
+ "19456315000973730806536321027874670859833970960897936694996600353725317883622")
+ (f
+ "21061440963087133474232000056291675835264871548856631462674520301010000555217"
+ "10355550638736731478074820738649357468925461841619970427770137557461780567241"))
+
+let test_mul () =
+ Alcotest.(check fp2)
+ "equals"
+ (f
+ "11432323160814065864909960700129434587946493783507438700207928706738844119449"
+ "3577409691447205319998726389612822121306006806114173782377028664710279109705")
+ (Fp2.mul
+ (f
+ "11986626879605260056244902575435385107082913504490111545527498340756305531092"
+ "4818173862427609729222592608595100262022473140100027294220533759712947640512")
+ (f
+ "10960597259346866798564665428177212161645337477705428048049421023510569201652"
+ "17436818457664572454171416757524427055427814808946492827966722145668155530853"))
+
+let test_div () =
+ Alcotest.(check fp2)
+ "equals"
+ (f
+ "13419365922711575620625084257870785388560921645234544455343469677828280907673"
+ "10223115143172140697158827396733687716959251907957043435822040517781915535192")
+ (Fp2.div
+ (f
+ "5246975579657097196126930177221162000731887408914575127136817635764078403513"
+ "8292723131410576043572459680046943309298666101925027054973960663999641620758")
+ (f
+ "1886591325401429366956197735571233984030593880349024014320016420187469008836"
+ "1695527069488388814625626614375814226660393081483975270883481133990211272753"));
+ Alcotest.(check fp2)
+ "equals" Fp2.zero
+ (Fp2.div
+ (f
+ "15575901816686564911695664888216387456052221682627972942517791604577648718373"
+ "4074937799975106177793779962697923305068793270789454615564141613169465455800")
+ Fp2.zero)
+
+let test_neg () =
+ let a =
+ f
+ "15368678686417640939410004345384562254158052587497136227450829712392291541108"
+ "12426777655707647255726782198814182406150331554284032768572910376412464025883"
+ in
+ Alcotest.(check fp2)
+ "equals"
+ (f
+ "6519564185421634282836401399872712834538258569800687435238208182252934667475"
+ "9461465216131627966519623546443092682545979603013790894116127518232762182700")
+ (Fp2.neg a);
+ Alcotest.(check fp2) "equals" Fp2.zero (Fp2.add a (Fp2.neg a))
+
+(*
+ ========================================================================
+ multiplication
+ (11432323160814065864909960700129434587946493783507438700207928706738844119449, 3577409691447205319998726389612822121306006806114173782377028664710279109705)
+ = (11986626879605260056244902575435385107082913504490111545527498340756305531092, 4818173862427609729222592608595100262022473140100027294220533759712947640512)
+ * (10960597259346866798564665428177212161645337477705428048049421023510569201652, 17436818457664572454171416757524427055427814808946492827966722145668155530853)
+ division
+ (13419365922711575620625084257870785388560921645234544455343469677828280907673, 10223115143172140697158827396733687716959251907957043435822040517781915535192)
+ = (5246975579657097196126930177221162000731887408914575127136817635764078403513, 8292723131410576043572459680046943309298666101925027054973960663999641620758)
+ / (1886591325401429366956197735571233984030593880349024014320016420187469008836, 1695527069488388814625626614375814226660393081483975270883481133990211272753)
+ multiplicative inverse
+ (1787014003341926122725734505165381529419163570425444088707405348791540627564, 3072592406275333378136945391036461572636993084074366352517366836269594879853)
+ = ~/(5846831721439900768152735149922159251212076902324998439686567351122268450217, 15974811941941150933977163259953349229614582170845394085548476048658545196484)
+*)
+
+let test_inv () =
+ let a =
+ f
+ "5846831721439900768152735149922159251212076902324998439686567351122268450217"
+ "15974811941941150933977163259953349229614582170845394085548476048658545196484"
+ in
+ Alcotest.(check fp2)
+ "equals"
+ (f
+ "1787014003341926122725734505165381529419163570425444088707405348791540627564"
+ "3072592406275333378136945391036461572636993084074366352517366836269594879853")
+ (Fp2.inv a);
+ Alcotest.(check fp2) "equals" Fp2.one (Fp2.mul a (Fp2.inv a));
+ Alcotest.(check fp2) "equals" Fp2.zero (Fp2.inv Fp2.zero);
+ Alcotest.(check fp2) "equals" Fp2.one (Fp2.inv Fp2.one)
+
+let tests =
+ Alcotest.
+ [
+ test_case "Addition" `Quick test_add;
+ test_case "Subtraction" `Quick test_sub;
+ test_case "Multiplication" `Quick test_mul;
+ test_case "Division" `Quick test_div;
+ test_case "Additive Inverse" `Quick test_neg;
+ test_case "Multiplicative Inverse" `Quick test_inv;
+ ]
diff --git a/test/test_plonkers.ml b/test/test_plonkers.ml
index 16db4ca..672cad5 100644
--- a/test/test_plonkers.ml
+++ b/test/test_plonkers.ml
@@ -1 +1,2 @@
-let () = Alcotest.run "Plonkers" [ ("Fp", Test_fp.tests) ]
+let () =
+ Alcotest.run "Plonkers" [ ("Fp", Test_fp.tests); ("Fp2", Test_fp2.tests) ]