-
Notifications
You must be signed in to change notification settings - Fork 62
/
Cryptol.sawcore
1676 lines (1363 loc) · 53.9 KB
/
Cryptol.sawcore
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
-------------------------------------------------------------------------------
-- Cryptol primitives for SAWCore
module Cryptol where
import Prelude;
--------------------------------------------------------------------------------
-- Additional operations on Prelude types
const : (a b : sort 0) -> a -> b -> a;
const a b x y = x;
compose : (a b c : sort 0) -> (b -> c) -> (a -> b) -> (a -> c);
compose _ _ _ f g x = f (g x);
bvExp : (n : Nat) -> Vec n Bool -> Vec n Bool -> Vec n Bool;
bvExp n x y = foldr Bool (Vec n Bool) n
(\ (b : Bool) -> \ (a : Vec n Bool) ->
ite (Vec n Bool) b (bvMul n x (bvMul n a a)) (bvMul n a a))
(bvNat n 1)
(reverse n Bool y);
updFst : (a b : sort 0) -> (a -> a) -> (a * b) -> (a * b);
updFst a b f x = (f x.(1), x.(2));
updSnd : (a b : sort 0) -> (b -> b) -> (a * b) -> (a * b);
updSnd a b f x = (x.(1), f x.(2));
--------------------------------------------------------------------------------
-- Extended natural numbers
data Num : sort 0 where {
TCNum : Nat -> Num;
TCInf : Num;
}
Num_rec : (p: Num -> sort 1) -> ((n:Nat) -> p (TCNum n)) -> p TCInf ->
(n:Num) -> p n;
Num_rec p f1 f2 n = Num#rec p f1 f2 n;
-- Helper function: take a Num that we expect to be finite, and extract its Nat,
-- raising an error if that Num is not finite
getFinNat : (n:Num) -> Nat;
getFinNat n =
Num#rec (\ (n:Num) -> Nat) (\ (n:Nat) -> n)
(error Nat "Unexpected Fin constraint violation!") n;
-- Helper function: destruct a Num that we expect to be finite
finNumRec : (p: Num -> sort 1) -> ((n:Nat) -> p (TCNum n)) ->
(n:Num) -> p n;
finNumRec p f n =
Num#rec p f (error (p TCInf) "Unexpected Fin constraint violation!") n;
-- Helper function: destruct two Nums that we expect to be finite
finNumRec2 : (p: Num -> Num -> sort 1) ->
((m n:Nat) -> p (TCNum m) (TCNum n)) ->
(m n:Num) -> p m n;
finNumRec2 p f =
finNumRec
(\ (m:Num) -> (n:Num) -> p m n)
(\ (m:Nat) -> finNumRec (p (TCNum m)) (f m));
-- Build a binary function on Nums by lifting a binary function on Nats (the
-- first argument) and using additional cases for: when the first argument is a
-- Nat and the second is infinite; when the second is a Nat and the first is
-- infinite; and when both are infinite
binaryNumFun : (Nat -> Nat -> Nat) -> (Nat -> Num) -> (Nat -> Num) -> Num ->
Num -> Num -> Num;
binaryNumFun f1 f2 f3 f4 num1 num2 =
Num#rec (\ (num1':Num) -> Num)
(\ (n1:Nat) ->
Num#rec (\ (num2':Num) -> Num)
(\ (n2:Nat) -> TCNum (f1 n1 n2))
(f2 n1) num2)
(Num#rec (\ (num2':Num) -> Num) f3 f4 num2)
num1;
-- Build a ternary function on Nums by lifting a ternary function on Nats, with
-- a single default case if any of the Nums is infinite
ternaryNumFun : (Nat -> Nat -> Nat -> Nat) -> Num ->
Num -> Num -> Num -> Num;
ternaryNumFun f1 f2 num1 num2 num3 =
Num#rec
(\ (num1':Num) -> Num)
(\ (n1:Nat) ->
Num#rec
(\ (num2':Num) -> Num)
(\ (n2:Nat) ->
Num#rec
(\ (num3':Num) -> Num)
(\ (n3:Nat) -> TCNum (f1 n1 n2 n3))
f2 num3)
f2 num2)
f2 num1;
tcWidth : Num -> Num;
tcWidth n = Num#rec (\ (n:Num) -> Num)
(\ (x:Nat) -> TCNum (widthNat x)) TCInf n;
tcAdd : Num -> Num -> Num;
tcAdd =
binaryNumFun addNat (\ (x:Nat) -> TCInf) (\ (y:Nat) -> TCInf) TCInf;
tcSub : Num -> Num -> Num;
tcSub =
binaryNumFun subNat
-- x - infinity = 0
(\ (x:Nat) -> TCNum 0)
-- infinity - y = infinity
(\ (y:Nat) -> TCInf)
-- infinity - infinity = 0
(TCNum 0);
tcMul : Num -> Num -> Num;
tcMul =
binaryNumFun mulNat
(\ (x:Nat) -> if0Nat Num x (TCNum 0) TCInf)
(\ (y:Nat) -> if0Nat Num y (TCNum 0) TCInf)
TCInf;
tcDiv : Num -> Num -> Num;
tcDiv =
binaryNumFun (\ (x:Nat) -> \ (y:Nat) -> divNat x y)
(\ (x:Nat) -> TCNum 0)
(\ (y:Nat) -> TCInf)
-- infinity / infinity = 1
(TCNum 1);
tcMod : Num -> Num -> Num;
tcMod =
binaryNumFun (\ (x:Nat) -> \ (y:Nat) -> modNat x y)
(\ (x:Nat) -> TCNum 0)
-- infinity % y = 0, since y*infinity + 0 = infinity
(\ (y:Nat) -> TCNum 0)
-- infinity % infinity = 0
(TCNum 0);
tcExp : Num -> Num -> Num;
tcExp =
binaryNumFun expNat
(\ (x:Nat) ->
natCase
(\ (_:Nat) -> Num) (TCNum 0)
(\ (x_minus_1:Nat) ->
if0Nat Num x_minus_1 (TCNum 1) TCInf)
x)
(\ (y:Nat) -> if0Nat Num y (TCNum 1) TCInf)
TCInf;
tcMin : Num -> Num -> Num;
tcMin =
binaryNumFun minNat (\ (x:Nat) -> TCNum x) (\ (y:Nat) -> TCNum y) TCInf;
tcMax : Num -> Num -> Num;
tcMax =
binaryNumFun maxNat (\ (x:Nat) -> TCInf) (\ (y:Nat) -> TCInf) TCInf;
ceilDivNat : Nat -> Nat -> Nat;
ceilDivNat x y = divNat (addNat x (subNat y 1)) y;
ceilModNat : Nat -> Nat -> Nat;
ceilModNat x y = subNat (mulNat (ceilDivNat x y) y) x;
tcCeilDiv : Num -> Num -> Num;
tcCeilDiv =
binaryNumFun ceilDivNat (\ (x:Nat) -> TCNum 0) (\ (y:Nat) -> TCInf) TCInf;
tcCeilMod : Num -> Num -> Num;
tcCeilMod =
binaryNumFun ceilModNat (\ (x:Nat) -> TCNum 0) (\ (y:Nat) -> TCInf) TCInf;
tcLenFromThenTo_Nat : Nat -> Nat -> Nat -> Nat;
tcLenFromThenTo_Nat x y z =
ite Nat (ltNat x y)
(ite Nat (ltNat z x) 0
(addNat (divNat (subNat z x) (subNat y x)) 1)) -- increasing
(ite Nat (ltNat x z) 0
(addNat (divNat (subNat x z) (subNat x y)) 1)); -- decreasing
tcLenFromThenTo : Num -> Num -> Num -> Num;
tcLenFromThenTo = ternaryNumFun tcLenFromThenTo_Nat TCInf;
--------------------------------------------------------------------------------
-- Possibly infinite sequences
seq : Num -> sort 0 -> sort 0;
seq num a =
Num#rec (\ (num:Num) -> sort 0) (\ (n:Nat) -> Vec n a) (Stream a) num;
-- FIXME: this rule should be derived by scDefRewriteRules
seq_TCNum : (n:Nat) -> (a:sort 0) -> Eq (sort 0) (seq (TCNum n) a) (Vec n a);
seq_TCNum n a = Refl (sort 0) (Vec n a);
seq_TCInf : (a:sort 0) -> Eq (sort 0) (seq TCInf a) (Stream a);
seq_TCInf a = Refl (sort 0) (Stream a);
seqMap : (a b : sort 0) -> (n : Num) -> (a -> b) -> seq n a -> seq n b;
seqMap a b num f =
Num#rec (\ (n:Num) -> seq n a -> seq n b) (map a b f) (streamMap a b f) num;
seqConst : (n : Num) -> (a : sort 0) -> a -> seq n a;
seqConst n =
Num#rec (\ (n:Num) -> (a : sort 0) -> a -> seq n a) replicate streamConst n;
--------------------------------------------------------------------------------
-- Integers mod n
IntModNum : (num : Num) -> sort 0;
IntModNum num =
Num#rec (\ (n : Num) -> sort 0) IntMod Integer num;
-------------------------------------------------------------------------------
-- Rationals (TODO)
Rational : sort 0;
Rational = #();
ecRatio : Integer -> Integer -> Rational;
ecRatio x y = ();
eqRational : Rational -> Rational -> Bool;
eqRational x y = error Bool "Unimplemented: (==) Rational";
ltRational : Rational -> Rational -> Bool;
ltRational x y = error Bool "Unimplemented: (<) Rational";
addRational : Rational -> Rational -> Rational;
addRational x y = error Rational "Unimplemented: (+) Rational";
subRational : Rational -> Rational -> Rational;
subRational x y = error Rational "Unimplemented: (-) Rational";
mulRational : Rational -> Rational -> Rational;
mulRational x y = error Rational "Unimplemented: (*) Rational";
negRational : Rational -> Rational;
negRational x = error Rational "Unimplemented: negate Rational";
integerToRational : Integer -> Rational;
integerToRational x = error Rational "Unimplemented: fromInteger Rational";
--------------------------------------------------------------------------------
-- Type coercions
seq_cong : (m : Num) -> (n : Num) -> (a : sort 0) -> (b : sort 0) ->
Eq Num m n -> Eq (sort 0) a b -> Eq (sort 0) (seq m a) (seq n b);
seq_cong m n a b eq_mn eq_ab =
trans
(sort 0) (seq m a) (seq n a) (seq n b)
(eq_cong Num m n eq_mn (sort 0) (\ (x:Num) -> seq x a))
(eq_cong (sort 0) a b eq_ab (sort 0) (\ (x:sort 0) -> seq n x));
seq_cong1 : (m : Num) -> (n : Num) -> (a : sort 0) ->
Eq Num m n -> Eq (sort 0) (seq m a) (seq n a);
seq_cong1 m n a eq_mn =
eq_cong Num m n eq_mn (sort 0) (\ (x:Num) -> seq x a);
IntModNum_cong :
(m : Num) -> (n : Num) -> Eq Num m n -> Eq (sort 0) (IntModNum m) (IntModNum n);
IntModNum_cong m n eq_mn =
eq_cong Num m n eq_mn (sort 0) IntModNum;
fun_cong : (a : sort 0) -> (b : sort 0) -> (c : sort 0) -> (d : sort 0) ->
Eq (sort 0) a b -> Eq (sort 0) c d -> Eq (sort 0) (a -> c) (b -> d);
fun_cong a b c d eq_ab eq_cd =
trans
(sort 0) (a -> c) (b -> c) (b -> d)
(eq_cong (sort 0) a b eq_ab (sort 0) (\ (x:sort 0) -> (x -> c)))
(eq_cong (sort 0) c d eq_cd (sort 0) (\ (x:sort 0) -> (b -> x)));
pair_cong : (a : sort 0) -> (a' : sort 0) -> (b : sort 0) -> (b' : sort 0) ->
Eq (sort 0) a a' -> Eq (sort 0) b b' -> Eq (sort 0) (a * b) (a' * b');
pair_cong a a' b b' eq_a eq_b =
trans
(sort 0) (a * b) (a' * b) (a' * b')
(eq_cong (sort 0) a a' eq_a (sort 0) (\ (x:sort 0) -> (x * b)))
(eq_cong (sort 0) b b' eq_b (sort 0) (\ (x:sort 0) -> (a' * x)));
pair_cong1 : (a : sort 0) -> (a' : sort 0) -> (b : sort 0) ->
Eq (sort 0) a a' -> Eq (sort 0) (a * b) (a' * b);
pair_cong1 a a' b eq_a =
(eq_cong (sort 0) a a' eq_a (sort 0) (\ (x:sort 0) -> (x * b)));
pair_cong2 : (a : sort 0) -> (b : sort 0) -> (b' : sort 0) ->
Eq (sort 0) b b' -> Eq (sort 0) (a * b) (a * b');
pair_cong2 a b b' eq_b =
(eq_cong (sort 0) b b' eq_b (sort 0) (\ (x:sort 0) -> (a * x)));
axiom unsafeAssert_same_Num :
(n : Num) -> Eq (Eq Num n n) (unsafeAssert Num n n) (Refl Num n);
--------------------------------------------------------------------------------
-- Auxiliary functions
eListSel : (a : sort 0) -> (n : Num) -> seq n a -> Nat -> a;
eListSel a n =
Num#rec (\ (num:Num) -> seq num a -> Nat -> a)
(\ (n:Nat) -> at n a) (streamGet a) n;
--------------------------------------------------------------------------------
-- List comprehensions
from : (a b : sort 0) -> (m n : Num) -> seq m a -> (a -> seq n b) ->
seq (tcMul m n) (a * b);
from a b m n =
Num#rec
(\ (m:Num) -> seq m a -> (a -> seq n b) -> seq (tcMul m n) (a * b))
(\ (m:Nat) ->
Num#rec
(\ (n:Num) -> Vec m a -> (a -> seq n b) ->
seq (tcMul (TCNum m) n) (a * b))
-- Case 1: (TCNum m, TCNum n)
(\ (n:Nat) ->
\ (xs : Vec m a) ->
\ (k : a -> Vec n b) ->
join m n (a * b)
(map a (Vec n (a * b))
(\ (x : a) ->
map b (a * b) (\ (y : b) -> (x, y)) n (k x))
m xs))
-- Case 2: n = (TCNum m, TCInf)
(natCase
(\ (m':Nat) -> (Vec m' a -> (a -> Stream b) ->
seq (if0Nat Num m' (TCNum 0) TCInf) (a * b)))
(\ (xs : Vec 0 a) ->
\ (k : a -> Stream b) -> EmptyVec (a * b))
(\ (m' : Nat) ->
\ (xs : Vec (Succ m') a) ->
\ (k : a -> Stream b) ->
(\ (x : a) -> streamMap b (a * b) (\ (y:b) -> (x, y)) (k x))
(at (Succ m') a xs 0))
m)
n)
(Num#rec
(\ (n:Num) -> Stream a -> (a -> seq n b) -> seq (tcMul TCInf n) (a * b))
-- Case 3: (TCInf, TCNum n)
(\ (n:Nat) ->
natCase
(\ (n':Nat) -> (Stream a -> (a -> Vec n' b) ->
seq (if0Nat Num n' (TCNum 0) TCInf) (a * b)))
(\ (xs : Stream a) ->
\ (k : a -> Vec 0 b) -> EmptyVec (a * b))
(\ (n' : Nat) ->
\ (xs : Stream a) ->
\ (k : a -> Vec (Succ n') b) ->
streamJoin
(a * b) n'
(streamMap
a (Vec (Succ n') (a * b))
(\ (x:a) ->
map b (a * b) (\ (y:b) -> (x, y)) (Succ n') (k x))
xs))
n)
-- Case 4: (TCInf, TCInf)
(\ (xs : Stream a) ->
\ (k : a -> Stream b) ->
(\ (x : a) -> streamMap b (a * b) (\ (y : b) -> (x, y)) (k x))
(streamGet a xs 0))
n)
m;
mlet : (a b : sort 0) -> (n : Num) -> a -> (a -> seq n b) -> seq n (a * b);
mlet a b n =
Num#rec
(\ (n:Num) -> a -> (a -> seq n b) -> seq n (a * b))
(\ (n:Nat) -> \ (x:a) -> \ (f:a -> Vec n b) ->
map b (a * b) (\ (y : b) -> (x, y)) n (f x))
(\ (x:a) -> \ (f:a -> Stream b) ->
streamMap b (a * b) (\ (y : b) -> (x, y)) (f x))
n;
seqZip : (a b : sort 0) -> (m n : Num) -> seq m a -> seq n b ->
seq (tcMin m n) (a * b);
seqZip a b m n =
Num#rec
(\ (m:Num) -> seq m a -> seq n b -> seq (tcMin m n) (a * b))
(\ (m : Nat) ->
Num#rec
(\ (n:Num) -> Vec m a -> seq n b -> seq (tcMin (TCNum m) n) (a * b))
(\ (n:Nat) -> zip a b m n)
(\ (xs:Vec m a) -> \ (ys:Stream b) ->
gen m (a * b) (\ (i : Nat) -> (at m a xs i, streamGet b ys i)))
n)
(Num#rec
(\ (n:Num) -> Stream a -> seq n b -> seq (tcMin TCInf n) (a * b))
(\ (n:Nat) ->
\ (xs:Stream a) -> \ (ys:Vec n b) ->
gen n (a * b) (\ (i : Nat) -> (streamGet a xs i, at n b ys i)))
(streamMap2 a b (a * b) (\ (x:a) -> \ (y:b) -> (x, y)))
n)
m;
--------------------------------------------------------------------------------
-- Ring and Logic functions
seqBinary : (n : Num) -> (a : sort 0) -> (a -> a -> a) ->
seq n a -> seq n a -> seq n a;
seqBinary num a f =
Num#rec
(\ (n:Num) -> seq n a -> seq n a -> seq n a)
(\ (n:Nat) -> zipWith a a a f n)
(streamMap2 a a a f)
num;
unitUnary : #() -> #();
unitUnary _ = ();
unitBinary : #() -> #() -> #();
unitBinary _ _ = ();
pairUnary : (a b : sort 0) -> (a -> a) -> (b -> b) -> (a * b) -> (a * b);
pairUnary a b f g xy = (f (fst a b xy), g (snd a b xy));
pairBinary : (a b : sort 0) -> (a -> a -> a) -> (b -> b -> b)
-> (a * b) -> (a * b) -> (a * b);
pairBinary a b f g x12 y12 = (f (fst a b x12) (fst a b y12),
g (snd a b x12) (snd a b y12));
funBinary : (a b : sort 0) -> (b -> b -> b) -> (a -> b) -> (a -> b) -> (a -> b);
funBinary a b op f g x = op (f x) (g x);
errorUnary : (s : String) -> (a : sort 0) -> a -> a;
errorUnary s a _ = error a s;
errorBinary : (s : String) -> (a : sort 0) -> a -> a -> a;
errorBinary s a _ _ = error a s;
--------------------------------------------------------------------------------
-- Comparisons
boolCmp : Bool -> Bool -> Bool -> Bool;
boolCmp x y k = ite Bool x (and y k) (or y k);
integerCmp : Integer -> Integer -> Bool -> Bool;
integerCmp x y k = or (intLt x y) (and (intEq x y) k);
rationalCmp : Rational -> Rational -> Bool -> Bool;
rationalCmp x y k = or (ltRational x y) (and (eqRational x y) k);
bvCmp : (n : Nat) -> Vec n Bool -> Vec n Bool -> Bool -> Bool;
bvCmp n x y k = or (bvult n x y) (and (bvEq n x y) k);
bvSCmp : (n : Nat) -> Vec n Bool -> Vec n Bool -> Bool -> Bool;
bvSCmp n x y k = or (bvslt n x y) (and (bvEq n x y) k);
vecCmp : (n : Nat) -> (a : sort 0) -> (a -> a -> Bool -> Bool)
-> (Vec n a -> Vec n a -> Bool -> Bool);
vecCmp n a f xs ys k =
foldr (Bool -> Bool) Bool n (\ (f : Bool -> Bool) -> f) k
(zipWith a a (Bool -> Bool) f n xs ys);
unitCmp : #() -> #() -> Bool -> Bool;
unitCmp _ _ _ = False;
pairCmp : (a b : sort 0) -> (a -> a -> Bool -> Bool) -> (b -> b -> Bool -> Bool)
-> a * b -> a * b -> Bool -> Bool;
pairCmp a b f g x12 y12 k =
f (fst a b x12) (fst a b y12) (g (snd a b x12) (snd a b y12) k);
--------------------------------------------------------------------------------
-- Dictionaries and overloading
-- Eq class
PEq : sort 0 -> sort 1;
PEq a = #{ eq : a -> a -> Bool };
PEqBit : PEq Bool;
PEqBit = { eq = boolEq };
PEqInteger : PEq Integer;
PEqInteger = { eq = intEq };
PEqRational : PEq Rational;
PEqRational = { eq = eqRational };
PEqIntMod : (n : Nat) -> PEq (IntMod n);
PEqIntMod n = { eq = intModEq n };
PEqIntModNum : (num : Num) -> PEq (IntModNum num);
PEqIntModNum num =
Num#rec (\ (n : Num) -> PEq (IntModNum n)) PEqIntMod PEqInteger num;
PEqVec : (n : Nat) -> (a : sort 0) -> PEq a -> PEq (Vec n a);
PEqVec n a pa = { eq = vecEq n a pa.eq };
PEqSeq : (n : Num) -> (a : sort 0) -> PEq a -> PEq (seq n a);
PEqSeq n =
Num#rec (\ (n:Num) -> (a : sort 0) -> PEq a -> PEq (seq n a))
(\ (n:Nat) -> PEqVec n)
(\ (a:sort 0) (pa : PEq a) -> error (PEq (Stream a)) "invalid Eq instance")
n;
PEqWord : (n : Nat) -> PEq (Vec n Bool);
PEqWord n = { eq = bvEq n };
PEqSeqBool : (n : Num) -> PEq (seq n Bool);
PEqSeqBool n =
Num#rec (\ (n : Num) -> PEq (seq n Bool))
(\ (n:Nat) -> PEqWord n)
(error (PEq (Stream Bool)) "invalid Eq instance")
n;
PEqUnit : PEq #();
PEqUnit = { eq = \ (x y : #()) -> True };
PEqPair : (a b : sort 0) -> PEq a -> PEq b -> PEq (a * b);
PEqPair a b pa pb = { eq = pairEq a b pa.eq pb.eq };
-- Cmp class
PCmp : sort 0 -> sort 1;
PCmp a =
#{ cmpEq : PEq a
, cmp : a -> a -> Bool -> Bool
};
PCmpBit : PCmp Bool;
PCmpBit = { cmpEq = PEqBit, cmp = boolCmp };
PCmpInteger : PCmp Integer;
PCmpInteger = { cmpEq = PEqInteger, cmp = integerCmp };
PCmpRational : PCmp Rational;
PCmpRational = { cmpEq = PEqRational, cmp = rationalCmp };
PCmpVec : (n : Nat) -> (a : sort 0) -> PCmp a -> PCmp (Vec n a);
PCmpVec n a pa = { cmpEq = PEqVec n a pa.cmpEq, cmp = vecCmp n a pa.cmp };
PCmpSeq : (n : Num) -> (a : sort 0) -> PCmp a -> PCmp (seq n a);
PCmpSeq n =
Num#rec (\ (n:Num) -> (a : sort 0) -> PCmp a -> PCmp (seq n a))
(\ (n:Nat) -> PCmpVec n)
(\ (a:sort 0) (pa : PCmp a) -> error (PCmp (Stream a)) "invalid Cmp instance")
n;
PCmpWord : (n : Nat) -> PCmp (Vec n Bool);
PCmpWord n = { cmpEq = PEqWord n, cmp = bvCmp n };
PCmpSeqBool : (n : Num) -> PCmp (seq n Bool);
PCmpSeqBool n =
Num#rec (\ (n : Num) -> PCmp (seq n Bool))
(\ (n:Nat) -> PCmpWord n)
(error (PCmp (Stream Bool)) "invalid Cmp instance")
n;
PCmpUnit : PCmp #();
PCmpUnit = { cmpEq = PEqUnit, cmp = unitCmp };
PCmpPair : (a b : sort 0) -> PCmp a -> PCmp b -> PCmp (a * b);
PCmpPair a b pa pb =
{ cmpEq = PEqPair a b pa.cmpEq pb.cmpEq
, cmp = pairCmp a b pa.cmp pb.cmp
};
-- SignedCmp class
PSignedCmp : sort 0 -> sort 1;
PSignedCmp a =
#{ signedCmpEq : PEq a
, scmp : a -> a -> Bool -> Bool
};
PSignedCmpVec : (n : Nat) -> (a : sort 0) -> PSignedCmp a -> PSignedCmp (Vec n a);
PSignedCmpVec n a pa =
{ signedCmpEq = PEqVec n a pa.signedCmpEq
, scmp = vecCmp n a pa.scmp
};
PSignedCmpSeq : (n : Num) -> (a : sort 0) -> PSignedCmp a -> PSignedCmp (seq n a);
PSignedCmpSeq n =
Num#rec (\ (n:Num) -> (a : sort 0) -> PSignedCmp a -> PSignedCmp (seq n a))
(\ (n:Nat) -> PSignedCmpVec n)
(\ (a:sort 0) (pa : PSignedCmp a) -> error (PSignedCmp (Stream a)) "invalid SignedCmp instance")
n;
PSignedCmpWord : (n : Nat) -> PSignedCmp (Vec n Bool);
PSignedCmpWord n = { signedCmpEq = PEqWord n, scmp = bvSCmp n };
PSignedCmpSeqBool : (n : Num) -> PSignedCmp (seq n Bool);
PSignedCmpSeqBool n =
Num#rec (\ (n : Num) -> PSignedCmp (seq n Bool))
(\ (n:Nat) -> PSignedCmpWord n)
(error (PSignedCmp (Stream Bool)) "invalid SignedCmp instance")
n;
PSignedCmpUnit : PSignedCmp #();
PSignedCmpUnit = { signedCmpEq = PEqUnit, scmp = unitCmp };
PSignedCmpPair : (a b : sort 0) -> PSignedCmp a -> PSignedCmp b -> PSignedCmp (a * b);
PSignedCmpPair a b pa pb =
{ signedCmpEq = PEqPair a b pa.signedCmpEq pb.signedCmpEq
, scmp = pairCmp a b pa.scmp pb.scmp
};
-- Zero class
PZero : sort 0 -> sort 0;
PZero a = a;
PZeroBit : PZero Bool;
PZeroBit = False;
PZeroInteger : PZero Integer;
PZeroInteger = natToInt 0;
PZeroIntMod : (n : Nat) -> PZero (IntMod n);
PZeroIntMod n = toIntMod n (natToInt 0);
PZeroRational : PZero Rational;
PZeroRational = integerToRational (natToInt 0);
PZeroIntModNum : (num : Num) -> PZero (IntModNum num);
PZeroIntModNum num = Num#rec (\ (n : Num) -> PZero (IntModNum n)) PZeroIntMod PZeroInteger num;
PZeroSeq : (n : Num) -> (a : sort 0) -> PZero a -> PZero (seq n a);
PZeroSeq n a pa = seqConst n a pa;
PZeroSeqBool : (n : Num) -> PZero (seq n Bool);
PZeroSeqBool n =
Num#rec (\ (n:Num) -> PZero (seq n Bool))
(\ (n:Nat) -> bvNat n 0)
(streamConst Bool False)
n;
PZeroFun : (a b : sort 0) -> PZero b -> PZero (a -> b);
PZeroFun a b pb = (\(_ : a) -> pb);
-- Logic class
PLogic : sort 0 -> sort 1;
PLogic a =
#{ logicZero : PZero a
, and : a -> a -> a
, or : a -> a -> a
, xor : a -> a -> a
, not : a -> a
};
PLogicBit : PLogic Bool;
PLogicBit =
{ logicZero = PZeroBit
, and = and
, or = or
, xor = xor
, not = not
};
PLogicVec : (n : Nat) -> (a : sort 0) -> PLogic a -> PLogic (Vec n a);
PLogicVec n a pa =
{ logicZero = replicate n a pa.logicZero
, and = zipWith a a a pa.and n
, or = zipWith a a a pa.or n
, xor = zipWith a a a pa.xor n
, not = map a a pa.not n
};
PLogicStream : (a : sort 0) -> PLogic a -> PLogic (Stream a);
PLogicStream a pa =
{ logicZero = streamConst a pa.logicZero
, and = streamMap2 a a a pa.and
, or = streamMap2 a a a pa.or
, xor = streamMap2 a a a pa.xor
, not = streamMap a a pa.not
};
PLogicSeq : (n : Num) -> (a : sort 0) -> PLogic a -> PLogic (seq n a);
PLogicSeq n =
Num#rec (\ (n:Num) -> (a:sort 0) -> PLogic a -> PLogic (seq n a))
(\ (n:Nat) -> PLogicVec n) PLogicStream n;
PLogicWord : (n : Nat) -> PLogic (Vec n Bool);
PLogicWord n =
{ logicZero = bvNat n 0
, and = bvAnd n
, or = bvOr n
, xor = bvXor n
, not = bvNot n
};
PLogicSeqBool : (n : Num) -> PLogic (seq n Bool);
PLogicSeqBool n =
Num#rec (\ (n:Num) -> PLogic (seq n Bool))
(\ (n:Nat) -> PLogicWord n) (PLogicStream Bool PLogicBit) n;
PLogicFun : (a b : sort 0) -> PLogic b -> PLogic (a -> b);
PLogicFun a b pb =
{ logicZero = PZeroFun a b pb.logicZero
, and = funBinary a b pb.and
, or = funBinary a b pb.or
, xor = funBinary a b pb.xor
, not = compose a b b pb.not
};
PLogicUnit : PLogic #();
PLogicUnit =
{ logicZero = ()
, and = unitBinary
, or = unitBinary
, xor = unitBinary
, not = unitUnary
};
PLogicPair : (a b : sort 0) -> PLogic a -> PLogic b -> PLogic (a * b);
PLogicPair a b pa pb =
{ logicZero = (pa.logicZero, pb.logicZero)
, and = pairBinary a b pa.and pb.and
, or = pairBinary a b pa.or pb.or
, xor = pairBinary a b pa.xor pb.xor
, not = pairUnary a b pa.not pb.not
};
-- Ring class
PRing : sort 0 -> sort 1;
PRing a =
#{ ringZero : PZero a
, add : a -> a -> a
, sub : a -> a -> a
, mul : a -> a -> a
, neg : a -> a
, int : Integer -> a
};
PRingInteger : PRing Integer;
PRingInteger =
{ ringZero = PZeroInteger
, add = intAdd
, sub = intSub
, mul = intMul
, neg = intNeg
, int = \ (i : Integer) -> i
};
PRingIntMod : (n : Nat) -> PRing (IntMod n);
PRingIntMod n =
{ ringZero = PZeroIntMod n
, add = intModAdd n
, sub = intModSub n
, mul = intModMul n
, neg = intModNeg n
, int = toIntMod n
};
PRingIntModNum : (num : Num) -> PRing (IntModNum num);
PRingIntModNum num =
Num#rec (\ (n : Num) -> PRing (IntModNum n)) PRingIntMod PRingInteger num;
PRingRational : PRing Rational;
PRingRational =
{ ringZero = PZeroRational
, add = addRational
, sub = subRational
, mul = mulRational
, neg = negRational
, int = integerToRational
};
PRingVec : (n : Nat) -> (a : sort 0) -> PRing a -> PRing (Vec n a);
PRingVec n a pa =
{ ringZero = replicate n a pa.ringZero
, add = zipWith a a a pa.add n
, sub = zipWith a a a pa.sub n
, mul = zipWith a a a pa.mul n
, neg = map a a pa.neg n
, int = \ (i : Integer) -> replicate n a (pa.int i)
};
PRingStream : (a : sort 0) -> PRing a -> PRing (Stream a);
PRingStream a pa =
{ ringZero = streamConst a pa.ringZero
, add = streamMap2 a a a pa.add
, sub = streamMap2 a a a pa.sub
, mul = streamMap2 a a a pa.mul
, neg = streamMap a a pa.neg
, int = \ (i : Integer) -> streamConst a (pa.int i)
};
PRingSeq : (n : Num) -> (a : sort 0) -> PRing a -> PRing (seq n a);
PRingSeq n =
Num#rec (\ (n : Num) -> (a : sort 0) -> PRing a -> PRing (seq n a))
(\ (n:Nat) -> PRingVec n)
PRingStream
n;
PRingWord : (n : Nat) -> PRing (Vec n Bool);
PRingWord n =
{ ringZero = bvNat n 0
, add = bvAdd n
, sub = bvSub n
, mul = bvMul n
, neg = bvNeg n
, int = intToBv n
};
PRingSeqBool : (n : Num) -> PRing (seq n Bool);
PRingSeqBool n =
Num#rec (\ (n:Num) -> PRing (seq n Bool))
(\ (n:Nat) -> PRingWord n)
(error (PRing (Stream Bool)) "PRingSeqBool: no instance for streams")
n;
PRingFun : (a b : sort 0) -> PRing b -> PRing (a -> b);
PRingFun a b pb =
{ ringZero = PZeroFun a b pb.ringZero
, add = funBinary a b pb.add
, sub = funBinary a b pb.sub
, mul = funBinary a b pb.mul
, neg = compose a b b pb.neg
, int = \ (i : Integer) -> \ (_ : a) -> pb.int i
};
PRingUnit : PRing #();
PRingUnit =
{ ringZero = ()
, add = unitBinary
, sub = unitBinary
, mul = unitBinary
, neg = unitUnary
, int = \ (i : Integer) -> ()
};
PRingPair : (a b : sort 0) -> PRing a -> PRing b -> PRing (a * b);
PRingPair a b pa pb =
{ ringZero = (pa.ringZero, pb.ringZero)
, add = pairBinary a b pa.add pb.add
, sub = pairBinary a b pa.sub pb.sub
, mul = pairBinary a b pa.mul pb.mul
, neg = pairUnary a b pa.neg pb.neg
, int = \ (i : Integer) -> (pa.int i, pb.int i)
};
-- Integral class
PIntegral : sort 0 -> sort 1;
PIntegral a =
#{ integralRing : PRing a
, div : a -> a -> a
, mod : a -> a -> a
, toInt : a -> Integer
, posNegCases :
(r : sort 0) ->
(Nat -> r) ->
(Nat -> r) ->
a -> r
};
PIntegralInteger : PIntegral Integer;
PIntegralInteger =
{ integralRing = PRingInteger
, div = intDiv
, mod = intMod
, toInt = \(i:Integer) -> i
, posNegCases = \ (r:sort 0) -> \ (pos neg:Nat -> r) -> \ (i:Integer) ->
ite r (intLe (natToInt 0) i) (pos (intToNat i)) (neg (intToNat (intNeg i)))
};
PIntegralWord : (n : Nat) -> PIntegral (Vec n Bool);
PIntegralWord n =
{ integralRing = PRingWord n
, div = bvUDiv n
, mod = bvURem n
, toInt = bvToInt n
-- words are always considered non-negative
, posNegCases = \ (r:sort 0) -> \ (pos neg:Nat -> r) -> \(i:Vec n Bool) -> pos (bvToNat n i)
};
PIntegralSeqBool : (n : Num) -> PIntegral (seq n Bool);
PIntegralSeqBool n =
Num#rec (\ (n:Num) -> PIntegral (seq n Bool))
(\ (n:Nat) -> PIntegralWord n)
(error (PIntegral (Stream Bool)) "PIntegralSeqBool: no instance for streams")
n;
-- Field class
PField : sort 0 -> sort 1;
PField a =
#{ fieldRing : PRing a
, recip : a -> a
, fieldDiv : a -> a -> a
};
PFieldRational : PField Rational;
PFieldRational =
{ fieldRing = PRingRational
, recip = \(x : Rational) -> error Rational "Unimplemented: recip Rational"
, fieldDiv = \(x y : Rational) -> error Rational "Unimplemented: (/.) Rational"
};
PFieldIntMod : (n : Nat) -> PField (IntMod n);
PFieldIntMod n =
{ fieldRing = PRingIntMod n
, recip = \(x : IntMod n) -> error (IntMod n) "Unimplemented: recip IntMod"
, fieldDiv = \(x y : IntMod n) -> error (IntMod n) "Unimplemented: (/.) IntMod"
};
PFieldIntModNum : (n : Num) -> PField (IntModNum n);
PFieldIntModNum num =
Num#rec (\ (n : Num) -> PField (IntModNum n))
PFieldIntMod
(error (PField (IntModNum TCInf)) "PFieldIntModNum: no instance for inf")
num;
-- Round class
PRound : sort 0 -> sort 1;
PRound a =
#{ roundField : PField a
, roundCmp : PCmp a
, floor : a -> Integer
, ceiling : a -> Integer
, trunc : a -> Integer
, roundAway : a -> Integer
, roundToEven : a -> Integer
};
PRoundRational : PRound Rational;
PRoundRational =
{ roundField = PFieldRational
, roundCmp = PCmpRational
, floor = \(x : Rational) -> error Integer "Unimplemented: floor Rational"
, ceiling = \(x : Rational) -> error Integer "Unimplemented: ceiling Rational"
, trunc = \(x : Rational) -> error Integer "Unimplemented: trunc Rational"
, roundAway = \(x : Rational) -> error Integer "Unimplemented: roundAway Rational"
, roundToEven = \(x : Rational) -> error Integer "Unimplemented: roundToEven Rational"
};
-- Literal class
-- Compared to Cryptol class 'Literal val a', we omit the 'val' parameter here.
-- As 'PLiteral' and 'PLiteralLessThan' are definitionally equal in saw-core,
-- the same dictionary constructors do double duty for both type classes.
PLiteral : (a : sort 0) -> sort 0;
PLiteral a = Nat -> a;
PLiteralLessThan : (a : sort 0) -> sort 0;
PLiteralLessThan a = Nat -> a;
PLiteralSeqBool : (n : Num) -> PLiteral (seq n Bool);
PLiteralSeqBool n =
Num#rec (\ (n : Num) -> PLiteral (seq n Bool)) bvNat
(error (PLiteral (Stream Bool)) "PLiteralSeqBool: no instance for streams") n;
PLiteralBit : PLiteral Bool;
PLiteralBit = Nat_cases Bool False (\ (n:Nat) -> \ (b:Bool) -> True);
PLiteralInteger : PLiteral Integer;
PLiteralInteger = natToInt;
PLiteralIntMod : (n : Nat) -> PLiteral (IntMod n);
PLiteralIntMod n = \ (x : Nat) -> toIntMod n (natToInt x);
PLiteralIntModNum : (num : Num) -> PLiteral (IntModNum num);
PLiteralIntModNum num =
Num#rec (\ (n : Num) -> PLiteral (IntModNum n)) PLiteralIntMod PLiteralInteger num;
PLiteralRational : PLiteral Rational;
PLiteralRational = \ (x : Nat) -> error Rational "Unimplemented: Literal Rational";
-- TODO: FLiteral class
--------------------------------------------------------------------------------
-- Primitive Cryptol functions
ecNumber : (val : Num) -> (a : sort 0) -> PLiteral a -> a;
ecNumber val a pa =
Num#rec (\ (_ : Num) -> a) pa (pa 0) val;
-- Dummy case: treat `inf as `0 (this never happens anyway)
ecFromZ : (n : Num) -> IntModNum n -> Integer;
ecFromZ n =
Num#rec (\ (n : Num) -> IntModNum n -> Integer)
fromIntMod
(\ (x : Integer) -> x)
n;
-- Ring
ecFromInteger : (a : sort 0) -> PRing a -> Integer -> a;
ecFromInteger a pa = pa.int;
ecPlus : (a : sort 0) -> PRing a -> a -> a -> a;
ecPlus a pa = pa.add;
ecMinus : (a : sort 0) -> PRing a -> a -> a -> a;
ecMinus a pa = pa.sub;