diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-08-15 22:02:43 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-08-15 22:02:43 +0200 |
| commit | 58c6ddc205a8e9c7b76c6ec982186481af0db48f (patch) | |
| tree | b754a2328e8064764fc0a35d38d6d0a373bd1ea2 | |
| parent | 1b4cf1c710f002b53bee48197c128f791c063517 (diff) | |
Started Implementing Elliptic Curves
Mostly implemented, but notably missing string conversion methods as
well as point addition and scalar multiplication.
| -rw-r--r-- | lib/curves.ml | 105 | ||||
| -rw-r--r-- | lib/curves.mli | 13 | ||||
| -rw-r--r-- | lib/fields.ml | 31 | ||||
| -rw-r--r-- | lib/plonkers.ml | 1 |
4 files changed, 135 insertions, 15 deletions
diff --git a/lib/curves.ml b/lib/curves.ml new file mode 100644 index 0000000..082a752 --- /dev/null +++ b/lib/curves.ml @@ -0,0 +1,105 @@ +open Fields + +let n = + Z.of_string + "52435875175126190479447740508185965837690552500527637822603658699938581184513" + +type 'a point = Inf | Point of 'a * 'a + +module Eimpl (E : sig + module F : Field.S + + val b : F.t + val g : F.t * F.t +end) : Curve.S with type t = E.F.t point = struct + type t = E.F.t point + + let inf = Inf + + let g = + let gx, gy = E.g in + Point (gx, gy) + + let n = n + + let equal a b = + match (a, b) with + | Inf, Inf -> true + | Point (ax, ay), Point (bx, by) -> E.F.equal ax bx && E.F.equal ay by + | _ -> false + + let neg = function Inf -> Inf | Point (x, y) -> Point (x, E.F.neg y) + + let add a b = + let _ = (a, b) in + failwith "todo" + + let sub a b = add a (neg b) + + let scalar_mul i a = + let _ = (i, a) in + failwith "todo" + + let ( ~- ) = neg + let ( + ) = add + let ( - ) = sub + let ( $* ) = scalar_mul +end + +module E1 = Eimpl (struct + module F = Fp + + let b = Fp.of_string "4" + + let g = + Fp. + ( of_string + "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507", + of_string + "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569" + ) +end) + +let g2 = + Fp2. + ( of_string + "(352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160,3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758)", + of_string + "(1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905,927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582)" + ) + +let twist_coords (x, y) = + let w = + Fp.(zero, one, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero) + |> Fp12.of_coeffs + in + let w2 = Fp12.(w * w) in + let w3 = Fp12.(w2 * w) in + let iso a = + let c0, c1 = Fp2.to_coeffs a in + let c0' = Fp.(c0 - c1) in + Fp.(c0', zero, zero, zero, zero, zero, c1, zero, zero, zero, zero, zero) + |> Fp12.of_coeffs + in + let nx, ny = (iso x, iso y) in + Fp12.(nx * w2, ny * w3) + +module E2 = Eimpl (struct + module F = Fp2 + + let b = Fp2.of_string "(4,4)" + let g = g2 +end) + +module E12 = Eimpl (struct + module F = Fp12 + + let b = Fp.of_string "4" |> Fp12.of_fp + let g = twist_coords g2 +end) + +let twist = function + | Inf -> Inf + | Point (x, y) -> + let x', y' = twist_coords (x, y) in + Point (x', y') diff --git a/lib/curves.mli b/lib/curves.mli new file mode 100644 index 0000000..667e943 --- /dev/null +++ b/lib/curves.mli @@ -0,0 +1,13 @@ +(** BLS12-381 curves used for pairings *) + +module E1 : Curve.S +(* The BLS12-381 curve over [Fields.Fp]. *) + +module E2 : Curve.S +(* The BLS12-381 curve over [Fields.Fp2]. *) + +module E12 : Curve.S +(* 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 index 14f3dfd..2523bba 100644 --- a/lib/fields.ml +++ b/lib/fields.ml @@ -151,7 +151,7 @@ end module Fp2 = struct include FpN (struct - let p = [| Fp.one; Fp.zero |] + let p = Fp.[| one; zero |] end) type coeffs = Fp.t * Fp.t @@ -163,20 +163,21 @@ end module Fp12 = struct include FpN (struct let p = - [| - Fp.of_string "2"; - Fp.zero; - Fp.zero; - Fp.zero; - Fp.zero; - Fp.zero; - Fp.of_string "-2"; - Fp.zero; - Fp.zero; - Fp.zero; - Fp.zero; - Fp.zero; - |] + Fp. + [| + of_string "2"; + zero; + zero; + zero; + zero; + zero; + of_string "-2"; + zero; + zero; + zero; + zero; + zero; + |] end) type coeffs = diff --git a/lib/plonkers.ml b/lib/plonkers.ml index 8ce4014..f18fe19 100644 --- a/lib/plonkers.ml +++ b/lib/plonkers.ml @@ -1 +1,2 @@ module Fields = Fields +module Curves = Curves |
