aboutsummaryrefslogtreecommitdiff
path: root/lib/curves.ml
blob: 082a75290a410513dc59aba52de28aba699fd5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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')