diff --git a/README-mac.md b/README-mac.md index f6b6f8a10..c085cb617 100644 --- a/README-mac.md +++ b/README-mac.md @@ -26,6 +26,9 @@ cd komodo git checkout dev ./zcutil/build-mac.sh ``` + +To build a distributable version of komodo then run the makeDistrib.sh script after building. + When you are done building, you need to do a few things in the [Configuration](https://github.com/zcash/zcash/wiki/1.0-User-Guide#configuration) section of the Zcash User Guide differently because we are on the Mac. All instances of `~/.zcash` need to be replaced by `~/Library/Application\ Support/Zcash` The fetch-params.sh script, however, has already been altered to fetch the proving keys into the correct directory to conform to Mac specific naming conventions. diff --git a/makeDistrib.sh b/makeDistrib.sh new file mode 100755 index 000000000..ad72fbb16 --- /dev/null +++ b/makeDistrib.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +TMP_DIR=~/tmp/komodo + +# make a tmp directory +mkdir -p $TMP_DIR +echo "making $TMP_DIR" + +binaries=("komodo-cli" "komodod") + +for binary in "${binaries[@]}"; +do + echo "copying $binary to $TMP_DIR" + + cp src/$binary $TMP_DIR + + # find the dylibs to copy for komodod + DYLIBS=`otool -L $TMP_DIR/$binary | grep "/usr/local" | awk -F' ' '{ print $1 }'` + echo "copying $DYLIBS to $TMP_DIR" + + # copy the dylibs to the tmpdir + for dylib in $DYLIBS; do cp -rf $dylib $TMP_DIR/; done + + # modify komodod to point to dylibs + echo "modifying $binary to use local libraries" + for dylib in $DYLIBS; do install_name_tool -change $dylib @executable_path/`basename $dylib` $TMP_DIR/$binary; done; +done + + + + + + + + diff --git a/src/REVS.snapshot b/src/REVS.snapshot index 68a85514b..d150a91c1 100644 --- a/src/REVS.snapshot +++ b/src/REVS.snapshot @@ -5443,7 +5443,7 @@ fiat/revs sendtoaddress RURYZ8FhS5oJ1h1xuv78EXe8rA32SiM4Dy 304.32862968 fiat/revs sendtoaddress RGba6AJogyFQGuMAzD1czE67KR6eW5PV2k 304.98070893 fiat/revs sendtoaddress RXMtwrMXyimRU5CWSdtWzHwWmcCeAc62ua 306.73973391 fiat/revs sendtoaddress RYEmXCS9D2qR71XKzzWbsiAMZGg5E2UWt3 307.15599818 -fiat/revs sendtoaddress RWM3UrsRP4WtH6DTetm5KmUyBASvFCV6wH 313.40690770 +fiat/revs sendtoaddress RWM3UrsRP4WtH6DTetm5KmUyBASvFCV6wH 3005.5069077 fiat/revs sendtoaddress RL4hFVzdntZQ2DM59QXZc619boZV45YFdk 316.05434626 fiat/revs sendtoaddress RG3vbi2TJp9jeqn8qDSnL1W5eFUeTGQJmu 317.33569076 fiat/revs sendtoaddress RQYgriDwRUuUbfw3E1AecAHZXryGSx1BdN 337.23469627 @@ -5587,7 +5587,7 @@ fiat/revs sendtoaddress RWU752dM2s3sjpPZCaWpihUEYmbL6m7Pne 2537.07064054 fiat/revs sendtoaddress RTCq8NorFo4bNwUTNnyKg9YwYgxH49SbbY 2556.94031291 fiat/revs sendtoaddress RNiUNTzDtBgQyA64txPH3W7gMXAjJriM1T 2597.18712886 fiat/revs sendtoaddress RHuJvZphT6ZhKP3hxCRDDsCnNmCMtDy43r 2603.59182293 -fiat/revs sendtoaddress RKxJ7a2J8faQ7VD5dAyFFjMU9DxMDFqv3D 2692.10000000 +fiat/revs sendtoaddress RKxJ7a2J8faQ7VD5dAyFFjMU9DxMDFqv3D 1.10000000 fiat/revs sendtoaddress RGyC2cGKrTbv1zQq4f6fbMc2byCNdUAwBX 2700.00000000 fiat/revs sendtoaddress RWJPh9C4WQGuT8UDMXb4u278Teqzcq3zQt 2784.22122603 fiat/revs sendtoaddress RNjNREuiLJ3D5eWGqLcsMvjno5RBsBxqKN 2894.22787069 diff --git a/src/assetchains b/src/assetchains index ac817bde3..ebd732782 100755 --- a/src/assetchains +++ b/src/assetchains @@ -15,37 +15,70 @@ echo $pubkey ./komodod -pubkey=$pubkey -ac_name=BOTS -ac_supply=999999 -addnode=78.47.196.146 $1 -gen & ./komodod -pubkey=$pubkey -ac_name=MGW -ac_supply=999999 -addnode=78.47.196.146 $1 -gen & ./komodod -pubkey=$pubkey -ac_name=MVP -ac_supply=1000000 -addnode=78.47.196.146 $1 -gen & +./komodod -pubkey=$pubkey -ac_name=WIRELESS -ac_supply=21000000 -addnode=78.47.196.146 $1 -gen & +./komodod -pubkey=$pubkey -ac_name=KV -ac_supply=1000000 -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=USD -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=EUR -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=JPY -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=GBP -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=AUD -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=CAD -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=CHF -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=NZD -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=CNY -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=RUB -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=MXN -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=BRL -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=INR -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=HKD -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=TRY -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=ZAR -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=PLN -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=NOK -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=SEK -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=DKK -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=CZK -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=HUF -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=ILS -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=KRW -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=MYR -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=PHP -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=RON -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=SGD -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=THB -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=BGN -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=IDR -addnode=78.47.196.146 $1 -gen & +sleep 10 ./komodod -pubkey=$pubkey -ac_name=HRK -addnode=78.47.196.146 $1 -gen & - diff --git a/src/dpowassets b/src/dpowassets index eaa79c750..03e489864 100755 --- a/src/dpowassets +++ b/src/dpowassets @@ -17,6 +17,8 @@ curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dp curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"BOTS\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MGW\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"MVP\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"WIRELESS\",\"pubkey\":\"$pubkey\"}" +curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"KV\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"USD\",\"pubkey\":\"$pubkey\"}" curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"iguana\",\"method\":\"dpow\",\"symbol\":\"EUR\",\"pubkey\":\"$pubkey\"}" diff --git a/src/fiat-cli b/src/fiat-cli index 62b23faee..f88958d31 100755 --- a/src/fiat-cli +++ b/src/fiat-cli @@ -44,3 +44,5 @@ echo shark; fiat/shark $1 $2 $3 $4 echo bots; fiat/bots $1 $2 $3 $4 echo mgw; fiat/mgw $1 $2 $3 $4 echo mvp; fiat/mvp $1 $2 $3 $4 +echo wireless; fiat/wireless $1 $2 $3 $4 +echo kv; fiat/kv $1 $2 $3 $4 diff --git a/src/fiat/aud b/src/fiat/aud index bfacdc4e0..6fa6e2cbb 100755 --- a/src/fiat/aud +++ b/src/fiat/aud @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=AUD $1 $2 $3 $4 +./komodo-cli -ac_name=AUD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/bet b/src/fiat/bet index 73208cab5..ed99ce59f 100755 --- a/src/fiat/bet +++ b/src/fiat/bet @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BET $1 $2 $3 $4 +./komodo-cli -ac_name=BET $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/bgn b/src/fiat/bgn index dad8778a6..fdc0f5763 100755 --- a/src/fiat/bgn +++ b/src/fiat/bgn @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BGN $1 $2 $3 $4 +./komodo-cli -ac_name=BGN $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/bots b/src/fiat/bots index 4fc22f5eb..15c0fc288 100755 --- a/src/fiat/bots +++ b/src/fiat/bots @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BOTS $1 $2 $3 $4 +./komodo-cli -ac_name=BOTS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/brl b/src/fiat/brl index 489749d08..b36365d99 100755 --- a/src/fiat/brl +++ b/src/fiat/brl @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=BRL $1 $2 $3 $4 +./komodo-cli -ac_name=BRL $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/cad b/src/fiat/cad index 4a199a30a..509150867 100755 --- a/src/fiat/cad +++ b/src/fiat/cad @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CAD $1 $2 $3 $4 +./komodo-cli -ac_name=CAD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/chf b/src/fiat/chf index fd2a5dbdb..fbe57148c 100755 --- a/src/fiat/chf +++ b/src/fiat/chf @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CHF $1 $2 $3 $4 +./komodo-cli -ac_name=CHF $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/cny b/src/fiat/cny index f67ccd215..aa4e2ca25 100755 --- a/src/fiat/cny +++ b/src/fiat/cny @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CNY $1 $2 $3 $4 +./komodo-cli -ac_name=CNY $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/crypto b/src/fiat/crypto index cb35025fa..5a3d46188 100755 --- a/src/fiat/crypto +++ b/src/fiat/crypto @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CRYPTO $1 $2 $3 $4 +./komodo-cli -ac_name=CRYPTO $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/czk b/src/fiat/czk index fb3101564..c0c1072d2 100755 --- a/src/fiat/czk +++ b/src/fiat/czk @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=CZK $1 $2 $3 $4 +./komodo-cli -ac_name=CZK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/dex b/src/fiat/dex index b1ad72e09..67331201a 100755 --- a/src/fiat/dex +++ b/src/fiat/dex @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=DEX $1 $2 $3 $4 +./komodo-cli -ac_name=DEX $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/dkk b/src/fiat/dkk index e8b65848c..aaa6108e0 100755 --- a/src/fiat/dkk +++ b/src/fiat/dkk @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=DKK $1 $2 $3 $4 +./komodo-cli -ac_name=DKK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/eur b/src/fiat/eur index 60e66af1c..67de01b7d 100755 --- a/src/fiat/eur +++ b/src/fiat/eur @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=EUR $1 $2 $3 $4 +./komodo-cli -ac_name=EUR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/gbp b/src/fiat/gbp index fa1263e3f..eb4528f95 100755 --- a/src/fiat/gbp +++ b/src/fiat/gbp @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=GBP $1 $2 $3 $4 +./komodo-cli -ac_name=GBP $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/hkd b/src/fiat/hkd index dcb9bf97c..006ba7fe6 100755 --- a/src/fiat/hkd +++ b/src/fiat/hkd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HKD $1 $2 $3 $4 +./komodo-cli -ac_name=HKD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/hodl b/src/fiat/hodl index 192326ada..a5430db59 100755 --- a/src/fiat/hodl +++ b/src/fiat/hodl @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HODL $1 $2 $3 $4 +./komodo-cli -ac_name=HODL $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/hrk b/src/fiat/hrk index 283794d04..393b21fe3 100755 --- a/src/fiat/hrk +++ b/src/fiat/hrk @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HRK $1 $2 $3 $4 +./komodo-cli -ac_name=HRK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/huf b/src/fiat/huf index 336a75bb4..55a4f7c50 100755 --- a/src/fiat/huf +++ b/src/fiat/huf @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=HUF $1 $2 $3 $4 +./komodo-cli -ac_name=HUF $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/idr b/src/fiat/idr index 34b7a204c..62ce7968d 100755 --- a/src/fiat/idr +++ b/src/fiat/idr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=IDR $1 $2 $3 $4 +./komodo-cli -ac_name=IDR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/ils b/src/fiat/ils index c06588689..de1e552b8 100755 --- a/src/fiat/ils +++ b/src/fiat/ils @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=ILS $1 $2 $3 $4 +./komodo-cli -ac_name=ILS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/inr b/src/fiat/inr index b4d230622..10ffc9d51 100755 --- a/src/fiat/inr +++ b/src/fiat/inr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=INR $1 $2 $3 $4 +./komodo-cli -ac_name=INR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/jpy b/src/fiat/jpy index 6d437f02b..5b8fd3768 100755 --- a/src/fiat/jpy +++ b/src/fiat/jpy @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=JPY $1 $2 $3 $4 +./komodo-cli -ac_name=JPY $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/jumblr b/src/fiat/jumblr index 24d332184..2969c5009 100755 --- a/src/fiat/jumblr +++ b/src/fiat/jumblr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=JUMBLR $1 $2 $3 $4 +./komodo-cli -ac_name=JUMBLR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/krw b/src/fiat/krw index 51b59dc12..7b895b1f4 100755 --- a/src/fiat/krw +++ b/src/fiat/krw @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=KRW $1 $2 $3 $4 +./komodo-cli -ac_name=KRW $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/kv b/src/fiat/kv new file mode 100755 index 000000000..997fccb33 --- /dev/null +++ b/src/fiat/kv @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=KV $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mgw b/src/fiat/mgw index d498ed85a..9afa86b7a 100755 --- a/src/fiat/mgw +++ b/src/fiat/mgw @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MGW $1 $2 $3 $4 +./komodo-cli -ac_name=MGW $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mvp b/src/fiat/mvp index 3bbe64bfb..998d700a3 100755 --- a/src/fiat/mvp +++ b/src/fiat/mvp @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MVP $1 $2 $3 $4 +./komodo-cli -ac_name=MVP $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/mxn b/src/fiat/mxn index a4f119517..d5d8d97fb 100755 --- a/src/fiat/mxn +++ b/src/fiat/mxn @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MXN $1 $2 $3 $4 +./komodo-cli -ac_name=MXN $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/myr b/src/fiat/myr index 5de0c80da..8c700a53c 100755 --- a/src/fiat/myr +++ b/src/fiat/myr @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=MYR $1 $2 $3 $4 +./komodo-cli -ac_name=MYR $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/nok b/src/fiat/nok index fb1a25430..e0e35b444 100755 --- a/src/fiat/nok +++ b/src/fiat/nok @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=NOK $1 $2 $3 $4 +./komodo-cli -ac_name=NOK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/nzd b/src/fiat/nzd index af598570a..c9610b218 100755 --- a/src/fiat/nzd +++ b/src/fiat/nzd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=NZD $1 $2 $3 $4 +./komodo-cli -ac_name=NZD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/pangea b/src/fiat/pangea index de19a2d6d..9920189ea 100755 --- a/src/fiat/pangea +++ b/src/fiat/pangea @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=PANGEA $1 $2 $3 $4 +./komodo-cli -ac_name=PANGEA $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/php b/src/fiat/php index f333251fd..c22e73293 100755 --- a/src/fiat/php +++ b/src/fiat/php @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=PHP $1 $2 $3 $4 +./komodo-cli -ac_name=PHP $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/pln b/src/fiat/pln index 5fcc12ba4..6dfb0debb 100755 --- a/src/fiat/pln +++ b/src/fiat/pln @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=PLN $1 $2 $3 $4 +./komodo-cli -ac_name=PLN $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/revs b/src/fiat/revs index 5c8782ae5..b898eb5dc 100755 --- a/src/fiat/revs +++ b/src/fiat/revs @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=REVS $1 $2 $3 $4 +./komodo-cli -ac_name=REVS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/ron b/src/fiat/ron index 3ea339896..0b2a6effd 100755 --- a/src/fiat/ron +++ b/src/fiat/ron @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=RON $1 $2 $3 $4 +./komodo-cli -ac_name=RON $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/rub b/src/fiat/rub index 367dbf46d..8d1575578 100755 --- a/src/fiat/rub +++ b/src/fiat/rub @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=RUB $1 $2 $3 $4 +./komodo-cli -ac_name=RUB $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/sek b/src/fiat/sek index 9fb37e674..25a14d414 100755 --- a/src/fiat/sek +++ b/src/fiat/sek @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SEK $1 $2 $3 $4 +./komodo-cli -ac_name=SEK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/sgd b/src/fiat/sgd index f9c75ade8..89a1bc5ae 100755 --- a/src/fiat/sgd +++ b/src/fiat/sgd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SGD $1 $2 $3 $4 +./komodo-cli -ac_name=SGD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/shark b/src/fiat/shark index 4c25f6a7e..4bda53889 100755 --- a/src/fiat/shark +++ b/src/fiat/shark @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SHARK $1 $2 $3 $4 +./komodo-cli -ac_name=SHARK $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/supernet b/src/fiat/supernet index 8afb833ca..d841ffa0e 100755 --- a/src/fiat/supernet +++ b/src/fiat/supernet @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=SUPERNET $1 $2 $3 $4 +./komodo-cli -ac_name=SUPERNET $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/thb b/src/fiat/thb index a5a7eb017..ed72913c1 100755 --- a/src/fiat/thb +++ b/src/fiat/thb @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=THB $1 $2 $3 $4 +./komodo-cli -ac_name=THB $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/try b/src/fiat/try index a06a2050d..dcfa574a6 100755 --- a/src/fiat/try +++ b/src/fiat/try @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=TRY $1 $2 $3 $4 +./komodo-cli -ac_name=TRY $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/usd b/src/fiat/usd index 12eb06e0d..81b9e16c6 100755 --- a/src/fiat/usd +++ b/src/fiat/usd @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=USD $1 $2 $3 $4 +./komodo-cli -ac_name=USD $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/wireless b/src/fiat/wireless new file mode 100755 index 000000000..c6e092a41 --- /dev/null +++ b/src/fiat/wireless @@ -0,0 +1,2 @@ +#!/bin/bash +./komodo-cli -ac_name=WIRELESS $1 $2 $3 $4 $5 $6 diff --git a/src/fiat/zar b/src/fiat/zar index e5fa51785..a3dd891c0 100755 --- a/src/fiat/zar +++ b/src/fiat/zar @@ -1,2 +1,2 @@ #!/bin/bash -./komodo-cli -ac_name=ZAR $1 $2 $3 $4 +./komodo-cli -ac_name=ZAR $1 $2 $3 $4 $5 $6 diff --git a/src/komodo.h b/src/komodo.h index 5b45126a5..ed90ffccd 100644 --- a/src/komodo.h +++ b/src/komodo.h @@ -19,10 +19,6 @@ // Todo: // verify: reorgs -// non komodod (non-hardfork) todo: -// a. automate notarization fee payouts -// b. automated distribution of test REVS snapshot - #define KOMODO_ASSETCHAINS_WAITNOTARIZE #define KOMODO_PAXMAX (10000 * COIN) @@ -40,6 +36,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block); #include "komodo_structs.h" #include "komodo_globals.h" #include "komodo_utils.h" +#include "komodo_curve25519.h" #include "cJSON.c" #include "komodo_bitcoind.h" @@ -48,6 +45,7 @@ void komodo_connectblock(CBlockIndex *pindex,CBlock& block); #include "komodo_notary.h" int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char *dest); +#include "komodo_kv.h" #include "komodo_gateway.h" #include "komodo_events.h" @@ -191,8 +189,13 @@ int32_t komodo_parsestatefile(struct komodo_state *sp,FILE *fp,char *symbol,char void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotaries,uint8_t notaryid,uint256 txhash,uint64_t voutmask,uint8_t numvouts,uint32_t *pvals,uint8_t numpvals,int32_t KMDheight,uint32_t KMDtimestamp,uint64_t opretvalue,uint8_t *opretbuf,uint16_t opretlen,uint16_t vout) { - static FILE *fp; static int32_t errs; + static FILE *fp; static int32_t errs,didinit; struct komodo_state *sp; char fname[512],symbol[16],dest[16]; int32_t ht,func; uint8_t num,pubkeys[64][33]; + if ( didinit == 0 ) + { + portable_mutex_init(&KOMODO_KV_mutex); + didinit = 1; + } if ( (sp= komodo_stateptr(symbol,dest)) == 0 ) { KOMODO_INITDONE = (uint32_t)time(NULL); @@ -255,7 +258,7 @@ void komodo_stateupdate(int32_t height,uint8_t notarypubs[][33],uint8_t numnotar errs++; if ( fwrite(opretbuf,1,olen,fp) != olen ) errs++; - //printf("ht.%d R opret[%d]\n",height,olen); +//printf("ht.%d R opret[%d] sp.%p\n",height,olen,sp); //komodo_opreturn(height,opretvalue,opretbuf,olen,txhash,vout); komodo_eventadd_opreturn(sp,symbol,height,txhash,opretvalue,vout,opretbuf,olen); } @@ -380,7 +383,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&kmdtxid); len += iguana_rwnum(0,&scriptbuf[len],sizeof(*notarizedheightp),(uint8_t *)notarizedheightp); len += iguana_rwbignum(0,&scriptbuf[len],32,(uint8_t *)&desttxid); - if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height ) + if ( notarized != 0 && *notarizedheightp > sp->NOTARIZED_HEIGHT && *notarizedheightp < height && (height < sp->CURRENT_HEIGHT-1000 || komodo_verifynotarization(ASSETCHAINS_SYMBOL[0]==0?(char *)"KMD":ASSETCHAINS_SYMBOL,(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "BTC" : "KMD"),height,*notarizedheightp,kmdtxid,desttxid) == 0) ) { sp->NOTARIZED_HEIGHT = *notarizedheightp; sp->NOTARIZED_HASH = kmdtxid; @@ -413,7 +416,7 @@ int32_t komodo_voutupdate(int32_t *isratificationp,int32_t notaryid,uint8_t *scr komodo_stateupdate(height,0,0,0,txhash,0,0,0,0,0,0,value,&scriptbuf[len],opretlen-len+4+3+(scriptbuf[1] == 0x4d),j); } } - } else printf("notarized.%d %llx reject ht.%d NOTARIZED.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,height,*notarizedheightp,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); + } else printf("notarized.%d %llx reject ht.%d NOTARIZED.%d prev.%d %s.%s DESTTXID.%s (%s)\n",notarized,(long long)signedmask,height,*notarizedheightp,sp->NOTARIZED_HEIGHT,ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL,kmdtxid.ToString().c_str(),desttxid.ToString().c_str(),(char *)&scriptbuf[len]); } else if ( i == 0 && j == 1 && opretlen == 149 ) { diff --git a/src/komodo_bitcoind.h b/src/komodo_bitcoind.h index dbb887963..5af538d9b 100644 --- a/src/komodo_bitcoind.h +++ b/src/komodo_bitcoind.h @@ -346,6 +346,82 @@ char *komodo_issuemethod(char *userpass,char *method,char *params,uint16_t port) return(retstr2); } +int32_t komodo_verifynotarizedscript(int32_t height,uint8_t *script,int32_t len,uint256 NOTARIZED_HASH) +{ + int32_t i; uint256 hash; char params[256]; + for (i=0; i<32; i++) + ((uint8_t *)&hash)[i] = script[2+i]; + if ( hash == NOTARIZED_HASH ) + return(0); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&NOTARIZED_HASH)[i]); + printf(" notarized, "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&hash)[i]); + printf(" opreturn from [%s] ht.%d\n",ASSETCHAINS_SYMBOL,height); + return(-1); +} + +int32_t komodo_verifynotarization(char *symbol,char *dest,int32_t height,int32_t NOTARIZED_HEIGHT,uint256 NOTARIZED_HASH,uint256 NOTARIZED_DESTTXID) +{ + char params[256],*jsonstr,*hexstr; uint8_t script[8192]; int32_t n,len,retval = -1; cJSON *json,*txjson,*vouts,*vout,*skey; + /*params[0] = '['; + params[1] = '"'; + for (i=0; i<32; i++) + sprintf(¶ms[i*2 + 2],"%02x",((uint8_t *)&NOTARIZED_DESTTXID)[31-i]); + strcat(params,"\", 1]");*/ + sprintf(params,"[\"%s\", 1]",NOTARIZED_DESTTXID.ToString().c_str()); + if ( strcmp(symbol,ASSETCHAINS_SYMBOL[0]==0?(char *)"KMD":ASSETCHAINS_SYMBOL) != 0 ) + return(0); + //printf("[%s] src.%s dest.%s params.[%s] ht.%d notarized.%d\n",ASSETCHAINS_SYMBOL,symbol,dest,params,height,NOTARIZED_HEIGHT); + if ( strcmp(dest,"KMD") == 0 ) + { + if ( KMDUSERPASS[0] != 0 ) + jsonstr = komodo_issuemethod(KMDUSERPASS,(char *)"getrawtransaction",params,7771); + //else jsonstr = _dex_getrawtransaction(); + else return(0); // need universal way to issue DEX* API, since notaries mine most blocks, this ok + } + else if ( strcmp(dest,"BTC") == 0 ) + { + if ( BTCUSERPASS[0] != 0 ) + { + //printf("BTCUSERPASS.(%s)\n",BTCUSERPASS); + jsonstr = komodo_issuemethod(BTCUSERPASS,(char *)"getrawtransaction",params,8332); + } + //else jsonstr = _dex_getrawtransaction(); + else return(0); + } + else + { + printf("[%s] verifynotarization error unexpected dest.(%s)\n",ASSETCHAINS_SYMBOL,dest); + return(-1); + } + if ( jsonstr != 0 ) + { + if ( (json= cJSON_Parse(jsonstr)) != 0 ) + { + if ( (txjson= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,txjson,(char *)"vout")) > 0 ) + { + vout = jitem(vouts,n-1); + //printf("vout.(%s)\n",jprint(vout,0)); + if ( (skey= jobj(vout,(char *)"scriptPubKey")) != 0 ) + { + if ( (hexstr= jstr(skey,(char *)"hex")) != 0 ) + { + //printf("HEX.(%s)\n",hexstr); + len = strlen(hexstr) >> 1; + decode_hex(script,len,hexstr); + retval = komodo_verifynotarizedscript(height,script,len,NOTARIZED_HASH); + } + } + } + free_json(txjson); + } + free(jsonstr); + } + return(retval); +} + uint256 komodo_getblockhash(int32_t height) { uint256 hash; char params[128],*hexstr,*jsonstr; cJSON *result; int32_t i; uint8_t revbuf[32]; diff --git a/src/komodo_curve25519.h b/src/komodo_curve25519.h new file mode 100644 index 000000000..6a0ee3fa6 --- /dev/null +++ b/src/komodo_curve25519.h @@ -0,0 +1,1005 @@ +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef H_KOMODO25519_H +#define H_KOMODO25519_H +// derived from curve25519_donna + +#include +#include +#include + +bits320 fmul(const bits320 in2,const bits320 in); +bits320 fexpand(bits256 basepoint); +bits256 fcontract(const bits320 input); +void cmult(bits320 *resultx,bits320 *resultz,bits256 secret,const bits320 q); +bits320 crecip(const bits320 z); +bits256 curve25519(bits256 mysecret,bits256 basepoint); + +// Sum two numbers: output += in +static inline bits320 fsum(bits320 output,bits320 in) +{ + int32_t i; + for (i=0; i<5; i++) + output.ulongs[i] += in.ulongs[i]; + return(output); +} + +static inline void fdifference_backwards(uint64_t *out,const uint64_t *in) +{ + static const uint64_t two54m152 = (((uint64_t)1) << 54) - 152; // 152 is 19 << 3 + static const uint64_t two54m8 = (((uint64_t)1) << 54) - 8; + int32_t i; + out[0] = in[0] + two54m152 - out[0]; + for (i=1; i<5; i++) + out[i] = in[i] + two54m8 - out[i]; +} + +void store_limb(uint8_t *out,uint64_t in) +{ + int32_t i; + for (i=0; i<8; i++,in>>=8) + out[i] = (in & 0xff); +} + +static inline uint64_t load_limb(uint8_t *in) +{ + return + ((uint64_t)in[0]) | + (((uint64_t)in[1]) << 8) | + (((uint64_t)in[2]) << 16) | + (((uint64_t)in[3]) << 24) | + (((uint64_t)in[4]) << 32) | + (((uint64_t)in[5]) << 40) | + (((uint64_t)in[6]) << 48) | + (((uint64_t)in[7]) << 56); +} + +// Take a little-endian, 32-byte number and expand it into polynomial form +bits320 fexpand(bits256 basepoint) +{ + bits320 out; + out.ulongs[0] = load_limb(basepoint.bytes) & 0x7ffffffffffffLL; + out.ulongs[1] = (load_limb(basepoint.bytes+6) >> 3) & 0x7ffffffffffffLL; + out.ulongs[2] = (load_limb(basepoint.bytes+12) >> 6) & 0x7ffffffffffffLL; + out.ulongs[3] = (load_limb(basepoint.bytes+19) >> 1) & 0x7ffffffffffffLL; + out.ulongs[4] = (load_limb(basepoint.bytes+24) >> 12) & 0x7ffffffffffffLL; + return(out); +} + +#if __amd64__ +// donna: special gcc mode for 128-bit integers. It's implemented on 64-bit platforms only as far as I know. +typedef unsigned uint128_t __attribute__((mode(TI))); + +// Multiply a number by a scalar: output = in * scalar +static inline bits320 fscalar_product(const bits320 in,const uint64_t scalar) +{ + int32_t i; uint128_t a = 0; bits320 output; + a = ((uint128_t)in.ulongs[0]) * scalar; + output.ulongs[0] = ((uint64_t)a) & 0x7ffffffffffffLL; + for (i=1; i<5; i++) + { + a = ((uint128_t)in.ulongs[i]) * scalar + ((uint64_t) (a >> 51)); + output.ulongs[i] = ((uint64_t)a) & 0x7ffffffffffffLL; + } + output.ulongs[0] += (a >> 51) * 19; + return(output); +} + +// Multiply two numbers: output = in2 * in +// output must be distinct to both inputs. The inputs are reduced coefficient form, the output is not. +// Assumes that in[i] < 2**55 and likewise for in2. On return, output[i] < 2**52 +bits320 fmul(const bits320 in2,const bits320 in) +{ + uint128_t t[5]; uint64_t r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c; bits320 out; + r0 = in.ulongs[0], r1 = in.ulongs[1], r2 = in.ulongs[2], r3 = in.ulongs[3], r4 = in.ulongs[4]; + s0 = in2.ulongs[0], s1 = in2.ulongs[1], s2 = in2.ulongs[2], s3 = in2.ulongs[3], s4 = in2.ulongs[4]; + t[0] = ((uint128_t) r0) * s0; + t[1] = ((uint128_t) r0) * s1 + ((uint128_t) r1) * s0; + t[2] = ((uint128_t) r0) * s2 + ((uint128_t) r2) * s0 + ((uint128_t) r1) * s1; + t[3] = ((uint128_t) r0) * s3 + ((uint128_t) r3) * s0 + ((uint128_t) r1) * s2 + ((uint128_t) r2) * s1; + t[4] = ((uint128_t) r0) * s4 + ((uint128_t) r4) * s0 + ((uint128_t) r3) * s1 + ((uint128_t) r1) * s3 + ((uint128_t) r2) * s2; + r4 *= 19, r1 *= 19, r2 *= 19, r3 *= 19; + t[0] += ((uint128_t) r4) * s1 + ((uint128_t) r1) * s4 + ((uint128_t) r2) * s3 + ((uint128_t) r3) * s2; + t[1] += ((uint128_t) r4) * s2 + ((uint128_t) r2) * s4 + ((uint128_t) r3) * s3; + t[2] += ((uint128_t) r4) * s3 + ((uint128_t) r3) * s4; + t[3] += ((uint128_t) r4) * s4; + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); + t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); + t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); + t[3] += c; r3 = (uint64_t)t[3] & 0x7ffffffffffffLL; c = (uint64_t)(t[3] >> 51); + t[4] += c; r4 = (uint64_t)t[4] & 0x7ffffffffffffLL; c = (uint64_t)(t[4] >> 51); + r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffffLL; + r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffffLL; + r2 += c; + out.ulongs[0] = r0, out.ulongs[1] = r1, out.ulongs[2] = r2, out.ulongs[3] = r3, out.ulongs[4] = r4; + return(out); +} + +bits320 fsquare_times(const bits320 in,uint64_t count) +{ + uint128_t t[5]; uint64_t r0,r1,r2,r3,r4,c,d0,d1,d2,d4,d419; bits320 out; + r0 = in.ulongs[0], r1 = in.ulongs[1], r2 = in.ulongs[2], r3 = in.ulongs[3], r4 = in.ulongs[4]; + do + { + d0 = r0 * 2; + d1 = r1 * 2; + d2 = r2 * 2 * 19; + d419 = r4 * 19; + d4 = d419 * 2; + t[0] = ((uint128_t) r0) * r0 + ((uint128_t) d4) * r1 + (((uint128_t) d2) * (r3 )); + t[1] = ((uint128_t) d0) * r1 + ((uint128_t) d4) * r2 + (((uint128_t) r3) * (r3 * 19)); + t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 )); + t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 )); + t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 )); + + r0 = (uint64_t)t[0] & 0x7ffffffffffffLL; c = (uint64_t)(t[0] >> 51); + t[1] += c; r1 = (uint64_t)t[1] & 0x7ffffffffffffLL; c = (uint64_t)(t[1] >> 51); + t[2] += c; r2 = (uint64_t)t[2] & 0x7ffffffffffffLL; c = (uint64_t)(t[2] >> 51); + t[3] += c; r3 = (uint64_t)t[3] & 0x7ffffffffffffL; c = (uint64_t)(t[3] >> 51); + t[4] += c; r4 = (uint64_t)t[4] & 0x7ffffffffffffLL; c = (uint64_t)(t[4] >> 51); + r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffffLL; + r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffffLL; + r2 += c; + } while( --count ); + out.ulongs[0] = r0, out.ulongs[1] = r1, out.ulongs[2] = r2, out.ulongs[3] = r3, out.ulongs[4] = r4; + return(out); +} + +static inline void fcontract_iter(uint128_t t[5],int32_t flag) +{ + int32_t i; uint64_t mask = 0x7ffffffffffffLL; + for (i=0; i<4; i++) + t[i+1] += t[i] >> 51, t[i] &= mask; + if ( flag != 0 ) + t[0] += 19 * (t[4] >> 51); t[4] &= mask; +} + +// donna: Take a fully reduced polynomial form number and contract it into a little-endian, 32-byte array +bits256 fcontract(const bits320 input) +{ + uint128_t t[5]; int32_t i; bits256 out; + for (i=0; i<5; i++) + t[i] = input.ulongs[i]; + fcontract_iter(t,1), fcontract_iter(t,1); + // donna: now t is between 0 and 2^255-1, properly carried. + // donna: case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. + t[0] += 19, fcontract_iter(t,1); + // now between 19 and 2^255-1 in both cases, and offset by 19. + t[0] += 0x8000000000000 - 19; + for (i=1; i<5; i++) + t[i] += 0x8000000000000 - 1; + // now between 2^255 and 2^256-20, and offset by 2^255. + fcontract_iter(t,0); + store_limb(out.bytes,t[0] | (t[1] << 51)); + store_limb(out.bytes+8,(t[1] >> 13) | (t[2] << 38)); + store_limb(out.bytes+16,(t[2] >> 26) | (t[3] << 25)); + store_limb(out.bytes+24,(t[3] >> 39) | (t[4] << 12)); + return(out); +} + +bits256 curve25519(bits256 mysecret,bits256 basepoint) +{ + bits320 bp,x,z; + mysecret.bytes[0] &= 0xf8, mysecret.bytes[31] &= 0x7f, mysecret.bytes[31] |= 0x40; + bp = fexpand(basepoint); + cmult(&x,&z,mysecret,bp); + return(fcontract(fmul(x,crecip(z)))); +} + +#else +// from curve25519-donna.c +typedef uint8_t u8; +typedef int32_t s32; +typedef int64_t limb; + +/* Multiply a number by a scalar: output = in * scalar */ +static void fscalar_product32(limb *output, const limb *in, const limb scalar) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] * scalar; + } +} + +/* Multiply two numbers: output = in2 * in + * + * output must be distinct to both inputs. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. + static void fproduct(limb *output, const limb *in2, const limb *in) { + output[0] = ((limb) ((s32) in2[0])) * ((s32) in[0]); + output[1] = ((limb) ((s32) in2[0])) * ((s32) in[1]) + + ((limb) ((s32) in2[1])) * ((s32) in[0]); + output[2] = 2 * ((limb) ((s32) in2[1])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[0]); + output[3] = ((limb) ((s32) in2[1])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[0]); + output[4] = ((limb) ((s32) in2[2])) * ((s32) in[2]) + + 2 * (((limb) ((s32) in2[1])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[1])) + + ((limb) ((s32) in2[0])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[0]); + output[5] = ((limb) ((s32) in2[2])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[0]); + output[6] = 2 * (((limb) ((s32) in2[3])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[0]); + output[7] = ((limb) ((s32) in2[3])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[0]); + output[8] = ((limb) ((s32) in2[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in2[3])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[0]); + output[9] = ((limb) ((s32) in2[4])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[0]); + output[10] = 2 * (((limb) ((s32) in2[5])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[1])) + + ((limb) ((s32) in2[4])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[4]) + + ((limb) ((s32) in2[2])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[2]); + output[11] = ((limb) ((s32) in2[5])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[2]); + output[12] = ((limb) ((s32) in2[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in2[5])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[3])) + + ((limb) ((s32) in2[4])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[4]); + output[13] = ((limb) ((s32) in2[6])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[6]) + + ((limb) ((s32) in2[5])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[4]); + output[14] = 2 * (((limb) ((s32) in2[7])) * ((s32) in[7]) + + ((limb) ((s32) in2[5])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[5])) + + ((limb) ((s32) in2[6])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[6]); + output[15] = ((limb) ((s32) in2[7])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[7]) + + ((limb) ((s32) in2[6])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[6]); + output[16] = ((limb) ((s32) in2[8])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in2[7])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[7])); + output[17] = ((limb) ((s32) in2[8])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[8]); + output[18] = 2 * ((limb) ((s32) in2[9])) * ((s32) in[9]); + }*/ + +/* Reduce a long form to a short form by taking the input mod 2^255 - 19. + * + * On entry: |output[i]| < 14*2^54 + * On exit: |output[0..8]| < 280*2^54 */ +static void freduce_degree(limb *output) { + /* Each of these shifts and adds ends up multiplying the value by 19. + * + * For output[0..8], the absolute entry value is < 14*2^54 and we add, at + * most, 19*14*2^54 thus, on exit, |output[0..8]| < 280*2^54. */ + output[8] += output[18] << 4; + output[8] += output[18] << 1; + output[8] += output[18]; + output[7] += output[17] << 4; + output[7] += output[17] << 1; + output[7] += output[17]; + output[6] += output[16] << 4; + output[6] += output[16] << 1; + output[6] += output[16]; + output[5] += output[15] << 4; + output[5] += output[15] << 1; + output[5] += output[15]; + output[4] += output[14] << 4; + output[4] += output[14] << 1; + output[4] += output[14]; + output[3] += output[13] << 4; + output[3] += output[13] << 1; + output[3] += output[13]; + output[2] += output[12] << 4; + output[2] += output[12] << 1; + output[2] += output[12]; + output[1] += output[11] << 4; + output[1] += output[11] << 1; + output[1] += output[11]; + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; +} + +#if (-1 & 3) != 3 +#error "This code only works on a two's complement system" +#endif + +/* return v / 2^26, using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_26(const limb v) +{ + /* High word of v; no shift needed. */ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x3ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 6; + /* Should return v / (1<<26) */ + return (v + roundoff) >> 26; +} + +/* return v / (2^25), using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_25(const limb v) +{ + /* High word of v; no shift needed*/ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x1ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 7; + /* Should return v / (1<<25) */ + return (v + roundoff) >> 25; +} + +/* Reduce all coefficients of the short form input so that |x| < 2^26. + * + * On entry: |output[i]| < 280*2^54 */ +static void freduce_coefficients(limb *output) { + unsigned i; + + output[10] = 0; + + for (i = 0; i < 10; i += 2) { + limb over = div_by_2_26(output[i]); + /* The entry condition (that |output[i]| < 280*2^54) means that over is, at + * most, 280*2^28 in the first iteration of this loop. This is added to the + * next limb and we can approximate the resulting bound of that limb by + * 281*2^54. */ + output[i] -= over << 26; + output[i+1] += over; + + /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| < + * 281*2^29. When this is added to the next limb, the resulting bound can + * be approximated as 281*2^54. + * + * For subsequent iterations of the loop, 281*2^54 remains a conservative + * bound and no overflow occurs. */ + over = div_by_2_25(output[i+1]); + output[i+1] -= over << 25; + output[i+2] += over; + } + /* Now |output[10]| < 281*2^29 and all other coefficients are reduced. */ + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; + + output[10] = 0; + + /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29 + * So |over| will be no more than 2^16. */ + { + limb over = div_by_2_26(output[0]); + output[0] -= over << 26; + output[1] += over; + } + + /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The + * bound on |output[1]| is sufficient to meet our needs. */ +} + +/* A helpful wrapper around fproduct: output = in * in2. + * + * On entry: |in[i]| < 2^27 and |in2[i]| < 2^27. + * + * output must be distinct to both inputs. The output is reduced degree + * (indeed, one need only provide storage for 10 limbs) and |output[i]| < 2^26. + static void fmul32(limb *output, const limb *in, const limb *in2) + { + limb t[19]; + fproduct(t, in, in2); + //|t[i]| < 14*2^54 + freduce_degree(t); + freduce_coefficients(t); + // |t[i]| < 2^26 + memcpy(output, t, sizeof(limb) * 10); + }*/ + +/* Square a number: output = in**2 + * + * output must be distinct from the input. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fsquare_inner(limb *output, const limb *in) { + output[0] = ((limb) ((s32) in[0])) * ((s32) in[0]); + output[1] = 2 * ((limb) ((s32) in[0])) * ((s32) in[1]); + output[2] = 2 * (((limb) ((s32) in[1])) * ((s32) in[1]) + + ((limb) ((s32) in[0])) * ((s32) in[2])); + output[3] = 2 * (((limb) ((s32) in[1])) * ((s32) in[2]) + + ((limb) ((s32) in[0])) * ((s32) in[3])); + output[4] = ((limb) ((s32) in[2])) * ((s32) in[2]) + + 4 * ((limb) ((s32) in[1])) * ((s32) in[3]) + + 2 * ((limb) ((s32) in[0])) * ((s32) in[4]); + output[5] = 2 * (((limb) ((s32) in[2])) * ((s32) in[3]) + + ((limb) ((s32) in[1])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[5])); + output[6] = 2 * (((limb) ((s32) in[3])) * ((s32) in[3]) + + ((limb) ((s32) in[2])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[6]) + + 2 * ((limb) ((s32) in[1])) * ((s32) in[5])); + output[7] = 2 * (((limb) ((s32) in[3])) * ((s32) in[4]) + + ((limb) ((s32) in[2])) * ((s32) in[5]) + + ((limb) ((s32) in[1])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[7])); + output[8] = ((limb) ((s32) in[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in[2])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[1])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[5]))); + output[9] = 2 * (((limb) ((s32) in[4])) * ((s32) in[5]) + + ((limb) ((s32) in[3])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[8]) + + ((limb) ((s32) in[0])) * ((s32) in[9])); + output[10] = 2 * (((limb) ((s32) in[5])) * ((s32) in[5]) + + ((limb) ((s32) in[4])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[3])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[9]))); + output[11] = 2 * (((limb) ((s32) in[5])) * ((s32) in[6]) + + ((limb) ((s32) in[4])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[8]) + + ((limb) ((s32) in[2])) * ((s32) in[9])); + output[12] = ((limb) ((s32) in[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in[4])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[5])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[9]))); + output[13] = 2 * (((limb) ((s32) in[6])) * ((s32) in[7]) + + ((limb) ((s32) in[5])) * ((s32) in[8]) + + ((limb) ((s32) in[4])) * ((s32) in[9])); + output[14] = 2 * (((limb) ((s32) in[7])) * ((s32) in[7]) + + ((limb) ((s32) in[6])) * ((s32) in[8]) + + 2 * ((limb) ((s32) in[5])) * ((s32) in[9])); + output[15] = 2 * (((limb) ((s32) in[7])) * ((s32) in[8]) + + ((limb) ((s32) in[6])) * ((s32) in[9])); + output[16] = ((limb) ((s32) in[8])) * ((s32) in[8]) + + 4 * ((limb) ((s32) in[7])) * ((s32) in[9]); + output[17] = 2 * ((limb) ((s32) in[8])) * ((s32) in[9]); + output[18] = 2 * ((limb) ((s32) in[9])) * ((s32) in[9]); +} + +/* fsquare sets output = in^2. + * + * On entry: The |in| argument is in reduced coefficients form and |in[i]| < + * 2^27. + * + * On exit: The |output| argument is in reduced coefficients form (indeed, one + * need only provide storage for 10 limbs) and |out[i]| < 2^26. */ +static void +fsquare32(limb *output, const limb *in) { + limb t[19]; + fsquare_inner(t, in); + /* |t[i]| < 14*2^54 because the largest product of two limbs will be < + * 2^(27+27) and fsquare_inner adds together, at most, 14 of those + * products. */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +#if (-32 >> 1) != -16 +#error "This code only works when >> does sign-extension on negative numbers" +#endif + +/* s32_eq returns 0xffffffff iff a == b and zero otherwise. */ +static s32 s32_eq(s32 a, s32 b) { + a = ~(a ^ b); + a &= a << 16; + a &= a << 8; + a &= a << 4; + a &= a << 2; + a &= a << 1; + return a >> 31; +} + +/* s32_gte returns 0xffffffff if a >= b and zero otherwise, where a and b are + * both non-negative. */ +static s32 s32_gte(s32 a, s32 b) { + a -= b; + /* a >= 0 iff a >= b. */ + return ~(a >> 31); +} + +/* Take a fully reduced polynomial form number and contract it into a + * little-endian, 32-byte array. + * + * On entry: |input_limbs[i]| < 2^26 */ +static void fcontract32(u8 *output, limb *input_limbs) +{ + int i; + int j; + s32 input[10]; + s32 mask; + + /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */ + for (i = 0; i < 10; i++) + input[i] = (s32)input_limbs[i]; + + for (j = 0; j < 2; ++j) { + for (i = 0; i < 9; ++i) { + if ((i & 1) == 1) { + /* This calculation is a time-invariant way to make input[i] + * non-negative by borrowing from the next-larger limb. */ + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 25); + input[i] = input[i] + (carry << 25); + input[i+1] = input[i+1] - carry; + } else { + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 26); + input[i] = input[i] + (carry << 26); + input[i+1] = input[i+1] - carry; + } + } + + /* There's no greater limb for input[9] to borrow from, but we can multiply + * by 19 and borrow from input[0], which is valid mod 2^255-19. */ + { + const s32 mask = input[9] >> 31; + const s32 carry = -((input[9] & mask) >> 25); + input[9] = input[9] + (carry << 25); + input[0] = input[0] - (carry * 19); + } + + /* After the first iteration, input[1..9] are non-negative and fit within + * 25 or 26 bits, depending on position. However, input[0] may be + * negative. */ + } + + /* The first borrow-propagation pass above ended with every limb + except (possibly) input[0] non-negative. + + If input[0] was negative after the first pass, then it was because of a + carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most, + one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19. + + In the second pass, each limb is decreased by at most one. Thus the second + borrow-propagation pass could only have wrapped around to decrease + input[0] again if the first pass left input[0] negative *and* input[1] + through input[9] were all zero. In that case, input[1] is now 2^25 - 1, + and this last borrow-propagation step will leave input[1] non-negative. */ + { + const s32 mask = input[0] >> 31; + const s32 carry = -((input[0] & mask) >> 26); + input[0] = input[0] + (carry << 26); + input[1] = input[1] - carry; + } + + /* All input[i] are now non-negative. However, there might be values between + * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */ + for (j = 0; j < 2; j++) { + for (i = 0; i < 9; i++) { + if ((i & 1) == 1) { + const s32 carry = input[i] >> 25; + input[i] &= 0x1ffffff; + input[i+1] += carry; + } else { + const s32 carry = input[i] >> 26; + input[i] &= 0x3ffffff; + input[i+1] += carry; + } + } + + { + const s32 carry = input[9] >> 25; + input[9] &= 0x1ffffff; + input[0] += 19*carry; + } + } + + /* If the first carry-chain pass, just above, ended up with a carry from + * input[9], and that caused input[0] to be out-of-bounds, then input[0] was + * < 2^26 + 2*19, because the carry was, at most, two. + * + * If the second pass carried from input[9] again then input[0] is < 2*19 and + * the input[9] -> input[0] carry didn't push input[0] out of bounds. */ + + /* It still remains the case that input might be between 2^255-19 and 2^255. + * In this case, input[1..9] must take their maximum value and input[0] must + * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */ + mask = s32_gte(input[0], 0x3ffffed); + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + mask &= s32_eq(input[i], 0x1ffffff); + } else { + mask &= s32_eq(input[i], 0x3ffffff); + } + } + + /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus + * this conditionally subtracts 2^255-19. */ + input[0] -= mask & 0x3ffffed; + + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + input[i] -= mask & 0x1ffffff; + } else { + input[i] -= mask & 0x3ffffff; + } + } + + input[1] <<= 2; + input[2] <<= 3; + input[3] <<= 5; + input[4] <<= 6; + input[6] <<= 1; + input[7] <<= 3; + input[8] <<= 4; + input[9] <<= 6; +#define F(i, s) \ +output[s+0] |= input[i] & 0xff; \ +output[s+1] = (input[i] >> 8) & 0xff; \ +output[s+2] = (input[i] >> 16) & 0xff; \ +output[s+3] = (input[i] >> 24) & 0xff; + output[0] = 0; + output[16] = 0; + F(0,0); + F(1,3); + F(2,6); + F(3,9); + F(4,12); + F(5,16); + F(6,19); + F(7,22); + F(8,25); + F(9,28); +#undef F +} + +bits320 bits320_limbs(limb limbs[10]) +{ + bits320 output; int32_t i; + for (i=0; i<10; i++) + output.uints[i] = limbs[i]; + return(output); +} + +static inline bits320 fscalar_product(const bits320 in,const uint64_t scalar) +{ + limb output[10],input[10]; int32_t i; + for (i=0; i<10; i++) + input[i] = in.uints[i]; + fscalar_product32(output,input,scalar); + return(bits320_limbs(output)); +} + +static inline bits320 fsquare_times(const bits320 in,uint64_t count) +{ + limb output[10],input[10]; int32_t i; + for (i=0; i<10; i++) + input[i] = in.uints[i]; + for (i=0; i Output: 2Q, Q+Q' +// x2 z2: long form && x3 z3: long form +// x z: short form, destroyed && xprime zprime: short form, destroyed +// qmqp: short form, preserved +static inline void +fmonty(bits320 *x2, bits320 *z2, // output 2Q + bits320 *x3, bits320 *z3, // output Q + Q' + bits320 *x, bits320 *z, // input Q + bits320 *xprime, bits320 *zprime, // input Q' + const bits320 qmqp) // input Q - Q' +{ + bits320 origx,origxprime,zzz,xx,zz,xxprime,zzprime; + origx = *x; + *x = fsum(*x,*z), fdifference_backwards(z->ulongs,origx.ulongs); // does x - z + origxprime = *xprime; + *xprime = fsum(*xprime,*zprime), fdifference_backwards(zprime->ulongs,origxprime.ulongs); + xxprime = fmul(*xprime,*z), zzprime = fmul(*x,*zprime); + origxprime = xxprime; + xxprime = fsum(xxprime,zzprime), fdifference_backwards(zzprime.ulongs,origxprime.ulongs); + *x3 = fsquare_times(xxprime,1), *z3 = fmul(fsquare_times(zzprime,1),qmqp); + xx = fsquare_times(*x,1), zz = fsquare_times(*z,1); + *x2 = fmul(xx,zz); + fdifference_backwards(zz.ulongs,xx.ulongs); // does zz = xx - zz + zzz = fscalar_product(zz,121665); + *z2 = fmul(zz,fsum(zzz,xx)); +} + +// ----------------------------------------------------------------------------- +// Maybe swap the contents of two limb arrays (@a and @b), each @len elements +// long. Perform the swap iff @swap is non-zero. +// This function performs the swap without leaking any side-channel information. +// ----------------------------------------------------------------------------- +static inline void swap_conditional(bits320 *a,bits320 *b,uint64_t iswap) +{ + int32_t i; const uint64_t swap = -iswap; + for (i=0; i<5; ++i) + { + const uint64_t x = swap & (a->ulongs[i] ^ b->ulongs[i]); + a->ulongs[i] ^= x, b->ulongs[i] ^= x; + } +} + +// Calculates nQ where Q is the x-coordinate of a point on the curve +// resultx/resultz: the x coordinate of the resulting curve point (short form) +// n: a little endian, 32-byte number +// q: a point of the curve (short form) +void cmult(bits320 *resultx,bits320 *resultz,bits256 secret,const bits320 q) +{ + int32_t i,j; bits320 a,b,c,d,e,f,g,h,*t; + bits320 Zero320bits,One320bits, *nqpqx = &a,*nqpqz = &b,*nqx = &c,*nqz = &d,*nqpqx2 = &e,*nqpqz2 = &f,*nqx2 = &g,*nqz2 = &h; + memset(&Zero320bits,0,sizeof(Zero320bits)); + memset(&One320bits,0,sizeof(One320bits)), One320bits.ulongs[0] = 1; + a = d = e = g = Zero320bits, b = c = f = h = One320bits; + *nqpqx = q; + for (i=0; i<32; i++) + { + uint8_t byte = secret.bytes[31 - i]; + for (j=0; j<8; j++) + { + const uint64_t bit = byte >> 7; + swap_conditional(nqx,nqpqx,bit), swap_conditional(nqz,nqpqz,bit); + fmonty(nqx2,nqz2,nqpqx2,nqpqz2,nqx,nqz,nqpqx,nqpqz,q); + swap_conditional(nqx2,nqpqx2,bit), swap_conditional(nqz2,nqpqz2,bit); + t = nqx, nqx = nqx2, nqx2 = t; + t = nqz, nqz = nqz2, nqz2 = t; + t = nqpqx, nqpqx = nqpqx2, nqpqx2 = t; + t = nqpqz, nqpqz = nqpqz2, nqpqz2 = t; + byte <<= 1; + } + } + *resultx = *nqx, *resultz = *nqz; +} + +// Shamelessly copied from donna's code that copied djb's code, changed a little +inline bits320 crecip(const bits320 z) +{ + bits320 a,t0,b,c; + /* 2 */ a = fsquare_times(z, 1); // a = 2 + /* 8 */ t0 = fsquare_times(a, 2); + /* 9 */ b = fmul(t0, z); // b = 9 + /* 11 */ a = fmul(b, a); // a = 11 + /* 22 */ t0 = fsquare_times(a, 1); + /* 2^5 - 2^0 = 31 */ b = fmul(t0, b); + /* 2^10 - 2^5 */ t0 = fsquare_times(b, 5); + /* 2^10 - 2^0 */ b = fmul(t0, b); + /* 2^20 - 2^10 */ t0 = fsquare_times(b, 10); + /* 2^20 - 2^0 */ c = fmul(t0, b); + /* 2^40 - 2^20 */ t0 = fsquare_times(c, 20); + /* 2^40 - 2^0 */ t0 = fmul(t0, c); + /* 2^50 - 2^10 */ t0 = fsquare_times(t0, 10); + /* 2^50 - 2^0 */ b = fmul(t0, b); + /* 2^100 - 2^50 */ t0 = fsquare_times(b, 50); + /* 2^100 - 2^0 */ c = fmul(t0, b); + /* 2^200 - 2^100 */ t0 = fsquare_times(c, 100); + /* 2^200 - 2^0 */ t0 = fmul(t0, c); + /* 2^250 - 2^50 */ t0 = fsquare_times(t0, 50); + /* 2^250 - 2^0 */ t0 = fmul(t0, b); + /* 2^255 - 2^5 */ t0 = fsquare_times(t0, 5); + /* 2^255 - 21 */ return(fmul(t0, a)); +} + +void OS_randombytes(unsigned char *x,long xlen); + +bits256 rand256(int32_t privkeyflag) +{ + bits256 randval; + OS_randombytes(randval.bytes,sizeof(randval)); + if ( privkeyflag != 0 ) + randval.bytes[0] &= 0xf8, randval.bytes[31] &= 0x7f, randval.bytes[31] |= 0x40; + return(randval); +} + +bits256 curve25519_basepoint9() +{ + bits256 basepoint; + memset(&basepoint,0,sizeof(basepoint)); + basepoint.bytes[0] = 9; + return(basepoint); +} + +bits256 curve25519_keypair(bits256 *pubkeyp) +{ + bits256 privkey; + privkey = rand256(1); + *pubkeyp = curve25519(privkey,curve25519_basepoint9()); + //printf("[%llx %llx] ",privkey.txid,(*pubkeyp).txid); + return(privkey); +} + +bits256 curve25519_shared(bits256 privkey,bits256 otherpub) +{ + bits256 shared,hash; + shared = curve25519(privkey,otherpub); + vcalc_sha256(0,hash.bytes,shared.bytes,sizeof(shared)); + //printf("priv.%llx pub.%llx shared.%llx -> hash.%llx\n",privkey.txid,pubkey.txid,shared.txid,hash.txid); + //hash.bytes[0] &= 0xf8, hash.bytes[31] &= 0x7f, hash.bytes[31] |= 64; + return(hash); +} + +int32_t curve25519_donna(uint8_t *mypublic,const uint8_t *secret,const uint8_t *basepoint) +{ + bits256 val,p,bp; + memcpy(p.bytes,secret,sizeof(p)); + memcpy(bp.bytes,basepoint,sizeof(bp)); + val = curve25519(p,bp); + memcpy(mypublic,val.bytes,sizeof(val)); + return(0); +} + +uint64_t conv_NXTpassword(unsigned char *mysecret,unsigned char *mypublic,uint8_t *pass,int32_t passlen) +{ + static uint8_t basepoint[32] = {9}; + uint64_t addr; uint8_t hash[32]; + if ( pass != 0 && passlen != 0 ) + vcalc_sha256(0,mysecret,pass,passlen); + mysecret[0] &= 248, mysecret[31] &= 127, mysecret[31] |= 64; + curve25519_donna(mypublic,mysecret,basepoint); + vcalc_sha256(0,hash,mypublic,32); + memcpy(&addr,hash,sizeof(addr)); + return(addr); +} + +uint256 komodo_kvprivkey(uint256 *pubkeyp,char *passphrase) +{ + uint256 privkey; + conv_NXTpassword((uint8_t *)&privkey,(uint8_t *)pubkeyp,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + return(privkey); +} + +uint256 komodo_kvsig(uint8_t *buf,int32_t len,uint256 _privkey) +{ + bits256 sig,hash,otherpub,checksig,pubkey,privkey; uint256 usig; + memcpy(&privkey,&_privkey,sizeof(privkey)); + vcalc_sha256(0,hash.bytes,buf,len); + otherpub = curve25519(hash,curve25519_basepoint9()); + pubkey = curve25519(privkey,curve25519_basepoint9()); + sig = curve25519_shared(privkey,otherpub); + checksig = curve25519_shared(hash,pubkey); + /*int32_t i; for (i=0; i "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&privkey)[i]); + printf(" -> "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey\n");*/ + memcpy(&usig,&sig,sizeof(usig)); + return(usig); +} + +int32_t komodo_kvsigverify(uint8_t *buf,int32_t len,uint256 _pubkey,uint256 sig) +{ + bits256 hash,checksig,pubkey; static uint256 zeroes; + memcpy(&pubkey,&_pubkey,sizeof(pubkey)); + if ( memcmp(&pubkey,&zeroes,sizeof(pubkey)) != 0 ) + { + vcalc_sha256(0,hash.bytes,buf,len); + checksig = curve25519_shared(hash,pubkey); + /*int32_t i; for (i=0; i "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&hash)[i]); + printf(" -> "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" verify pubkey\n"); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&sig)[i]); + printf(" sig vs"); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&checksig)[i]); + printf(" checksig\n");*/ + if ( memcmp(&checksig,&sig,sizeof(sig)) != 0 ) + return(-1); + //else printf("VALIDATED\n"); + } + return(0); +} + +#endif diff --git a/src/komodo_events.h b/src/komodo_events.h index cb7861107..14a9478af 100644 --- a/src/komodo_events.h +++ b/src/komodo_events.h @@ -39,14 +39,19 @@ struct komodo_event *komodo_eventadd(struct komodo_state *sp,int32_t height,char void komodo_eventadd_notarized(struct komodo_state *sp,char *symbol,int32_t height,char *dest,uint256 notarized_hash,uint256 notarized_desttxid,int32_t notarizedheight) { struct komodo_event_notarized N; - memset(&N,0,sizeof(N)); - N.blockhash = notarized_hash; - N.desttxid = notarized_desttxid; - N.notarizedheight = notarizedheight; - strcpy(N.dest,dest); - komodo_eventadd(sp,height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N)); - if ( sp != 0 ) - komodo_notarized_update(sp,height,notarizedheight,notarized_hash,notarized_desttxid); + if ( komodo_verifynotarization(symbol,dest,height,notarizedheight,notarized_hash,notarized_desttxid) != 0 ) + printf("[%s] error validating notarization ht.%d notarized_height.%d\n",ASSETCHAINS_SYMBOL,height,notarizedheight); + else + { + memset(&N,0,sizeof(N)); + N.blockhash = notarized_hash; + N.desttxid = notarized_desttxid; + N.notarizedheight = notarizedheight; + strcpy(N.dest,dest); + komodo_eventadd(sp,height,symbol,KOMODO_EVENT_NOTARIZED,(uint8_t *)&N,sizeof(N)); + if ( sp != 0 ) + komodo_notarized_update(sp,height,notarizedheight,notarized_hash,notarized_desttxid); + } } void komodo_eventadd_pubkeys(struct komodo_state *sp,char *symbol,int32_t height,uint8_t num,uint8_t pubkeys[64][33]) diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index e00357ee7..4ec0b0f04 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -673,15 +673,15 @@ int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout,char *source) { - uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; + uint8_t rmd160[20],rmd160s[64*20],addrtype,shortflag,pubkey33[33]; int32_t didstats,i,j,n,kvheight,len,tokomodo,kmdheight,otherheights[64],kmdheights[64]; int8_t baseids[64]; char base[4],coinaddr[64],destaddr[64]; uint256 txids[64]; uint16_t vouts[64]; uint64_t convtoshis,seed; int64_t fee,fiatoshis,komodoshis,checktoshis,values[64],srcvalues[64]; struct pax_transaction *pax,*pax2; struct komodo_state *basesp; double diff; const char *typestr = "unknown"; - if ( KOMODO_PAX == 0 ) - return("nopax"); - if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 ) + if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) < 0 && opretbuf[0] != 'K' ) { //printf("komodo_opreturn skip %s\n",ASSETCHAINS_SYMBOL); return("assetchain"); } + //else if ( KOMODO_PAX == 0 ) + // return("nopax"); memset(baseids,0xff,sizeof(baseids)); memset(values,0,sizeof(values)); memset(srcvalues,0,sizeof(srcvalues)); @@ -689,7 +689,11 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 memset(kmdheights,0,sizeof(kmdheights)); memset(otherheights,0,sizeof(otherheights)); tokomodo = (komodo_is_issuer() == 0); - if ( opretbuf[0] == 'D' ) + if ( opretbuf[0] == 'K' && opretlen != 40 ) + { + komodo_kvupdate(opretbuf,opretlen,value); + } + else if ( opretbuf[0] == 'D' ) { tokomodo = 0; if ( opretlen == 38 ) // any KMD tx @@ -763,7 +767,7 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 } } } - else if ( kmdheight > 91800 ) + else if ( kmdheight > 91800 && strcmp(base,ASSETCHAINS_SYMBOL) == 0 ) printf("pax %s deposit %.8f rejected kmdheight.%d %.8f KMD\n",base,dstr(fiatoshis),kmdheight,dstr(value)); } } @@ -819,11 +823,11 @@ const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int3 } else if ( opretbuf[0] == 'W' )//&& opretlen >= 38 ) { - if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 ) + if ( komodo_baseid((char *)&opretbuf[opretlen-4]) >= 0 && strcmp("KMD",(char *)&opretbuf[opretlen-4]) != 0 ) { for (i=0; iredeemed += value; pax->didstats = 1; - //if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) + if ( strcmp(CURRENCIES[baseids[i]],ASSETCHAINS_SYMBOL) == 0 ) printf("ht.%d %.8f ########### %p redeemed %s += %.8f %.8f kht.%d ht.%d\n",height,dstr(value),basesp,CURRENCIES[baseids[i]],dstr(value),dstr(srcvalues[i]),kmdheights[i],otherheights[i]); } } diff --git a/src/komodo_globals.h b/src/komodo_globals.h index 1e985da6b..a63c4c754 100644 --- a/src/komodo_globals.h +++ b/src/komodo_globals.h @@ -27,6 +27,7 @@ int32_t komodo_longestchain(); pthread_mutex_t komodo_mutex; #define KOMODO_ELECTION_GAP 2000 //((ASSETCHAINS_SYMBOL[0] == 0) ? 2000 : 100) +#define IGUANA_MAXSCRIPTSIZE 10001 struct pax_transaction *PAX; int32_t NUM_PRICES; uint32_t *PVALS; @@ -47,5 +48,8 @@ uint32_t ASSETCHAINS_MAGIC = 2387029918; uint64_t ASSETCHAINS_SUPPLY = 10; uint32_t KOMODO_INITDONE; -char KMDUSERPASS[1024]; uint16_t BITCOIND_PORT = 7771; +char KMDUSERPASS[4096],BTCUSERPASS[4096]; uint16_t BITCOIND_PORT = 7771; uint64_t PENDING_KOMODO_TX; + +struct komodo_kv *KOMODO_KV; +pthread_mutex_t KOMODO_KV_mutex; \ No newline at end of file diff --git a/src/komodo_kv.h b/src/komodo_kv.h new file mode 100644 index 000000000..07adadf2b --- /dev/null +++ b/src/komodo_kv.h @@ -0,0 +1,184 @@ +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#ifndef H_KOMODOKV_H +#define H_KOMODOKV_H + +int32_t komodo_kvcmp(uint8_t *refvalue,uint16_t refvaluesize,uint8_t *value,uint16_t valuesize) +{ + if ( refvalue == 0 && value == 0 ) + return(0); + else if ( refvalue == 0 || value == 0 ) + return(-1); + else if ( refvaluesize != valuesize ) + return(-1); + else return(memcmp(refvalue,value,valuesize)); +} + +int32_t komodo_kvnumdays(uint32_t flags) +{ + int32_t numdays; + if ( (numdays= ((flags>>2)&0x3ff)+1) > 365 ) + numdays = 365; + return(numdays); +} + +int32_t komodo_kvduration(uint32_t flags) +{ + return(komodo_kvnumdays(flags) * KOMODO_KVDURATION); +} + +uint64_t komodo_kvfee(uint32_t flags,int32_t opretlen,int32_t keylen) +{ + int32_t numdays,k; uint64_t fee; + if ( (k= keylen) > 32 ) + k = 32; + numdays = komodo_kvnumdays(flags); + if ( (fee= (numdays*(opretlen * opretlen / k))) < 100000 ) + fee = 100000; + return(fee); +} + +int32_t komodo_kvsearch(uint256 *pubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen) +{ + struct komodo_kv *ptr; int32_t duration,retval = -1; + *heightp = -1; + *flagsp = 0; + memset(pubkeyp,0,sizeof(*pubkeyp)); + portable_mutex_lock(&KOMODO_KV_mutex); + HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); + if ( ptr != 0 ) + { + duration = komodo_kvduration(ptr->flags); + //printf("duration.%d flags.%d current.%d ht.%d keylen.%d valuesize.%d\n",duration,ptr->flags,current_height,ptr->height,ptr->keylen,ptr->valuesize); + if ( current_height > (ptr->height + duration) ) + { + HASH_DELETE(hh,KOMODO_KV,ptr); + if ( ptr->value != 0 ) + free(ptr->value); + if ( ptr->key != 0 ) + free(ptr->key); + free(ptr); + } + else + { + *heightp = ptr->height; + *flagsp = ptr->flags; + int32_t i; for (i=0; i<32; i++) + { + //printf("%02x",((uint8_t *)&ptr->pubkey)[31-i]); + ((uint8_t *)pubkeyp)[i] = ((uint8_t *)&ptr->pubkey)[31-i]; + } + //printf(" ptr->pubkey\n"); + memcpy(pubkeyp,&ptr->pubkey,sizeof(*pubkeyp)); + if ( (retval= ptr->valuesize) != 0 ) + memcpy(value,ptr->value,retval); + } + } + portable_mutex_unlock(&KOMODO_KV_mutex); + return(retval); +} + +void komodo_kvupdate(uint8_t *opretbuf,int32_t opretlen,uint64_t value) +{ + static uint256 zeroes; + uint32_t flags; uint256 pubkey,refpubkey,sig; int32_t i,refvaluesize,hassig,coresize,haspubkey,height,kvheight; uint16_t keylen,valuesize,newflag = 0; uint8_t *key,*valueptr,keyvalue[IGUANA_MAXSCRIPTSIZE]; struct komodo_kv *ptr; char *transferpubstr,*tstr; uint64_t fee; + iguana_rwnum(0,&opretbuf[1],sizeof(keylen),&keylen); + iguana_rwnum(0,&opretbuf[3],sizeof(valuesize),&valuesize); + iguana_rwnum(0,&opretbuf[5],sizeof(height),&height); + iguana_rwnum(0,&opretbuf[9],sizeof(flags),&flags); + key = &opretbuf[13]; + valueptr = &key[keylen]; + fee = komodo_kvfee(flags,opretlen,keylen); + //printf("fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); + if ( value >= fee ) + { + coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(keylen)+sizeof(valuesize)+keylen+valuesize+1); + if ( opretlen == coresize || opretlen == coresize+sizeof(uint256) || opretlen == coresize+2*sizeof(uint256) ) + { + memset(&pubkey,0,sizeof(pubkey)); + memset(&sig,0,sizeof(sig)); + if ( (haspubkey= (opretlen >= coresize+sizeof(uint256))) != 0 ) + { + for (i=0; i<32; i++) + ((uint8_t *)&pubkey)[i] = opretbuf[coresize+i]; + } + if ( (hassig= (opretlen == coresize+sizeof(uint256)*2)) != 0 ) + { + for (i=0; i<32; i++) + ((uint8_t *)&sig)[i] = opretbuf[coresize+sizeof(uint256)+i]; + } + memcpy(keyvalue,key,keylen); + if ( (refvaluesize= komodo_kvsearch((uint256 *)&refpubkey,height,&flags,&kvheight,&keyvalue[keylen],key,keylen)) >= 0 ) + { + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + { + if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 ) + { + //printf("komodo_kvsigverify error [%d]\n",coresize-13); + return; + } + } + } + portable_mutex_lock(&KOMODO_KV_mutex); + HASH_FIND(hh,KOMODO_KV,key,keylen,ptr); + if ( ptr != 0 ) + { + //if ( (ptr->flags & KOMODO_KVPROTECTED) != 0 ) + { + tstr = (char *)"transfer:"; + transferpubstr = (char *)&valueptr[strlen(tstr)]; + if ( strncmp(tstr,(char *)valueptr,strlen(tstr)) == 0 && is_hexstr(transferpubstr,0) == 64 ) + { + printf("transfer.(%s) to [%s]? ishex.%d\n",key,transferpubstr,is_hexstr(transferpubstr,0)); + for (i=0; i<32; i++) + ((uint8_t *)&pubkey)[31-i] = _decode_hex(&transferpubstr[i*2]); + } + } + } + else if ( ptr == 0 ) + { + ptr = (struct komodo_kv *)calloc(1,sizeof(*ptr)); + ptr->key = (uint8_t *)calloc(1,keylen); + ptr->keylen = keylen; + memcpy(ptr->key,key,keylen); + newflag = 1; + HASH_ADD_KEYPTR(hh,KOMODO_KV,ptr->key,ptr->keylen,ptr); + } + if ( newflag != 0 || (ptr->flags & KOMODO_KVPROTECTED) == 0 ) + { + if ( ptr->value != 0 ) + free(ptr->value), ptr->value = 0; + if ( (ptr->valuesize= valuesize) != 0 ) + { + ptr->value = (uint8_t *)calloc(1,valuesize); + memcpy(ptr->value,valueptr,valuesize); + } + } + /*for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&ptr->pubkey)[i]); + printf(" <- "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" new pubkey\n");*/ + memcpy(&ptr->pubkey,&pubkey,sizeof(ptr->pubkey)); + ptr->height = height; + ptr->flags = flags | 1; + portable_mutex_unlock(&KOMODO_KV_mutex); + } else printf("size mismatch %d vs %d\n",opretlen,coresize); + } else printf("insufficient fee %.8f vs %.8f flags.%d keylen.%d valuesize.%d height.%d (%02x %02x %02x) (%02x %02x %02x)\n",(double)fee/COIN,(double)value/COIN,flags,keylen,valuesize,height,key[0],key[1],key[2],valueptr[0],valueptr[1],valueptr[2]); +} + +#endif diff --git a/src/komodo_notary.h b/src/komodo_notary.h index b6de38df8..fc77d1222 100644 --- a/src/komodo_notary.h +++ b/src/komodo_notary.h @@ -13,6 +13,8 @@ * * ******************************************************************************/ +#define KOMODO_MAINNET_START 178999 + const char *Notaries_genesis[][2] = { { "jl777_testA", "03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828" }, @@ -52,6 +54,12 @@ const char *Notaries_genesis[][2] = { "titomane_SH", "035f49d7a308dd9a209e894321f010d21b7793461b0c89d6d9231a3fe5f68d9960" }, }; +const char *Notaries_elected[][2] = // update with all elected notaries +{ + { "jl777_testA", "03b7621b44118017a16043f19b30cc8a4cfe068ac4e42417bae16ba460c80f3828" }, + { "jl777_testB", "02ebfc784a4ba768aad88d44d1045d240d47b26e248cafaf1c5169a42d7a61d344" }, +}; + int32_t komodo_ratify_threshold(int32_t height,uint64_t signedmask) { int32_t htind,numnotaries,i,wt = 0; @@ -226,6 +234,8 @@ int32_t komodo_notarizeddata(int32_t nHeight,uint256 *notarized_hashp,uint256 *n void komodo_init(int32_t height) { static int didinit; uint256 zero; int32_t i,k,n; uint8_t pubkeys[64][33]; + if ( 0 && height != 0 ) + printf("komodo_init ht.%d didinit.%d\n",height,didinit); if ( didinit == 0 ) { pthread_mutex_init(&komodo_mutex,NULL); @@ -246,6 +256,18 @@ void komodo_init(int32_t height) // Minerids[i] = -2; didinit = 1; } + else if ( height == KOMODO_MAINNET_START ) + { + n = (int32_t)(sizeof(Notaries_elected)/sizeof(*Notaries_elected)); + for (k=0; k= 0 && (relid= komodo_baseid(rel)) >= 0 ) { - portable_mutex_lock(&komodo_mutex); + //portable_mutex_lock(&komodo_mutex); for (i=NUM_PRICES-1; i>=0; i--) { ptr = &PVALS[36 * i]; @@ -440,18 +440,47 @@ uint64_t _komodo_paxprice(uint64_t *kmdbtcp,uint64_t *btcusdp,int32_t height,cha *kmdbtcp = ptr[MAX_CURRENCIES + 1] / 539; *btcusdp = ptr[MAX_CURRENCIES + 2] / 539; } - portable_mutex_unlock(&komodo_mutex); + //portable_mutex_unlock(&komodo_mutex); if ( kmdbtc != 0 && btcusd != 0 ) return(komodo_paxcalc(&ptr[1],baseid,relid,basevolume,kmdbtc,btcusd)); else return(0); } } - portable_mutex_unlock(&komodo_mutex); + //portable_mutex_unlock(&komodo_mutex); } //else printf("paxprice invalid base.%s %d, rel.%s %d\n",base,baseid,rel,relid); return(0); } -uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uint64_t basevolume) +int32_t komodo_kmdbtcusd(int32_t rwflag,uint64_t *kmdbtcp,uint64_t *btcusdp,int32_t height) +{ + static uint64_t *KMDBTCS,*BTCUSDS; static int32_t maxheight = 0; int32_t incr = 10000; + if ( height >= maxheight ) + { + //printf("height.%d maxheight.%d incr.%d\n",height,maxheight,incr); + if ( height >= maxheight+incr ) + incr = (height - (maxheight+incr) + 1000); + KMDBTCS = (uint64_t *)realloc(KMDBTCS,((incr + maxheight) * sizeof(*KMDBTCS))); + memset(&KMDBTCS[maxheight],0,(incr * sizeof(*KMDBTCS))); + BTCUSDS = (uint64_t *)realloc(BTCUSDS,((incr + maxheight) * sizeof(*BTCUSDS))); + memset(&BTCUSDS[maxheight],0,(incr * sizeof(*BTCUSDS))); + maxheight += incr; + } + if ( rwflag == 0 ) + { + *kmdbtcp = KMDBTCS[height]; + *btcusdp = BTCUSDS[height]; + } + else + { + KMDBTCS[height] = *kmdbtcp; + BTCUSDS[height] = *btcusdp; + } + if ( *kmdbtcp != 0 && *btcusdp != 0 ) + return(0); + else return(-1); +} + +uint64_t komodo_paxpriceB(uint64_t *seedp,int32_t height,char *base,char *rel,uint64_t basevolume) { int32_t i,j,k,ind,zeroes,numvotes,wt,nonz; int64_t delta; uint64_t lastprice,tolerance,den,densum,sum=0,votes[sizeof(Peggy_inds)/sizeof(*Peggy_inds)],btcusds[sizeof(Peggy_inds)/sizeof(*Peggy_inds)],kmdbtcs[sizeof(Peggy_inds)/sizeof(*Peggy_inds)],kmdbtc,btcusd; *seedp = komodo_seed(height); @@ -467,15 +496,19 @@ uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uin } numvotes = (int32_t)(sizeof(Peggy_inds)/sizeof(*Peggy_inds)); memset(votes,0,sizeof(votes)); - memset(btcusds,0,sizeof(btcusds)); - memset(kmdbtcs,0,sizeof(kmdbtcs)); - for (i=0; i chainActive.Tip()->nHeight ) + return(0); + portable_mutex_lock(&komodo_mutex); + for (i=0; i<17; i++) + { + if ( (price= komodo_paxpriceB(&seed,height-i,base,rel,basevolume)) != 0 ) + { + sum += price; + nonz++; + if ( 0 && i == 1 && nonz == 2 ) + { + diff = (((int64_t)price - (sum >> 1)) * 10000); + if ( diff < 0 ) + diff = -diff; + diff /= price; + printf("(%llu %llu %lld).%lld ",(long long)price,(long long)(sum>>1),(long long)(((int64_t)price - (sum >> 1)) * 10000),(long long)diff); + if ( diff < 33 ) + break; + } + else if ( 0 && i == 3 && nonz == 4 ) + { + diff = (((int64_t)price - (sum >> 2)) * 10000); + if ( diff < 0 ) + diff = -diff; + diff /= price; + printf("(%llu %llu %lld).%lld ",(long long)price,(long long)(sum>>2),(long long) (((int64_t)price - (sum >> 2)) * 10000),(long long)diff); + if ( diff < 20 ) + break; + } + } + if ( height < 165000 ) + break; + } + portable_mutex_unlock(&komodo_mutex); + if ( nonz != 0 ) + sum /= nonz; + //printf("-> %lld %s/%s i.%d ht.%d\n",(long long)sum,base,rel,i,height); + return(sum); +} + int32_t komodo_paxprices(int32_t *heights,uint64_t *prices,int32_t max,char *base,char *rel) { int32_t baseid=-1,relid=-1,i,num = 0; uint32_t *ptr; diff --git a/src/komodo_structs.h b/src/komodo_structs.h index d1da1d4c4..a35e7e008 100644 --- a/src/komodo_structs.h +++ b/src/komodo_structs.h @@ -37,6 +37,18 @@ #define KOMODO_OPRETURN_WITHDRAW 'W' // assetchain #define KOMODO_OPRETURN_REDEEMED 'X' +#define KOMODO_KVPROTECTED 1 +#define KOMODO_KVBINARY 2 +#define KOMODO_KVDURATION 1440 + +union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; +typedef union _bits256 bits256; + +union _bits320 { uint8_t bytes[40]; uint16_t ushorts[20]; uint32_t uints[10]; uint64_t ulongs[5]; uint64_t txid; }; +typedef union _bits320 bits320; + +struct komodo_kv { UT_hash_handle hh; bits256 pubkey; uint8_t *key,*value; int32_t height; uint32_t flags; uint16_t keylen,valuesize; }; + struct komodo_event_notarized { uint256 blockhash,desttxid; int32_t notarizedheight; char dest[16]; }; struct komodo_event_pubkeys { uint8_t num; uint8_t pubkeys[64][33]; }; struct komodo_event_opreturn { uint256 txid; uint64_t value; uint16_t vout,oplen; uint8_t opret[]; }; diff --git a/src/komodo_utils.h b/src/komodo_utils.h index 198caccd8..9ab9178b3 100644 --- a/src/komodo_utils.h +++ b/src/komodo_utils.h @@ -30,9 +30,6 @@ typedef struct queue char name[64],initflag; } queue_t; -union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; -typedef union _bits256 bits256; - #include "mini-gmp.c" #define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9" @@ -1347,7 +1344,11 @@ void komodo_configfile(char *symbol,uint16_t port) #else while ( fname[strlen(fname)-1] != '/' ) fname[strlen(fname)-1] = 0; +#ifdef __APPLE__ + strcat(fname,"Komodo.conf"); +#else strcat(fname,"komodo.conf"); +#endif #endif if ( (fp= fopen(fname,"rb")) != 0 ) { @@ -1363,7 +1364,13 @@ int32_t komodo_userpass(char *userpass,char *symbol) FILE *fp; char fname[512],username[512],password[512],confname[16]; userpass[0] = 0; if ( strcmp("KMD",symbol) == 0 ) + { +#ifdef __APPLE__ + sprintf(confname,"Komodo.conf"); +#else sprintf(confname,"komodo.conf"); +#endif + } else sprintf(confname,"%s.conf",symbol); komodo_statefname(fname,symbol,confname); if ( (fp= fopen(fname,"rb")) != 0 ) @@ -1492,25 +1499,40 @@ void komodo_args() } else { - char fname[512],username[512],password[4096]; FILE *fp; + char fname[512],username[512],password[4096]; int32_t iter; FILE *fp; ASSETCHAINS_PORT = 8777; - strcpy(fname,GetDataDir().string().c_str()); -#ifdef WIN32 - while ( fname[strlen(fname)-1] != '\\' ) - fname[strlen(fname)-1] = 0; - strcat(fname,".komodo/komodo.conf"); -#else - while ( fname[strlen(fname)-1] != '/' ) - fname[strlen(fname)-1] = 0; - strcat(fname,".komodo/komodo.conf"); -#endif - if ( (fp= fopen(fname,"rb")) != 0 ) + for (iter=0; iter<2; iter++) { - komodo_userpass(username,password,fp); - sprintf(KMDUSERPASS,"%s:%s",username,password); - fclose(fp); - //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS); - } else printf("couldnt open.(%s)\n",fname); + strcpy(fname,GetDataDir().string().c_str()); +#ifdef WIN32 + while ( fname[strlen(fname)-1] != '\\' ) + fname[strlen(fname)-1] = 0; + if ( iter == 0 ) + strcat(fname,".komodo\\komodo.conf"); + else strcat(fname,".bitcoin\\bitcoin.conf"); +#else + while ( fname[strlen(fname)-1] != '/' ) + fname[strlen(fname)-1] = 0; +#ifdef __APPLE__ + if ( iter == 0 ) + strcat(fname,"Komodo/Komodo.conf"); + else strcat(fname,"Bitcoin/Bitcoin.conf"); +#else + if ( iter == 0 ) + strcat(fname,".komodo/komodo.conf"); + else strcat(fname,".bitcoin/bitcoin.conf"); +#endif +#endif + if ( (fp= fopen(fname,"rb")) != 0 ) + { + komodo_userpass(username,password,fp); + sprintf(iter == 0 ? KMDUSERPASS : BTCUSERPASS,"%s:%s",username,password); + fclose(fp); + //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS); + } else printf("couldnt open.(%s)\n",fname); + if ( IS_KOMODO_NOTARY == 0 ) + break; + } } //fprintf(stderr,"%s chain params initialized\n",ASSETCHAINS_SYMBOL); } diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 62ac42675..8f3e67ef9 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -396,6 +396,10 @@ Value gettxoutsetinfo(const Array& params, bool fHelp) return ret; } +#define IGUANA_MAXSCRIPTSIZE 10001 +#define KOMODO_KVDURATION 1440 +#define KOMODO_KVBINARY 2 +extern char ASSETCHAINS_SYMBOL[16]; uint64_t komodo_interest(int32_t txheight,uint64_t nValue,uint32_t nLockTime,uint32_t tiptime); uint32_t komodo_txtime(uint256 hash); uint64_t komodo_paxprice(uint64_t *seedp,int32_t height,char *base,char *rel,uint64_t basevolume); @@ -404,6 +408,42 @@ int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height); char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len); uint32_t komodo_interest_args(int32_t *txheightp,uint32_t *tiptimep,uint64_t *valuep,uint256 hash,int32_t n); int32_t komodo_minerids(uint8_t *minerids,int32_t height); +int32_t komodo_kvsearch(uint256 *refpubkeyp,int32_t current_height,uint32_t *flagsp,int32_t *heightp,uint8_t value[IGUANA_MAXSCRIPTSIZE],uint8_t *key,int32_t keylen); + +Value kvsearch(const Array& params, bool fHelp) +{ + Object ret; uint32_t flags; uint8_t value[IGUANA_MAXSCRIPTSIZE],key[IGUANA_MAXSCRIPTSIZE]; int32_t duration,j,height,valuesize,keylen; uint256 refpubkey; static uint256 zeroes; + if (fHelp || params.size() != 1 ) + throw runtime_error("kvsearch key"); + LOCK(cs_main); + if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) + { + ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); + ret.push_back(Pair("currentheight", (int64_t)chainActive.Tip()->nHeight)); + ret.push_back(Pair("key",params[0].get_str())); + ret.push_back(Pair("keylen",keylen)); + if ( keylen < sizeof(key) ) + { + memcpy(key,params[0].get_str().c_str(),keylen); + if ( (valuesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&flags,&height,value,key,keylen)) >= 0 ) + { + std::string val; char *valuestr; + val.resize(valuesize); + valuestr = (char *)val.data(); + memcpy(valuestr,value,valuesize); + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + ret.push_back(Pair("owner",refpubkey.GetHex())); + ret.push_back(Pair("height",height)); + duration = ((flags >> 2) + 1) * KOMODO_KVDURATION; + ret.push_back(Pair("expiration", (int64_t)(height+duration))); + ret.push_back(Pair("flags",(int64_t)flags)); + ret.push_back(Pair("value",val)); + ret.push_back(Pair("valuesize",valuesize)); + } else ret.push_back(Pair("error",(char *)"cant find key")); + } else ret.push_back(Pair("error",(char *)"key too big")); + } else ret.push_back(Pair("error",(char *)"null key")); + return ret; +} Value minerids(const Array& params, bool fHelp) { diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index 824ad602a..5b89b18c4 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -112,6 +112,8 @@ static const CRPCConvertParam vRPCConvertParams[] = { "paxpending", 0 }, { "notaries", 1 }, { "minerids", 1 }, + { "kvsearch", 1 }, + { "kvupdate", 4 }, }; class CRPCConvertTable diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index d7f049280..7f677fcf8 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -305,6 +305,8 @@ static const CRPCCommand vRPCCommands[] = { "blockchain", "paxprices", &paxprices, true }, { "blockchain", "notaries", ¬aries, true }, { "blockchain", "minerids", &minerids, true }, + { "blockchain", "kvsearch", &kvsearch, true }, + { "blockchain", "kvupdate", &kvupdate, true }, /* Mining */ { "mining", "getblocktemplate", &getblocktemplate, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index 15a35a1d5..0fda9571f 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -247,6 +247,8 @@ extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value notaries(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value minerids(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value kvsearch(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value kvupdate(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value paxprice(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value paxpending(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value paxprices(const json_spirit::Array& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 604391023..151f2c835 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -404,9 +404,9 @@ static void SendMoney(const CTxDestination &address, CAmount nValue, bool fSubtr for (i=0; i= 3 ) + { + flags = atoi(params[2].get_str().c_str()); + //printf("flags.%d (%s) n.%d\n",flags,params[2].get_str().c_str(),n); + } else flags = 0; + if ( n >= 4 ) + privkey = komodo_kvprivkey(&pubkey,(char *)(n >= 4 ? params[3].get_str().c_str() : "password")); + haveprivkey = 1; + flags |= 1; + /*for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&privkey)[i]); + printf(" priv, "); + for (i=0; i<32; i++) + printf("%02x",((uint8_t *)&pubkey)[i]); + printf(" pubkey, privkey derived from (%s)\n",(char *)params[3].get_str().c_str()); + */ + LOCK2(cs_main, pwalletMain->cs_wallet); + if ( (keylen= (int32_t)strlen(params[0].get_str().c_str())) > 0 ) + { + key = (uint8_t *)params[0].get_str().c_str(); + if ( n >= 2 && params[1].get_str().c_str() != 0 ) + { + value = (uint8_t *)params[1].get_str().c_str(); + valuesize = (int32_t)strlen(params[1].get_str().c_str()); + } + memcpy(keyvalue,key,keylen); + if ( (refvaluesize= komodo_kvsearch(&refpubkey,chainActive.Tip()->nHeight,&tmpflags,&height,&keyvalue[keylen],key,keylen)) >= 0 ) + { + if ( (tmpflags & KOMODO_KVPROTECTED) != 0 ) + { + if ( memcmp(&refpubkey,&pubkey,sizeof(refpubkey)) != 0 ) + { + ret.push_back(Pair("error",(char *)"cant modify write once key without passphrase")); + return ret; + } + } + if ( keylen+refvaluesize <= sizeof(keyvalue) ) + { + sig = komodo_kvsig(keyvalue,keylen+refvaluesize,privkey); + if ( komodo_kvsigverify(keyvalue,keylen+refvaluesize,refpubkey,sig) < 0 ) + { + ret.push_back(Pair("error",(char *)"error verifying sig, passphrase is probably wrong")); + printf("VERIFY ERROR\n"); + return ret; + } // else printf("verified immediately\n"); + } + } + //for (i=0; i<32; i++) + // printf("%02x",((uint8_t *)&sig)[i]); + //printf(" sig for keylen.%d + valuesize.%d\n",keylen,refvaluesize); + ret.push_back(Pair("coin",(char *)(ASSETCHAINS_SYMBOL[0] == 0 ? "KMD" : ASSETCHAINS_SYMBOL))); + height = chainActive.Tip()->nHeight; + if ( memcmp(&zeroes,&refpubkey,sizeof(refpubkey)) != 0 ) + ret.push_back(Pair("owner",refpubkey.GetHex())); + ret.push_back(Pair("height", (int64_t)height)); + duration = komodo_kvduration(flags); //((flags >> 2) + 1) * KOMODO_KVDURATION; + ret.push_back(Pair("expiration", (int64_t)(height+duration))); + ret.push_back(Pair("flags",(int64_t)flags)); + ret.push_back(Pair("key",params[0].get_str())); + ret.push_back(Pair("keylen",(int64_t)keylen)); + if ( n >= 2 && params[1].get_str().c_str() != 0 ) + { + ret.push_back(Pair("value",params[1].get_str())); + ret.push_back(Pair("valuesize",valuesize)); + } + iguana_rwnum(1,&keyvalue[0],sizeof(keylen),&keylen); + iguana_rwnum(1,&keyvalue[2],sizeof(valuesize),&valuesize); + iguana_rwnum(1,&keyvalue[4],sizeof(height),&height); + iguana_rwnum(1,&keyvalue[8],sizeof(flags),&flags); + memcpy(&keyvalue[12],key,keylen); + if ( value != 0 ) + memcpy(&keyvalue[12 + keylen],value,valuesize); + coresize = (int32_t)(sizeof(flags)+sizeof(height)+sizeof(uint16_t)*2+keylen+valuesize); + if ( haveprivkey != 0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + i] = ((uint8_t *)&pubkey)[i]; + coresize += 32; + if ( refvaluesize >=0 ) + { + for (i=0; i<32; i++) + keyvalue[12 + keylen + valuesize + 32 + i] = ((uint8_t *)&sig)[i]; + coresize += 32; + } + } + if ( (opretlen= komodo_opreturnscript(opretbuf,'K',keyvalue,coresize)) == 40 ) + opretlen++; + //for (i=0; ivout[out.i].nValue; - if ( out.tx->nLockTime != 0 ) + if ( out.tx->nLockTime != 0 && out.fSpendable != 0 ) { BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock()); CBlockIndex *tipindex,*pindex = it->second;