[MLton-commit] r7223

Wesley Terpstra wesley at mlton.org
Sat Sep 5 04:39:53 PDT 2009


Where there is a "to", there should be a "from".

Converting an intinf to/from a byte-stream is a pretty important operation 
in cryptography. The basis library doesn't provide a way to convert to bytes
aside from >> and WordX.fromLargeInt. It is not possible to convert an integer
to bytes in linear time with these operations. Therefore, this patch extends
the MLton extension to converts IntInf to/from it's underlying representation.


----------------------------------------------------------------------

U   mlton/trunk/basis-library/integer/int-inf.sig
U   mlton/trunk/basis-library/integer/int-inf0.sml
U   mlton/trunk/basis-library/mlton/int-inf.sig

----------------------------------------------------------------------

Modified: mlton/trunk/basis-library/integer/int-inf.sig
===================================================================
--- mlton/trunk/basis-library/integer/int-inf.sig	2009-08-21 15:18:46 UTC (rev 7222)
+++ mlton/trunk/basis-library/integer/int-inf.sig	2009-09-05 11:39:52 UTC (rev 7223)
@@ -29,6 +29,7 @@
          Big of BigWord.word Vector.vector
        | Small of SmallInt.int
       val rep: int -> rep
+      val fromRep: rep -> int option
 
       val zero: int
       val one: int

Modified: mlton/trunk/basis-library/integer/int-inf0.sml
===================================================================
--- mlton/trunk/basis-library/integer/int-inf0.sml	2009-08-21 15:18:46 UTC (rev 7222)
+++ mlton/trunk/basis-library/integer/int-inf0.sml	2009-09-05 11:39:52 UTC (rev 7223)
@@ -24,6 +24,7 @@
          Big of C_MPLimb.word vector
        | Small of ObjptrInt.int
       val rep: int -> rep
+      val fromRep: rep -> int option
 
       structure Prim : 
          sig
@@ -423,6 +424,24 @@
          if isSmall i
             then Small (dropTagCoerceInt i)
             else Big (Prim.toVector i)
+      
+      fun fromRep r =
+         case r of
+            Big v => 
+               let
+                  val ok =
+                     SeqIndex.> (Vector.length v, 1) andalso
+                     W.<= (V.subUnsafe (v, 0), 0w1)
+               in
+                  if ok then SOME (Prim.fromVector v) else NONE
+               end
+          | Small i => 
+                let
+                   val out = addTagCoerceInt i
+                   val undo = dropTagCoerceInt out
+                in
+                   if i = undo then SOME out else NONE
+                end
 
       local
          fun 'a make {zextdToMPLimb: 'a -> MPLimb.word,

Modified: mlton/trunk/basis-library/mlton/int-inf.sig
===================================================================
--- mlton/trunk/basis-library/mlton/int-inf.sig	2009-08-21 15:18:46 UTC (rev 7222)
+++ mlton/trunk/basis-library/mlton/int-inf.sig	2009-09-05 11:39:52 UTC (rev 7223)
@@ -19,4 +19,5 @@
          Big of BigWord.word vector
        | Small of SmallInt.int
       val rep: t -> rep
+      val fromRep: rep -> t option
    end




More information about the MLton-commit mailing list