Skip to content

Commit 4faa85a

Browse files
authored
Merge pull request #11 from ltcsuite/losh11/0.9.0-dev
ltcd v0.20.1
2 parents 92166e4 + 571a66d commit 4faa85a

33 files changed

+1314
-386
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cache:
77
- $GOPATH/github.com/golang
88
- $GOPATH/gopkg.in/alecthomas
99
go:
10-
- "1.11.x"
10+
- "1.13.x"
1111
sudo: false
1212
install:
1313
- export PATH=$PATH:$PWD/linux-amd64/

README.md

+20-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
ltcd
2-
====
1+
# ltcd
32

43
[![Build Status](https://travis-ci.org/ltcsuite/ltcd.png?branch=master)](https://travis-ci.org/ltcsuite/ltcd)
54

65
ltcd is an alternative full node bitcoin implementation written in Go (golang).
76

8-
This project is currently under active development and is in a Beta state. It
7+
This project is currently under active development and is in a Beta state. It
98
is extremely stable and has been in production use since October 2013.
109

1110
It properly downloads, validates, and serves the block chain using the exact
12-
rules (including consensus bugs) for block acceptance as Bitcoin Core. We have
13-
taken great care to avoid ltcd causing a fork to the block chain. It includes a
11+
rules (including consensus bugs) for block acceptance as Bitcoin Core. We have
12+
taken great care to avoid ltcd causing a fork to the block chain. It includes a
1413
full block validation testing framework which contains all of the 'official'
1514
block acceptance tests (and some additional ones) that is run on every pull
16-
request to help ensure it properly follows consensus. Also, it passes all of
15+
request to help ensure it properly follows consensus. Also, it passes all of
1716
the JSON test data in the Bitcoin Core code.
1817

1918
It also properly relays newly mined blocks, maintains a transaction pool, and
20-
relays individual transactions that have not yet made it into a block. It
19+
relays individual transactions that have not yet made it into a block. It
2120
ensures all individual transactions admitted to the pool follow the rules
2221
required by the block chain and also includes more strict checks which filter
2322
transactions based on miner requirements ("standard" transactions).
2423

25-
One key difference between ltcd and Bitcoin Core is that ltcd does *NOT* include
26-
wallet functionality and this was a very intentional design decision. See the
24+
One key difference between ltcd and Bitcoin Core is that ltcd does _NOT_ include
25+
wallet functionality and this was a very intentional design decision. See the
2726
blog entry [here](https://blog.conformal.com/ltcd-not-your-moms-bitcoin-daemon)
28-
for more details. This means you can't actually make or receive payments
29-
directly with ltcd. That functionality is provided by the
27+
for more details. This means you can't actually make or receive payments
28+
directly with ltcd. That functionality is provided by the
3029
[btcwallet](https://github.com/btcsuite/btcwallet) and
3130
[Paymetheus](https://github.com/btcsuite/Paymetheus) (Windows-only) projects
3231
which are both under active development.
3332

3433
## Requirements
3534

36-
[Go](http://golang.org) 1.11 or newer.
35+
[Go](http://golang.org) 1.12 or newer.
3736

3837
## Installation
3938

@@ -53,9 +52,9 @@ $ go version
5352
$ go env GOROOT GOPATH
5453
```
5554

56-
NOTE: The `GOROOT` and `GOPATH` above must not be the same path. It is
55+
NOTE: The `GOROOT` and `GOPATH` above must not be the same path. It is
5756
recommended that `GOPATH` is set to a directory in your home directory such as
58-
`~/goprojects` to avoid write permission issues. It is also recommended to add
57+
`~/goprojects` to avoid write permission issues. It is also recommended to add
5958
`$GOPATH/bin` to your `PATH` at this point.
6059

6160
- Run the following commands to obtain ltcd, all dependencies, and install it:
@@ -65,7 +64,7 @@ $ cd $GOPATH/src/github.com/ltcsuite/ltcd
6564
$ GO111MODULE=on go install -v . ./cmd/...
6665
```
6766

68-
- ltcd (and utilities) will now be installed in ```$GOPATH/bin```. If you did
67+
- ltcd (and utilities) will now be installed in `$GOPATH/bin`. If you did
6968
not already add the bin directory to your system path during Go installation,
7069
we recommend you do so now.
7170

@@ -114,27 +113,14 @@ is used for this project.
114113

115114
## Documentation
116115

117-
The documentation is a work-in-progress. It is located in the [docs](https://github.com/ltcsuite/ltcd/tree/master/docs) folder.
116+
The documentation is a work-in-progress. It is located in the [docs](https://github.com/ltcsuite/ltcd/tree/master/docs) folder.
118117

119-
## GPG Verification Key
118+
## Release Verification
120119

121-
All official release tags are signed by Conformal so users can ensure the code
122-
has not been tampered with and is coming from the ltcsuite developers. To
123-
verify the signature perform the following:
124-
125-
- Download the Conformal public key:
126-
https://raw.githubusercontent.com/ltcsuite/ltcd/master/release/GIT-GPG-KEY-conformal.txt
127-
128-
- Import the public key into your GPG keyring:
129-
```bash
130-
gpg --import GIT-GPG-KEY-conformal.txt
131-
```
132-
133-
- Verify the release tag with the following command where `TAG_NAME` is a
134-
placeholder for the specific tag:
135-
```bash
136-
git tag -v TAG_NAME
137-
```
120+
Please see our [documentation on the current build/verification
121+
process](https://github.com/ltcsuite/ltcd/tree/master/release) for all our
122+
releases for information on how to verify the integrity of published releases
123+
using our reproducible build system.
138124

139125
## License
140126

btcec/bench_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package btcec
66

7-
import "testing"
7+
import (
8+
"encoding/hex"
9+
"testing"
10+
)
811

912
// BenchmarkAddJacobian benchmarks the secp256k1 curve addJacobian function with
1013
// Z values of 1 so that the associated optimizations are used.
@@ -121,3 +124,22 @@ func BenchmarkFieldNormalize(b *testing.B) {
121124
f.Normalize()
122125
}
123126
}
127+
128+
// BenchmarkParseCompressedPubKey benchmarks how long it takes to decompress and
129+
// validate a compressed public key from a byte array.
130+
func BenchmarkParseCompressedPubKey(b *testing.B) {
131+
rawPk, _ := hex.DecodeString("0234f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6")
132+
133+
var (
134+
pk *PublicKey
135+
err error
136+
)
137+
138+
b.ReportAllocs()
139+
b.ResetTimer()
140+
for i := 0; i < b.N; i++ {
141+
pk, err = ParsePubKey(rawPk, S256())
142+
}
143+
_ = pk
144+
_ = err
145+
}

btcec/btcec.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ var (
3636
// interface from crypto/elliptic.
3737
type KoblitzCurve struct {
3838
*elliptic.CurveParams
39-
q *big.Int
39+
40+
// q is the value (P+1)/4 used to compute the square root of field
41+
// elements.
42+
q *big.Int
43+
4044
H int // cofactor of the curve.
4145
halfOrder *big.Int // half the order N
4246

47+
// fieldB is the constant B of the curve as a fieldVal.
48+
fieldB *fieldVal
49+
4350
// byteSize is simply the bit size / 8 and is provided for convenience
4451
// since it is calculated repeatedly.
4552
byteSize int
@@ -879,12 +886,22 @@ func (curve *KoblitzCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
879886
return curve.fieldJacobianToBigAffine(qx, qy, qz)
880887
}
881888

882-
// QPlus1Div4 returns the Q+1/4 constant for the curve for use in calculating
883-
// square roots via exponention.
889+
// QPlus1Div4 returns the (P+1)/4 constant for the curve for use in calculating
890+
// square roots via exponentiation.
891+
//
892+
// DEPRECATED: The actual value returned is (P+1)/4, where as the original
893+
// method name implies that this value is (((P+1)/4)+1)/4. This method is kept
894+
// to maintain backwards compatibility of the API. Use Q() instead.
884895
func (curve *KoblitzCurve) QPlus1Div4() *big.Int {
885896
return curve.q
886897
}
887898

899+
// Q returns the (P+1)/4 constant for the curve for use in calculating square
900+
// roots via exponentiation.
901+
func (curve *KoblitzCurve) Q() *big.Int {
902+
return curve.q
903+
}
904+
888905
var initonce sync.Once
889906
var secp256k1 KoblitzCurve
890907

@@ -917,6 +934,7 @@ func initS256() {
917934
big.NewInt(1)), big.NewInt(4))
918935
secp256k1.H = 1
919936
secp256k1.halfOrder = new(big.Int).Rsh(secp256k1.N, 1)
937+
secp256k1.fieldB = new(fieldVal).SetByteSlice(secp256k1.B.Bytes())
920938

921939
// Provided for convenience since this gets computed repeatedly.
922940
secp256k1.byteSize = secp256k1.BitSize / 8

btcec/field.go

+129
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ const (
102102
fieldPrimeWordOne = 0x3ffffbf
103103
)
104104

105+
var (
106+
// fieldQBytes is the value Q = (P+1)/4 for the secp256k1 prime P. This
107+
// value is used to efficiently compute the square root of values in the
108+
// field via exponentiation. The value of Q in hex is:
109+
//
110+
// Q = 3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c
111+
fieldQBytes = []byte{
112+
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
113+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
114+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
115+
0xff, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0x0c,
116+
}
117+
)
118+
105119
// fieldVal implements optimized fixed-precision arithmetic over the
106120
// secp256k1 finite field. This means all arithmetic is performed modulo
107121
// 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f. It
@@ -1221,3 +1235,118 @@ func (f *fieldVal) Inverse() *fieldVal {
12211235
f.Square().Square().Square().Square().Square() // f = a^(2^256 - 4294968320)
12221236
return f.Mul(&a45) // f = a^(2^256 - 4294968275) = a^(p-2)
12231237
}
1238+
1239+
// SqrtVal computes the square root of x modulo the curve's prime, and stores
1240+
// the result in f. The square root is computed via exponentiation of x by the
1241+
// value Q = (P+1)/4 using the curve's precomputed big-endian representation of
1242+
// the Q. This method uses a modified version of square-and-multiply
1243+
// exponentiation over secp256k1 fieldVals to operate on bytes instead of bits,
1244+
// which offers better performance over both big.Int exponentiation and bit-wise
1245+
// square-and-multiply.
1246+
//
1247+
// NOTE: This method only works when P is intended to be the secp256k1 prime and
1248+
// is not constant time. The returned value is of magnitude 1, but is
1249+
// denormalized.
1250+
func (f *fieldVal) SqrtVal(x *fieldVal) *fieldVal {
1251+
// The following computation iteratively computes x^((P+1)/4) = x^Q
1252+
// using the recursive, piece-wise definition:
1253+
//
1254+
// x^n = (x^2)^(n/2) mod P if n is even
1255+
// x^n = x(x^2)^(n-1/2) mod P if n is odd
1256+
//
1257+
// Given n in its big-endian representation b_k, ..., b_0, x^n can be
1258+
// computed by defining the sequence r_k+1, ..., r_0, where:
1259+
//
1260+
// r_k+1 = 1
1261+
// r_i = (r_i+1)^2 * x^b_i for i = k, ..., 0
1262+
//
1263+
// The final value r_0 = x^n.
1264+
//
1265+
// See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more
1266+
// details.
1267+
//
1268+
// This can be further optimized, by observing that the value of Q in
1269+
// secp256k1 has the value:
1270+
//
1271+
// Q = 3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c
1272+
//
1273+
// We can unroll the typical bit-wise interpretation of the
1274+
// exponentiation algorithm above to instead operate on bytes.
1275+
// This reduces the number of comparisons by an order of magnitude,
1276+
// reducing the overhead of failed branch predictions and additional
1277+
// comparisons in this method.
1278+
//
1279+
// Since there there are only 4 unique bytes of Q, this keeps the jump
1280+
// table small without the need to handle all possible 8-bit values.
1281+
// Further, we observe that 29 of the 32 bytes are 0xff; making the
1282+
// first case handle 0xff therefore optimizes the hot path.
1283+
f.SetInt(1)
1284+
for _, b := range fieldQBytes {
1285+
switch b {
1286+
1287+
// Most common case, where all 8 bits are set.
1288+
case 0xff:
1289+
f.Square().Mul(x)
1290+
f.Square().Mul(x)
1291+
f.Square().Mul(x)
1292+
f.Square().Mul(x)
1293+
f.Square().Mul(x)
1294+
f.Square().Mul(x)
1295+
f.Square().Mul(x)
1296+
f.Square().Mul(x)
1297+
1298+
// First byte of Q (0x3f), where all but the top two bits are
1299+
// set. Note that this case only applies six operations, since
1300+
// the highest bit of Q resides in bit six of the first byte. We
1301+
// ignore the first two bits, since squaring for these bits will
1302+
// result in an invalid result. We forgo squaring f before the
1303+
// first multiply, since 1^2 = 1.
1304+
case 0x3f:
1305+
f.Mul(x)
1306+
f.Square().Mul(x)
1307+
f.Square().Mul(x)
1308+
f.Square().Mul(x)
1309+
f.Square().Mul(x)
1310+
f.Square().Mul(x)
1311+
1312+
// Byte 28 of Q (0xbf), where only bit 7 is unset.
1313+
case 0xbf:
1314+
f.Square().Mul(x)
1315+
f.Square()
1316+
f.Square().Mul(x)
1317+
f.Square().Mul(x)
1318+
f.Square().Mul(x)
1319+
f.Square().Mul(x)
1320+
f.Square().Mul(x)
1321+
f.Square().Mul(x)
1322+
1323+
// Byte 31 of Q (0x0c), where only bits 3 and 4 are set.
1324+
default:
1325+
f.Square()
1326+
f.Square()
1327+
f.Square()
1328+
f.Square()
1329+
f.Square().Mul(x)
1330+
f.Square().Mul(x)
1331+
f.Square()
1332+
f.Square()
1333+
}
1334+
}
1335+
1336+
return f
1337+
}
1338+
1339+
// Sqrt computes the square root of f modulo the curve's prime, and stores the
1340+
// result in f. The square root is computed via exponentiation of x by the value
1341+
// Q = (P+1)/4 using the curve's precomputed big-endian representation of the Q.
1342+
// This method uses a modified version of square-and-multiply exponentiation
1343+
// over secp256k1 fieldVals to operate on bytes instead of bits, which offers
1344+
// better performance over both big.Int exponentiation and bit-wise
1345+
// square-and-multiply.
1346+
//
1347+
// NOTE: This method only works when P is intended to be the secp256k1 prime and
1348+
// is not constant time. The returned value is of magnitude 1, but is
1349+
// denormalized.
1350+
func (f *fieldVal) Sqrt() *fieldVal {
1351+
return f.SqrtVal(f)
1352+
}

0 commit comments

Comments
 (0)