package octez-libs

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

Source file gadget_schnorr.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
(*****************************************************************************)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module Make
    (Curve : Mec.CurveSig.AffineEdwardsT) (H : sig
      module P : Hash_sig.P_HASH

      module V : Hash_sig.HASH
    end) =
struct
  module Curve = Curve
  open Lang_core

  (* vanilla implementation of Schnorr signature using ec-jubjub
     * general idea based on
     * https://github.com/dusk-network/schnorr/blob/main/src/key_variants/single_key.rs *)
  module P : sig
    type pk = Curve.t

    type signature = {
      sig_u_bytes : bool list;
      sig_r : Curve.t;
      c_bytes : bool list;
    }

    type sk = Curve.Scalar.t

    val neuterize : sk -> pk

    val sign : ?compressed:bool -> sk -> S.t -> Curve.Scalar.t -> signature

    val verify :
      ?compressed:bool ->
      msg:S.t ->
      pk:pk ->
      signature:signature ->
      unit ->
      bool
  end = struct
    module H = H.P

    (* S.t and Curve.t are the same but Curve.t is abstract *)
    let of_bls_scalar s = S.of_z (Curve.Base.to_z s)

    type sk = Curve.Scalar.t

    type pk = Curve.t

    type signature = {
      sig_u_bytes : bool list;
      sig_r : Curve.t;
      c_bytes : bool list;
    }

    let neuterize sk = Curve.mul Curve.one sk

    let bls_scalar_to_curve_scalar s = Curve.Scalar.of_z (S.to_z s)

    let hash_full a =
      let ctx = H.init () in
      let ctx = H.digest ctx a in
      H.get ctx

    let sign ?(compressed = false) sk msg rand =
      let r = Curve.mul Curve.one rand in
      let r_u = Curve.get_u_coordinate r |> of_bls_scalar in
      let r_v = Curve.get_v_coordinate r |> of_bls_scalar in
      let c =
        if compressed then H.direct ~input_length:2 [|r_u; msg|]
        else hash_full [|r_u; r_v; msg|]
      in

      let u =
        Curve.Scalar.sub
          rand
          (Curve.Scalar.mul (bls_scalar_to_curve_scalar c) sk)
      in
      let nb_bits_base = Z.numbits S.order in
      let sig_u_bytes =
        Utils.bool_list_of_z ~nb_bits:nb_bits_base (Curve.Scalar.to_z u)
      in
      let c_bytes = Utils.bool_list_of_z ~nb_bits:nb_bits_base (S.to_z c) in
      {sig_u_bytes; sig_r = r; c_bytes}

    let verify ?(compressed = false) ~msg ~pk ~signature () =
      let sig_u =
        Curve.Scalar.of_z @@ Utils.bool_list_to_z signature.sig_u_bytes
      in
      let sig_c = Utils.bool_list_to_scalar signature.c_bytes in
      let sig_r_u = Curve.get_u_coordinate signature.sig_r |> of_bls_scalar in
      let sig_r_v = Curve.get_v_coordinate signature.sig_r |> of_bls_scalar in
      let c =
        if compressed then H.direct ~input_length:2 [|sig_r_u; msg|]
        else hash_full [|sig_r_u; sig_r_v; msg|]
      in
      let c_check = S.eq c sig_c in
      let challenge_r =
        Curve.add
          (Curve.mul Curve.one sig_u)
          (Curve.mul
             pk
             (Curve.Scalar.of_z @@ Utils.bool_list_to_z signature.c_bytes))
      in
      c_check && signature.sig_r = challenge_r
  end

  open Lang_stdlib
  open Gadget_edwards

  module V : functor (L : LIB) -> sig
    module Affine : Affine_curve_intf.S_Edwards with module L = L

    open L
    open Affine
    open Encodings

    (* TODO make abstract once compression is done with encodings *)
    type pk = point

    val pk_encoding : (P.pk, pk repr, pk) encoding

    type signature = {
      sig_u_bytes : bool list repr;
      sig_r : point repr;
      c_bytes : bool list repr;
    }

    val signature_encoding :
      (P.signature, signature, bool list * (pk * bool list)) encoding

    val verify :
      ?compressed:bool ->
      g:point repr ->
      msg:scalar repr ->
      pk:pk repr ->
      signature:signature ->
      unit ->
      bool repr t
  end =
  functor
    (L : LIB)
    ->
    struct
      module Affine = MakeAffine (Curve) (L)
      open Affine
      open L
      open Encodings

      open H.V (L)

      type pk = point

      let point_encoding : (Curve.t, pk repr, pk) encoding =
        let curve_base_to_s c = Lang_core.S.of_z @@ Curve.Base.to_z c in
        let curve_base_of_s c = Curve.Base.of_z @@ Lang_core.S.to_z c in
        with_implicit_bool_check is_on_curve
        @@ conv
             (fun r -> of_pair r)
             (fun (u, v) -> pair u v)
             (fun c ->
               ( curve_base_to_s @@ Curve.get_u_coordinate c,
                 curve_base_to_s @@ Curve.get_v_coordinate c ))
             (fun (u, v) ->
               Curve.from_coordinates_exn
                 ~u:(curve_base_of_s u)
                 ~v:(curve_base_of_s v))
             (obj2_encoding scalar_encoding scalar_encoding)

      let pk_encoding = point_encoding

      type signature = {
        sig_u_bytes : bool list repr;
        sig_r : point repr;
        c_bytes : bool list repr;
      }

      let signature_encoding =
        conv
          (fun {sig_u_bytes; sig_r; c_bytes} -> (sig_u_bytes, (sig_r, c_bytes)))
          (fun (sig_u_bytes, (sig_r, c_bytes)) -> {sig_u_bytes; sig_r; c_bytes})
          (fun ({sig_u_bytes; sig_r; c_bytes} : P.signature) ->
            (sig_u_bytes, (sig_r, c_bytes)))
          (fun (sig_u_bytes, (sig_r, c_bytes)) -> {sig_u_bytes; sig_r; c_bytes})
          (obj3_encoding
             (atomic_list_encoding bool_encoding)
             point_encoding
             (atomic_list_encoding bool_encoding))

      (* In the compressed variant, we drop sig_r_v of the challenge input as it
         can be represented with a single bit: its parity (because of Edwards curves
         are symmetric). We do so as we want to use a hash function with a fixed
         input length of 2 to minimize the number of constraints. We can still prove
         the security of this scheme using the Forking lemma and forking thrice.

         If we really want to include sig_r_v in the challenge input, we could use
         its parity bit instead of the whole scalar and compress it with the message
         if it is either small or the output of a hash. This however is expensive as
         we have to check the decomposition of sig_r_v with the parity bit.

         Netherless, if the msg input actually is the output of the hash,
         (msg = H(msg')) of a single scalar, we could fit sig_r_v in this inner hash
          while keeping the full security:
         c = H( g**r; msg) = H(g**r; H(msg'))
                           =  H( r_u, r_v, H(msg'))
                           ~  H( r_u, H(msg', r_v))  (equivalent in term of security)
         Doing this would not lead to an additional compression round in the hash,
         and as such the resulting overhead would be minimal (one constraint for
         Poseidon128).

         Assuming that max_account = max_counter = 2**32 (< 10**10) in the Transfer
         circuit (privacy-team/plompiler/test/benchmark.ml), and max_amount =
         max_fee = 2**64 (< 10**20), the signature message can be compressed in one
         scalar:
             msg = H(msg') = H(src || dst || fee || amount || counter)
             len(msg') = 192 < BLS12-381.Fr order ~ 2**255
         As such, we could use the above scheme to compute the challenge:
         c = H(r_u, H(src || dst || fee || amount || counter, r_v)) *)

      let hash ~compressed sig_r msg =
        with_label ~label:"Schnorr.hash"
        @@
        let sig_r_x = get_x_coordinate sig_r in
        if compressed then digest ~input_length:2 @@ to_list [sig_r_x; msg]
        else
          let sig_r_y = get_y_coordinate sig_r in
          digest @@ to_list [sig_r_x; sig_r_y; msg]

      (* Requires : [c_bytes] is computed correctly by the prover.
         Otherwise an assert is triggered. *)
      (* TODO: now msg is just one scalar, it will probably be a list of scalars *)
      let verify ?(compressed = false) ~g ~msg ~pk ~signature () =
        with_label ~label:"Schnorr.verify"
        @@
        (* assert bytes' length *)
        let {sig_u_bytes; sig_r; c_bytes} = signature in
        assert (
          List.compare_length_with (of_list sig_u_bytes) (Z.numbits base_order)
          = 0) ;
        assert (
          List.compare_length_with (of_list c_bytes) (Z.numbits base_order) = 0) ;

        (* generating challenge *)
        let* c = hash ~compressed sig_r msg in
        (* check challenge binary decomposition : of_bytes c_bytes = c *)
        let* c' = Num.scalar_of_bytes c_bytes in
        let* e = equal c' c in
        (* re-computing randomness challenge_r = u * G_E + c * pk *)
        let* challenge_r =
          let scalars = to_list [sig_u_bytes; c_bytes] in
          let points = to_list [g; pk] in
          multi_scalar_mul scalars points
        in
        (* check signature randomness sig_r equals challenge_r *)
        let* b = equal sig_r challenge_r in
        Bool.band e b
    end
end
OCaml

Innovation. Community. Security.