aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2025-08-01 18:02:12 +0200
committerNicholas Rodrigues Lordello <[email protected]>2025-08-01 18:02:12 +0200
commit51e1e445215c14e3ce90ab12906f07ffb2b09aaf (patch)
tree97bac16ee731e4f531dc778cd7bbf650939ad891 /lib
parent4ce648df90034d7dade55cadd5bd6251619cf922 (diff)
Refactor Project
We now put all field, curve, and pairing instantiations under `bls12_381` module. We no longer have an `E12` curve - since we don't use it.
Diffstat (limited to 'lib')
-rw-r--r--lib/bls12_381.ml123
-rw-r--r--lib/curves.ml146
-rw-r--r--lib/curves.mli13
-rw-r--r--lib/fields.ml32
-rw-r--r--lib/fields.mli10
-rw-r--r--lib/pairing.ml121
-rw-r--r--lib/pairing.mli48
-rw-r--r--lib/plonkers.ml6
8 files changed, 197 insertions, 302 deletions
diff --git a/lib/bls12_381.ml b/lib/bls12_381.ml
new file mode 100644
index 0000000..d9399f2
--- /dev/null
+++ b/lib/bls12_381.ml
@@ -0,0 +1,123 @@
+module Fields = struct
+ module Fp = Field.Make (struct
+ let p =
+ Z.of_string
+ "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"
+ end)
+
+ module Fp2 = Field.MakeExtension (struct
+ module F = Fp
+
+ let q = Fp.[| one; zero |]
+ end)
+
+ module Fp12 = Field.MakeExtension (struct
+ module F = Fp
+
+ let q =
+ Fp.
+ [|
+ of_string "2";
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ of_string "-2";
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ |]
+ end)
+end
+
+module Curves = struct
+ open Fields
+
+ let n =
+ Z.of_string
+ "52435875175126190479447740508185965837690552500527637822603658699938581184513"
+
+ module E = Curve.Make (struct
+ module F = Fp
+
+ let b = Fp.of_string "4"
+
+ let g =
+ Fp.
+ ( of_string
+ "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507",
+ of_string
+ "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"
+ )
+
+ let n = n
+ end)
+
+ module E2 = Curve.Make (struct
+ module F = Fp2
+
+ let of_string_coeffs s = Fp2.of_coeffs @@ Array.map Fp.of_string s
+ let b = of_string_coeffs [| "4"; "4" |]
+
+ let g =
+ ( of_string_coeffs
+ [|
+ "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
+ "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
+ |],
+ of_string_coeffs
+ [|
+ "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
+ "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
+ |] )
+
+ let n = n
+ end)
+end
+
+module Pairing = Pairing.Make (struct
+ open Fields
+ open Curves
+ module G1 = E
+ module G2 = E2
+ module F = Fp12
+
+ let x = Z.of_string "-0xd201000000010000"
+
+ let g1_extend p =
+ E.to_coords p |> Option.map (fun (x, y) -> (Fp12.of_f x, Fp12.of_f y))
+
+ let g2_twist =
+ let o, z = Fp.(one, zero) in
+ let ws =
+ lazy
+ (let w = Fp12.of_coeffs [| z; o; z; z; z; z; z; z; z; z; z; z |] in
+ let w2 = Fp12.(w * w) in
+ let w3 = Fp12.(w2 * w) in
+ let w2inv = Fp12.inv w2 in
+ let w3inv = Fp12.inv w3 in
+ (w2inv, w3inv))
+ in
+ let iso a =
+ let c = Fp2.to_coeffs a in
+ let r0 = Fp.(c.(0) - c.(1)) in
+ let r6 = c.(1) in
+ Fp12.of_coeffs [| r0; z; z; z; z; z; r6; z; z; z; z; z |]
+ in
+ fun p ->
+ match E2.to_coords p with
+ | None -> None
+ | Some (x, y) ->
+ let nx, ny = (iso x, iso y) in
+ let w2inv, w3inv = Lazy.force ws in
+ Some Fp12.(nx * w2inv, ny * w3inv)
+end)
+
+module G1 = Curves.E
+module G2 = Curves.E2
+module Gt = Pairing.Gt
+
+let pairing = Pairing.check
diff --git a/lib/curves.ml b/lib/curves.ml
deleted file mode 100644
index 1182352..0000000
--- a/lib/curves.ml
+++ /dev/null
@@ -1,146 +0,0 @@
-module E = Curve.Make (struct
- module F = Fields.Fp
-
- let b = F.of_string "4"
-
- let g =
- F.
- ( of_string
- "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507",
- of_string
- "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"
- )
-
- let n =
- Z.of_string
- "52435875175126190479447740508185965837690552500527637822603658699938581184513"
-end)
-
-module E2 = Curve.Make (struct
- module F = Fields.Fp2
-
- let b = F.of_string "(4,4)"
-
- let g =
- F.
- ( of_coeffs
- Fields.Fp.
- [|
- of_string
- "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
- of_string
- "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
- |],
- of_coeffs
- Fields.Fp.
- [|
- of_string
- "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
- of_string
- "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
- |] )
-
- let n = E.n
-end)
-
-module E12 = Curve.Make (struct
- module F = Fields.Fp12
-
- let b = F.of_f @@ Fields.Fp.of_string "4"
-
- let g =
- F.
- ( of_coeffs
- Fields.Fp.
- [|
- zero;
- zero;
- zero;
- zero;
- of_string
- "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
- zero;
- zero;
- zero;
- zero;
- zero;
- of_string
- "1353221637328373545892060349371360746048220186341936159219732281025920769516621702780080981380240920942561918531299";
- zero;
- |],
- of_coeffs
- Fields.Fp.
- [|
- zero;
- zero;
- zero;
- of_string
- "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
- zero;
- zero;
- zero;
- zero;
- zero;
- of_string
- "1472406309213353956798468635167385199506877017588472562248853724925831900559844855499883907296819622485916638215732";
- zero;
- zero;
- |] )
-
- let n = E.n
-end)
-
-let twist =
- let ws =
- lazy
- (let w =
- Fields.Fp12.of_coeffs
- Fields.Fp.
- [|
- zero;
- one;
- zero;
- zero;
- zero;
- zero;
- zero;
- zero;
- zero;
- zero;
- zero;
- zero;
- |]
- in
- let w2 = Fields.Fp12.(w * w) in
- let w3 = Fields.Fp12.(w2 * w) in
- let w2inv = Fields.Fp12.inv w2 in
- let w3inv = Fields.Fp12.inv w3 in
- (w2inv, w3inv))
- in
- let iso a =
- let c = Fields.Fp2.to_coeffs a in
- Fields.Fp12.of_coeffs
- Fields.Fp.
- [|
- Fields.Fp.(c.(0) - c.(1));
- zero;
- zero;
- zero;
- zero;
- zero;
- c.(1);
- zero;
- zero;
- zero;
- zero;
- zero;
- |]
- in
- fun p ->
- match E2.to_coords p with
- | None -> E12.inf
- | Some (x, y) ->
- let nx, ny = (iso x, iso y) in
- let w2inv, w3inv = Lazy.force ws in
- let c = Fields.Fp12.(nx * w2inv, ny * w3inv) in
- E12.Unsafe.of_coords_unchecked c
diff --git a/lib/curves.mli b/lib/curves.mli
deleted file mode 100644
index 35f2bd4..0000000
--- a/lib/curves.mli
+++ /dev/null
@@ -1,13 +0,0 @@
-(** BLS12-381 curves used for pairings *)
-
-module E : Curve.S with type f = Fields.Fp.t
-(** The BLS12-381 curve over [Fields.Fp]. *)
-
-module E2 : Curve.S with type f = Fields.Fp2.t
-(** The BLS12-381 curve over [Fields.Fp2]. *)
-
-module E12 : Curve.S with type f = Fields.Fp12.t
-(** The BLS12-381 curve over [Fields.Fp12]. *)
-
-val twist : E2.t -> E12.t
-(** [twist a] is the twisted point of [E2] in [E12] *)
diff --git a/lib/fields.ml b/lib/fields.ml
deleted file mode 100644
index 8bb4c5e..0000000
--- a/lib/fields.ml
+++ /dev/null
@@ -1,32 +0,0 @@
-module Fp = Field.Make (struct
- let p =
- Z.of_string
- "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"
-end)
-
-module Fp2 = Field.MakeExtension (struct
- module F = Fp
-
- let q = Fp.[| one; zero |]
-end)
-
-module Fp12 = Field.MakeExtension (struct
- module F = Fp
-
- let q =
- Fp.
- [|
- of_string "2";
- zero;
- zero;
- zero;
- zero;
- zero;
- of_string "-2";
- zero;
- zero;
- zero;
- zero;
- zero;
- |]
-end)
diff --git a/lib/fields.mli b/lib/fields.mli
deleted file mode 100644
index a3d9adc..0000000
--- a/lib/fields.mli
+++ /dev/null
@@ -1,10 +0,0 @@
-(** Finite fields used by the BLS12-381 curve and pairings *)
-
-module Fp : Field.S
-(** The prime field over which the BLS12-381 curve is defined. *)
-
-module Fp2 : Field.SX with type f = Fp.t
-(** The polynomial field extension of degree 2 for BLS12-381 pairings. *)
-
-module Fp12 : Field.SX with type f = Fp.t
-(** The polynomial field extension of degree 12 for BLS12-381 pairings. *)
diff --git a/lib/pairing.ml b/lib/pairing.ml
index ef67901..33c09c6 100644
--- a/lib/pairing.ml
+++ b/lib/pairing.ml
@@ -1,23 +1,86 @@
+(** Boneh-Lynn-Shacham (BLS) pairing curves implementation. *)
+
+(** Module signature BLS pairing curves. *)
+module type S = sig
+ type g1
+ (** The elliptic curve element of the first pairing group. *)
+
+ type g2
+ (** The elliptic curve element of the second pairing group. *)
+
+ (** The target group of the pairing, usually denoted as [GT]. *)
+ module Gt : sig
+ type t
+ (** An element in the pairing target group. *)
+
+ type f
+ (** The underlying field type of the pairing target group. *)
+
+ val id : t
+ (** The group's identity element. *)
+
+ val dot : t -> t -> t
+ (** [dot a b] is the group operation applied to [a] and [b]. *)
+
+ val inv : t -> t
+ (** [inv a] is the inverse of [a]. Note that the invariant
+ [equal id (dot a (inv a))] always holds. *)
+
+ val equal : t -> t -> bool
+ (** [equal a b] is true if [a] has the same value as [b] *)
+
+ val to_f : t -> f
+ (** [to_fp12 a] is the underlying field value of the group element [a]. *)
+
+ val to_string : t -> string
+ (** [to_string a] is the string representation of [a]. *)
+
+ (** Unsafe pairing group construction functions. *)
+ module Unsafe : sig
+ val of_f_unchecked : f -> t
+ (** [of_fp12_unchecked a] is a pairing element of value [a]. This function
+ does not check whether the value actually belongs to the pairing group
+ [Gt]. *)
+
+ val of_string_unchecked : string -> t
+ (** [of_string_unchecked s] returns the pairing element represented by
+ [s]. This function does not check whether the value belongs to the
+ group [Gt]. *)
+ end
+ end
+
+ val e : g1 -> g2 -> Gt.t
+ (** [e p q] is the elliptic curve pairing of [p] and [q]. *)
+
+ val check : (g1 * g2) list -> bool
+ (** [check pairs] checks whether the product of the pairings is equal to
+ [Gt.id]. *)
+end
+
+(** BLS pairing curves construction. *)
module Make (BLS : sig
module G1 : Curve.S
module G2 : Curve.S
module F : Field.SX
val x : Z.t
- val g : F.t
- val g1_to_coords : G1.t -> (F.t * F.t) option
- val g2_to_coords : G2.t -> (F.t * F.t) option
-end) =
+ val g1_extend : G1.t -> (F.t * F.t) option
+ val g2_twist : G2.t -> (F.t * F.t) option
+end) :
+ S with type g1 = BLS.G1.t and type g2 = BLS.G2.t and type Gt.f = BLS.F.t =
struct
module G1 = BLS.G1
module G2 = BLS.G2
module F = BLS.F
+ type g1 = G1.t
+ type g2 = G2.t
+
module Gt = struct
type t = F.t
+ type f = F.t
let id = F.one
- let g = BLS.g
let dot = F.mul
let inv = F.inv
let to_f a = a
@@ -47,17 +110,17 @@ struct
| Some x -> x
| None -> invalid_arg "line with point at infinity"
in
- let g1_to_coords p = BLS.g1_to_coords p |> unwrap in
- let g2_to_coords p = BLS.g2_to_coords p |> unwrap in
+ let g1_extend p = BLS.g1_extend p |> unwrap in
+ let g2_twist p = BLS.g2_twist p |> unwrap in
fun p q r ->
- let xp, yp = g2_to_coords p in
- let xr, yr = g1_to_coords r in
+ let xp, yp = g2_twist p in
+ let xr, yr = g1_extend r in
if G2.equal p q then
let m = F.(three * xp * xp / (two * yp)) in
F.((m * (xr - xp)) - (yr - yp))
else if G2.(equal p (neg q)) then F.(xr - xp)
else
- let xq, yq = g2_to_coords q in
+ let xq, yq = g2_twist q in
let m = F.((yq - yp) / (xq - xp)) in
F.((m * (xr - xp)) - (yr - yp))
@@ -88,41 +151,3 @@ struct
let e p q = pairing [ (p, q) ]
let check pairs = pairing pairs |> Gt.(equal id)
end
-
-module P = Make (struct
- module G1 = Curves.E
- module G2 = Curves.E2
- module F = Fields.Fp12
-
- let x = Z.of_string "-0xd201000000010000"
-
- let g =
- F.of_coeffs
- @@ Array.map Fields.Fp.of_string
- [|
- "3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532";
- "1017299873256115394687936133146817339783508925356207904663165117501336940438300180831508525168135575931758510829235";
- "1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694";
- "233557331756520805040932826416017142246700002714928636467514104620558679338483625829873520553081791155104026612174";
- "2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068";
- "579878855790610229145575079003483905368886712027403088810647643659132674704592975818766405092658613973478871626949";
- "3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511";
- "2688711983546324406847903726099677850388169649284572370749691792743294691894680263880362424577638309433508620982332";
- "3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719";
- "1555267885602801620621747613743921378408444002839854792606782732724136830563310004098024452104661167182680744848589";
- "2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447";
- "873321072950766150592434764742436722571574260472569072177205897167854308426485977134306337249021436737462538398830";
- |]
-
- let g1_to_coords p =
- G1.to_coords p |> Option.map (fun (x, y) -> (F.of_f x, F.of_f y))
-
- let g2_to_coords p = Curves.E12.to_coords @@ Curves.twist p
-end)
-
-module G1 = P.G1
-module G2 = P.G2
-module Gt = P.Gt
-
-let e = P.e
-let check = P.check
diff --git a/lib/pairing.mli b/lib/pairing.mli
deleted file mode 100644
index 3d63df6..0000000
--- a/lib/pairing.mli
+++ /dev/null
@@ -1,48 +0,0 @@
-module G1 : Curve.S with type t = Curves.E.t and type f = Fields.Fp.t
-module G2 : Curve.S with type t = Curves.E2.t and type f = Fields.Fp2.t
-
-module Gt : sig
- type t
-
- val id : t
- (** The group's identity element. *)
-
- val g : t
- (** The group's generator. *)
-
- val dot : t -> t -> t
- (** [dot a b] is the group operation applied to [a] and [b]. *)
-
- val inv : t -> t
- (** [inv a] is the inverse of [a]. Note that the invariant
- [equal id (dot a (inv a))] always holds. *)
-
- val equal : t -> t -> bool
- (** [equal a b] is true if [a] has the same value as [b] *)
-
- val to_f : t -> Fields.Fp12.t
- (** [to_fp12 a] is the underlying field value of the group element [a]. *)
-
- val to_string : t -> string
- (** [to_string a] is the string representation of [a]. *)
-
- (** Unsafe pairing group construction functions. *)
- module Unsafe : sig
- val of_f_unchecked : Fields.Fp12.t -> t
- (** [of_fp12_unchecked a] is a pairing element of value [a]. This function
- does not check whether the value actually belongs to the pairing group
- [Gt]. *)
-
- val of_string_unchecked : string -> t
- (** [of_string_unchecked s] returns the pairing element represented by [s].
- This function does not check whether the value belongs to the group
- [Gt]. *)
- end
-end
-
-val e : G1.t -> G2.t -> Gt.t
-(** [e p q] is the elliptic curve pairing of [p] and [q]. *)
-
-val check : (G1.t * G2.t) list -> bool
-(** [check pairs] checks whether the product of the pairings is equal to
- [Gt.id]. *)
diff --git a/lib/plonkers.ml b/lib/plonkers.ml
index aa14f53..572a3cb 100644
--- a/lib/plonkers.ml
+++ b/lib/plonkers.ml
@@ -1,5 +1 @@
-module Field = Field
-module Fields = Fields
-module Curve = Curve
-module Curves = Curves
-module Pairing = Pairing
+module Bls12_381 = Bls12_381