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

MinaProtocol / mina / 2977

25 Nov 2024 08:53PM UTC coverage: 60.689% (-1.4%) from 62.102%
2977

push

buildkite

web-flow
Merge pull request #16236 from MinaProtocol/dkijania/implement_test_ledger_apply

Introduce LedgerTestApplyTx to Ci

49216 of 81095 relevant lines covered (60.69%)

473960.49 hits per line

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

94.52
/src/lib/rosetta_coding/coding.ml
1
(* coding.ml -- hex encoding/decoding for Rosetta *)
107✔
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' ->
5,358✔
11
      [ false; false; false; false ]
12
  | '1' ->
2,332✔
13
      [ false; false; false; true ]
14
  | '2' ->
2,423✔
15
      [ false; false; true; false ]
16
  | '3' ->
2,298✔
17
      [ false; false; true; true ]
18
  | '4' ->
2,297✔
19
      [ false; true; false; false ]
20
  | '5' ->
2,105✔
21
      [ false; true; false; true ]
22
  | '6' ->
2,160✔
23
      [ false; true; true; false ]
24
  | '7' ->
2,111✔
25
      [ false; true; true; true ]
26
  | '8' ->
2,212✔
27
      [ true; false; false; false ]
28
  | '9' ->
2,208✔
29
      [ true; false; false; true ]
30
  | 'A' | 'a' ->
11✔
31
      [ true; false; true; false ]
32
  | 'B' | 'b' ->
5✔
33
      [ true; false; true; true ]
34
  | 'C' | 'c' ->
10✔
35
      [ true; true; false; false ]
36
  | 'D' | 'd' ->
6✔
37
      [ true; true; false; true ]
38
  | 'E' | 'e' ->
13✔
39
      [ true; true; true; false ]
40
  | 'F' | 'f' ->
7✔
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)
38,656✔
47
  |> List.fold ~init:0 ~f:( + )
38,656✔
48
  |> fun n ->
38,656✔
49
  let s = sprintf "%0X" n in
38,656✔
50
  s.[0]
38,656✔
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 =
1,812✔
61
    if List.is_empty bits then List.rev acc
1,812✔
62
    else
63
      let bitsn, rest = List.split_n bits n in
77,312✔
64
      go rest (bitsn :: acc)
77,312✔
65
  in
66
  go bits []
67

68
let bits_by_4s = bits_by_n 4
107✔
69

70
let bits_by_8s = bits_by_n 8
107✔
71

72
let of_unpackable (type t) (module M : Packed with type t = t)
73
    ?(padding_bit = false) (packed : t) =
601✔
74
  let bits0 = M.unpack packed |> List.rev in
604✔
75
  assert (Mina_stdlib.List.Length.Compare.(bits0 = 255)) ;
604✔
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
604✔
83
  let bits' = List.concat bytes' in
604✔
84
  let cs = List.map (bits_by_4s bits') ~f:bits4_to_hex_char in
604✔
85
  String.of_char_list cs
604✔
86

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

89
let of_scalar = of_unpackable (module Scalar)
107✔
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) ;
604✔
101
  let bits =
102
    String.to_list raw |> List.map ~f:hex_char_to_bits4 |> List.concat
604✔
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
604✔
107
  let bytes_rev = List.rev bytes in
604✔
108
  let bits' = List.concat bytes_rev in
604✔
109

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

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

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

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

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

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

129
let to_public_key raw =
130
  to_public_key_compressed raw |> Public_key.decompress_exn
2✔
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
1✔
136
  let hex = of_field field0 in
1✔
137
  let field1 = to_field hex in
1✔
138
  Field.equal field0 field1
1✔
139

140
let pk_roundtrip_test () =
141
  let pk =
1✔
142
    { Public_key.Compressed.Poly.x = Field.of_int 123123; is_odd = true }
1✔
143
  in
144
  let hex = of_public_key_compressed pk in
145
  let pk' = to_public_key_compressed hex in
1✔
146
  Public_key.Compressed.equal pk pk'
1✔
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
2✔
156
  let hex' = of_public_key pk in
2✔
157
  String.equal (String.lowercase hex_key) (String.lowercase hex')
2✔
158

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

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

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

166
let%test "public key compressed roundtrip even" =
167
  pk_compressed_roundtrip_test hex_key_even ()
1✔
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 )
107✔
176
  ; ( "public key compressed round-trip even"
177
    , pk_compressed_roundtrip_test hex_key_even )
107✔
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