2015-02-01 08:55:16 +01:00
|
|
|
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
2014-09-24 19:41:07 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2015-02-01 08:55:16 +01:00
|
|
|
|
|
|
|
// This file is ignored during the regular build due to the following build tag.
|
|
|
|
// This build tag is set during go generate.
|
2014-09-24 19:41:07 +02:00
|
|
|
// +build gensecp256k1
|
|
|
|
|
|
|
|
package btcec
|
|
|
|
|
2015-02-01 08:55:16 +01:00
|
|
|
import "encoding/binary"
|
|
|
|
|
|
|
|
// secp256k1BytePoints are dummy points used so the code which generates the
|
|
|
|
// real values can compile.
|
|
|
|
var secp256k1BytePoints = []byte{}
|
2014-09-24 19:41:07 +02:00
|
|
|
|
|
|
|
// getDoublingPoints returns all the possible G^(2^i) for i in
|
|
|
|
// 0..n-1 where n is the curve's bit size (256 in the case of secp256k1)
|
|
|
|
// the coordinates are recorded as Jacobian coordinates.
|
|
|
|
func (curve *KoblitzCurve) getDoublingPoints() [][3]fieldVal {
|
2015-01-22 17:06:00 +01:00
|
|
|
doublingPoints := make([][3]fieldVal, curve.BitSize)
|
2014-09-24 19:41:07 +02:00
|
|
|
|
|
|
|
// initialize px, py, pz to the Jacobian coordinates for the base point
|
|
|
|
px, py := curve.bigAffineToField(curve.Gx, curve.Gy)
|
|
|
|
pz := new(fieldVal).SetInt(1)
|
|
|
|
for i := 0; i < bitSize; i++ {
|
|
|
|
doublingPoints[i] = [3]fieldVal{*px, *py, *pz}
|
|
|
|
// P = 2*P
|
|
|
|
curve.doubleJacobian(px, py, pz, px, py, pz)
|
|
|
|
}
|
|
|
|
return doublingPoints
|
|
|
|
}
|
|
|
|
|
2015-02-01 08:55:16 +01:00
|
|
|
// SerializedBytePoints returns a serialized byte slice which contains all of
|
|
|
|
// the possible points per 8-bit window. This is used to when generating
|
|
|
|
// secp256k1.go.
|
|
|
|
func (curve *KoblitzCurve) SerializedBytePoints() []byte {
|
2014-09-24 19:41:07 +02:00
|
|
|
doublingPoints := curve.getDoublingPoints()
|
2015-02-01 08:55:16 +01:00
|
|
|
|
2014-09-24 19:41:07 +02:00
|
|
|
// Segregate the bits into byte-sized windows
|
2015-01-22 17:13:00 +01:00
|
|
|
serialized := make([]byte, curve.byteSize*256*3*10*4)
|
2015-02-01 08:55:16 +01:00
|
|
|
offset := 0
|
2015-01-22 17:13:00 +01:00
|
|
|
for byteNum := 0; byteNum < curve.byteSize; byteNum++ {
|
2015-02-01 08:55:16 +01:00
|
|
|
// Grab the 8 bits that make up this byte from doublingPoints.
|
2015-01-22 17:13:00 +01:00
|
|
|
startingBit := 8 * (curve.byteSize - byteNum - 1)
|
2014-09-24 19:41:07 +02:00
|
|
|
computingPoints := doublingPoints[startingBit : startingBit+8]
|
2015-02-01 08:55:16 +01:00
|
|
|
|
|
|
|
// Compute all points in this window and serialize them.
|
2014-09-24 19:41:07 +02:00
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
px, py, pz := new(fieldVal), new(fieldVal), new(fieldVal)
|
|
|
|
for j := 0; j < 8; j++ {
|
|
|
|
if i>>uint(j)&1 == 1 {
|
|
|
|
curve.addJacobian(px, py, pz, &computingPoints[j][0],
|
|
|
|
&computingPoints[j][1], &computingPoints[j][2], px, py, pz)
|
|
|
|
}
|
|
|
|
}
|
2015-02-01 08:55:16 +01:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
binary.LittleEndian.PutUint32(serialized[offset:], px.n[i])
|
|
|
|
offset += 4
|
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
binary.LittleEndian.PutUint32(serialized[offset:], py.n[i])
|
|
|
|
offset += 4
|
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
binary.LittleEndian.PutUint32(serialized[offset:], pz.n[i])
|
|
|
|
offset += 4
|
|
|
|
}
|
2014-09-24 19:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-01 08:55:16 +01:00
|
|
|
|
|
|
|
return serialized
|
2014-09-24 19:41:07 +02:00
|
|
|
}
|