aboutsummaryrefslogtreecommitdiff
path: root/lib/bls12_381.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bls12_381.ml')
-rw-r--r--lib/bls12_381.ml123
1 files changed, 123 insertions, 0 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