-
Notifications
You must be signed in to change notification settings - Fork 123
/
Generic.hs
1965 lines (1674 loc) · 62.1 KB
/
Generic.hs
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
-- |
-- Module : Cryptol.Eval.Generic
-- Copyright : (c) 2013-2020 Galois, Inc.
-- License : BSD3
-- Maintainer : cryptol@galois.com
-- Stability : provisional
-- Portability : portable
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Cryptol.Eval.Generic where
import qualified Control.Exception as X
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad (join, unless)
import Data.Bits (testBit)
import Data.Maybe (fromMaybe)
import Data.Ratio ((%))
import Cryptol.TypeCheck.AST
import Cryptol.TypeCheck.Solver.InfNat (Nat'(..),nMul,widthInteger)
import Cryptol.Eval.Backend
import Cryptol.Eval.Concrete.Value (Concrete(..))
import Cryptol.Eval.Monad
import Cryptol.Eval.Type
import Cryptol.Eval.Value
import Cryptol.Utils.Panic (panic)
import Cryptol.Utils.RecordMap
{-# SPECIALIZE mkLit :: Concrete -> TValue -> Integer -> Eval (GenValue Concrete)
#-}
-- | Make a numeric literal value at the given type.
mkLit :: Backend sym => sym -> TValue -> Integer -> SEval sym (GenValue sym)
mkLit sym ty i =
case ty of
TVInteger -> VInteger <$> integerLit sym i
TVIntMod m
| m == 0 -> evalPanic "mkLit" ["0 modulus not allowed"]
| otherwise -> VInteger <$> integerLit sym (i `mod` m)
TVFloat e p -> VFloat <$> fpLit sym e p (fromInteger i)
TVSeq w TVBit -> pure $ word sym w i
TVRational -> VRational <$> (intToRational sym =<< integerLit sym i)
_ -> evalPanic "Cryptol.Eval.Prim.evalConst"
[ "Invalid type for number" ]
{-# SPECIALIZE ecNumberV :: Concrete -> GenValue Concrete
#-}
-- | Make a numeric constant.
ecNumberV :: Backend sym => sym -> GenValue sym
ecNumberV sym =
nlam $ \valT ->
VPoly $ \ty ->
case valT of
Nat v -> mkLit sym ty v
_ -> evalPanic "Cryptol.Eval.Prim.evalConst"
["Unexpected Inf in constant."
, show valT
, show ty
]
{-# SPECIALIZE intV :: Concrete -> Integer -> TValue -> Eval (GenValue Concrete)
#-}
intV :: Backend sym => sym -> SInteger sym -> TValue -> SEval sym (GenValue sym)
intV sym i = ringNullary sym (\w -> wordFromInt sym w i) (pure i) (\m -> intToZn sym m i) (intToRational sym i)
(\e p -> fpRndMode sym >>= \r -> fpFromInteger sym e p r i)
{-# SPECIALIZE ratioV :: Concrete -> GenValue Concrete #-}
ratioV :: Backend sym => sym -> GenValue sym
ratioV sym =
lam $ \x -> return $
lam $ \y ->
do x' <- fromVInteger <$> x
y' <- fromVInteger <$> y
VRational <$> ratio sym x' y'
{-# SPECIALIZE ecFractionV :: Concrete -> GenValue Concrete
#-}
ecFractionV :: Backend sym => sym -> GenValue sym
ecFractionV sym =
ilam \n ->
ilam \d ->
ilam \_r ->
VPoly \ty ->
case ty of
TVFloat e p -> VFloat <$> fpLit sym e p (n % d)
TVRational ->
do x <- integerLit sym n
y <- integerLit sym d
VRational <$> ratio sym x y
_ -> evalPanic "ecFractionV"
[ "Unexpected `FLiteral` type: " ++ show ty ]
{-# SPECIALIZE fromZV :: Concrete -> GenValue Concrete #-}
fromZV :: Backend sym => sym -> GenValue sym
fromZV sym =
nlam $ \(finNat' -> n) ->
lam $ \v -> VInteger <$> (znToInt sym n . fromVInteger =<< v)
-- Operation Lifting -----------------------------------------------------------
type Binary sym = TValue -> GenValue sym -> GenValue sym -> SEval sym (GenValue sym)
{-# SPECIALIZE binary :: Binary Concrete -> GenValue Concrete
#-}
binary :: Backend sym => Binary sym -> GenValue sym
binary f = tlam $ \ ty ->
lam $ \ a -> return $
lam $ \ b -> do
--io $ putStrLn "Entering a binary function"
join (f ty <$> a <*> b)
type Unary sym = TValue -> GenValue sym -> SEval sym (GenValue sym)
{-# SPECIALIZE unary :: Unary Concrete -> GenValue Concrete
#-}
unary :: Backend sym => Unary sym -> GenValue sym
unary f = tlam $ \ ty ->
lam $ \ a -> f ty =<< a
type BinWord sym = Integer -> SWord sym -> SWord sym -> SEval sym (SWord sym)
{-# SPECIALIZE ringBinary :: Concrete -> BinWord Concrete ->
(SInteger Concrete -> SInteger Concrete -> SEval Concrete (SInteger Concrete)) ->
(Integer -> SInteger Concrete -> SInteger Concrete -> SEval Concrete (SInteger Concrete)) ->
(SRational Concrete -> SRational Concrete -> SEval Concrete (SRational Concrete)) ->
(SFloat Concrete -> SFloat Concrete -> SEval Concrete (SFloat Concrete)) ->
Binary Concrete
#-}
ringBinary :: forall sym.
Backend sym =>
sym ->
BinWord sym ->
(SInteger sym -> SInteger sym -> SEval sym (SInteger sym)) ->
(Integer -> SInteger sym -> SInteger sym -> SEval sym (SInteger sym)) ->
(SRational sym -> SRational sym -> SEval sym (SRational sym)) ->
(SFloat sym -> SFloat sym -> SEval sym (SFloat sym)) ->
Binary sym
ringBinary sym opw opi opz opq opfp = loop
where
loop' :: TValue
-> SEval sym (GenValue sym)
-> SEval sym (GenValue sym)
-> SEval sym (GenValue sym)
loop' ty l r = join (loop ty <$> l <*> r)
loop :: TValue
-> GenValue sym
-> GenValue sym
-> SEval sym (GenValue sym)
loop ty l r = case ty of
TVBit ->
evalPanic "ringBinary" ["Bit not in class Ring"]
TVInteger ->
VInteger <$> opi (fromVInteger l) (fromVInteger r)
TVIntMod n ->
VInteger <$> opz n (fromVInteger l) (fromVInteger r)
TVFloat {} ->
VFloat <$> opfp (fromVFloat l) (fromVFloat r)
TVRational ->
VRational <$> opq (fromVRational l) (fromVRational r)
TVArray{} ->
evalPanic "arithBinary" ["Array not in class Ring"]
TVSeq w a
-- words and finite sequences
| isTBit a -> do
lw <- fromVWord sym "ringLeft" l
rw <- fromVWord sym "ringRight" r
return $ VWord w (WordVal <$> opw w lw rw)
| otherwise -> VSeq w <$> (join (zipSeqMap (loop a) <$>
(fromSeq "ringBinary left" l) <*>
(fromSeq "ringBinary right" r)))
TVStream a ->
-- streams
VStream <$> (join (zipSeqMap (loop a) <$>
(fromSeq "ringBinary left" l) <*>
(fromSeq "ringBinary right" r)))
-- functions
TVFun _ ety ->
return $ lam $ \ x -> loop' ety (fromVFun l x) (fromVFun r x)
-- tuples
TVTuple tys ->
do ls <- mapM (sDelay sym Nothing) (fromVTuple l)
rs <- mapM (sDelay sym Nothing) (fromVTuple r)
return $ VTuple (zipWith3 loop' tys ls rs)
-- records
TVRec fs ->
do VRecord <$>
traverseRecordMap
(\f fty -> sDelay sym Nothing (loop' fty (lookupRecord f l) (lookupRecord f r)))
fs
TVAbstract {} ->
evalPanic "ringBinary" ["Abstract type not in `Ring`"]
type UnaryWord sym = Integer -> SWord sym -> SEval sym (SWord sym)
{-# SPECIALIZE ringUnary ::
Concrete ->
UnaryWord Concrete ->
(SInteger Concrete -> SEval Concrete (SInteger Concrete)) ->
(Integer -> SInteger Concrete -> SEval Concrete (SInteger Concrete)) ->
(SRational Concrete -> SEval Concrete (SRational Concrete)) ->
(SFloat Concrete -> SEval Concrete (SFloat Concrete)) ->
Unary Concrete
#-}
ringUnary :: forall sym.
Backend sym =>
sym ->
UnaryWord sym ->
(SInteger sym -> SEval sym (SInteger sym)) ->
(Integer -> SInteger sym -> SEval sym (SInteger sym)) ->
(SRational sym -> SEval sym (SRational sym)) ->
(SFloat sym -> SEval sym (SFloat sym)) ->
Unary sym
ringUnary sym opw opi opz opq opfp = loop
where
loop' :: TValue -> SEval sym (GenValue sym) -> SEval sym (GenValue sym)
loop' ty v = loop ty =<< v
loop :: TValue -> GenValue sym -> SEval sym (GenValue sym)
loop ty v = case ty of
TVBit ->
evalPanic "ringUnary" ["Bit not in class Ring"]
TVInteger ->
VInteger <$> opi (fromVInteger v)
TVIntMod n ->
VInteger <$> opz n (fromVInteger v)
TVFloat {} ->
VFloat <$> opfp (fromVFloat v)
TVRational ->
VRational <$> opq (fromVRational v)
TVArray{} ->
evalPanic "arithUnary" ["Array not in class Ring"]
TVSeq w a
-- words and finite sequences
| isTBit a -> do
wx <- fromVWord sym "ringUnary" v
return $ VWord w (WordVal <$> opw w wx)
| otherwise -> VSeq w <$> (mapSeqMap (loop a) =<< fromSeq "ringUnary" v)
TVStream a ->
VStream <$> (mapSeqMap (loop a) =<< fromSeq "ringUnary" v)
-- functions
TVFun _ ety ->
return $ lam $ \ y -> loop' ety (fromVFun v y)
-- tuples
TVTuple tys ->
do as <- mapM (sDelay sym Nothing) (fromVTuple v)
return $ VTuple (zipWith loop' tys as)
-- records
TVRec fs ->
VRecord <$>
traverseRecordMap
(\f fty -> sDelay sym Nothing (loop' fty (lookupRecord f v)))
fs
TVAbstract {} -> evalPanic "ringUnary" ["Abstract type not in `Ring`"]
{-# SPECIALIZE ringNullary ::
Concrete ->
(Integer -> SEval Concrete (SWord Concrete)) ->
SEval Concrete (SInteger Concrete) ->
(Integer -> SEval Concrete (SInteger Concrete)) ->
SEval Concrete (SRational Concrete) ->
(Integer -> Integer -> SEval Concrete (SFloat Concrete)) ->
TValue ->
SEval Concrete (GenValue Concrete)
#-}
ringNullary :: forall sym.
Backend sym =>
sym ->
(Integer -> SEval sym (SWord sym)) ->
SEval sym (SInteger sym) ->
(Integer -> SEval sym (SInteger sym)) ->
SEval sym (SRational sym) ->
(Integer -> Integer -> SEval sym (SFloat sym)) ->
TValue ->
SEval sym (GenValue sym)
ringNullary sym opw opi opz opq opfp = loop
where
loop :: TValue -> SEval sym (GenValue sym)
loop ty =
case ty of
TVBit -> evalPanic "ringNullary" ["Bit not in class Ring"]
TVInteger -> VInteger <$> opi
TVIntMod n -> VInteger <$> opz n
TVFloat e p -> VFloat <$> opfp e p
TVRational -> VRational <$> opq
TVArray{} -> evalPanic "arithNullary" ["Array not in class Ring"]
TVSeq w a
-- words and finite sequences
| isTBit a -> pure $ VWord w $ (WordVal <$> opw w)
| otherwise ->
do v <- sDelay sym Nothing (loop a)
pure $ VSeq w $ IndexSeqMap $ const v
TVStream a ->
do v <- sDelay sym Nothing (loop a)
pure $ VStream $ IndexSeqMap $ const v
TVFun _ b ->
do v <- sDelay sym Nothing (loop b)
pure $ lam $ const $ v
TVTuple tys ->
do xs <- mapM (sDelay sym Nothing . loop) tys
pure $ VTuple xs
TVRec fs ->
do xs <- traverse (sDelay sym Nothing . loop) fs
pure $ VRecord xs
TVAbstract {} ->
evalPanic "ringNullary" ["Abstract type not in `Ring`"]
{-# SPECIALIZE integralBinary :: Concrete -> BinWord Concrete ->
(SInteger Concrete -> SInteger Concrete -> SEval Concrete (SInteger Concrete)) ->
Binary Concrete
#-}
integralBinary :: forall sym.
Backend sym =>
sym ->
BinWord sym ->
(SInteger sym -> SInteger sym -> SEval sym (SInteger sym)) ->
Binary sym
integralBinary sym opw opi ty l r = case ty of
TVInteger ->
VInteger <$> opi (fromVInteger l) (fromVInteger r)
-- bitvectors
TVSeq w a
| isTBit a ->
do wl <- fromVWord sym "integralBinary left" l
wr <- fromVWord sym "integralBinary right" r
return $ VWord w (WordVal <$> opw w wl wr)
_ -> evalPanic "integralBinary" [show ty ++ " not int class `Integral`"]
---------------------------------------------------------------------------
-- Ring
{-# SPECIALIZE fromIntegerV :: Concrete -> GenValue Concrete
#-}
-- | Convert an unbounded integer to a value in Ring
fromIntegerV :: Backend sym => sym -> GenValue sym
fromIntegerV sym =
tlam $ \ a ->
lam $ \ v ->
do i <- fromVInteger <$> v
intV sym i a
{-# INLINE addV #-}
addV :: Backend sym => sym -> Binary sym
addV sym = ringBinary sym opw opi opz opq opfp
where
opw _w x y = wordPlus sym x y
opi x y = intPlus sym x y
opz m x y = znPlus sym m x y
opq x y = rationalAdd sym x y
opfp x y = fpRndMode sym >>= \r -> fpPlus sym r x y
{-# INLINE subV #-}
subV :: Backend sym => sym -> Binary sym
subV sym = ringBinary sym opw opi opz opq opfp
where
opw _w x y = wordMinus sym x y
opi x y = intMinus sym x y
opz m x y = znMinus sym m x y
opq x y = rationalSub sym x y
opfp x y = fpRndMode sym >>= \r -> fpMinus sym r x y
{-# INLINE negateV #-}
negateV :: Backend sym => sym -> Unary sym
negateV sym = ringUnary sym opw opi opz opq opfp
where
opw _w x = wordNegate sym x
opi x = intNegate sym x
opz m x = znNegate sym m x
opq x = rationalNegate sym x
opfp x = fpNeg sym x
{-# INLINE mulV #-}
mulV :: Backend sym => sym -> Binary sym
mulV sym = ringBinary sym opw opi opz opq opfp
where
opw _w x y = wordMult sym x y
opi x y = intMult sym x y
opz m x y = znMult sym m x y
opq x y = rationalMul sym x y
opfp x y = fpRndMode sym >>= \r -> fpMult sym r x y
--------------------------------------------------
-- Integral
{-# INLINE divV #-}
divV :: Backend sym => sym -> Binary sym
divV sym = integralBinary sym opw opi
where
opw _w x y = wordDiv sym x y
opi x y = intDiv sym x y
{-# SPECIALIZE expV :: Concrete -> GenValue Concrete #-}
expV :: Backend sym => sym -> GenValue sym
expV sym =
tlam $ \aty ->
tlam $ \ety ->
lam $ \am -> return $
lam $ \em ->
do a <- am
e <- em
case ety of
TVInteger ->
let ei = fromVInteger e in
case integerAsLit sym ei of
Just n
| n == 0 ->
do onei <- integerLit sym 1
intV sym onei aty
| n > 0 ->
do ebits <- enumerateIntBits' sym n ei
computeExponent sym aty a ebits
| otherwise -> raiseError sym NegativeExponent
Nothing -> liftIO (X.throw (UnsupportedSymbolicOp "integer exponentiation"))
TVSeq _w el | isTBit el ->
do ebits <- enumerateWordValue sym =<< fromWordVal "(^^)" e
computeExponent sym aty a ebits
_ -> evalPanic "expV" [show ety ++ " not int class `Integral`"]
{-# SPECIALIZE computeExponent ::
Concrete -> TValue -> GenValue Concrete -> [SBit Concrete] -> SEval Concrete (GenValue Concrete)
#-}
computeExponent :: Backend sym =>
sym -> TValue -> GenValue sym -> [SBit sym] -> SEval sym (GenValue sym)
computeExponent sym aty a bs0 =
do onei <- integerLit sym 1
one <- intV sym onei aty
loop one (dropLeadingZeros bs0)
where
dropLeadingZeros [] = []
dropLeadingZeros (b:bs)
| Just False <- bitAsLit sym b = dropLeadingZeros bs
| otherwise = (b:bs)
loop acc [] = return acc
loop acc (b:bs) =
do sq <- mulV sym aty acc acc
acc' <- iteValue sym b
(mulV sym aty a sq)
(pure sq)
loop acc' bs
{-# INLINE modV #-}
modV :: Backend sym => sym -> Binary sym
modV sym = integralBinary sym opw opi
where
opw _w x y = wordMod sym x y
opi x y = intMod sym x y
{-# SPECIALIZE toIntegerV :: Concrete -> GenValue Concrete #-}
-- | Convert a word to a non-negative integer.
toIntegerV :: Backend sym => sym -> GenValue sym
toIntegerV sym =
tlam $ \a ->
lam $ \v ->
case a of
TVSeq _w el | isTBit el ->
VInteger <$> (wordToInt sym =<< (fromVWord sym "toInteger" =<< v))
TVInteger -> v
_ -> evalPanic "toInteger" [show a ++ " not in class `Integral`"]
-----------------------------------------------------------------------------
-- Field
{-# SPECIALIZE recipV :: Concrete -> GenValue Concrete #-}
recipV :: Backend sym => sym -> GenValue sym
recipV sym =
tlam $ \a ->
lam $ \x ->
case a of
TVRational -> VRational <$> (rationalRecip sym . fromVRational =<< x)
TVFloat e p ->
do one <- fpLit sym e p 1
r <- fpRndMode sym
xv <- fromVFloat <$> x
VFloat <$> fpDiv sym r one xv
_ -> evalPanic "recip" [show a ++ "is not a Field"]
{-# SPECIALIZE fieldDivideV :: Concrete -> GenValue Concrete #-}
fieldDivideV :: Backend sym => sym -> GenValue sym
fieldDivideV sym =
tlam $ \a ->
lam $ \x -> return $
lam $ \y ->
case a of
TVRational ->
do x' <- fromVRational <$> x
y' <- fromVRational <$> y
VRational <$> rationalDivide sym x' y'
TVFloat _e _p ->
do xv <- fromVFloat <$> x
yv <- fromVFloat <$> y
r <- fpRndMode sym
VFloat <$> fpDiv sym r xv yv
_ -> evalPanic "recip" [show a ++ "is not a Field"]
--------------------------------------------------------------
-- Round
{-# SPECIALIZE roundOp ::
Concrete ->
String ->
(SRational Concrete -> SEval Concrete (SInteger Concrete)) ->
(SFloat Concrete -> SEval Concrete (SInteger Concrete)) ->
Unary Concrete #-}
roundOp ::
Backend sym =>
sym ->
String ->
(SRational sym -> SEval sym (SInteger sym)) ->
(SFloat sym -> SEval sym (SInteger sym)) ->
Unary sym
roundOp _sym nm qop opfp ty v =
case ty of
TVRational -> VInteger <$> (qop (fromVRational v))
TVFloat _ _ -> VInteger <$> opfp (fromVFloat v)
_ -> evalPanic nm [show ty ++ " is not a Field"]
{-# INLINE floorV #-}
floorV :: Backend sym => sym -> Unary sym
floorV sym = roundOp sym "floor" opq opfp
where
opq = rationalFloor sym
opfp = \x -> fpRndRTN sym >>= \r -> fpToInteger sym "floor" r x
{-# INLINE ceilingV #-}
ceilingV :: Backend sym => sym -> Unary sym
ceilingV sym = roundOp sym "ceiling" opq opfp
where
opq = rationalCeiling sym
opfp = \x -> fpRndRTP sym >>= \r -> fpToInteger sym "ceiling" r x
{-# INLINE truncV #-}
truncV :: Backend sym => sym -> Unary sym
truncV sym = roundOp sym "trunc" opq opfp
where
opq = rationalTrunc sym
opfp = \x -> fpRndRTZ sym >>= \r -> fpToInteger sym "trunc" r x
{-# INLINE roundAwayV #-}
roundAwayV :: Backend sym => sym -> Unary sym
roundAwayV sym = roundOp sym "roundAway" opq opfp
where
opq = rationalRoundAway sym
opfp = \x -> fpRndRNA sym >>= \r -> fpToInteger sym "roundAway" r x
{-# INLINE roundToEvenV #-}
roundToEvenV :: Backend sym => sym -> Unary sym
roundToEvenV sym = roundOp sym "roundToEven" opq opfp
where
opq = rationalRoundToEven sym
opfp = \x -> fpRndRNE sym >>= \r -> fpToInteger sym "roundToEven" r x
--------------------------------------------------------------
-- Logic
{-# INLINE andV #-}
andV :: Backend sym => sym -> Binary sym
andV sym = logicBinary sym (bitAnd sym) (wordAnd sym)
{-# INLINE orV #-}
orV :: Backend sym => sym -> Binary sym
orV sym = logicBinary sym (bitOr sym) (wordOr sym)
{-# INLINE xorV #-}
xorV :: Backend sym => sym -> Binary sym
xorV sym = logicBinary sym (bitXor sym) (wordXor sym)
{-# INLINE complementV #-}
complementV :: Backend sym => sym -> Unary sym
complementV sym = logicUnary sym (bitComplement sym) (wordComplement sym)
-- Bitvector signed div and modulus
{-# INLINE lg2V #-}
lg2V :: Backend sym => sym -> GenValue sym
lg2V sym =
nlam $ \(finNat' -> w) ->
wlam sym $ \x -> return $
VWord w (WordVal <$> wordLg2 sym x)
{-# SPECIALIZE sdivV :: Concrete -> GenValue Concrete #-}
sdivV :: Backend sym => sym -> GenValue sym
sdivV sym =
nlam $ \(finNat' -> w) ->
wlam sym $ \x -> return $
wlam sym $ \y -> return $
VWord w (WordVal <$> wordSignedDiv sym x y)
{-# SPECIALIZE smodV :: Concrete -> GenValue Concrete #-}
smodV :: Backend sym => sym -> GenValue sym
smodV sym =
nlam $ \(finNat' -> w) ->
wlam sym $ \x -> return $
wlam sym $ \y -> return $
VWord w (WordVal <$> wordSignedMod sym x y)
-- Cmp -------------------------------------------------------------------------
{-# SPECIALIZE cmpValue ::
Concrete ->
(SBit Concrete -> SBit Concrete -> SEval Concrete a -> SEval Concrete a) ->
(SWord Concrete -> SWord Concrete -> SEval Concrete a -> SEval Concrete a) ->
(SInteger Concrete -> SInteger Concrete -> SEval Concrete a -> SEval Concrete a) ->
(Integer -> SInteger Concrete -> SInteger Concrete -> SEval Concrete a -> SEval Concrete a) ->
(SRational Concrete -> SRational Concrete -> SEval Concrete a -> SEval Concrete a) ->
(SFloat Concrete -> SFloat Concrete -> SEval Concrete a -> SEval Concrete a) ->
(TValue -> GenValue Concrete -> GenValue Concrete -> SEval Concrete a -> SEval Concrete a)
#-}
cmpValue ::
Backend sym =>
sym ->
(SBit sym -> SBit sym -> SEval sym a -> SEval sym a) ->
(SWord sym -> SWord sym -> SEval sym a -> SEval sym a) ->
(SInteger sym -> SInteger sym -> SEval sym a -> SEval sym a) ->
(Integer -> SInteger sym -> SInteger sym -> SEval sym a -> SEval sym a) ->
(SRational sym -> SRational sym -> SEval sym a -> SEval sym a) ->
(SFloat sym -> SFloat sym -> SEval sym a -> SEval sym a) ->
(TValue -> GenValue sym -> GenValue sym -> SEval sym a -> SEval sym a)
cmpValue sym fb fw fi fz fq ff = cmp
where
cmp ty v1 v2 k =
case ty of
TVBit -> fb (fromVBit v1) (fromVBit v2) k
TVInteger -> fi (fromVInteger v1) (fromVInteger v2) k
TVFloat _ _ -> ff (fromVFloat v1) (fromVFloat v2) k
TVIntMod n -> fz n (fromVInteger v1) (fromVInteger v2) k
TVRational -> fq (fromVRational v1) (fromVRational v2) k
TVArray{} -> panic "Cryptol.Prims.Value.cmpValue"
[ "Arrays are not comparable" ]
TVSeq n t
| isTBit t -> do w1 <- fromVWord sym "cmpValue" v1
w2 <- fromVWord sym "cmpValue" v2
fw w1 w2 k
| otherwise -> cmpValues (repeat t)
(enumerateSeqMap n (fromVSeq v1))
(enumerateSeqMap n (fromVSeq v2))
k
TVStream _ -> panic "Cryptol.Prims.Value.cmpValue"
[ "Infinite streams are not comparable" ]
TVFun _ _ -> panic "Cryptol.Prims.Value.cmpValue"
[ "Functions are not comparable" ]
TVTuple tys -> cmpValues tys (fromVTuple v1) (fromVTuple v2) k
TVRec fields -> cmpValues
(recordElements fields)
(recordElements (fromVRecord v1))
(recordElements (fromVRecord v2))
k
TVAbstract {} -> evalPanic "cmpValue"
[ "Abstract type not in `Cmp`" ]
cmpValues (t : ts) (x1 : xs1) (x2 : xs2) k =
do x1' <- x1
x2' <- x2
cmp t x1' x2' (cmpValues ts xs1 xs2 k)
cmpValues _ _ _ k = k
{-# INLINE bitLessThan #-}
bitLessThan :: Backend sym => sym -> SBit sym -> SBit sym -> SEval sym (SBit sym)
bitLessThan sym x y =
do xnot <- bitComplement sym x
bitAnd sym xnot y
{-# INLINE bitGreaterThan #-}
bitGreaterThan :: Backend sym => sym -> SBit sym -> SBit sym -> SEval sym (SBit sym)
bitGreaterThan sym x y = bitLessThan sym y x
{-# INLINE valEq #-}
valEq :: Backend sym => sym -> TValue -> GenValue sym -> GenValue sym -> SEval sym (SBit sym)
valEq sym ty v1 v2 = cmpValue sym fb fw fi fz fq ff ty v1 v2 (pure $ bitLit sym True)
where
fb x y k = eqCombine sym (bitEq sym x y) k
fw x y k = eqCombine sym (wordEq sym x y) k
fi x y k = eqCombine sym (intEq sym x y) k
fz m x y k = eqCombine sym (znEq sym m x y) k
fq x y k = eqCombine sym (rationalEq sym x y) k
ff x y k = eqCombine sym (fpEq sym x y) k
{-# INLINE valLt #-}
valLt :: Backend sym =>
sym -> TValue -> GenValue sym -> GenValue sym -> SBit sym -> SEval sym (SBit sym)
valLt sym ty v1 v2 final = cmpValue sym fb fw fi fz fq ff ty v1 v2 (pure final)
where
fb x y k = lexCombine sym (bitLessThan sym x y) (bitEq sym x y) k
fw x y k = lexCombine sym (wordLessThan sym x y) (wordEq sym x y) k
fi x y k = lexCombine sym (intLessThan sym x y) (intEq sym x y) k
fz _ _ _ _ = panic "valLt" ["Z_n is not in `Cmp`"]
fq x y k = lexCombine sym (rationalLessThan sym x y) (rationalEq sym x y) k
ff x y k = lexCombine sym (fpLessThan sym x y) (fpEq sym x y) k
{-# INLINE valGt #-}
valGt :: Backend sym =>
sym -> TValue -> GenValue sym -> GenValue sym -> SBit sym -> SEval sym (SBit sym)
valGt sym ty v1 v2 final = cmpValue sym fb fw fi fz fq ff ty v1 v2 (pure final)
where
fb x y k = lexCombine sym (bitGreaterThan sym x y) (bitEq sym x y) k
fw x y k = lexCombine sym (wordGreaterThan sym x y) (wordEq sym x y) k
fi x y k = lexCombine sym (intGreaterThan sym x y) (intEq sym x y) k
fz _ _ _ _ = panic "valGt" ["Z_n is not in `Cmp`"]
fq x y k = lexCombine sym (rationalGreaterThan sym x y) (rationalEq sym x y) k
ff x y k = lexCombine sym (fpGreaterThan sym x y) (fpEq sym x y) k
{-# INLINE eqCombine #-}
eqCombine :: Backend sym =>
sym ->
SEval sym (SBit sym) ->
SEval sym (SBit sym) ->
SEval sym (SBit sym)
eqCombine sym eq k = join (bitAnd sym <$> eq <*> k)
{-# INLINE lexCombine #-}
lexCombine :: Backend sym =>
sym ->
SEval sym (SBit sym) ->
SEval sym (SBit sym) ->
SEval sym (SBit sym) ->
SEval sym (SBit sym)
lexCombine sym cmp eq k =
do c <- cmp
e <- eq
bitOr sym c =<< bitAnd sym e =<< k
{-# INLINE eqV #-}
eqV :: Backend sym => sym -> Binary sym
eqV sym ty v1 v2 = VBit <$> valEq sym ty v1 v2
{-# INLINE distinctV #-}
distinctV :: Backend sym => sym -> Binary sym
distinctV sym ty v1 v2 = VBit <$> (bitComplement sym =<< valEq sym ty v1 v2)
{-# INLINE lessThanV #-}
lessThanV :: Backend sym => sym -> Binary sym
lessThanV sym ty v1 v2 = VBit <$> valLt sym ty v1 v2 (bitLit sym False)
{-# INLINE lessThanEqV #-}
lessThanEqV :: Backend sym => sym -> Binary sym
lessThanEqV sym ty v1 v2 = VBit <$> valLt sym ty v1 v2 (bitLit sym True)
{-# INLINE greaterThanV #-}
greaterThanV :: Backend sym => sym -> Binary sym
greaterThanV sym ty v1 v2 = VBit <$> valGt sym ty v1 v2 (bitLit sym False)
{-# INLINE greaterThanEqV #-}
greaterThanEqV :: Backend sym => sym -> Binary sym
greaterThanEqV sym ty v1 v2 = VBit <$> valGt sym ty v1 v2 (bitLit sym True)
{-# INLINE signedLessThanV #-}
signedLessThanV :: Backend sym => sym -> Binary sym
signedLessThanV sym ty v1 v2 = VBit <$> cmpValue sym fb fw fi fz fq ff ty v1 v2 (pure $ bitLit sym False)
where
fb _ _ _ = panic "signedLessThan" ["Attempted to perform signed comparison on bit type"]
fw x y k = lexCombine sym (wordSignedLessThan sym x y) (wordEq sym x y) k
fi _ _ _ = panic "signedLessThan" ["Attempted to perform signed comparison on Integer type"]
fz m _ _ _ = panic "signedLessThan" ["Attempted to perform signed comparison on Z_" ++ show m ++ " type"]
fq _ _ _ = panic "signedLessThan" ["Attempted to perform signed comparison on Rational type"]
ff _ _ _ = panic "signedLessThan" ["Attempted to perform signed comparison on Float"]
{-# SPECIALIZE zeroV ::
Concrete ->
TValue ->
SEval Concrete (GenValue Concrete)
#-}
zeroV :: forall sym.
Backend sym =>
sym ->
TValue ->
SEval sym (GenValue sym)
zeroV sym ty = case ty of
-- bits
TVBit ->
pure (VBit (bitLit sym False))
-- integers
TVInteger ->
VInteger <$> integerLit sym 0
-- integers mod n
TVIntMod _ ->
VInteger <$> integerLit sym 0
TVRational ->
VRational <$> (intToRational sym =<< integerLit sym 0)
TVArray{} -> evalPanic "zeroV" ["Array not in class Zero"]
-- floating point
TVFloat e p ->
VFloat <$> fpLit sym e p 0
-- sequences
TVSeq w ety
| isTBit ety -> pure $ word sym w 0
| otherwise ->
do z <- sDelay sym Nothing (zeroV sym ety)
pure $ VSeq w (IndexSeqMap $ const z)
TVStream ety ->
do z <- sDelay sym Nothing (zeroV sym ety)
pure $ VStream (IndexSeqMap $ const z)
-- functions
TVFun _ bty ->
do z <- sDelay sym Nothing (zeroV sym bty)
pure $ lam (const z)
-- tuples
TVTuple tys ->
do xs <- mapM (sDelay sym Nothing . zeroV sym) tys
pure $ VTuple xs
-- records
TVRec fields ->
do xs <- traverse (sDelay sym Nothing . zeroV sym) fields
pure $ VRecord xs
TVAbstract {} -> evalPanic "zeroV" [ "Abstract type not in `Zero`" ]
-- | otherwise = evalPanic "zeroV" ["invalid type for zero"]
{-# INLINE joinWordVal #-}
joinWordVal :: Backend sym => sym -> WordValue sym -> WordValue sym -> SEval sym (WordValue sym)
joinWordVal sym (WordVal w1) (WordVal w2)
| wordLen sym w1 + wordLen sym w2 < largeBitSize
= WordVal <$> joinWord sym w1 w2
joinWordVal sym w1 w2
= pure $ LargeBitsVal (n1+n2) (concatSeqMap n1 (asBitsMap sym w1) (asBitsMap sym w2))
where n1 = wordValueSize sym w1
n2 = wordValueSize sym w2
{-# SPECIALIZE joinWords ::
Concrete ->
Integer ->
Integer ->
SeqMap Concrete ->
SEval Concrete (GenValue Concrete)
#-}
joinWords :: forall sym.
Backend sym =>
sym ->
Integer ->
Integer ->
SeqMap sym ->
SEval sym (GenValue sym)
joinWords sym nParts nEach xs =
loop (WordVal <$> wordLit sym 0 0) (enumerateSeqMap nParts xs)
where
loop :: SEval sym (WordValue sym) -> [SEval sym (GenValue sym)] -> SEval sym (GenValue sym)
loop !wv [] =
VWord (nParts * nEach) <$> sDelay sym Nothing wv
loop !wv (w : ws) =
w >>= \case
VWord _ w' ->
loop (join (joinWordVal sym <$> wv <*> w')) ws
_ -> evalPanic "joinWords: expected word value" []
{-# SPECIALIZE joinSeq ::
Concrete ->
Nat' ->
Integer ->
TValue ->
SeqMap Concrete ->
SEval Concrete (GenValue Concrete)
#-}
joinSeq ::
Backend sym =>
sym ->
Nat' ->
Integer ->
TValue ->
SeqMap sym ->
SEval sym (GenValue sym)
-- Special case for 0 length inner sequences.
joinSeq sym _parts 0 a _xs
= zeroV sym (TVSeq 0 a)
-- finite sequence of words
joinSeq sym (Nat parts) each TVBit xs
| parts * each < largeBitSize
= joinWords sym parts each xs
| otherwise
= do let zs = IndexSeqMap $ \i ->
do let (q,r) = divMod i each
ys <- fromWordVal "join seq" =<< lookupSeqMap xs q
VBit <$> indexWordValue sym ys r
return $ VWord (parts * each) $ pure $ LargeBitsVal (parts * each) zs
-- infinite sequence of words
joinSeq sym Inf each TVBit xs
= return $ VStream $ IndexSeqMap $ \i ->
do let (q,r) = divMod i each
ys <- fromWordVal "join seq" =<< lookupSeqMap xs q
VBit <$> indexWordValue sym ys r
-- finite or infinite sequence of non-words
joinSeq _sym parts each _a xs
= return $ vSeq $ IndexSeqMap $ \i -> do
let (q,r) = divMod i each
ys <- fromSeq "join seq" =<< lookupSeqMap xs q
lookupSeqMap ys r
where
len = parts `nMul` (Nat each)
vSeq = case len of
Inf -> VStream
Nat n -> VSeq n
{-# INLINE joinV #-}
-- | Join a sequence of sequences into a single sequence.
joinV ::
Backend sym =>
sym ->
Nat' ->
Integer ->
TValue ->
GenValue sym ->
SEval sym (GenValue sym)
joinV sym parts each a val = joinSeq sym parts each a =<< fromSeq "joinV" val