package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file bounded.ml

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
(*****************************************************************************)
(*                                                                           *)
(* MIT License                                                               *)
(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Lang_stdlib
open Lang_core

module Make (Bound : sig
  type 'a t = private Z.t

  val v : 'a t -> Z.t
end) =
struct
  module P : sig
    type 'a t = private Z.t * Z.t

    val make : ?unsafe:bool -> Z.t -> bound:'a Bound.t -> 'a t

    val v : 'a t -> Z.t

    val add : ?unsafe:bool -> 'a t -> 'b t -> unit t

    val add_left : ?unsafe:bool -> 'a t -> 'b t -> 'a t

    val sub_left : ?unsafe:bool -> 'a t -> 'b t -> 'a t

    val succ : ?unsafe:bool -> 'a t -> 'a t

    val random : ?maxv:Z.t -> 'a Bound.t -> 'a t

    val check : 'a t -> bool

    val f : 'a t -> unit t
  end = struct
    type 'a t = Z.t * Z.t

    let make ?(unsafe = false) v ~bound =
      let bound = (bound : 'a Bound.t :> Z.t) in
      assert (bound < Bls12_381.Fr.order) ;
      if not unsafe then assert (v < bound) ;
      (v, bound)

    let v (x, _) = x

    let add ?(unsafe = false) (a, bnd) (b, bnd') =
      let n_bnd = Z.(bnd + bnd') in
      if not unsafe then assert (n_bnd < Bls12_381.Fr.order) ;
      let r = Z.add a b in
      if not unsafe then assert (Z.zero <= r && r < n_bnd) ;
      (r, n_bnd)

    let add_left ?(unsafe = false) (a, bnd) (b, bnd') =
      if not unsafe then assert (Z.(bnd + bnd' < Bls12_381.Fr.order)) ;
      let r = Z.add a b in
      if not unsafe then assert (Z.zero <= r && r < bnd) ;
      (r, bnd)

    let sub_left ?(unsafe = false) (a, bnd) (b, bnd') =
      if not unsafe then assert (Z.(bnd + bnd' < Bls12_381.Fr.order)) ;
      let r = Z.sub a b in
      if not unsafe then assert (Z.zero <= r && r < bnd) ;
      (r, bnd)

    let succ ?(unsafe = false) (a, bnd) =
      if not unsafe then assert (Z.(bnd + one < Bls12_381.Fr.order)) ;
      let r = Z.succ a in
      if not unsafe then assert (Z.zero <= r && r < bnd) ;
      (r, bnd)

    let random ?maxv bound =
      let random_z bound = Random.int64 (Z.to_int64 bound) |> Z.of_int64 in
      let bound = (bound : 'a Bound.t :> Z.t) in
      let maxv =
        match maxv with
        | None -> bound
        | Some maxv ->
            assert (maxv <= bound) ;
            maxv
      in
      (random_z maxv, bound)

    let check (v, bound) = Z.(v >= zero && v < bound)

    let f x = x
  end

  module V (L : LIB) : sig
    open L

    type 'a t = private scalar repr * Z.t

    val make : scalar repr -> bound:'a Bound.t -> 'a t L.t

    val make_unsafe : scalar repr -> bound:'a Bound.t -> 'a t

    val succ : ?unsafe:bool -> 'a t -> 'a t L.t

    val add : ?unsafe:bool -> 'a t -> 'b t -> 'c t L.t

    val add_left : ?unsafe:bool -> 'a t -> 'b t -> 'a t L.t

    val sub_left : ?unsafe:bool -> 'a t -> 'b t -> 'a t L.t

    val f : 'a t -> unit t
  end = struct
    open L

    type 'a t = scalar repr * Z.t

    let make w ~bound =
      let bound = (bound : 'a Bound.t :> Z.t) in
      assert (bound < Bls12_381.Fr.order) ;
      with_bool_check (Num.is_upper_bounded w ~bound) >* ret (w, bound)

    let make_unsafe w ~bound = (w, Bound.v bound)

    let add ?(unsafe = false) (a, bound) (b, bound') =
      assert (Z.(bound + bound' < Bls12_381.Fr.order)) ;
      let* r = Num.add a b in
      if unsafe then ret (r, Z.add bound bound')
      else
        with_bool_check
          (Num.is_upper_bounded_unsafe r ~bound:(Z.add bound bound'))
        >* ret (r, Z.add bound bound')

    let add_left ?(unsafe = false) (a, bound) (b, bound') =
      assert (Z.(bound + bound' < Bls12_381.Fr.order)) ;
      let* r = Num.add a b in
      let nb_bits = Z.(numbits (add bound bound')) in
      if unsafe then ret (r, bound)
      else
        with_bool_check (Num.is_upper_bounded_unsafe ~nb_bits ~bound r)
        >* ret (r, bound)

    let sub_left ?(unsafe = false) (a, bound) (b, bound') =
      assert (Z.(bound + bound' < Bls12_381.Fr.order)) ;
      let* r = Num.add ~qr:S.mone a b in
      if unsafe then ret (r, bound)
      else with_bool_check (Num.geq (a, bound) (b, bound')) >* ret (r, bound)

    let succ ?(unsafe = false) (a, bound) =
      assert (Z.(bound + one < Bls12_381.Fr.order)) ;
      let* r = Num.add_constant S.one a in
      let nb_bits = Z.(numbits (succ bound)) in
      if unsafe then ret (r, bound)
      else
        with_bool_check (Num.is_upper_bounded_unsafe ~nb_bits ~bound r)
        >* ret (r, bound)

    let f x = x
  end

  module Encoding (L : LIB) = struct
    open L
    module VL = V (L)
    open L.Encodings

    type bound_check_safety = Safe | Unsafe | NoCheck

    let encoding ~safety (bound : 'a Bound.t) : ('a P.t, 'a VL.t, _) encoding =
      let z_bound = Bound.v bound in
      let f =
        match safety with
        | Safe -> with_implicit_bool_check (Num.is_upper_bounded ~bound:z_bound)
        | Unsafe ->
            with_implicit_bool_check
              (Num.is_upper_bounded_unsafe ~bound:z_bound)
        | NoCheck -> Fun.id
      in
      f
      @@ conv
           (fun (x : 'a VL.t) ->
             let w, b = (x : 'a VL.t :> scalar repr * Z.t) in
             assert (z_bound = b) ;
             w)
           (fun w -> VL.make_unsafe w ~bound)
           (fun (x : 'a P.t) ->
             let v, _ = (x : 'a P.t :> Z.t * Z.t) in
             S.of_z v)
           (fun v -> P.make ~unsafe:true (S.to_z v) ~bound)
           scalar_encoding
  end
end
OCaml

Innovation. Community. Security.