Skip to content

Commit 8df0b17

Browse files
change the repository structure to contain multiple packages
1 parent 1d21e58 commit 8df0b17

File tree

17 files changed

+360
-227
lines changed

17 files changed

+360
-227
lines changed

scripts/format

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env bash
22

3-
find src -type f -name "*.hs" | xargs ormolu -i
4-
find test -type f -name "*.hs" | xargs ormolu -i
3+
REPO_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )
4+
5+
find $REPO_DIR/secp256k1-haskell/src -type f -name "*.hs" | xargs ormolu -i
6+
find $REPO_DIR/secp256k1-haskell/test -type f -name "*.hs" | xargs ormolu -i

CHANGELOG.md renamed to secp256k1-haskell/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.1]
8+
9+
### Changed
10+
11+
- Reworked the structure of the Internal modules to allow having add-on packages for optional libsecp256k1 features.
12+
713
## [1.0.0] - 2023-07-28
814

915
### Changed

secp256k1-haskell/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE

secp256k1-haskell/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md
File renamed without changes.

package.yaml renamed to secp256k1-haskell/package.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: secp256k1-haskell
2-
version: 1.0.0
2+
version: 1.0.1
33
synopsis: Bindings for secp256k1
44
description: Sign and verify signatures using the secp256k1 library.
55
category: Crypto
@@ -22,7 +22,7 @@ dependencies:
2222
- hashable >=1.2.6 && <1.5
2323
- QuickCheck >=2.9.2 && <2.15
2424
- string-conversions >=0.4 && <0.5
25-
- unliftio-core >=0.1.0 && <0.3
25+
- unliftio-core >=0.1.0 && <0.3
2626
library:
2727
source-dirs: src
2828
generated-exposed-modules:

secp256k1-haskell.cabal renamed to secp256k1-haskell/secp256k1-haskell.cabal

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cabal-version: 2.0
55
-- see: https://github.com/sol/hpack
66

77
name: secp256k1-haskell
8-
version: 1.0.0
8+
version: 1.0.1
99
synopsis: Bindings for secp256k1
1010
description: Sign and verify signatures using the secp256k1 library.
1111
category: Crypto
@@ -28,7 +28,11 @@ source-repository head
2828
library
2929
exposed-modules:
3030
Crypto.Secp256k1
31-
Crypto.Secp256k1.Internal
31+
Crypto.Secp256k1.Internal.Base
32+
Crypto.Secp256k1.Internal.BaseOps
33+
Crypto.Secp256k1.Internal.Context
34+
Crypto.Secp256k1.Internal.ForeignTypes
35+
Crypto.Secp256k1.Internal.Util
3236
Paths_secp256k1_haskell
3337
autogen-modules:
3438
Paths_secp256k1_haskell
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- |
2+
-- Module : Crypto.Secp256k1
3+
-- License : UNLICENSE
4+
-- Maintainer : Jean-Pierre Rupp <[email protected]>
5+
-- Stability : experimental
6+
-- Portability : POSIX
7+
--
8+
-- Crytpographic functions from Bitcoin’s secp256k1 library.
9+
module Crypto.Secp256k1
10+
( -- * Context
11+
Ctx (..),
12+
withContext,
13+
randomizeContext,
14+
createContext,
15+
cloneContext,
16+
destroyContext,
17+
18+
-- * Messages
19+
Msg (..),
20+
msg,
21+
22+
-- * Secret Keys
23+
SecKey (..),
24+
secKey,
25+
derivePubKey,
26+
27+
-- * Public Keys
28+
PubKey (..),
29+
pubKey,
30+
importPubKey,
31+
exportPubKey,
32+
33+
-- * Signatures
34+
Sig (..),
35+
sig,
36+
signMsg,
37+
verifySig,
38+
normalizeSig,
39+
40+
-- ** DER
41+
importSig,
42+
exportSig,
43+
44+
-- ** Compact
45+
CompactSig (..),
46+
compactSig,
47+
exportCompactSig,
48+
importCompactSig,
49+
50+
-- * Addition & Multiplication
51+
Tweak (..),
52+
tweak,
53+
tweakAddSecKey,
54+
tweakMulSecKey,
55+
tweakAddPubKey,
56+
tweakMulPubKey,
57+
combinePubKeys,
58+
tweakNegate,
59+
)
60+
where
61+
62+
import Crypto.Secp256k1.Internal.Base
63+
import Crypto.Secp256k1.Internal.Context

src/Crypto/Secp256k1.hs renamed to secp256k1-haskell/src/Crypto/Secp256k1/Internal/Base.hs

Lines changed: 45 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,51 @@
1515
-- Portability : POSIX
1616
--
1717
-- Crytpographic functions from Bitcoin’s secp256k1 library.
18-
module Crypto.Secp256k1
19-
( -- * Context
20-
Ctx (..),
21-
withContext,
22-
randomizeContext,
23-
createContext,
24-
cloneContext,
25-
destroyContext,
26-
27-
-- * Messages
28-
Msg (..),
29-
msg,
30-
31-
-- * Secret Keys
32-
SecKey (..),
33-
secKey,
34-
derivePubKey,
35-
36-
-- * Public Keys
37-
PubKey (..),
38-
pubKey,
39-
importPubKey,
40-
exportPubKey,
41-
42-
-- * Signatures
43-
Sig (..),
44-
sig,
45-
signMsg,
46-
verifySig,
47-
normalizeSig,
48-
49-
-- ** DER
50-
importSig,
51-
exportSig,
52-
53-
-- ** Compact
54-
CompactSig (..),
55-
compactSig,
56-
exportCompactSig,
57-
importCompactSig,
58-
59-
-- * Addition & Multiplication
60-
Tweak (..),
61-
tweak,
62-
tweakAddSecKey,
63-
tweakMulSecKey,
64-
tweakAddPubKey,
65-
tweakMulPubKey,
66-
combinePubKeys,
67-
tweakNegate,
68-
)
69-
where
18+
--
19+
-- The API for this module may change at any time. This is an internal module only
20+
-- exposed for hacking and experimentation.
21+
module Crypto.Secp256k1.Internal.Base where
7022

7123
import Control.DeepSeq (NFData)
7224
import Control.Exception (bracket)
7325
import Control.Monad (replicateM, unless, (<=<))
74-
import Crypto.Secp256k1.Internal
75-
import Data.Base16.Types (assertBase16, extractBase16)
26+
import Crypto.Secp256k1.Internal.BaseOps
27+
( ecPubKeyCombine,
28+
ecPubKeyCreate,
29+
ecPubKeyParse,
30+
ecPubKeySerialize,
31+
ecPubKeyTweakAdd,
32+
ecPubKeyTweakMul,
33+
ecSecKeyTweakAdd,
34+
ecSecKeyTweakMul,
35+
ecTweakNegate,
36+
ecdsaSign,
37+
ecdsaSignatureNormalize,
38+
ecdsaSignatureParseCompact,
39+
ecdsaSignatureParseDer,
40+
ecdsaSignatureSerializeCompact,
41+
ecdsaSignatureSerializeDer,
42+
ecdsaVerify,
43+
)
44+
import Crypto.Secp256k1.Internal.Context (Ctx (..))
45+
import Crypto.Secp256k1.Internal.ForeignTypes
46+
( LCtx,
47+
compressed,
48+
isSuccess,
49+
uncompressed,
50+
)
51+
import Crypto.Secp256k1.Internal.Util
52+
( decodeHex,
53+
packByteString,
54+
showsHex,
55+
unsafePackByteString,
56+
unsafeUseByteString,
57+
)
7658
import Data.ByteString (ByteString)
7759
import Data.ByteString qualified as BS
78-
import Data.ByteString.Base16 (decodeBase16, encodeBase16, isBase16)
7960
import Data.Hashable (Hashable (..))
8061
import Data.Maybe (fromJust, fromMaybe, isJust)
8162
import Data.String (IsString (..))
82-
import Data.String.Conversions (ConvertibleStrings, cs)
8363
import Foreign
8464
( Bits (bitSize),
8565
Ptr,
@@ -109,8 +89,6 @@ import Text.Read
10989
readPrec,
11090
)
11191

112-
newtype Ctx = Ctx {get :: Ptr LCtx}
113-
11492
newtype PubKey = PubKey {get :: ByteString}
11593
deriving (Eq, Generic, Hashable, NFData)
11694

@@ -129,12 +107,6 @@ newtype Tweak = Tweak {get :: ByteString}
129107
newtype CompactSig = CompactSig {get :: ByteString}
130108
deriving (Eq, Generic, Hashable, NFData)
131109

132-
decodeHex :: (ConvertibleStrings a ByteString) => a -> Maybe ByteString
133-
decodeHex str =
134-
if isBase16 $ cs str
135-
then Just . decodeBase16 $ assertBase16 $ cs str
136-
else Nothing
137-
138110
instance Read PubKey where
139111
readPrec = parens $ do
140112
String str <- lexP
@@ -146,7 +118,7 @@ instance IsString PubKey where
146118
e = error "Could not decode public key from hex string"
147119

148120
instance Show PubKey where
149-
showsPrec _ = shows . extractBase16 . encodeBase16 . (.get)
121+
showsPrec _ = showsHex . (.get)
150122

151123
instance Read Msg where
152124
readPrec = parens $ do
@@ -158,8 +130,8 @@ instance IsString Msg where
158130
where
159131
e = error "Could not decode message from hex string"
160132

161-
instance Show Sig where
162-
showsPrec _ = shows . extractBase16 . encodeBase16 . (.get)
133+
instance Show Msg where
134+
showsPrec _ = showsHex . (.get)
163135

164136
instance Read Sig where
165137
readPrec = parens $ do
@@ -171,8 +143,8 @@ instance IsString Sig where
171143
where
172144
e = error "Could not decode signature from hex string"
173145

174-
instance Show Msg where
175-
showsPrec _ = shows . extractBase16 . encodeBase16 . (.get)
146+
instance Show Sig where
147+
showsPrec _ = showsHex . (.get)
176148

177149
instance Read SecKey where
178150
readPrec = parens $ do
@@ -185,7 +157,7 @@ instance IsString SecKey where
185157
e = error "Colud not decode secret key from hex string"
186158

187159
instance Show SecKey where
188-
showsPrec _ = shows . extractBase16 . encodeBase16 . (.get)
160+
showsPrec _ = showsHex . (.get)
189161

190162
instance Read Tweak where
191163
readPrec = parens $ do
@@ -198,36 +170,15 @@ instance IsString Tweak where
198170
e = error "Could not decode tweak from hex string"
199171

200172
instance Show Tweak where
201-
showsPrec _ = shows . extractBase16 . encodeBase16 . (.get)
202-
203-
randomizeContext :: Ctx -> IO ()
204-
randomizeContext (Ctx ctx) = do
205-
ret <- withRandomSeed $ contextRandomize ctx
206-
unless (isSuccess ret) $ error "Could not randomize context"
207-
208-
createContext :: IO Ctx
209-
createContext = Ctx <$> contextCreate signVerify
210-
211-
cloneContext :: Ctx -> IO Ctx
212-
cloneContext = fmap Ctx . contextClone . (.get)
213-
214-
destroyContext :: Ctx -> IO ()
215-
destroyContext = contextDestroy . (.get)
216-
217-
withContext :: (Ctx -> IO a) -> IO a
218-
withContext = bracket create destroy
219-
where
220-
create = do
221-
ctx <- createContext
222-
randomizeContext ctx
223-
return ctx
224-
destroy = destroyContext
173+
showsPrec _ = showsHex . (.get)
225174

175+
-- | Import 64-byte 'ByteString' as 'Sig'.
226176
sig :: ByteString -> Maybe Sig
227177
sig bs
228178
| BS.length bs == 64 = Just (Sig bs)
229179
| otherwise = Nothing
230180

181+
-- | Import 64-byte 'ByteString' as 'PubKey'.
231182
pubKey :: ByteString -> Maybe PubKey
232183
pubKey bs
233184
| BS.length bs == 64 = Just (PubKey bs)

0 commit comments

Comments
 (0)