hF2 (empty) → 0.1
raw patch · 5 files changed
+256/−0 lines, 5 filesdep +basedep +bitvecdep +vectorsetup-changed
Dependencies added: base, bitvec, vector
Files
- COPYING +24/−0
- README +49/−0
- Setup.hs +2/−0
- hF2.cabal +25/−0
- src/Data/F2.hs +156/−0
+ COPYING view
@@ -0,0 +1,24 @@+Copyright (c) 20[11..12], Marcel Fourné+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+ * Neither the name of the <organization> nor the+ names of its contributors may be used to endorse or promote products+ derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README view
@@ -0,0 +1,49 @@+ECC+---++RSA just doesn't cut it anymore for fast public-key crypto. Keys are large for reasonable security making it quite slow...+Enter elliptic curves: smaller numbers are necessary and everything is faster. Maybe this library is not for embedded system usage, but now people can experiment with ECC for those use-cases where some form of RSA would be chosen otherwise.+++Hecc.Base+-----------++This is the Haskell-Elliptic-Curve-Cryptography-library, or maybe more appropriately atm it is only the basic math for many ECC-algorithms the user of this library may wish to implement.+As an example the EC-variant of the Diffie-Hellman key-exchange is included which shows how the values can be computed with this library (a better variant will follow, this is just an example).+Also included is a basic speed-benchmarking-file (point multiplication) for example on the NIST Curve P-256 (the author wants some usage results and performance-numbers... so...).+++The API+-------++...is not stable right now. If anybody wants to use the library in its current state for serious cryptographic uses, then by all means contact the author!++The Code began as a prototyped script and has since been polished, but this is best-effort work in progress.+++pmul+----++Point multiplication can be done by this library using one of two algorithms: double-and-add and mongomery ladder. The latter is the default and is intended to be mostly resistant to timing attacks, the former may be faster, depending on the number it multiplies by.+++Timing Attack Resistance+------------------------+The point multiplication uses the montgomery ladder algorithm which should be timing attack resistant, but when mul by a number in binary form 1000..0 the operation gets strangely fast (us instead of ms) and 1000..0001 it is strangely slow (1.5 times), which hints to something fishy going on. More research will follow, but sidechannel-resistance is not totally out-of-focus. +Testing has given me the idea that the following-zeroes-case massively benefits from branch-prediction and the trailing-one-case throws it totally off (will have to check that on other CPUs). "More natural" numbers are safer (tested), but I wouldn't dare to say that the matter is resolved.+P.S.: 2^N-1 does not show the cache-problem, only long rows of zeroes.+++Plan+----++Some algorithms using these primitives will likely follow (ECDH, maybe ECDSA; also: better versions of the primitives).+Speed is a good goal, may have some more improvements in the future.+Testing is on the list.+Hyperelliptic Curves... maybe.+The upcoming Integer/GMP switch... haven't decided whether side to take... GMP-bindings or pure Haskell.++Motivation+----------++This is a side-project from which other people may benefit. Due to time-constraints, I can't work as much on it as I would like. If you use/like it or want to make some criticism heard, please write me an email.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hF2.cabal view
@@ -0,0 +1,25 @@+Name: hF2+Version: 0.1+Synopsis: F(2^e) math for cryptography+Description: A timing attack resistant F(2^e) backend, all operations on little-endian data in unboxed bit vectors+License: BSD3+License-file: COPYING+Copyright: (c) Marcel Fourné, 2011-2012+Author: Marcel Fourné+Maintainer: Marcel Fourné (hF2@bitrot.dyndns.org)+Category: Cryptography+Stability: alpha+Build-Type: Simple+Cabal-Version: >=1.6+Data-Files: README+Library+ hs-source-dirs:+ src+ Build-Depends:+ base >= 4 && < 5,+ bitvec,+ vector+ Exposed-modules:+ Data.F2+ ghc-options:+ -Wall -fllvm -feager-blackholing -O2
+ src/Data/F2.hs view
@@ -0,0 +1,156 @@+-----------------------------------------------------------------------------+-- |+-- Module : Data.F2+-- Copyright : (c) Marcel Fourné 2011-2012+-- License : BSD3+-- Maintainer : Marcel Fourné (hecc@bitrot.dyndns.org)+--+-- A timing attack resistant F(2^e) backend, all operations on little-endian data in unboxed bit vectors+--+-----------------------------------------------------------------------------++module Data.F2 (+ Data.F2.F2,+ Data.F2.add,+ Data.F2.shift,+ Data.F2.mul,+ Data.F2.reduceBy,+ Data.F2.pow,+ Data.F2.fromInteger,+ Data.F2.toInteger,+ Data.F2.length,+ Data.F2.even,+ Data.F2.odd,+ Data.F2.div,+ Data.F2.bininv+ )+ where++import Numeric+import Data.Char+import Data.Maybe+import qualified Data.List(map)+import qualified Data.Vector.Unboxed as V+import qualified Data.Bits as B+import qualified Data.Bit as Bit+import qualified Data.Vector.Unboxed.Bit as VB++type F2 = V.Vector Bit.Bit++-- |the binary representation of an Integer+binary :: Integer -> [Char]+binary = flip (showIntAtBase (2::Integer) intToDigit) []++-- |binary addition of @a@ and @b@+add :: F2 -> F2 -> F2+add a b = let l1 = V.length a+ l2 = V.length b+ in if l1 <= l2 then VB.zipWords (B.xor) (VB.pad l2 a) b+ else VB.zipWords (B.xor) a (VB.pad l1 b)++-- |a simple bitshift where @n@ shifts right, a negative @n@ shifts left+shift :: F2 -> Int -> F2+shift a n = if n == 0 then a+ else if n > 0 then V.replicate n (Bit.fromBool False) V.++ a+ else V.take ((V.length a) + n) a++-- |binary multiplication of @a@ and @b@+mul :: F2 -> F2 -> F2+mul a b = let l1 = V.length a+ l2 = V.length b+ len = l1 + l2+ nullen = V.replicate len (Bit.fromBool False)+ pseudo = V.replicate l2 (Bit.fromBool False)+ fun a1 b1 | not $ V.null a1 = let ltemp = (V.length a1) - 1+ in if V.last a1 == Bit.fromBool True + -- real branch+ then fun (V.take ltemp a1) (add b1 (shift b ltemp))+ -- for timing-attack-resistance xor with 0s+ else fun (V.take ltemp a1) (add b1 (shift pseudo ltemp))+ | otherwise = b1+ in elimFalses $ fun a nullen++-- |polynomial reduction of @a@ via @r@ +reduceBy :: F2 -> F2 -> F2+reduceBy a r | V.length r == 1 && V.head r == VB.fromBool True = V.singleton $ VB.fromBool False+ | V.length r == 1 && V.head r == VB.fromBool False = a+ | otherwise = let lr = V.length r+ pseudo = V.replicate lr (Bit.fromBool False)+ fun z | V.length z >= lr = let ltemp = (V.length z) - 1+ in if V.last z == Bit.fromBool True+ -- real branch+ then fun (V.take ltemp $ add z (shift r (ltemp - lr)))+ -- for timing-attack-resistance xor with 0s+ else fun (V.take ltemp $ add z (shift pseudo (ltemp - lr)))+ | otherwise = z+ in elimFalses $ fun a++-- | the power function, @b@ ^ @k@, using Montgomery ladder and some low-@k@ hardcoding against overheads+pow :: F2 -> F2 -> F2+pow b k | k == Data.F2.fromInteger 0 = V.singleton $ Bit.fromBool True+ | k == Data.F2.fromInteger 1 = b+ | k == Data.F2.fromInteger 2 = mul b b+ | k == Data.F2.fromInteger 3 = mul b $ mul b b+ | otherwise = let power2 a = mul a a+ ex p1 p2 i+ | i < 0 = p1+ | not $ Bit.toBool $ k V.! i = ex (power2 p1) (mul p1 p2) (i - 1)+ | otherwise = ex (mul p1 p2) (power2 p2) (i - 1)+ in ex b (power2 b) ((V.length k) - 2)++-- | a helper function to shorten @a@ to length @n@+shortenTo :: F2 -> Int -> F2+shortenTo a n = V.take n a+ +-- | a helper function to shorten all MSB-leading "0" from @a@, this shortens unreduced results from multiplications+elimFalses :: F2 -> F2+elimFalses a = let i = V.length a+ r = V.reverse a+ find = VB.first (Bit.fromBool True) r+ in if find == Nothing then V.singleton $ VB.fromBool False+ else shortenTo a $ i - (fromJust find)++-- |conversion helper function+fromInteger :: Integer -> F2+fromInteger z = let helper a = if a == '1' then Bit.fromBool True+ else Bit.fromBool False+ bin = binary z+ in V.reverse $ V.fromList $ Data.List.map helper bin++-- |conversion helper function+toInteger :: F2 -> Integer+toInteger z = let helper a = if a == Bit.fromBool True then 1+ else 0+ it rest n = let len = V.length rest+ in if len > 0 then let el = V.last rest+ in it (V.take (len - 1) rest) (n + (helper el)*2^(len-1))+ else n+ in it z 0++-- | the length of an F(2^e)+length :: F2 -> Int+length z = V.length z++-- | is the number even? The last bit decides...+even :: F2 -> Bool+even a = not $ Data.F2.odd a++-- | is the number odd? The last bit decides...+odd :: F2 -> Bool+odd a = Bit.toBool $ V.last a++-- | computing @k@/@f@ mod @m@ by binary inversion of @f@ in @m@+div :: F2 -> F2 -> F2 -> F2+div k f m = mul k $ bininv f m++-- | computing the modular inverse of "@f@ `mod` @m@"+bininv :: F2 -> F2 -> F2+bininv f m = let helper u v g1 g2 | u == Data.F2.fromInteger 1 = g1+ | otherwise = let j = (V.length u) - (V.length v)+ in if j < 0 + then helper (elimFalses (v `add` (shift u (-j)))) u (elimFalses (g2 `add` (shift g1 (-j)))) g1+ else helper (elimFalses (u `add` (shift v j))) v (elimFalses (g1 `add` (shift g2 j))) g2+ in helper f m (Data.F2.fromInteger 1) (Data.F2.fromInteger 0) + +-- add (mul (Data.F2.fromInteger 371) (Data.F2.fromInteger 1794)) (mul (Data.F2.fromInteger 203) (Data.F2.fromInteger 2053))+-- Data.F2.toInteger $ bininv (Data.F2.fromInteger 371) (Data.F2.fromInteger 2053)