This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from ocheron/eddsa-gen
Generic EdDSA implementation
- Loading branch information
Showing
9 changed files
with
651 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-- | | ||
-- Module : Crypto.Internal.Builder | ||
-- License : BSD-style | ||
-- Maintainer : Olivier Chéron <[email protected]> | ||
-- Stability : stable | ||
-- Portability : Good | ||
-- | ||
-- Delaying and merging ByteArray allocations. This is similar to module | ||
-- "Data.ByteArray.Pack" except the total length is computed automatically based | ||
-- on what is appended. | ||
-- | ||
{-# LANGUAGE BangPatterns #-} | ||
module Crypto.Internal.Builder | ||
( Builder | ||
, buildAndFreeze | ||
, builderLength | ||
, byte | ||
, bytes | ||
, zero | ||
) where | ||
|
||
import Data.ByteArray (ByteArray, ByteArrayAccess) | ||
import qualified Data.ByteArray as B | ||
import Data.Memory.PtrMethods (memSet) | ||
|
||
import Foreign.Ptr (Ptr, plusPtr) | ||
import Foreign.Storable (poke) | ||
|
||
import Crypto.Internal.Imports hiding (empty) | ||
|
||
data Builder = Builder !Int (Ptr Word8 -> IO ()) -- size and initializer | ||
|
||
instance Semigroup Builder where | ||
(Builder s1 f1) <> (Builder s2 f2) = Builder (s1 + s2) f | ||
where f p = f1 p >> f2 (p `plusPtr` s1) | ||
|
||
builderLength :: Builder -> Int | ||
builderLength (Builder s _) = s | ||
|
||
buildAndFreeze :: ByteArray ba => Builder -> ba | ||
buildAndFreeze (Builder s f) = B.allocAndFreeze s f | ||
|
||
byte :: Word8 -> Builder | ||
byte !b = Builder 1 (`poke` b) | ||
|
||
bytes :: ByteArrayAccess ba => ba -> Builder | ||
bytes bs = Builder (B.length bs) (B.copyByteArrayToPtr bs) | ||
|
||
zero :: Int -> Builder | ||
zero s = if s > 0 then Builder s (\p -> memSet p 0 s) else empty | ||
|
||
empty :: Builder | ||
empty = Builder 0 (const $ return ()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.