Target generation, ideally, to those files we'll need for doing crypto.
This commit is contained in:
@@ -11,13 +11,14 @@ import Control.Monad(replicateM, void)
|
||||
import Conversions(conversions, signedConversions)
|
||||
import CryptoNum(cryptoNum)
|
||||
import Control.Monad(forM_,unless)
|
||||
import Data.List(nub)
|
||||
import Data.Text.Lazy(Text, pack)
|
||||
import Division(divisionOps)
|
||||
import GHC.Conc(getNumCapabilities)
|
||||
import ModInv(generateModInvOps)
|
||||
import ModOps(modulusOps)
|
||||
import Multiply(safeMultiplyOps, unsafeMultiplyOps)
|
||||
import RustModule(RustModule,Task(..),generateTasks)
|
||||
import RustModule(RustModule(suggested),Task(..),generateTasks)
|
||||
import Scale(safeScaleOps, unsafeScaleOps)
|
||||
import Shift(shiftOps, signedShiftOps)
|
||||
import Signed(signedBaseOps)
|
||||
@@ -30,14 +31,19 @@ import System.IO(IOMode(..),withFile)
|
||||
import System.ProgressBar(Label(..), Progress(..), ProgressBar, Timing, defStyle, newProgressBar, stylePrefix, updateProgress)
|
||||
import System.Random(getStdGen)
|
||||
|
||||
lowestBitsize :: Word
|
||||
lowestBitsize = 192
|
||||
rsaWordSizes :: [Word]
|
||||
rsaWordSizes = [512, 1024, 2048, 3072, 4096, 8192, 15360]
|
||||
|
||||
highestBitsize :: Word
|
||||
highestBitsize = 512
|
||||
dsaWordSizes :: [Word]
|
||||
dsaWordSizes = [192, 256, 384, 1024, 2048, 3072]
|
||||
|
||||
ecdsaIntSizes :: [Word]
|
||||
ecdsaIntSizes = [192, 256, 384, 576]
|
||||
|
||||
bitsizes :: [Word]
|
||||
bitsizes = [lowestBitsize,lowestBitsize+64..highestBitsize]
|
||||
bitsizes = expandSizes initialSet
|
||||
where
|
||||
initialSet = nub (rsaWordSizes ++ dsaWordSizes ++ ecdsaIntSizes)
|
||||
|
||||
unsignedFiles :: [RustModule]
|
||||
unsignedFiles = [
|
||||
@@ -75,6 +81,11 @@ signedFiles = [
|
||||
allFiles :: [RustModule]
|
||||
allFiles = unsignedFiles ++ signedFiles
|
||||
|
||||
expandSizes :: [Word] -> [Word]
|
||||
expandSizes ls = bigger
|
||||
where
|
||||
bigger = nub (ls ++ concatMap (\ f -> concatMap (\ x -> suggested f x) ls) allFiles)
|
||||
|
||||
printLast :: Progress String -> Timing -> Text
|
||||
printLast prog _ = pack (progressCustom prog)
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ modexpLR bitsize b e m = go (bitsize - 1) 1
|
||||
where
|
||||
go bit r0
|
||||
| bit < 0 = r0
|
||||
| testBit e bit = trace ("1: r = " ++ showHex r2 "") $ go (bit - 1) r2
|
||||
| otherwise = trace ("0: r = " ++ showHex r1 "") $ go (bit - 1) r1
|
||||
| testBit e bit = go (bit - 1) r2
|
||||
| otherwise = go (bit - 1) r1
|
||||
where
|
||||
r1 = (r0 * r0) `mod` m
|
||||
r2 = (r1 * b) `mod` m
|
||||
|
||||
@@ -37,7 +37,6 @@ library
|
||||
Conversions,
|
||||
CryptoNum,
|
||||
Division,
|
||||
Gen,
|
||||
Generators,
|
||||
Karatsuba,
|
||||
ModInv,
|
||||
|
||||
@@ -10,7 +10,6 @@ module Add(
|
||||
import Data.Bits((.&.))
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -22,6 +21,7 @@ import System.Random(RandomGen)
|
||||
safeAddOps :: RustModule
|
||||
safeAddOps = RustModule {
|
||||
predicate = \ me others -> (me + 64) `elem` others,
|
||||
suggested = \ me -> [me + 64],
|
||||
outputName = "safe_add",
|
||||
isUnsigned = True,
|
||||
generator = declareSafeAddOperators,
|
||||
@@ -31,6 +31,7 @@ safeAddOps = RustModule {
|
||||
unsafeAddOps :: RustModule
|
||||
unsafeAddOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "unsafe_add",
|
||||
isUnsigned = True,
|
||||
generator = declareUnsafeAddOperators,
|
||||
@@ -40,6 +41,7 @@ unsafeAddOps = RustModule {
|
||||
safeSignedAddOps :: RustModule
|
||||
safeSignedAddOps = RustModule {
|
||||
predicate = \ me others -> (me + 64) `elem` others,
|
||||
suggested = \ me -> [me + 64],
|
||||
outputName = "safe_sadd",
|
||||
isUnsigned = False,
|
||||
generator = declareSafeSignedAddOperators,
|
||||
@@ -49,6 +51,7 @@ safeSignedAddOps = RustModule {
|
||||
unsafeSignedAddOps :: RustModule
|
||||
unsafeSignedAddOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "unsafe_sadd",
|
||||
isUnsigned = False,
|
||||
generator = declareUnsafeSignedAddOperators,
|
||||
|
||||
@@ -13,6 +13,7 @@ import RustModule
|
||||
base :: RustModule
|
||||
base = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "base",
|
||||
isUnsigned = True,
|
||||
generator = declareBaseStructure,
|
||||
|
||||
@@ -7,7 +7,6 @@ module BinaryOps(
|
||||
import Data.Bits(xor,(.&.),(.|.))
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -19,6 +18,7 @@ import System.Random(RandomGen)
|
||||
binaryOps :: RustModule
|
||||
binaryOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "binary",
|
||||
isUnsigned = True,
|
||||
generator = declareBinaryOperators,
|
||||
|
||||
@@ -15,6 +15,7 @@ import System.Random(RandomGen)
|
||||
comparisons :: RustModule
|
||||
comparisons = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "compare",
|
||||
isUnsigned = True,
|
||||
generator = declareComparators,
|
||||
@@ -24,6 +25,7 @@ comparisons = RustModule {
|
||||
signedComparisons :: RustModule
|
||||
signedComparisons = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "scompare",
|
||||
isUnsigned = False,
|
||||
generator = declareSignedComparators,
|
||||
|
||||
@@ -5,7 +5,7 @@ module Conversions(
|
||||
)
|
||||
where
|
||||
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
import Language.Rust.Quote
|
||||
@@ -15,6 +15,7 @@ import RustModule
|
||||
conversions :: RustModule
|
||||
conversions = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "conversions",
|
||||
isUnsigned = True,
|
||||
generator = declareConversions,
|
||||
@@ -24,6 +25,7 @@ conversions = RustModule {
|
||||
signedConversions :: RustModule
|
||||
signedConversions = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "sconversions",
|
||||
isUnsigned = False,
|
||||
generator = declareSignedConversions,
|
||||
|
||||
@@ -7,7 +7,6 @@ module CryptoNum(
|
||||
import Data.Bits(testBit)
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -19,6 +18,7 @@ import System.Random(RandomGen)
|
||||
cryptoNum :: RustModule
|
||||
cryptoNum = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "cryptonum",
|
||||
isUnsigned = True,
|
||||
generator = declareCryptoNumInstance,
|
||||
|
||||
@@ -4,7 +4,6 @@ module Division(divisionOps)
|
||||
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -16,6 +15,7 @@ import System.Random(RandomGen)
|
||||
divisionOps :: RustModule
|
||||
divisionOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "divmod",
|
||||
isUnsigned = True,
|
||||
generator = declareDivision,
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
module Gen(
|
||||
toLit
|
||||
)
|
||||
where
|
||||
|
||||
import Language.Rust.Data.Position
|
||||
import Language.Rust.Syntax
|
||||
|
||||
toLit :: Word -> Expr Span
|
||||
toLit i = Lit [] (Int Dec (fromIntegral i) Unsuffixed mempty) mempty
|
||||
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
module Generators
|
||||
where
|
||||
|
||||
import Language.Rust.Data.Position
|
||||
import Language.Rust.Syntax
|
||||
import Numeric(showHex)
|
||||
import System.Random(RandomGen,random,randomR)
|
||||
|
||||
toLit :: Word -> Expr Span
|
||||
toLit i = Lit [] (Int Dec (fromIntegral i) Unsuffixed mempty) mempty
|
||||
|
||||
generateNum :: RandomGen g => g -> Word -> (Integer, g)
|
||||
generateNum g size =
|
||||
let (x, g') = random g
|
||||
|
||||
@@ -20,6 +20,7 @@ import System.Random(RandomGen)
|
||||
generateModInvOps :: RustModule
|
||||
generateModInvOps = RustModule {
|
||||
predicate = \ me others -> (me + 64) `elem` others,
|
||||
suggested = \ me -> [me + 64],
|
||||
outputName = "modinv",
|
||||
isUnsigned = True,
|
||||
generator = declareModInv,
|
||||
|
||||
@@ -16,6 +16,7 @@ import System.Random(RandomGen)
|
||||
modulusOps :: RustModule
|
||||
modulusOps = RustModule {
|
||||
predicate = \ me others -> (me * 2) `elem` others,
|
||||
suggested = \ me -> [me * 2],
|
||||
outputName = "modops",
|
||||
isUnsigned = True,
|
||||
generator = declareModOps,
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
module Multiply
|
||||
-- (
|
||||
-- safeMultiplyOps
|
||||
-- , unsafeMultiplyOps
|
||||
-- )
|
||||
module Multiply(
|
||||
safeMultiplyOps
|
||||
, unsafeMultiplyOps
|
||||
)
|
||||
where
|
||||
|
||||
import Data.Bits((.&.))
|
||||
@@ -11,7 +10,6 @@ import Data.List(foldl')
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import qualified Data.Set as Set
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Karatsuba
|
||||
import Language.Rust.Data.Ident
|
||||
@@ -24,19 +22,21 @@ import System.Random(RandomGen)
|
||||
safeMultiplyOps :: RustModule
|
||||
safeMultiplyOps = RustModule {
|
||||
predicate = \ me others -> (me * 2) `elem` others,
|
||||
suggested = \ me -> [me * 2],
|
||||
outputName = "safe_mul",
|
||||
isUnsigned = True,
|
||||
generator = declareSafeMulOperators,
|
||||
testCase = Nothing -- Just generateSafeTest
|
||||
testCase = Just generateSafeTest
|
||||
}
|
||||
|
||||
unsafeMultiplyOps :: RustModule
|
||||
unsafeMultiplyOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "unsafe_mul",
|
||||
isUnsigned = True,
|
||||
generator = declareUnsafeMulOperators,
|
||||
testCase = Nothing -- Just generateUnsafeTest
|
||||
testCase = Just generateUnsafeTest
|
||||
}
|
||||
|
||||
declareSafeMulOperators :: Word -> [Word] -> SourceFile Span
|
||||
|
||||
@@ -38,6 +38,7 @@ targetTestGenerationPicos =
|
||||
|
||||
data RustModule = RustModule {
|
||||
predicate :: Word -> [Word] -> Bool,
|
||||
suggested :: Word -> [Word],
|
||||
outputName :: String,
|
||||
isUnsigned :: Bool,
|
||||
generator :: Word -> [Word] -> SourceFile Span,
|
||||
|
||||
@@ -8,7 +8,6 @@ module Scale(
|
||||
import Data.Bits((.&.))
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -20,6 +19,7 @@ import System.Random(RandomGen)
|
||||
safeScaleOps :: RustModule
|
||||
safeScaleOps = RustModule {
|
||||
predicate = \ me others -> (me + 64) `elem` others,
|
||||
suggested = \ me -> [me + 64],
|
||||
outputName = "safe_scale",
|
||||
isUnsigned = True,
|
||||
generator = declareSafeScaleOperators,
|
||||
@@ -29,6 +29,7 @@ safeScaleOps = RustModule {
|
||||
unsafeScaleOps :: RustModule
|
||||
unsafeScaleOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "unsafe_scale",
|
||||
isUnsigned = True,
|
||||
generator = declareUnsafeScaleOperators,
|
||||
|
||||
@@ -5,7 +5,6 @@ module Shift(shiftOps, signedShiftOps)
|
||||
import Data.Bits(shiftL,shiftR)
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -17,6 +16,7 @@ import System.Random(RandomGen)
|
||||
shiftOps :: RustModule
|
||||
shiftOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "shift",
|
||||
isUnsigned = True,
|
||||
generator = declareShiftOperators,
|
||||
@@ -26,6 +26,7 @@ shiftOps = RustModule {
|
||||
signedShiftOps :: RustModule
|
||||
signedShiftOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "sshift",
|
||||
isUnsigned = False,
|
||||
generator = declareSignedShiftOperators,
|
||||
|
||||
@@ -11,6 +11,7 @@ import RustModule
|
||||
signedBaseOps :: RustModule
|
||||
signedBaseOps = RustModule {
|
||||
predicate = const (const True),
|
||||
suggested = const [],
|
||||
outputName = "base",
|
||||
isUnsigned = False,
|
||||
generator = declareSigned,
|
||||
|
||||
@@ -10,7 +10,6 @@ module Subtract(
|
||||
import Data.Bits((.&.))
|
||||
import Data.Map.Strict(Map)
|
||||
import qualified Data.Map.Strict as Map
|
||||
import Gen(toLit)
|
||||
import Generators
|
||||
import Language.Rust.Data.Ident
|
||||
import Language.Rust.Data.Position
|
||||
@@ -22,6 +21,7 @@ import System.Random(RandomGen)
|
||||
safeSubtractOps :: RustModule
|
||||
safeSubtractOps = RustModule {
|
||||
predicate = \ me others -> (me + 64) `elem` others,
|
||||
suggested = \ me -> [me + 64],
|
||||
outputName = "safe_sub",
|
||||
isUnsigned = True,
|
||||
generator = declareSafeSubtractOperators,
|
||||
@@ -31,6 +31,7 @@ safeSubtractOps = RustModule {
|
||||
safeSignedSubtractOps :: RustModule
|
||||
safeSignedSubtractOps = RustModule {
|
||||
predicate = \ me others -> (me + 64) `elem` others,
|
||||
suggested = \ me -> [me + 64],
|
||||
outputName = "safe_ssub",
|
||||
isUnsigned = False,
|
||||
generator = declareSafeSignedSubtractOperators,
|
||||
@@ -40,6 +41,7 @@ safeSignedSubtractOps = RustModule {
|
||||
unsafeSubtractOps :: RustModule
|
||||
unsafeSubtractOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "unsafe_sub",
|
||||
isUnsigned = True,
|
||||
generator = declareUnsafeSubtractOperators,
|
||||
@@ -49,6 +51,7 @@ unsafeSubtractOps = RustModule {
|
||||
unsafeSignedSubtractOps :: RustModule
|
||||
unsafeSignedSubtractOps = RustModule {
|
||||
predicate = \ _ _ -> True,
|
||||
suggested = const [],
|
||||
outputName = "unsafe_ssub",
|
||||
isUnsigned = False,
|
||||
generator = declareUnsafeSignedSubtractOperators,
|
||||
|
||||
Reference in New Issue
Block a user