Remove lax signatures and dynamically link

* No more parsing of lax signatures
* Dynamically link binaries instead of embedding
This commit is contained in:
Jean-Pierre Rupp 2018-08-31 20:44:56 +01:00
parent f460a603fb
commit 5d218aad79
11 changed files with 20 additions and 329 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "secp256k1"]
path = secp256k1
url = https://github.com/bitcoin/secp256k1.git

View File

@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## Unreleased ## 1.1.0
### Removed
- Lax parsing for DER.
- DER parsing of secret keys.
- Embedded code for library.
## 1.0.0 [YANKED]
### Added ### Added
- Changelog - Changelog

View File

@ -1,63 +1,2 @@
import Control.Monad import Distribution.Simple
import Distribution.PackageDescription main = defaultMain
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import Distribution.Simple.Utils
import Distribution.Verbosity
import System.Environment
import System.Exit
main :: IO ()
main = defaultMainWithHooks autoconfUserHooks
{ preConf = autogen
, postConf = configure
, preBuild = make
, preClean = clean
}
runInRepo :: Verbosity
-> FilePath
-> [String]
-> Maybe [(String, String)]
-> IO ExitCode
runInRepo v prog args envM = rawSystemIOWithEnv v
prog args (Just "secp256k1") envM Nothing Nothing Nothing
autogen :: Args -> ConfigFlags -> IO HookedBuildInfo
autogen _ flags = do
maybeExit $ runInRepo v "sh" ["./autogen.sh"] Nothing
return emptyHookedBuildInfo
where
v = fromFlag $ configVerbosity flags
configure :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
configure args flags pd lbi = do
(ccProg, ccFlags) <- configureCCompiler v programConfig
env <- getEnvironment
let env' = appendToEnvironment ("CFLAGS", unwords ccFlags) env
args' = args ++ ["--with-gcc=" ++ ccProg]
maybeExit $ runInRepo v "sh" args' (Just env')
where
args = "./configure" : "--enable-module-recovery" : configureArgs False flags
v = fromFlag $ configVerbosity flags
appendToEnvironment (key, val) [] = [(key, val)]
appendToEnvironment (key, val) (kv@(k, v) : rest)
| key == k = (key, v ++ " " ++ val) : rest
| otherwise = kv : appendToEnvironment (key, val) rest
programConfig = withPrograms lbi
make :: Args -> BuildFlags -> IO HookedBuildInfo
make _ flags = do
runInRepo v "make" ["clean"] Nothing
runInRepo v "make" ["src/ecmult_static_context.h"] Nothing
return emptyHookedBuildInfo
where
v = fromFlag $ buildVerbosity flags
clean :: Args -> CleanFlags -> IO HookedBuildInfo
clean _ flags = do
runInRepo v "make" ["clean"] Nothing
return emptyHookedBuildInfo
where
v = fromFlag $ cleanVerbosity flags

View File

@ -1,5 +1,5 @@
name: secp256k1 name: secp256k1
version: '1.0.0' version: '1.1.0'
synopsis: Bindings for secp256k1 library from Bitcoin Core synopsis: Bindings for secp256k1 library from Bitcoin Core
description: Sign and verify signatures using the very fast C secp256k1 library from Pieter Wuille. Has Haskell types and abstractions for keys and signatures. description: Sign and verify signatures using the very fast C secp256k1 library from Pieter Wuille. Has Haskell types and abstractions for keys and signatures.
category: Crypto category: Crypto
@ -11,90 +11,7 @@ license-file: UNLICENSE
github: haskoin/secp256k1-haskell.git github: haskoin/secp256k1-haskell.git
homepage: http://github.com/haskoin/secp256k1-haskell#readme homepage: http://github.com/haskoin/secp256k1-haskell#readme
extra-source-files: extra-source-files:
- secp256k1/README.md - CHANGELOG.md
- secp256k1/autogen.sh
- secp256k1/configure.ac
- secp256k1/Makefile.am
- secp256k1/COPYING
- secp256k1/src/num_impl.h
- secp256k1/src/bench_verify.c
- secp256k1/src/gen_context.c
- secp256k1/src/bench_recover.c
- secp256k1/src/basic-config.h
- secp256k1/src/bench_ecdh.c
- secp256k1/src/ecmult_const.h
- secp256k1/src/scalar_low.h
- secp256k1/src/tests_exhaustive.c
- secp256k1/src/ecdsa_impl.h
- secp256k1/src/ecmult_impl.h
- secp256k1/src/ecmult_gen_impl.h
- secp256k1/src/scalar_8x32.h
- secp256k1/src/scalar_8x32_impl.h
- secp256k1/src/group.h
- secp256k1/src/testrand_impl.h
- secp256k1/src/modules/ecdh/main_impl.h
- secp256k1/src/modules/ecdh/tests_impl.h
- secp256k1/src/modules/ecdh/Makefile.am.include
- secp256k1/src/modules/recovery/main_impl.h
- secp256k1/src/modules/recovery/tests_impl.h
- secp256k1/src/modules/recovery/Makefile.am.include
- secp256k1/src/ecdsa.h
- secp256k1/src/testrand.h
- secp256k1/src/bench_internal.c
- secp256k1/src/field_10x26_impl.h
- secp256k1/src/eckey.h
- secp256k1/src/field_5x52.h
- secp256k1/src/field_5x52_asm_impl.h
- secp256k1/src/asm/field_10x26_arm.s
- secp256k1/src/field_10x26.h
- secp256k1/src/util.h
- secp256k1/src/num.h
- secp256k1/src/num_gmp.h
- secp256k1/src/scalar_4x64_impl.h
- secp256k1/src/hash.h
- secp256k1/src/scalar_4x64.h
- secp256k1/src/hash_impl.h
- secp256k1/src/tests.c
- secp256k1/src/scalar_impl.h
- secp256k1/src/bench_sign.c
- secp256k1/src/field_5x52_int128_impl.h
- secp256k1/src/field_5x52_impl.h
- secp256k1/src/field.h
- secp256k1/src/secp256k1.c
- secp256k1/src/group_impl.h
- secp256k1/src/java/org_bitcoin_NativeSecp256k1.h
- secp256k1/src/java/org/bitcoin/NativeSecp256k1Test.java
- secp256k1/src/java/org/bitcoin/NativeSecp256k1.java
- secp256k1/src/java/org/bitcoin/Secp256k1Context.java
- secp256k1/src/java/org/bitcoin/NativeSecp256k1Util.java
- secp256k1/src/java/org_bitcoin_Secp256k1Context.h
- secp256k1/src/java/org_bitcoin_NativeSecp256k1.c
- secp256k1/src/java/org_bitcoin_Secp256k1Context.c
- secp256k1/src/ecmult_gen.h
- secp256k1/src/ecmult_const_impl.h
- secp256k1/src/eckey_impl.h
- secp256k1/src/field_impl.h
- secp256k1/src/bench.h
- secp256k1/src/scalar_low_impl.h
- secp256k1/src/bench_schnorr_verify.c
- secp256k1/src/scalar.h
- secp256k1/src/num_gmp_impl.h
- secp256k1/src/ecmult.h
- secp256k1/TODO
- secp256k1/include/secp256k1_recovery.h
- secp256k1/include/secp256k1.h
- secp256k1/include/secp256k1_ecdh.h
- secp256k1/contrib/lax_der_parsing.c
- secp256k1/contrib/lax_der_privatekey_parsing.c
- secp256k1/contrib/lax_der_privatekey_parsing.h
- secp256k1/contrib/lax_der_parsing.h
- secp256k1/libsecp256k1.pc.in
- secp256k1/sage/group_prover.sage
- secp256k1/sage/weierstrass_prover.sage
- secp256k1/sage/secp256k1.sage
- secp256k1/build-aux/m4/ax_jni_include_dir.m4
- secp256k1/build-aux/m4/ax_prog_cc_for_build.m4
- secp256k1/build-aux/m4/bitcoin_secp.m4
- README.md - README.md
dependencies: dependencies:
- base >=4.8 && <5 - base >=4.8 && <5
@ -110,19 +27,7 @@ dependencies:
- string-conversions - string-conversions
library: library:
source-dirs: src source-dirs: src
c-sources: extra-libraries: secp256k1
- secp256k1/src/secp256k1.c
- secp256k1/contrib/lax_der_parsing.c
- secp256k1/contrib/lax_der_privatekey_parsing.c
ghc-options: -Wall
cc-options: -DHAVE_CONFIG_H
include-dirs:
- secp256k1
- secp256k1/contrib
- secp256k1/include
exposed-modules:
- Crypto.Secp256k1
- Crypto.Secp256k1.Internal
tests: tests:
spec: spec:
defaults: hspec/hspec@master defaults: hspec/hspec@master

@ -1 +0,0 @@
Subproject commit cbc20b8c34d44c2ef175420f3cdfe054f82e8e2c

View File

@ -2,10 +2,10 @@
-- --
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
-- --
-- hash: 9fd6f5be2cfb4b06721e74cd378459ffdf2d51e1a03a62d8484d9d342e985261 -- hash: c3dcb030db33b83fb04341ac5fd4aadc0fb7312f5a77a6484a0777fdc81cd351
name: secp256k1 name: secp256k1
version: 1.0.0 version: 1.1.0
synopsis: Bindings for secp256k1 library from Bitcoin Core synopsis: Bindings for secp256k1 library from Bitcoin Core
description: Sign and verify signatures using the very fast C secp256k1 library from Pieter Wuille. Has Haskell types and abstractions for keys and signatures. description: Sign and verify signatures using the very fast C secp256k1 library from Pieter Wuille. Has Haskell types and abstractions for keys and signatures.
category: Crypto category: Crypto
@ -19,91 +19,8 @@ license-file: UNLICENSE
build-type: Simple build-type: Simple
cabal-version: >= 1.10 cabal-version: >= 1.10
extra-source-files: extra-source-files:
CHANGELOG.md
README.md README.md
secp256k1/autogen.sh
secp256k1/build-aux/m4/ax_jni_include_dir.m4
secp256k1/build-aux/m4/ax_prog_cc_for_build.m4
secp256k1/build-aux/m4/bitcoin_secp.m4
secp256k1/configure.ac
secp256k1/contrib/lax_der_parsing.c
secp256k1/contrib/lax_der_parsing.h
secp256k1/contrib/lax_der_privatekey_parsing.c
secp256k1/contrib/lax_der_privatekey_parsing.h
secp256k1/COPYING
secp256k1/include/secp256k1.h
secp256k1/include/secp256k1_ecdh.h
secp256k1/include/secp256k1_recovery.h
secp256k1/libsecp256k1.pc.in
secp256k1/Makefile.am
secp256k1/README.md
secp256k1/sage/group_prover.sage
secp256k1/sage/secp256k1.sage
secp256k1/sage/weierstrass_prover.sage
secp256k1/src/asm/field_10x26_arm.s
secp256k1/src/basic-config.h
secp256k1/src/bench.h
secp256k1/src/bench_ecdh.c
secp256k1/src/bench_internal.c
secp256k1/src/bench_recover.c
secp256k1/src/bench_schnorr_verify.c
secp256k1/src/bench_sign.c
secp256k1/src/bench_verify.c
secp256k1/src/ecdsa.h
secp256k1/src/ecdsa_impl.h
secp256k1/src/eckey.h
secp256k1/src/eckey_impl.h
secp256k1/src/ecmult.h
secp256k1/src/ecmult_const.h
secp256k1/src/ecmult_const_impl.h
secp256k1/src/ecmult_gen.h
secp256k1/src/ecmult_gen_impl.h
secp256k1/src/ecmult_impl.h
secp256k1/src/field.h
secp256k1/src/field_10x26.h
secp256k1/src/field_10x26_impl.h
secp256k1/src/field_5x52.h
secp256k1/src/field_5x52_asm_impl.h
secp256k1/src/field_5x52_impl.h
secp256k1/src/field_5x52_int128_impl.h
secp256k1/src/field_impl.h
secp256k1/src/gen_context.c
secp256k1/src/group.h
secp256k1/src/group_impl.h
secp256k1/src/hash.h
secp256k1/src/hash_impl.h
secp256k1/src/java/org/bitcoin/NativeSecp256k1.java
secp256k1/src/java/org/bitcoin/NativeSecp256k1Test.java
secp256k1/src/java/org/bitcoin/NativeSecp256k1Util.java
secp256k1/src/java/org/bitcoin/Secp256k1Context.java
secp256k1/src/java/org_bitcoin_NativeSecp256k1.c
secp256k1/src/java/org_bitcoin_NativeSecp256k1.h
secp256k1/src/java/org_bitcoin_Secp256k1Context.c
secp256k1/src/java/org_bitcoin_Secp256k1Context.h
secp256k1/src/modules/ecdh/main_impl.h
secp256k1/src/modules/ecdh/Makefile.am.include
secp256k1/src/modules/ecdh/tests_impl.h
secp256k1/src/modules/recovery/main_impl.h
secp256k1/src/modules/recovery/Makefile.am.include
secp256k1/src/modules/recovery/tests_impl.h
secp256k1/src/num.h
secp256k1/src/num_gmp.h
secp256k1/src/num_gmp_impl.h
secp256k1/src/num_impl.h
secp256k1/src/scalar.h
secp256k1/src/scalar_4x64.h
secp256k1/src/scalar_4x64_impl.h
secp256k1/src/scalar_8x32.h
secp256k1/src/scalar_8x32_impl.h
secp256k1/src/scalar_impl.h
secp256k1/src/scalar_low.h
secp256k1/src/scalar_low_impl.h
secp256k1/src/secp256k1.c
secp256k1/src/testrand.h
secp256k1/src/testrand_impl.h
secp256k1/src/tests.c
secp256k1/src/tests_exhaustive.c
secp256k1/src/util.h
secp256k1/TODO
source-repository head source-repository head
type: git type: git
@ -112,16 +29,8 @@ source-repository head
library library
hs-source-dirs: hs-source-dirs:
src src
ghc-options: -Wall extra-libraries:
cc-options: -DHAVE_CONFIG_H
include-dirs:
secp256k1 secp256k1
secp256k1/contrib
secp256k1/include
c-sources:
secp256k1/contrib/lax_der_parsing.c
secp256k1/contrib/lax_der_privatekey_parsing.c
secp256k1/src/secp256k1.c
build-depends: build-depends:
HUnit HUnit
, QuickCheck , QuickCheck

View File

@ -20,7 +20,6 @@ module Crypto.Secp256k1
, secKey , secKey
, getSecKey , getSecKey
, derivePubKey , derivePubKey
, exportSecKey
-- * Public Keys -- * Public Keys
, PubKey , PubKey
@ -34,7 +33,6 @@ module Crypto.Secp256k1
, normalizeSig , normalizeSig
-- ** DER -- ** DER
, importSig , importSig
, laxImportSig
, exportSig , exportSig
-- ** Compact -- ** Compact
, CompactSig(..) , CompactSig(..)
@ -263,18 +261,6 @@ importPubKey bs = withContext $ \ctx -> useByteString bs $ \(b, l) -> do
ret <- withForeignPtr fp $ \p -> ecPubKeyParse ctx p b l ret <- withForeignPtr fp $ \p -> ecPubKeyParse ctx p b l
if isSuccess ret then return $ Just $ PubKey fp else return Nothing if isSuccess ret then return $ Just $ PubKey fp else return Nothing
-- | Encode secret key as DER. First argument 'True' for compressed output.
exportSecKey :: Bool -> SecKey -> ByteString
exportSecKey compress (SecKey fk) = withContext $ \ctx ->
withForeignPtr fk $ \k -> alloca $ \l -> allocaBytes 279 $ \o -> do
poke l 279
ret <- ecSecKeyExport ctx o l k c
unless (isSuccess ret) $ error "could not export secret key"
n <- peek l
packByteString (o, n)
where
c = if compress then compressed else uncompressed
-- | Encode public key as DER. First argument 'True' for compressed output. -- | Encode public key as DER. First argument 'True' for compressed output.
exportPubKey :: Bool -> PubKey -> ByteString exportPubKey :: Bool -> PubKey -> ByteString
exportPubKey compress (PubKey pub) = withContext $ \ctx -> exportPubKey compress (PubKey pub) = withContext $ \ctx ->
@ -310,14 +296,6 @@ importSig bs = withContext $ \ctx ->
ret <- withForeignPtr fg $ \g -> ecdsaSignatureParseDer ctx g b l ret <- withForeignPtr fg $ \g -> ecdsaSignatureParseDer ctx g b l
if isSuccess ret then return $ Just $ Sig fg else return Nothing if isSuccess ret then return $ Just $ Sig fg else return Nothing
-- | Relaxed DER parsing. Allows certain DER errors and violations.
laxImportSig :: ByteString -> Maybe Sig
laxImportSig bs = withContext $ \ctx ->
useByteString bs $ \(b, l) -> do
fg <- mallocForeignPtr
ret <- withForeignPtr fg $ \g -> laxDerParse ctx g b l
if isSuccess ret then return $ Just $ Sig fg else return Nothing
-- | Encode signature as strict DER. -- | Encode signature as strict DER.
exportSig :: Sig -> ByteString exportSig :: Sig -> ByteString
exportSig (Sig fg) = withContext $ \ctx -> exportSig (Sig fg) = withContext $ \ctx ->

View File

@ -352,15 +352,6 @@ foreign import ccall
-> Ptr Sig64 -- ^ input -> Ptr Sig64 -- ^ input
-> IO Ret -> IO Ret
foreign import ccall
"lax_der_parsing.h ecdsa_signature_parse_der_lax"
laxDerParse
:: Ptr Ctx
-> Ptr Sig64
-> Ptr CUChar
-> CSize
-> IO Ret
foreign import ccall foreign import ccall
"secp256k1.h secp256k1_ecdsa_sign" "secp256k1.h secp256k1_ecdsa_sign"
ecdsaSign ecdsaSign
@ -480,13 +471,3 @@ foreign import ccall
-> Ptr RecSig65 -> Ptr RecSig65
-> Ptr Msg32 -> Ptr Msg32
-> IO Ret -> IO Ret
foreign import ccall
"lax_der_privatekey_parsing.h ec_privkey_export_der"
ecSecKeyExport
:: Ptr Ctx
-> Ptr CUChar -- ^ array to store DER-encoded key (allocate 279 bytes)
-> Ptr CSize -- ^ size of previous array, will be updated
-> Ptr SecKey32
-> SerFlags
-> IO Ret

View File

@ -1,6 +1,4 @@
resolver: lts-12.2 resolver: lts-12.7
packages: nix:
- '.' packages:
extra-deps: [] - secp256k1
flags: {}
extra-package-dbs: []

View File

@ -25,7 +25,6 @@ spec = do
it "handles storable public key" pubkeyStorableTest it "handles storable public key" pubkeyStorableTest
it "handles storable signature" signatureStorableTest it "handles storable signature" signatureStorableTest
it "parses DER signature" ecdsaSignatureParseDerTest it "parses DER signature" ecdsaSignatureParseDerTest
it "parses lax DER signature" laxDerParseTest
it "serializes DER signature" ecdsaSignatureSerializeDerTest it "serializes DER signature" ecdsaSignatureSerializeDerTest
describe "signatures" $ do describe "signatures" $ do
it "verifies signature" ecdsaVerifyTest it "verifies signature" ecdsaVerifyTest
@ -161,17 +160,6 @@ ecdsaSignatureParseDerTest = do
\fb2202206f0415ab0e9a977afd78b2c26ef39b3952096d319fd4b101c768ad6c132e30\ \fb2202206f0415ab0e9a977afd78b2c26ef39b3952096d319fd4b101c768ad6c132e30\
\45" \45"
laxDerParseTest :: Assertion
laxDerParseTest = do
ret <- liftIO $ useAsCStringLen der $ \(d, dl) -> alloca $ \s -> do
x <- contextCreate verify
laxDerParse x s (castPtr d) (fromIntegral dl)
assertBool "parsed signature successfully" $ isSuccess ret
where
der = fst $ B16.decode
"30450220f502bfa07af43e7ef265618b0d929a7619ee01d6150e37eb6eaaf2c8bd37fb\
\2202206f0415ab0e9a977afd78b2c26ef39b3952096d319fd4b101c768ad6c132e3045"
parseDer :: Ptr Ctx -> ByteString -> IO Sig64 parseDer :: Ptr Ctx -> ByteString -> IO Sig64
parseDer x bs = useAsCStringLen bs $ \(d, dl) -> alloca $ \s -> do parseDer x bs = useAsCStringLen bs $ \(d, dl) -> alloca $ \s -> do
ret <- ecdsaSignatureParseDer x s (castPtr d) (fromIntegral dl) ret <- ecdsaSignatureParseDer x s (castPtr d) (fromIntegral dl)

View File

@ -24,7 +24,6 @@ spec = do
describe "serialization" $ do describe "serialization" $ do
it "serializes public key" $ property $ serializePubKeyTest it "serializes public key" $ property $ serializePubKeyTest
it "serializes DER signature" $ property $ serializeSigTest it "serializes DER signature" $ property $ serializeSigTest
it "serializes lax DER signature" $ property $ serializeLaxSigTest
it "serializes compact signature" $ property $ serializeCompactSigTest it "serializes compact signature" $ property $ serializeCompactSigTest
it "serializes compact recoverable signature" $ it "serializes compact recoverable signature" $
property $ serializeCompactRecSigTest property $ serializeCompactRecSigTest
@ -146,14 +145,6 @@ serializeSigTest (fm, fk) =
where where
fg = signMsg fk fm fg = signMsg fk fm
serializeLaxSigTest :: (Msg, SecKey) -> Bool
serializeLaxSigTest (fm, fk) =
case laxImportSig $ exportSig fg of
Just fg' -> fg == fg'
Nothing -> False
where
fg = signMsg fk fm
serializeCompactSigTest :: (Msg, SecKey) -> Bool serializeCompactSigTest :: (Msg, SecKey) -> Bool
serializeCompactSigTest (fm, fk) = serializeCompactSigTest (fm, fk) =
case importCompactSig $ exportCompactSig fg of case importCompactSig $ exportCompactSig fg of