secp256k1

Pure Haskell Schnorr, ECDSA on the elliptic curve secp256k1 (docs.ppad.tech/secp256k1).
git clone git://git.ppad.tech/secp256k1.git
Log | Files | Refs | README | LICENSE

Main.hs (6624B)


      1 {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
      2 {-# LANGUAGE BangPatterns #-}
      3 {-# LANGUAGE OverloadedStrings #-}
      4 
      5 module Main where
      6 
      7 import qualified Data.ByteString as BS
      8 import qualified Data.ByteString.Base16 as B16
      9 import Control.DeepSeq
     10 import Criterion.Main
     11 import qualified Crypto.Curve.Secp256k1 as S
     12 
     13 instance NFData S.Projective
     14 instance NFData S.Affine
     15 instance NFData S.ECDSA
     16 instance NFData S.Context
     17 instance NFData S.Word256
     18 
     19 main :: IO ()
     20 main = defaultMain [
     21     parse_point
     22   , add
     23   , mul
     24   , precompute
     25   , mul_wnaf
     26   , derive_pub
     27   , schnorr
     28   , ecdsa
     29   ]
     30 
     31 remQ :: Benchmark
     32 remQ = env setup $ \x ->
     33     bgroup "remQ (remainder modulo _CURVE_Q)" [
     34       bench "remQ 2 " $ nf S.remQ 2
     35     , bench "remQ (2 ^ 255 - 19)" $ nf S.remQ x
     36     ]
     37   where
     38     setup = pure . S.parse_int256 $ B16.decodeLenient
     39       "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
     40 
     41 parse_point :: Benchmark
     42 parse_point = bgroup "parse_point" [
     43     bench "compressed" $ nf S.parse_point p_bs
     44   , bench "uncompressed" $ nf S.parse_point t_bs
     45   , bench "bip0340" $ nf S.parse_point (BS.drop 1 p_bs)
     46   ]
     47 
     48 parse_integer :: Benchmark
     49 parse_integer = env setup $ \ ~(small, big) ->
     50     bgroup "parse_int256" [
     51       bench "parse_int256 (small)" $ nf S.parse_int256 small
     52     , bench "parse_int256 (big)" $ nf S.parse_int256 big
     53     ]
     54   where
     55     setup = do
     56       let small = BS.replicate 32 0x00
     57           big   = BS.replicate 32 0xFF
     58       pure (small, big)
     59 
     60 add :: Benchmark
     61 add = bgroup "add" [
     62     bench "2 p (double, trivial projective point)" $ nf (S.add p) p
     63   , bench "2 r (double, nontrivial projective point)" $ nf (S.add r) r
     64   , bench "p + q (trivial projective points)" $ nf (S.add p) q
     65   , bench "p + s (nontrivial mixed points)" $ nf (S.add p) s
     66   , bench "s + r (nontrivial projective points)" $ nf (S.add s) r
     67   ]
     68 
     69 mul :: Benchmark
     70 mul = env setup $ \x ->
     71     bgroup "mul" [
     72       bench "2 G" $ nf (S.mul S._CURVE_G) 2
     73     , bench "(2 ^ 255 - 19) G" $ nf (S.mul S._CURVE_G) x
     74     ]
     75   where
     76     setup = pure . S.parse_int256 $ B16.decodeLenient
     77       "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
     78 
     79 precompute :: Benchmark
     80 precompute = bench "precompute" $ nfIO (pure S.precompute)
     81 
     82 mul_wnaf :: Benchmark
     83 mul_wnaf = env setup $ \ ~(tex, x) ->
     84     bgroup "mul_wnaf" [
     85       bench "2 G" $ nf (S.mul_wnaf tex) 2
     86     , bench "(2 ^ 255 - 19) G" $ nf (S.mul_wnaf tex) x
     87     ]
     88   where
     89     setup = do
     90       let !tex = S.precompute
     91           !int = S.parse_int256 $ B16.decodeLenient
     92             "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
     93       pure (tex, int)
     94 
     95 derive_pub :: Benchmark
     96 derive_pub = env setup $ \x ->
     97     bgroup "derive_pub" [
     98       bench "sk = 2" $ nf S.derive_pub 2
     99     , bench "sk = 2 ^ 255 - 19" $ nf S.derive_pub x
    100     ]
    101   where
    102     setup = pure . S.parse_int256 $ B16.decodeLenient
    103       "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
    104 
    105 schnorr :: Benchmark
    106 schnorr = env setup $ \ ~(tex, big) ->
    107     bgroup "schnorr" [
    108       bench "sign_schnorr (small)" $ nf (S.sign_schnorr 2 s_msg) s_aux
    109     , bench "sign_schnorr (large)" $ nf (S.sign_schnorr big s_msg) s_aux
    110     , bench "sign_schnorr' (small)" $ nf (S.sign_schnorr' tex 2 s_msg) s_aux
    111     , bench "sign_schnorr' (large)" $ nf (S.sign_schnorr' tex big s_msg) s_aux
    112     , bench "verify_schnorr" $ nf (S.verify_schnorr s_msg s_pk) s_sig
    113     , bench "verify_schnorr'" $ nf (S.verify_schnorr' tex s_msg s_pk) s_sig
    114     ]
    115   where
    116     setup = do
    117       let !tex = S.precompute
    118           !int = S.parse_int256 $ B16.decodeLenient
    119             "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
    120       pure (tex, int)
    121 
    122 ecdsa :: Benchmark
    123 ecdsa = env setup $ \ ~(tex, big, pub, msg, sig) ->
    124     bgroup "ecdsa" [
    125       bench "sign_ecdsa (small)" $ nf (S.sign_ecdsa 2) s_msg
    126     , bench "sign_ecdsa (large)" $ nf (S.sign_ecdsa big) s_msg
    127     , bench "sign_ecdsa' (small)" $ nf (S.sign_ecdsa' tex 2) s_msg
    128     , bench "sign_ecdsa' (large)" $ nf (S.sign_ecdsa' tex big) s_msg
    129     , bench "verify_ecdsa" $ nf (S.verify_ecdsa msg pub) sig
    130     , bench "verify_ecdsa'" $ nf (S.verify_ecdsa' tex msg pub) sig
    131     ]
    132   where
    133     setup = do
    134       let !tex = S.precompute
    135           big = S.parse_int256 $ B16.decodeLenient
    136             "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
    137           pub = S.derive_pub big
    138           msg = "i approve of this message"
    139           sig = S.sign_ecdsa big s_msg
    140       pure (tex, big, pub, msg, sig)
    141 
    142 p_bs :: BS.ByteString
    143 p_bs = B16.decodeLenient
    144   "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
    145 
    146 p :: S.Projective
    147 p = case S.parse_point p_bs of
    148   Nothing -> error "bang"
    149   Just !pt -> pt
    150 
    151 q_bs :: BS.ByteString
    152 q_bs = B16.decodeLenient
    153   "02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9"
    154 
    155 q :: S.Projective
    156 q = case S.parse_point q_bs of
    157   Nothing -> error "bang"
    158   Just !pt -> pt
    159 
    160 r_bs :: BS.ByteString
    161 r_bs = B16.decodeLenient
    162   "03a2113cf152585d96791a42cdd78782757fbfb5c6b2c11b59857eb4f7fda0b0e8"
    163 
    164 r :: S.Projective
    165 r = case S.parse_point r_bs of
    166   Nothing -> error "bang"
    167   Just !pt -> pt
    168 
    169 s_bs :: BS.ByteString
    170 s_bs = B16.decodeLenient
    171   "0306413898a49c93cccf3db6e9078c1b6a8e62568e4a4770e0d7d96792d1c580ad"
    172 
    173 s :: S.Projective
    174 s = case S.parse_point s_bs of
    175   Nothing -> error "bang"
    176   Just !pt -> pt
    177 
    178 t_bs :: BS.ByteString
    179 t_bs = B16.decodeLenient "04b838ff44e5bc177bf21189d0766082fc9d843226887fc9760371100b7ee20a6ff0c9d75bfba7b31a6bca1974496eeb56de357071955d83c4b1badaa0b21832e9"
    180 
    181 t :: S.Projective
    182 t = case S.parse_point t_bs of
    183   Nothing -> error "bang"
    184   Just !pt -> pt
    185 
    186 s_sk :: Integer
    187 s_sk = S.parse_int256 . B16.decodeLenient $
    188   "B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF"
    189 
    190 s_sig :: BS.ByteString
    191 s_sig = B16.decodeLenient "6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A"
    192 
    193 s_pk_raw :: BS.ByteString
    194 s_pk_raw = B16.decodeLenient
    195   "DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659"
    196 
    197 s_pk :: S.Projective
    198 s_pk = case S.parse_point s_pk_raw of
    199   Nothing -> error "bang"
    200   Just !pt -> pt
    201 
    202 s_msg :: BS.ByteString
    203 s_msg = B16.decodeLenient
    204   "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89"
    205 
    206 s_aux :: BS.ByteString
    207 s_aux = B16.decodeLenient
    208   "0000000000000000000000000000000000000000000000000000000000000001"
    209 
    210 -- e_msg = B16.decodeLenient "313233343030"
    211 -- e_sig = B16.decodeLenient "3045022100813ef79ccefa9a56f7ba805f0e478584fe5f0dd5f567bc09b5123ccbc983236502206ff18a52dcc0336f7af62400a6dd9b810732baf1ff758000d6f613a556eb31ba"
    212