Source file kobeissi_bhargavan_blanchet.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
(* Copyright 2021 Diskuv, Inc.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. *)
(* Auto-generated using code from <dirsp-exchange>/src-proscript/proscript-messaging/ps2pv/ast2ocaml.ml *)

include Kobeissi_bhargavan_blanchet_intf

module Make (ProScript : Dirsp_proscript.S) :
  PROTOCOL with type t = ProScript.t = struct
  type t = ProScript.t

  type t_aes_decrypted = ProScript.Crypto.aes_decrypted

  include Kobeissi_bhargavan_blanchet_shims.Make (ProScript)

  module Type_key = struct
    let construct =
      (*
        ----------------
          AUDIT NOTICE
        ----------------
        
        The line and column of the original text that caused the problem with its programmatic description is:
          sp.js:11.18-11.22: <unknown expression> 
        
        The resolution to the problem was:
          The ps2ocaml translator has automatically inserted a call to a 'shim'
          function. The shim function will be defined in the accompanying
          _shims.ml file. Since that function is hand-written, please be sure
          to review the full definition of that shim function. 
      *)
      shim_Type_key_construct


    let toBitstring a =
      ProScript.concat
        [ ProScript.of_string ""
        ; ProScript.Encoding.b2h (ProScript.elem_at a 0)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 1)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 2)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 3)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 4)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 5)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 6)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 7)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 8)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 9)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 10)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 11)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 12)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 13)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 14)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 15)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 16)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 17)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 18)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 19)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 20)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 21)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 22)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 23)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 24)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 25)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 26)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 27)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 28)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 29)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 30)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 31)
        ]


    let fromBitstring a = ProScript.Encoding.hexStringTo32ByteArray a

    let xassert a =
      ProScript.of_elem_list
        [ ProScript.elem_at a 0
        ; ProScript.elem_at a 1
        ; ProScript.elem_at a 2
        ; ProScript.elem_at a 3
        ; ProScript.elem_at a 4
        ; ProScript.elem_at a 5
        ; ProScript.elem_at a 6
        ; ProScript.elem_at a 7
        ; ProScript.elem_at a 8
        ; ProScript.elem_at a 9
        ; ProScript.elem_at a 10
        ; ProScript.elem_at a 11
        ; ProScript.elem_at a 12
        ; ProScript.elem_at a 13
        ; ProScript.elem_at a 14
        ; ProScript.elem_at a 15
        ; ProScript.elem_at a 16
        ; ProScript.elem_at a 17
        ; ProScript.elem_at a 18
        ; ProScript.elem_at a 19
        ; ProScript.elem_at a 20
        ; ProScript.elem_at a 21
        ; ProScript.elem_at a 22
        ; ProScript.elem_at a 23
        ; ProScript.elem_at a 24
        ; ProScript.elem_at a 25
        ; ProScript.elem_at a 26
        ; ProScript.elem_at a 27
        ; ProScript.elem_at a 28
        ; ProScript.elem_at a 29
        ; ProScript.elem_at a 30
        ; ProScript.elem_at a 31
        ]


    let clone a =
      ProScript.of_elem_list
        [ ProScript.elem_at a 0
        ; ProScript.elem_at a 1
        ; ProScript.elem_at a 2
        ; ProScript.elem_at a 3
        ; ProScript.elem_at a 4
        ; ProScript.elem_at a 5
        ; ProScript.elem_at a 6
        ; ProScript.elem_at a 7
        ; ProScript.elem_at a 8
        ; ProScript.elem_at a 9
        ; ProScript.elem_at a 10
        ; ProScript.elem_at a 11
        ; ProScript.elem_at a 12
        ; ProScript.elem_at a 13
        ; ProScript.elem_at a 14
        ; ProScript.elem_at a 15
        ; ProScript.elem_at a 16
        ; ProScript.elem_at a 17
        ; ProScript.elem_at a 18
        ; ProScript.elem_at a 19
        ; ProScript.elem_at a 20
        ; ProScript.elem_at a 21
        ; ProScript.elem_at a 22
        ; ProScript.elem_at a 23
        ; ProScript.elem_at a 24
        ; ProScript.elem_at a 25
        ; ProScript.elem_at a 26
        ; ProScript.elem_at a 27
        ; ProScript.elem_at a 28
        ; ProScript.elem_at a 29
        ; ProScript.elem_at a 30
        ; ProScript.elem_at a 31
        ]
  end

  module Type_iv = struct
    let construct =
      (*
        ----------------
          AUDIT NOTICE
        ----------------
        
        The line and column of the original text that caused the problem with its programmatic description is:
          sp.js:84.18-84.22: <unknown expression> 
        
        The resolution to the problem was:
          The ps2ocaml translator has automatically inserted a call to a 'shim'
          function. The shim function will be defined in the accompanying
          _shims.ml file. Since that function is hand-written, please be sure
          to review the full definition of that shim function. 
      *)
      shim_Type_iv_construct


    let toBitstring a =
      ProScript.concat
        [ ProScript.of_string ""
        ; ProScript.Encoding.b2h (ProScript.elem_at a 0)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 1)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 2)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 3)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 4)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 5)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 6)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 7)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 8)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 9)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 10)
        ; ProScript.Encoding.b2h (ProScript.elem_at a 11)
        ]


    let fromBitstring a = ProScript.Encoding.hexStringTo12ByteArray a

    let xassert a =
      ProScript.of_elem_list
        [ ProScript.elem_at a 0
        ; ProScript.elem_at a 1
        ; ProScript.elem_at a 2
        ; ProScript.elem_at a 3
        ; ProScript.elem_at a 4
        ; ProScript.elem_at a 5
        ; ProScript.elem_at a 6
        ; ProScript.elem_at a 7
        ; ProScript.elem_at a 8
        ; ProScript.elem_at a 9
        ; ProScript.elem_at a 10
        ; ProScript.elem_at a 11
        ]
  end

  module Type_msg = struct
    let construct () =
      { valid = false
      ; ephemeralKey = Type_key.construct ()
      ; initEphemeralKey = Type_key.construct ()
      ; ciphertext = ProScript.of_string ""
      ; iv = Type_iv.construct ()
      ; tag = ProScript.of_string ""
      ; preKeyId = 0
      }


    let xassert a =
      let (_ : bool) = (* no-op statement-by-statement equivalence *) a.valid in
      () ;
      a.ephemeralKey <- Type_key.xassert a.ephemeralKey ;
      a.initEphemeralKey <- Type_key.xassert a.initEphemeralKey ;
      let (_ : t) =
        (* no-op statement-by-statement equivalence *) a.ciphertext
      in
      () ;
      a.iv <- Type_iv.xassert a.iv ;
      let (_ : t) = (* no-op statement-by-statement equivalence *) a.tag in
      () ;
      (*check s+t for overflow and underflow*)
      let s = a.preKeyId in
      let t = 1 in
      if s > 0
      then
        if t <= Int.max_int - s
        then ()
        else
          raise
            (Invalid_argument
               (Format.sprintf "(a.preKeyId+1)=(%d+%d) will overflow" s t) )
      else if s < 0
      then
        if t >= Int.min_int - s
        then ()
        else
          raise
            (Invalid_argument
               (Format.sprintf "(a.preKeyId+1)=(%d+%d) will underflow" s t) ) ;
      a
  end

  module Type_keypair = struct
    let construct () =
      { priv = Type_key.construct (); pub = Type_key.construct () }


    let xassert a =
      a.priv <- Type_key.xassert a.priv ;
      a.pub <- Type_key.xassert a.pub ;
      a


    let clone a =
      let b = construct () in
      b.priv <- Type_key.clone a.priv ;
      b.pub <- Type_key.clone a.pub ;
      b
  end

  module Type_them = struct
    let construct () =
      { signedPreKey = Type_key.construct ()
      ; signedPreKeySignature = ProScript.of_string ""
      ; identityKey = Type_key.construct ()
      ; identityDHKey = Type_key.construct ()
      ; myEphemeralKeyP0 = Type_keypair.construct ()
      ; myEphemeralKeyP1 = Type_keypair.construct ()
      ; ephemeralKey = Type_key.construct ()
      ; myPreKey = Type_keypair.construct ()
      ; preKey = Type_key.construct ()
      ; preKeyId = 0
      ; recvKeys = [| Type_key.construct (); Type_key.construct () |]
      ; sendKeys = [| Type_key.construct (); Type_key.construct () |]
      ; shared = Type_key.construct ()
      ; established = false
      }


    let xassert a =
      a.signedPreKey <- Type_key.xassert a.signedPreKey ;
      a.signedPreKeySignature <-
        ProScript.concat [ a.signedPreKeySignature; ProScript.of_string "" ] ;
      a.identityKey <- Type_key.xassert a.identityKey ;
      a.identityDHKey <- Type_key.xassert a.identityDHKey ;
      a.myEphemeralKeyP0 <- Type_keypair.xassert a.myEphemeralKeyP0 ;
      a.myEphemeralKeyP1 <- Type_keypair.xassert a.myEphemeralKeyP1 ;
      a.ephemeralKey <- Type_key.xassert a.ephemeralKey ;
      a.myPreKey <- Type_keypair.xassert a.myPreKey ;
      a.preKey <- Type_key.xassert a.preKey ;
      (*check s+t for overflow and underflow*)
      let s = a.preKeyId in
      let t = 1 in
      if s > 0
      then
        if t <= Int.max_int - s
        then ()
        else
          raise
            (Invalid_argument
               (Format.sprintf "(a.preKeyId+1)=(%d+%d) will overflow" s t) )
      else if s < 0
      then
        if t >= Int.min_int - s
        then ()
        else
          raise
            (Invalid_argument
               (Format.sprintf "(a.preKeyId+1)=(%d+%d) will underflow" s t) ) ;
      Array.set a.recvKeys 0 (Type_key.xassert a.recvKeys.(0)) ;
      Array.set a.recvKeys 1 (Type_key.xassert a.recvKeys.(1)) ;
      Array.set a.sendKeys 0 (Type_key.xassert a.sendKeys.(0)) ;
      Array.set a.sendKeys 1 (Type_key.xassert a.sendKeys.(1)) ;
      a.shared <- Type_key.xassert a.shared ;
      let (_ : bool) =
        (* no-op statement-by-statement equivalence *) a.established
      in
      () ;
      a
  end

  module Type_sendoutput = struct
    let construct () =
      { them = Type_them.construct (); output = Type_msg.construct () }


    let xassert (a : t record_sendoutput) =
      a.them <- Type_them.xassert a.them ;
      a.output <- Type_msg.xassert a.output ;
      a
  end

  module Type_recvoutput = struct
    let construct () =
      { them = Type_them.construct ()
      ; output = Type_msg.construct ()
      ; plaintext = ProScript.of_string ""
      }


    let xassert a =
      a.them <- Type_them.xassert a.them ;
      a.output <- Type_msg.xassert a.output ;
      let (_ : t) =
        (* no-op statement-by-statement equivalence *) a.plaintext
      in
      () ;
      a
  end

  module UTIL = struct
    let xHKDF ikm salt info =
      let prk = ProScript.Crypto.xHMACSHA256 salt (Type_key.toBitstring ikm) in
      let k0 =
        ProScript.Crypto.xHMACSHA256
          prk
          (ProScript.concat [ info; ProScript.of_string "01" ])
      in
      let k1 =
        ProScript.Crypto.xHMACSHA256
          prk
          (ProScript.concat
             [ Type_key.toBitstring k0; info; ProScript.of_string "02" ] )
      in
      [| k0; k1 |]


    let xQDHInit
        myIdentityKeyPriv
        myInitEphemeralKeyPriv
        theirIdentityKeyPub
        theirSignedPreKeyPub
        theirPreKeyPub =
      Type_key.fromBitstring
        (ProScript.Crypto.xSHA256
           (ProScript.concat
              [ Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     myIdentityKeyPriv
                     theirSignedPreKeyPub )
              ; Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     myInitEphemeralKeyPriv
                     theirIdentityKeyPub )
              ; Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     myInitEphemeralKeyPriv
                     theirSignedPreKeyPub )
              ; Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     myInitEphemeralKeyPriv
                     theirPreKeyPub )
              ] ) )


    let xQDHResp
        myIdentityKeyPriv
        mySignedPreKeyPriv
        myPreKeyPriv
        theirIdentityKeyPub
        theirEphemeralKeyPub =
      Type_key.fromBitstring
        (ProScript.Crypto.xSHA256
           (ProScript.concat
              [ Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     mySignedPreKeyPriv
                     theirIdentityKeyPub )
              ; Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     myIdentityKeyPriv
                     theirEphemeralKeyPub )
              ; Type_key.toBitstring
                  (ProScript.Crypto.xDH25519
                     mySignedPreKeyPriv
                     theirEphemeralKeyPub )
              ; Type_key.toBitstring
                  (ProScript.Crypto.xDH25519 myPreKeyPriv theirEphemeralKeyPub)
              ] ) )


    let newIdentityKey id =
      let identityKeyPriv =
        ProScript.Crypto.random32Bytes
          (ProScript.concat [ ProScript.of_string "aID"; id ])
      in
      Type_keypair.xassert
        { priv = identityKeyPriv
        ; pub = ProScript.Crypto.ED25519.publicKey identityKeyPriv
        }


    let newKeyPair =
      (*
        ----------------
          AUDIT NOTICE
        ----------------
        
        The line and column of the original text that caused the problem with its programmatic description is:
          sp.js:301.42-301.46: <unknown expression> 
        
        The resolution to the problem was:
          The ps2ocaml translator has automatically inserted a call to a 'shim'
          function. The shim function will be defined in the accompanying
          _shims.ml file. Since that function is hand-written, please be sure
          to review the full definition of that shim function. 
      *)
      shim_UTIL_newKeyPair


    let getDHPublicKey =
      (*
        ----------------
          AUDIT NOTICE
        ----------------
        
        The line and column of the original text that caused the problem with its programmatic description is:
          sp.js:310.42-310.46: <unknown expression> 
        
        The resolution to the problem was:
          The ps2ocaml translator has automatically inserted a call to a 'shim'
          function. The shim function will be defined in the accompanying
          _shims.ml file. Since that function is hand-written, please be sure
          to review the full definition of that shim function. 
      *)
      shim_UTIL_getDHPublicKey
  end

  module RATCHET = struct
    let deriveSendKeys (them : t record_them) myEphemeralKeyPriv =
      let kShared =
        ProScript.Crypto.xDH25519 myEphemeralKeyPriv them.ephemeralKey
      in
      let sendKeys =
        UTIL.xHKDF
          kShared
          them.recvKeys.(0)
          (ProScript.of_string "WhisperRatchet")
      in
      let kKeys =
        UTIL.xHKDF
          (ProScript.Crypto.xHMACSHA256 sendKeys.(1) (ProScript.of_string "1"))
          (Type_key.construct ())
          (ProScript.of_string "WhisperMessageKeys")
      in
      { sendKeys; kENC = kKeys.(0) }


    let deriveRecvKeys myShare (them : t record_them) theirEphemeralKeyPub =
      let kShared = ProScript.Crypto.xDH25519 myShare theirEphemeralKeyPub in
      let recvKeys =
        UTIL.xHKDF
          kShared
          them.sendKeys.(0)
          (ProScript.of_string "WhisperRatchet")
      in
      let kKeys =
        UTIL.xHKDF
          (ProScript.Crypto.xHMACSHA256 recvKeys.(1) (ProScript.of_string "1"))
          (Type_key.construct ())
          (ProScript.of_string "WhisperMessageKeys")
      in
      { recvKeys; kENC = kKeys.(0) }


    let tryDecrypt
        myIdentityKey myEphemeralKey (them : t record_them) (msg : t record_msg)
        =
      let keys =
        deriveRecvKeys
          myEphemeralKey.priv
          (Type_them.xassert them)
          msg.ephemeralKey
      in
      let hENC =
        Type_key.fromBitstring
          (ProScript.Crypto.xSHA256
             (ProScript.concat
                [ Type_key.toBitstring keys.kENC; Type_iv.toBitstring msg.iv ] ) )
      in
      let aes =
        ProScript.Crypto.xAESGCMDecrypt
          hENC
          msg.iv
          { ciphertext = msg.ciphertext; tag = msg.tag }
          (ProScript.concat
             [ Type_key.toBitstring msg.initEphemeralKey
             ; Type_key.toBitstring msg.ephemeralKey
             ; Type_key.toBitstring myEphemeralKey.pub
             ; Type_key.toBitstring them.identityKey
             ; Type_key.toBitstring myIdentityKey.pub
             ] )
      in
      aes
  end

  module HANDLE = struct
    let xAKENeeded myIdentityKey initEphemeralKey (them : t record_them) =
      let shared =
        UTIL.xQDHInit
          myIdentityKey.priv
          initEphemeralKey.priv
          them.identityDHKey
          them.signedPreKey
          them.preKey
      in
      let recvKeys =
        UTIL.xHKDF
          shared
          (Type_key.construct ())
          (ProScript.of_string "WhisperRatchet")
      in
      let validSig =
        ProScript.Crypto.ED25519.checkValid
          them.signedPreKeySignature
          (Type_key.toBitstring them.signedPreKey)
          them.identityKey
      in
      { signedPreKey = them.signedPreKey
      ; signedPreKeySignature = them.signedPreKeySignature
      ; identityKey = them.identityKey
      ; identityDHKey = them.identityDHKey
      ; myEphemeralKeyP0 = them.myEphemeralKeyP0
      ; myEphemeralKeyP1 = them.myEphemeralKeyP1
      ; ephemeralKey = them.ephemeralKey
      ; myPreKey = them.myPreKey
      ; preKey = them.preKey
      ; preKeyId = them.preKeyId
      ; recvKeys
      ; sendKeys = them.sendKeys
      ; shared
      ; established = validSig
      }


    let xAKEInit
        myIdentityKey mySignedPreKey (them : t record_them) (msg : t record_msg)
        =
      let shared =
        UTIL.xQDHResp
          myIdentityKey.priv
          mySignedPreKey.priv
          them.myPreKey.priv
          them.identityDHKey
          msg.initEphemeralKey
      in
      let sendKeys =
        UTIL.xHKDF
          shared
          (Type_key.construct ())
          (ProScript.of_string "WhisperRatchet")
      in
      { signedPreKey = them.signedPreKey
      ; signedPreKeySignature = them.signedPreKeySignature
      ; identityKey = them.identityKey
      ; identityDHKey = them.identityDHKey
      ; myEphemeralKeyP0 = them.myEphemeralKeyP0
      ; myEphemeralKeyP1 = them.myEphemeralKeyP1
      ; ephemeralKey = them.ephemeralKey
      ; myPreKey = them.myPreKey
      ; preKey = them.preKey
      ; preKeyId = msg.preKeyId
      ; recvKeys = them.recvKeys
      ; sendKeys
      ; shared
      ; established = true
      }


    let sending
        myIdentityKey (them : t record_them) initEphemeralKeyPub plaintext =
      let keys =
        RATCHET.deriveSendKeys
          (Type_them.xassert them)
          them.myEphemeralKeyP1.priv
      in
      let iv =
        Type_iv.xassert
          (ProScript.Crypto.random12Bytes (ProScript.of_string "a1"))
      in
      let hENC =
        Type_key.fromBitstring
          (ProScript.Crypto.xSHA256
             (ProScript.concat
                [ Type_key.toBitstring keys.kENC; Type_iv.toBitstring iv ] ) )
      in
      let enc =
        ProScript.Crypto.xAESGCMEncrypt
          hENC
          iv
          plaintext
          (ProScript.concat
             [ Type_key.toBitstring initEphemeralKeyPub
             ; Type_key.toBitstring them.myEphemeralKeyP1.pub
             ; Type_key.toBitstring them.ephemeralKey
             ; Type_key.toBitstring myIdentityKey.pub
             ; Type_key.toBitstring them.identityKey
             ] )
      in
      { them =
          { signedPreKey = them.signedPreKey
          ; signedPreKeySignature = them.signedPreKeySignature
          ; identityKey = them.identityKey
          ; identityDHKey = them.identityDHKey
          ; myEphemeralKeyP0 = them.myEphemeralKeyP0
          ; myEphemeralKeyP1 = them.myEphemeralKeyP1
          ; ephemeralKey = them.ephemeralKey
          ; myPreKey = them.myPreKey
          ; preKey = them.preKey
          ; preKeyId = them.preKeyId
          ; recvKeys = them.recvKeys
          ; sendKeys = them.sendKeys
          ; shared = them.shared
          ; established = them.established
          }
      ; output =
          { valid = them.established
          ; ephemeralKey = them.myEphemeralKeyP1.pub
          ; initEphemeralKey = initEphemeralKeyPub
          ; ciphertext = enc.ciphertext
          ; iv
          ; tag = enc.tag
          ; preKeyId = them.preKeyId
          }
      }


    let receiving myIdentityKey (them : t record_them) (msg : t record_msg) =
      let them = Type_them.xassert them in

      (*
        ----------------
          AUDIT NOTICE
        ----------------
        
        The line and column of the original text that caused the problem with its programmatic description is:
          sp.js:478.0-480.3: The ProScript function could not be automatically
          translated. The local variable mutations used in this function were
          too complex for ps2ocaml to handle; ps2ocaml is intentionally kept
          simple (auditable) and will fail with this error for safety. The
          ps2ocaml user can 1. Rewrite the offending ProScript code in static
          single assignment style so the assignment counts = [ dec=2 them=1 ]
          is either empty or all ones. Offending problem location 
        
        The resolution to the problem was:
          The ps2ocaml user has chosen to override the above problem. If you
          are auditing or reviewing, review the OCaml variable mutation
          immediately below and compare it to the original ProScript.
          ProScript/JavaScript mutations change the value of variables, but
          ps2ocaml will only generate OCaml code that creats a new variable. So
          if there are _any_ conditional jumps over a mutation statement (ex.
          a=a+1 within a loop) then the OCaml code will be unsafe and you
          should fail the review 
      *)
      let dec =
        RATCHET.tryDecrypt myIdentityKey them.myEphemeralKeyP1 them msg
      in
      if dec.valid
      then
        (*
          ----------------
            AUDIT NOTICE
          ----------------
          
          The line and column of the original text that caused the problem with its programmatic description is:
            sp.js:482.0-509.4: The ProScript 'return' statement could not be
            automatically translated because ps2ocaml applies conservative
            rules to detect whether a translation can occur. The rule for
            'return' statements is that the enclosing function must be either
            'let in' or sequential semicolon safe. To detect 'let in' safety
            the return value must be a terminal expression. To detect
            sequential semicolon safety, there can be no local variable
            mutations in the enclosing function and it must also satisfy the
            'let in' conditions. The ps2ocaml user can 1. Switch your ProScript
            code to have the return statement as the last statement -or- 2.
            Rewrite the offending ProScript code in static single assignment
            style so the assignment counts = [ dec=2 them=1 ] is either empty
            or all ones. Offending problem location 
          
          The resolution to the problem was:
            The ps2ocaml user has chosen to override the above problem. If you
            are auditing or reviewing, review the OCaml expression immediately
            below to make sure it RETURNS the expression result from the
            function. If there is _any_ subsequent expression executed _after_
            the OCaml expression, then fail the review. Formatting the code
            with ocp-indent or ocamlformat will help visually separate the
            expressions 
        *)
        { them =
            { signedPreKey = them.signedPreKey
            ; signedPreKeySignature = them.signedPreKeySignature
            ; identityKey = them.identityKey
            ; identityDHKey = them.identityDHKey
            ; myEphemeralKeyP0 = them.myEphemeralKeyP1
            ; myEphemeralKeyP1 = UTIL.newKeyPair (ProScript.of_string "a4")
            ; ephemeralKey = msg.ephemeralKey
            ; myPreKey = them.myPreKey
            ; preKey = them.preKey
            ; preKeyId = msg.preKeyId
            ; recvKeys = them.recvKeys
            ; sendKeys = them.sendKeys
            ; shared = them.shared
            ; established = them.established
            }
        ; output =
            { valid = dec.valid && them.established
            ; ephemeralKey = Type_key.construct ()
            ; initEphemeralKey = Type_key.construct ()
            ; ciphertext = ProScript.of_string ""
            ; iv = Type_iv.construct ()
            ; tag = ProScript.of_string ""
            ; preKeyId = msg.preKeyId
            }
        ; plaintext = dec.plaintext
        }
      else
        (*
          ----------------
            AUDIT NOTICE
          ----------------
          
          The line and column of the original text that caused the problem with its programmatic description is:
            sp.js:512.3-512.6: The ProScript function could not be
            automatically translated. The local variable mutations used in this
            function were too complex for ps2ocaml to handle; ps2ocaml is
            intentionally kept simple (auditable) and will fail with this error
            for safety. The ps2ocaml user can 1. Rewrite the offending
            ProScript code in static single assignment style so the assignment
            counts = [ dec=2 them=1 ] is either empty or all ones. Offending
            problem location 
          
          The resolution to the problem was:
            The ps2ocaml user has chosen to override the above problem. If you
            are auditing or reviewing, review the OCaml variable mutation
            immediately below and compare it to the original ProScript.
            ProScript/JavaScript mutations change the value of variables, but
            ps2ocaml will only generate OCaml code that creats a new variable.
            So if there are _any_ conditional jumps over a mutation statement
            (ex. a=a+1 within a loop) then the OCaml code will be unsafe and
            you should fail the review 
        *)
        let dec =
          RATCHET.tryDecrypt myIdentityKey them.myEphemeralKeyP0 them msg
        in

        (*
          ----------------
            AUDIT NOTICE
          ----------------
          
          The line and column of the original text that caused the problem with its programmatic description is:
            sp.js:515.0-542.4: The ProScript 'return' statement could not be
            automatically translated because ps2ocaml applies conservative
            rules to detect whether a translation can occur. The rule for
            'return' statements is that the enclosing function must be either
            'let in' or sequential semicolon safe. To detect 'let in' safety
            the return value must be a terminal expression. To detect
            sequential semicolon safety, there can be no local variable
            mutations in the enclosing function and it must also satisfy the
            'let in' conditions. The ps2ocaml user can 1. Switch your ProScript
            code to have the return statement as the last statement -or- 2.
            Rewrite the offending ProScript code in static single assignment
            style so the assignment counts = [ dec=2 them=1 ] is either empty
            or all ones. Offending problem location 
          
          The resolution to the problem was:
            The ps2ocaml user has chosen to override the above problem. If you
            are auditing or reviewing, review the OCaml expression immediately
            below to make sure it RETURNS the expression result from the
            function. If there is _any_ subsequent expression executed _after_
            the OCaml expression, then fail the review. Formatting the code
            with ocp-indent or ocamlformat will help visually separate the
            expressions 
        *)
        { them =
            { signedPreKey = them.signedPreKey
            ; signedPreKeySignature = them.signedPreKeySignature
            ; identityKey = them.identityKey
            ; identityDHKey = them.identityDHKey
            ; myEphemeralKeyP0 = them.myEphemeralKeyP0
            ; myEphemeralKeyP1 = them.myEphemeralKeyP1
            ; ephemeralKey = msg.ephemeralKey
            ; myPreKey = them.myPreKey
            ; preKey = them.preKey
            ; preKeyId = msg.preKeyId
            ; recvKeys = them.recvKeys
            ; sendKeys = them.sendKeys
            ; shared = them.shared
            ; established = them.established
            }
        ; output =
            { valid = dec.valid && them.established
            ; ephemeralKey = Type_key.construct ()
            ; initEphemeralKey = Type_key.construct ()
            ; ciphertext = ProScript.of_string ""
            ; iv = Type_iv.construct ()
            ; tag = ProScript.of_string ""
            ; preKeyId = msg.preKeyId
            }
        ; plaintext = dec.plaintext
        }
  end

  module TOPLEVEL = struct
    let newSession
        mySignedPreKey
        myPreKey
        theirIdentityKeyPub
        theirIdentityDHKeyPub
        theirSignedPreKeyPub
        theirSignedPreKeySignature
        theirPreKeyPub
        preKeyId =
      { signedPreKey = Type_key.fromBitstring theirSignedPreKeyPub
      ; signedPreKeySignature = theirSignedPreKeySignature
      ; identityKey = Type_key.fromBitstring theirIdentityKeyPub
      ; identityDHKey = Type_key.fromBitstring theirIdentityDHKeyPub
      ; myEphemeralKeyP0 = Type_keypair.xassert mySignedPreKey
      ; myEphemeralKeyP1 = UTIL.newKeyPair (ProScript.of_string "a2")
      ; ephemeralKey = Type_key.fromBitstring theirSignedPreKeyPub
      ; myPreKey = Type_keypair.xassert myPreKey
      ; preKey = Type_key.fromBitstring theirPreKeyPub
      ; preKeyId = preKeyId + 0
      ; recvKeys = [| Type_key.construct (); Type_key.construct () |]
      ; sendKeys = [| Type_key.construct (); Type_key.construct () |]
      ; shared = Type_key.construct ()
      ; established = false
      }


    let send myIdentityKey (them : t record_them) plaintext =
      let myIdentityKey = Type_keypair.xassert myIdentityKey in
      let them = Type_them.xassert them in

      (*
        ----------------
          AUDIT NOTICE
        ----------------
        
        The line and column of the original text that caused the problem with its programmatic description is:
          sp.js:574.0-577.3: The ProScript function could not be automatically
          translated. The local variable mutations used in this function were
          too complex for ps2ocaml to handle; ps2ocaml is intentionally kept
          simple (auditable) and will fail with this error for safety. The
          ps2ocaml user can 1. Rewrite the offending ProScript code in static
          single assignment style so the assignment counts = [
          initEphemeralKey=2 myIdentityKey=1 them=1 ] is either empty or all
          ones. Offending problem location 
        
        The resolution to the problem was:
          The ps2ocaml user has chosen to override the above problem. If you
          are auditing or reviewing, review the OCaml variable mutation
          immediately below and compare it to the original ProScript.
          ProScript/JavaScript mutations change the value of variables, but
          ps2ocaml will only generate OCaml code that creats a new variable. So
          if there are _any_ conditional jumps over a mutation statement (ex.
          a=a+1 within a loop) then the OCaml code will be unsafe and you
          should fail the review 
      *)
      let initEphemeralKey =
        { priv = Type_key.construct (); pub = Type_key.construct () }
      in
      if them.established = false
      then
        (*
          ----------------
            AUDIT NOTICE
          ----------------
          
          The line and column of the original text that caused the problem with its programmatic description is:
            sp.js:579.16-579.19: The ProScript function could not be
            automatically translated. The local variable mutations used in this
            function were too complex for ps2ocaml to handle; ps2ocaml is
            intentionally kept simple (auditable) and will fail with this error
            for safety. The ps2ocaml user can 1. Rewrite the offending
            ProScript code in static single assignment style so the assignment
            counts = [ initEphemeralKey=2 myIdentityKey=1 them=1 ] is either
            empty or all ones. Offending problem location 
          
          The resolution to the problem was:
            The ps2ocaml user has chosen to override the above problem. If you
            are auditing or reviewing, review the OCaml variable mutation
            immediately below and compare it to the original ProScript.
            ProScript/JavaScript mutations change the value of variables, but
            ps2ocaml will only generate OCaml code that creats a new variable.
            So if there are _any_ conditional jumps over a mutation statement
            (ex. a=a+1 within a loop) then the OCaml code will be unsafe and
            you should fail the review 
        *)
        let initEphemeralKey = UTIL.newKeyPair (ProScript.of_string "a3") in

        (*
          ----------------
            AUDIT NOTICE
          ----------------
          
          The line and column of the original text that caused the problem with its programmatic description is:
            sp.js:580.0-585.4: The ProScript 'return' statement could not be
            automatically translated because ps2ocaml applies conservative
            rules to detect whether a translation can occur. The rule for
            'return' statements is that the enclosing function must be either
            'let in' or sequential semicolon safe. To detect 'let in' safety
            the return value must be a terminal expression. To detect
            sequential semicolon safety, there can be no local variable
            mutations in the enclosing function and it must also satisfy the
            'let in' conditions. The ps2ocaml user can 1. Switch your ProScript
            code to have the return statement as the last statement -or- 2.
            Rewrite the offending ProScript code in static single assignment
            style so the assignment counts = [ initEphemeralKey=2
            myIdentityKey=1 them=1 ] is either empty or all ones. Offending
            problem location 
          
          The resolution to the problem was:
            The ps2ocaml user has chosen to override the above problem. If you
            are auditing or reviewing, review the OCaml expression immediately
            below to make sure it RETURNS the expression result from the
            function. If there is _any_ subsequent expression executed _after_
            the OCaml expression, then fail the review. Formatting the code
            with ocp-indent or ocamlformat will help visually separate the
            expressions 
        *)
        HANDLE.sending
          myIdentityKey
          (HANDLE.xAKENeeded myIdentityKey initEphemeralKey them)
          initEphemeralKey.pub
          plaintext
      else
        (*
          ----------------
            AUDIT NOTICE
          ----------------
          
          The line and column of the original text that caused the problem with its programmatic description is:
            sp.js:587.0-592.4: The ProScript 'return' statement could not be
            automatically translated because ps2ocaml applies conservative
            rules to detect whether a translation can occur. The rule for
            'return' statements is that the enclosing function must be either
            'let in' or sequential semicolon safe. To detect 'let in' safety
            the return value must be a terminal expression. To detect
            sequential semicolon safety, there can be no local variable
            mutations in the enclosing function and it must also satisfy the
            'let in' conditions. The ps2ocaml user can 1. Switch your ProScript
            code to have the return statement as the last statement -or- 2.
            Rewrite the offending ProScript code in static single assignment
            style so the assignment counts = [ initEphemeralKey=2
            myIdentityKey=1 them=1 ] is either empty or all ones. Offending
            problem location 
          
          The resolution to the problem was:
            The ps2ocaml user has chosen to override the above problem. If you
            are auditing or reviewing, review the OCaml expression immediately
            below to make sure it RETURNS the expression result from the
            function. If there is _any_ subsequent expression executed _after_
            the OCaml expression, then fail the review. Formatting the code
            with ocp-indent or ocamlformat will help visually separate the
            expressions 
        *)
        HANDLE.sending myIdentityKey them (Type_key.construct ()) plaintext


    let recv
        myIdentityKey mySignedPreKey (them : t record_them) (msg : t record_msg)
        =
      let myIdentityKey = Type_keypair.xassert myIdentityKey in
      let mySignedPreKey = Type_keypair.xassert mySignedPreKey in
      let them = Type_them.xassert them in
      let msg = Type_msg.xassert msg in
      if them.established = false
      then
        HANDLE.receiving
          myIdentityKey
          (HANDLE.xAKEInit myIdentityKey mySignedPreKey them msg)
          msg
      else HANDLE.receiving myIdentityKey them msg
  end
end