Skip to content

Commit

Permalink
Add sign/verify on numbers modn: signmsg-modn/verifysig-modn
Browse files Browse the repository at this point in the history
  • Loading branch information
np committed Nov 19, 2014
1 parent 1eb084a commit bec4bc7
Show file tree
Hide file tree
Showing 42 changed files with 610 additions and 12 deletions.
30 changes: 21 additions & 9 deletions Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import qualified Data.ByteString.Lazy.Char8 as LB8
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Base16 as B16
import Network.Haskoin.Crypto
import Network.Haskoin.Internals (FieldP, FieldN, getBigWordInteger, Point, curveN)
import Network.Haskoin.Internals (FieldP, FieldN, getBigWordInteger, Point, curveN, curveP)
import Network.Haskoin.Util

subst :: Eq a => (a,a) -> a -> a
Expand Down Expand Up @@ -127,12 +127,17 @@ getHex msg = decode' . decodeHex msg
withHex :: (Hex s, Hex s', Monoid s', IsString s') => (BS -> BS) -> s -> s'
withHex f = putLn . encodeHex . f . decodeHex "input"

integerN :: Integer -> FieldN
integerN i | i < curveN = fromInteger i
| otherwise = error $ "Integer not in FieldN: " ++ show i

integerP :: Integer -> FieldP
integerP i | i < curveP = fromInteger i
| otherwise = error $ "Integer not in FieldP: " ++ show i

-- Non DER
getFieldN :: Get FieldN
getFieldN = do
i <- getBigWordInteger <$> (get :: Get Word256)
unless (i < curveN) (fail $ "Get: Integer not in FieldN: " ++ show i)
return $ fromInteger i
getFieldN = integerN . getBigWordInteger <$> (get :: Get Word256)

-- Non DER
putFieldN :: FieldN -> Put
Expand All @@ -150,11 +155,18 @@ getHexP = getHex "field number modulo P"
putHexP :: Hex s => FieldP -> s
putHexP = putHex

getDecN :: String -> FieldN
getDecN = fromInteger . readDigits "integer modulo n in decimal"
getDecModN :: String -> FieldN
getDecModN = fromInteger . readDigits "integer modulo n in decimal"

getDecModP :: String -> FieldP
getDecModP = fromInteger . readDigits "integer modulo p in decimal"

getDecStrictN :: String -> FieldN
getDecStrictN = integerN . readDigits "integer modulo n in decimal"

getDecStrictP :: String -> FieldP
getDecStrictP = integerP . readDigits "integer modulo p in decimal"

getDecP :: String -> FieldP
getDecP = fromInteger . readDigits "integer modulo p in decimal"

putHex256 :: Hex s => Word256 -> s
putHex256 = putHex
Expand Down
18 changes: 16 additions & 2 deletions hx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ hx_brainwallet _ = error . brainwallet_usage $ "too many arguments"
brainwallet_usage :: String -> String
brainwallet_usage msg = unlines [msg, "Usage: hx brainwallet <PASSPHRASE>"]

getSig :: String -> Signature
getSig = getHex "signature"

hx_verifysig_modn :: [String] -> String
hx_verifysig_modn [msg,pub,sig] = putSuccess $ verifySig (fromIntegral $ getDecStrictN msg) (getSig sig) (getPubKey pub)
hx_verifysig_modn _ = error "Usage: hx verifysig-modn <MESSAGE-DECIMAL-INTEGER> <PUBKEY> <SIGNATURE>"

hx_signmsg_modn :: [String] -> String
hx_signmsg_modn [msg,prv] = putHex $ detSignMsg (fromIntegral $ getDecStrictN msg) (fromWIFE prv)
hx_signmsg_modn _ = error "Usage: hx signmsg-modn <MESSAGE-DECIMAL-INTEGER> <PRIVKEY>"

-- set-input FILENAME N SIGNATURE_AND_PUBKEY_SCRIPT
hx_set_input :: FilePath -> String -> String -> IO ()
hx_set_input file index script =
Expand Down Expand Up @@ -433,11 +444,11 @@ hx_ec_add_modn [x, y] = putHexN $ getHexN x + getHexN y
hx_ec_add_modn _ = error "Usage: hx ec-add-modn <HEX-FIELDN> <HEX-FIELDN>"

hx_ec_int_modp :: [String] -> String
hx_ec_int_modp [x] = putHexP $ getDecP x
hx_ec_int_modp [x] = putHexP $ getDecModP x
hx_ec_int_modp _ = error "Usage: hx ec-int-modp [<DECIMAL-INTEGER>]"

hx_ec_int_modn :: [String] -> String
hx_ec_int_modn [x] = putHexN $ getDecN x
hx_ec_int_modn [x] = putHexN $ getDecModN x
hx_ec_int_modn _ = error "Usage: hx ec-int-modn [<DECIMAL-INTEGER>]"

hx_ec_x :: Hex s => [s] -> s
Expand Down Expand Up @@ -526,6 +537,9 @@ mainArgs ["set-input",f,i,s] = hx_set_input f i s
mainArgs ["validsig",f,i,s,sig] = hx_validsig f i s sig
mainArgs ("showtx":args) = hx_showtx args

mainArgs ("verifysig-modn":args) = interactArgsLn hx_verifysig_modn args
mainArgs ("signmsg-modn":args) = interactArgsLn hx_signmsg_modn args

mainArgs ("rawscript":args) = interactArgsLn (hx_rawscript . unwords) args
mainArgs ["showscript"] = interactLn $ hx_showscript

Expand Down
2 changes: 1 addition & 1 deletion tests/ec-int-modn-non-digits-error.t/stderr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hx: Invalid number containing non digits (while reading integer mod n)
hx: Invalid number containing non digits (while reading integer modulo n in decimal)
42 changes: 42 additions & 0 deletions tests/signmsg-modn-42.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=signmsg-modn-42.t
command=hx
args=( signmsg-modn 42 - )
exit_code=0
stdin_file=stdin
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/signmsg-modn-42.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5KJvsngHeMpm884wtkJNzQGaCErckhHJBGFsvd3VyK5qMZXj3hS
1 change: 1 addition & 0 deletions tests/signmsg-modn-42.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3045022100ff93cc349ed2552cc384f60030968da4a947157657293adb0d9b680d006431060220015cc9abea73f46e3fddf43cc20997c8a4cd0267b321292e2d1ad00640af986f
42 changes: 42 additions & 0 deletions tests/signmsg-modn-43.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=signmsg-modn-43.t
command=hx
args=( signmsg-modn 43 - )
exit_code=0
stdin_file=stdin
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/signmsg-modn-43.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5KJvsngHeMpm884wtkJNzQGaCErckhHJBGFsvd3VyK5qMZXj3hS
1 change: 1 addition & 0 deletions tests/signmsg-modn-43.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
304402202580fa3c4aaa340eae28a196fccaddcc39fe3f500aa258e1edf1f39fa3e66bcc02204127babafaf28f1de98ee9a7cf4e59bde8994680b0f995dded9b179980a0f606
42 changes: 42 additions & 0 deletions tests/signmsg-modn-n-is-too-big.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=signmsg-modn-n-is-too-big.t
command=hx
args=( signmsg-modn 115792089237316195423570985008687907852837564279074904382605163141518161494337 - )
exit_code=1
stdin_file=stdin
stdout_file=/dev/null
stderr_file=stderr
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/signmsg-modn-n-is-too-big.t/stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hx: Integer not in FieldN: 115792089237316195423570985008687907852837564279074904382605163141518161494337
1 change: 1 addition & 0 deletions tests/signmsg-modn-n-is-too-big.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5KJvsngHeMpm884wtkJNzQGaCErckhHJBGFsvd3VyK5qMZXj3hS
42 changes: 42 additions & 0 deletions tests/signmsg-modn-p-is-too-big.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=signmsg-modn-p-is-too-big.t
command=hx
args=( signmsg-modn 115792089237316195423570985008687907853269984665640564039457584007908834671663 - )
exit_code=1
stdin_file=stdin
stdout_file=/dev/null
stderr_file=stderr
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/signmsg-modn-p-is-too-big.t/stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hx: Integer not in FieldN: 115792089237316195423570985008687907853269984665640564039457584007908834671663
1 change: 1 addition & 0 deletions tests/signmsg-modn-p-is-too-big.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5KJvsngHeMpm884wtkJNzQGaCErckhHJBGFsvd3VyK5qMZXj3hS
42 changes: 42 additions & 0 deletions tests/signmsg-modn-sha256-test.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=signmsg-modn-sha256-test.t
command=hx
args=( signmsg-modn 72155939486846849509759369733266486982821795810448245423168957390607644363272 - )
exit_code=0
stdin_file=stdin
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/signmsg-modn-sha256-test.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5KJvsngHeMpm884wtkJNzQGaCErckhHJBGFsvd3VyK5qMZXj3hS
1 change: 1 addition & 0 deletions tests/signmsg-modn-sha256-test.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3045022100d1c097ef490c0cbcd92410bbc3007bc8d98204ef55e37a1e6815aa33d709c6cf0220195d61e4a5029a2dc64097e4f5d54fccb55ff0107918d560125190bc4a8e8b10
42 changes: 42 additions & 0 deletions tests/verifysig-modn-42-as-43.t/TESTRECIPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

testname=verifysig-modn-42-as-43.t
command=hx
args=( verifysig-modn 43 0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455 -)
exit_code=0
stdin_file=stdin
stdout_file=stdout
stderr_file=/dev/null
sources=( )
products=( )

# Environment variables:
env_vars=( )

setup(){
: Perform here actions to be run before the tested program
}

munge(){
: Munge here the results of the tested program to ease the check
}

check(){
check_exit_code &&
check_stderr &&
check_stdout &&
check_products &&
: Perform here extra checks on the tested program
}

explain(){
explain_exit_code
explain_stdout
explain_stderr
explain_products
: Explain here more potential differences
}

teardown(){
: Undo here the actions of setup
}
1 change: 1 addition & 0 deletions tests/verifysig-modn-42-as-43.t/stdin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3045022100ff93cc349ed2552cc384f60030968da4a947157657293adb0d9b680d006431060220015cc9abea73f46e3fddf43cc20997c8a4cd0267b321292e2d1ad00640af986f
1 change: 1 addition & 0 deletions tests/verifysig-modn-42-as-43.t/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Status: Invalid
Loading

0 comments on commit bec4bc7

Please sign in to comment.