• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 2841

30 Oct 2024 07:56AM UTC coverage: 33.412% (-27.7%) from 61.098%
2841

push

buildkite

web-flow
Merge pull request #16306 from MinaProtocol/dkijania/fix_promotion_to_gcr

Fix promotion job PUBLISH misuse

22273 of 66661 relevant lines covered (33.41%)

119594.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.59
/src/lib/rosetta_coding/coding.ml
1
(* coding.ml -- hex encoding/decoding for Rosetta *)
10✔
2
open Core_kernel
3
module Field = Snark_params.Tick.Field
4
module Scalar = Snark_params.Tick.Inner_curve.Scalar
5
open Signature_lib
6

7
(* see RFC 0038, section "marshal-keys" for a specification *)
8

9
let hex_char_to_bits4 = function
10
  | '0' ->
×
11
      [ false; false; false; false ]
12
  | '1' ->
×
13
      [ false; false; false; true ]
14
  | '2' ->
×
15
      [ false; false; true; false ]
16
  | '3' ->
×
17
      [ false; false; true; true ]
18
  | '4' ->
×
19
      [ false; true; false; false ]
20
  | '5' ->
×
21
      [ false; true; false; true ]
22
  | '6' ->
×
23
      [ false; true; true; false ]
24
  | '7' ->
×
25
      [ false; true; true; true ]
26
  | '8' ->
×
27
      [ true; false; false; false ]
28
  | '9' ->
×
29
      [ true; false; false; true ]
30
  | 'A' | 'a' ->
×
31
      [ true; false; true; false ]
32
  | 'B' | 'b' ->
×
33
      [ true; false; true; true ]
34
  | 'C' | 'c' ->
×
35
      [ true; true; false; false ]
36
  | 'D' | 'd' ->
×
37
      [ true; true; false; true ]
38
  | 'E' | 'e' ->
×
39
      [ true; true; true; false ]
40
  | 'F' | 'f' ->
×
41
      [ true; true; true; true ]
42
  | _ ->
×
43
      failwith "Expected hex character"
44

45
let bits4_to_hex_char bits =
46
  List.mapi bits ~f:(fun i bit -> if bit then Int.pow 2 (3 - i) else 0)
×
47
  |> List.fold ~init:0 ~f:( + )
×
48
  |> fun n ->
×
49
  let s = sprintf "%0X" n in
×
50
  s.[0]
×
51

52
module type Packed = sig
53
  type t
54

55
  val unpack : t -> bool list
56
end
57

58
(* break of the bits byte by byte *)
59
let bits_by_n n bits =
60
  let rec go bits acc =
×
61
    if List.is_empty bits then List.rev acc
×
62
    else
63
      let bitsn, rest = List.split_n bits n in
×
64
      go rest (bitsn :: acc)
×
65
  in
66
  go bits []
67

68
let bits_by_4s = bits_by_n 4
10✔
69

70
let bits_by_8s = bits_by_n 8
10✔
71

72
let of_unpackable (type t) (module M : Packed with type t = t)
73
    ?(padding_bit = false) (packed : t) =
×
74
  let bits0 = M.unpack packed |> List.rev in
×
75
  assert (Mina_stdlib.List.Length.Compare.(bits0 = 255)) ;
×
76
  (* field elements, scalars are 255 bits, left-pad to get 32 bytes *)
77
  let bits = padding_bit :: bits0 in
78
  (* break of the bits byte by byte *)
79
  (* In our encoding, we want highest bytes at the end and lowest at the
80
     beginning. *)
81
  let bytes = bits_by_8s bits in
82
  let bytes' = List.rev bytes in
×
83
  let bits' = List.concat bytes' in
×
84
  let cs = List.map (bits_by_4s bits') ~f:bits4_to_hex_char in
×
85
  String.of_char_list cs
×
86

87
let of_field = of_unpackable (module Field)
10✔
88

89
let of_scalar = of_unpackable (module Scalar)
10✔
90

91
module type Unpacked = sig
92
  type t
93

94
  val project : bool list -> t
95
end
96

97
let pack (type t) (module M : Unpacked with type t = t) (raw : string) :
98
    bool * t =
99
  (* 256 bits = 64 hex chars *)
100
  assert (Int.equal (String.length raw) 64) ;
×
101
  let bits =
102
    String.to_list raw |> List.map ~f:hex_char_to_bits4 |> List.concat
×
103
  in
104
  (* In our encoding, we have highest bytes at the end and lowest at the
105
     beginning. *)
106
  let bytes = bits_by_8s bits in
×
107
  let bytes_rev = List.rev bytes in
×
108
  let bits' = List.concat bytes_rev in
×
109

110
  let padding_bit = List.hd_exn bits' in
×
111
  (* remove padding bit *)
112
  let bits'' = List.tl_exn bits' |> List.rev in
×
113
  (padding_bit, M.project bits'')
×
114

115
let to_field hex = pack (module Field) hex |> snd
×
116

117
let to_scalar hex = pack (module Scalar) hex |> snd
×
118

119
let of_public_key_compressed pk =
120
  let { Public_key.Compressed.Poly.x; is_odd } = pk in
×
121
  of_field ~padding_bit:is_odd x
122

123
let of_public_key pk = of_public_key_compressed (Public_key.compress pk)
×
124

125
let to_public_key_compressed raw =
126
  let is_odd, x = pack (module Field) raw in
×
127
  { Public_key.Compressed.Poly.x; is_odd }
×
128

129
let to_public_key raw =
130
  to_public_key_compressed raw |> Public_key.decompress_exn
×
131

132
(* inline tests hard-to-impossible to setup with JS *)
133

134
let field_hex_roundtrip_test () =
135
  let field0 = Field.of_int 123123 in
×
136
  let hex = of_field field0 in
×
137
  let field1 = to_field hex in
×
138
  Field.equal field0 field1
×
139

140
let pk_roundtrip_test () =
141
  let pk =
×
142
    { Public_key.Compressed.Poly.x = Field.of_int 123123; is_odd = true }
×
143
  in
144
  let hex = of_public_key_compressed pk in
145
  let pk' = to_public_key_compressed hex in
×
146
  Public_key.Compressed.equal pk pk'
×
147

148
let hex_key_odd =
149
  "fad1d3e31aede102793fb2cce62b4f1e71a214c94ce18ad5756eba67ef398390"
150

151
let hex_key_even =
152
  "7e406ca640115a8c44ece6ef5d0c56af343b1a993d8c871648ab7980ecaf8230"
153

154
let pk_compressed_roundtrip_test hex_key () =
155
  let pk = to_public_key hex_key in
×
156
  let hex' = of_public_key pk in
×
157
  String.equal (String.lowercase hex_key) (String.lowercase hex')
×
158

159
let%test "field_hex round-trip" = field_hex_roundtrip_test ()
×
160

161
let%test "public key round-trip" = pk_roundtrip_test ()
×
162

163
let%test "public key compressed roundtrip odd" =
164
  pk_compressed_roundtrip_test hex_key_odd ()
×
165

166
let%test "public key compressed roundtrip even" =
167
  pk_compressed_roundtrip_test hex_key_even ()
×
168

169
(* for running tests from JS *)
170

171
let unit_tests =
172
  [ ("field-hex round-trip", field_hex_roundtrip_test)
173
  ; ("public key round-trip", pk_roundtrip_test)
174
  ; ( "public key compressed round-trip odd"
175
    , pk_compressed_roundtrip_test hex_key_odd )
10✔
176
  ; ( "public key compressed round-trip even"
177
    , pk_compressed_roundtrip_test hex_key_even )
10✔
178
  ]
179

180
let run_unit_tests () =
181
  List.iter unit_tests ~f:(fun (name, test) ->
×
182
      printf "Running %s test\n%!" name ;
×
183
      assert (test ()) )
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc