aboutsummaryrefslogtreecommitdiff
path: root/lib/curves.ml
blob: 1459fcc4bb97ba99ef27a07df3ce0cd801f1d2d7 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 and type F.t = E.F.t = struct
  module F = E.F

  type t = F.t point

  let inf = Inf

  let g =
    let gx, gy = E.g in
    Point (gx, gy)

  let n = n
  let to_coords = function Inf -> None | Point (x, y) -> Some (x, y)

  let to_string = function
    | Inf -> "{∞}"
    | Point (x, y) -> Printf.sprintf "{%s;%s}" (F.to_string x) (F.to_string y)

  let equal a b =
    match (a, b) with
    | Inf, Inf -> true
    | Point (ax, ay), Point (bx, by) -> F.equal ax bx && F.equal ay by
    | _ -> false

  let neg = function Inf -> Inf | Point (x, y) -> Point (x, F.neg y)

  let add a b =
    let line m ax ay bx _ =
      let rx = F.((m * m) - ax - bx) in
      let ry = F.(~-(ay + (m * (rx - ax)))) in
      Point (rx, ry)
    in
    match (a, b) with
    | Inf, p | p, Inf -> p
    | Point (ax, ay), Point (bx, by) ->
        if F.(equal ax bx) then
          if F.(equal ay ~-by) then Inf
          else
            let ax2 = F.(ax * ax) in
            let m = F.((ax2 + ax2 + ax2) / (ay + ay)) in
            line m ax ay bx by
        else
          let m = F.((ay - by) / (ax - bx)) in
          line m ax ay bx by

  let sub a b = add a (neg b)

  let scalar_mul i a =
    let rec m acc i a =
      let acc' = if Z.testbit i 0 then add acc a else acc in
      let i' = Z.shift_right i 1 in
      if Z.(equal i' zero) then acc'
      else
        let a' = add a a in
        m acc' i' a'
    in
    m Inf i a

  let of_coords (x, y) =
    let p = Point (x, y) in
    let is_on_curve =
      let y2 = F.(y * y) in
      let x3 = F.(x * x * x) in
      F.(equal y2 (x3 + E.b))
    in
    if is_on_curve then () else raise Curve.Not_on_curve;
    let is_in_canonical_subgroup = scalar_mul n p |> equal Inf in
    if is_in_canonical_subgroup then p
    else raise Curve.Not_in_canonical_subgroup

  let of_string_f s f =
    let strip_braces s =
      if String.starts_with ~prefix:"{" s && String.ends_with ~suffix:"}" s then
        String.sub s 1 (String.length s - 2)
      else failwith "curve point missing braces"
    in
    match strip_braces s |> String.split_on_char ';' with
    | [ "∞" ] -> Inf
    | [ x; y ] -> f (F.of_string x, F.of_string y)
    | _ -> invalid_arg "wrong number of elements, expected {x;y}"

  let of_string s = of_string_f s of_coords

  module Unsafe = struct
    let of_coords_unchecked (x, y) = Point (x, y)
    let of_string_unchecked s = of_string_f s of_coords_unchecked
  end

  let ( ~- ) = neg
  let ( + ) = add
  let ( - ) = sub
  let ( $* ) = scalar_mul
end

module E = Eimpl (struct
  module F = Fields.Fp

  let b = F.of_string "4"

  let g =
    F.
      ( of_string
          "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507",
        of_string
          "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"
      )
end)

let g2 =
  Fields.Fp2.
    ( of_coeffs
        Fields.Fp.
          [|
            of_string
              "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
            of_string
              "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
          |],
      of_coeffs
        Fields.Fp.
          [|
            of_string
              "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
            of_string
              "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
          |] )

let twist_coords (x, y) =
  let open Fields in
  let w =
    Fp12.of_coeffs
      Fp.
        [|
          zero; one; zero; zero; zero; zero; zero; zero; zero; zero; zero; zero;
        |]
  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
  let iso a =
    let c = Fp2.to_coeffs a in
    let c0' = Fp.(c.(0) - c.(1)) in
    Fp12.of_coeffs
      Fp.
        [|
          c0'; zero; zero; zero; zero; zero; c.(1); zero; zero; zero; zero; zero;
        |]
  in
  let nx, ny = (iso x, iso y) in
  Fp12.(nx * w2inv, ny * w3inv)

module E2 = Eimpl (struct
  module F = Fields.Fp2

  let b = F.of_string "(4,4)"
  let g = g2
end)

module E12 = Eimpl (struct
  module F = Fields.Fp12

  let b = F.of_f @@ Fields.Fp.of_string "4"
  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')