2016-01-19 05:24:34 +03:00
|
|
|
package elkrem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2016-01-19 07:15:51 +03:00
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-01-19 05:24:34 +03:00
|
|
|
)
|
|
|
|
|
2016-01-19 07:15:51 +03:00
|
|
|
/* Serialization and Deserialization methods for the Elkrem structs.
|
|
|
|
Senders turn into 41 byte long slices. Receivers are variable length,
|
2016-06-30 02:54:24 +03:00
|
|
|
with 41 bytes for each stored hash, up to a maximum of 48. Receivers are
|
|
|
|
prepended with the total number of hashes, so the total max size is 1969 bytes.
|
2016-01-19 07:15:51 +03:00
|
|
|
*/
|
2016-01-19 05:24:34 +03:00
|
|
|
|
2016-01-19 07:15:51 +03:00
|
|
|
// ToBytes turns the Elkrem Receiver into a bunch of bytes in a slice.
|
2016-06-30 02:54:24 +03:00
|
|
|
// first the number of nodes (1 byte), then a series of 41 byte long
|
|
|
|
// serialized nodes, which are 1 byte height, 8 byte index, 32 byte hash.
|
2016-01-19 05:24:34 +03:00
|
|
|
func (e *ElkremReceiver) ToBytes() ([]byte, error) {
|
2016-01-19 07:15:51 +03:00
|
|
|
numOfNodes := uint8(len(e.s))
|
2016-06-30 02:54:24 +03:00
|
|
|
// 0 element receiver also OK. Just an empty slice.
|
2016-01-19 07:15:51 +03:00
|
|
|
if numOfNodes == 0 {
|
2016-06-30 02:54:24 +03:00
|
|
|
return nil, nil
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
if numOfNodes > maxHeight+1 {
|
2016-01-19 07:15:51 +03:00
|
|
|
return nil, fmt.Errorf("Broken ElkremReceiver has %d nodes, max 64",
|
|
|
|
len(e.s))
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer // create buffer
|
2016-06-30 02:54:24 +03:00
|
|
|
|
2016-01-19 07:15:51 +03:00
|
|
|
// write number of nodes (1 byte)
|
2016-06-30 02:54:24 +03:00
|
|
|
err := binary.Write(&buf, binary.BigEndian, numOfNodes)
|
2016-01-19 07:15:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, node := range e.s {
|
|
|
|
// write 1 byte height
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, node.h)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 8 byte index
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, node.i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
if node.sha == nil {
|
|
|
|
return nil, fmt.Errorf("node %d has nil hash", node.i)
|
|
|
|
}
|
2016-01-19 07:15:51 +03:00
|
|
|
// write 32 byte sha hash
|
2017-01-06 00:56:27 +03:00
|
|
|
n, err := buf.Write(node.sha[:])
|
2016-01-19 07:15:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != 32 { // make sure that was 32 bytes
|
|
|
|
return nil, fmt.Errorf("%d byte hash, expect 32", n)
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
if buf.Len() != (int(numOfNodes)*41)+1 {
|
2016-01-19 07:15:51 +03:00
|
|
|
return nil, fmt.Errorf("Somehow made wrong size buf, got %d expect %d",
|
2016-06-30 02:54:24 +03:00
|
|
|
buf.Len(), (numOfNodes*41)+1)
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
2016-01-19 05:24:34 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 02:54:24 +03:00
|
|
|
func ElkremReceiverFromBytes(b []byte) (*ElkremReceiver, error) {
|
2016-01-19 07:15:51 +03:00
|
|
|
var e ElkremReceiver
|
2016-06-30 02:54:24 +03:00
|
|
|
if len(b) == 0 { // empty receiver, which is OK
|
|
|
|
return &e, nil
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
buf := bytes.NewBuffer(b)
|
2016-01-19 07:15:51 +03:00
|
|
|
// read 1 byte number of nodes stored in receiver
|
2016-06-30 02:54:24 +03:00
|
|
|
numOfNodes, err := buf.ReadByte()
|
2016-01-19 07:15:51 +03:00
|
|
|
if err != nil {
|
2016-06-30 02:54:24 +03:00
|
|
|
return nil, err
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
if numOfNodes < 1 || numOfNodes > maxHeight+1 {
|
|
|
|
return nil, fmt.Errorf("Read invalid number of nodes: %d", numOfNodes)
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
2016-01-19 08:26:28 +03:00
|
|
|
if buf.Len() != (int(numOfNodes) * 41) {
|
2016-06-30 02:54:24 +03:00
|
|
|
return nil, fmt.Errorf("Remaining buf wrong size, expect %d got %d",
|
2016-01-19 08:26:28 +03:00
|
|
|
(numOfNodes * 41), buf.Len())
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 02:54:24 +03:00
|
|
|
e.s = make([]ElkremNode, numOfNodes)
|
|
|
|
|
|
|
|
for j, _ := range e.s {
|
2017-01-06 00:56:27 +03:00
|
|
|
e.s[j].sha = new(chainhash.Hash)
|
2016-01-19 07:15:51 +03:00
|
|
|
// read 1 byte height
|
2016-06-30 02:54:24 +03:00
|
|
|
err := binary.Read(buf, binary.BigEndian, &e.s[j].h)
|
2016-01-19 07:15:51 +03:00
|
|
|
if err != nil {
|
2016-06-30 02:54:24 +03:00
|
|
|
return nil, err
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
|
|
|
// read 8 byte index
|
2016-06-30 02:54:24 +03:00
|
|
|
err = binary.Read(buf, binary.BigEndian, &e.s[j].i)
|
2016-01-19 07:15:51 +03:00
|
|
|
if err != nil {
|
2016-06-30 02:54:24 +03:00
|
|
|
return nil, err
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
|
|
|
// read 32 byte sha hash
|
2016-06-30 02:54:24 +03:00
|
|
|
err = e.s[j].sha.SetBytes(buf.Next(32))
|
2016-01-19 07:15:51 +03:00
|
|
|
if err != nil {
|
2016-06-30 02:54:24 +03:00
|
|
|
return nil, err
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
|
|
|
// sanity check. Note that this doesn't check that index and height
|
|
|
|
// match. Could add that but it's slow.
|
2016-06-30 02:54:24 +03:00
|
|
|
if e.s[j].h > maxHeight { // check for super high nodes
|
|
|
|
return nil, fmt.Errorf("Read invalid node height %d", e.s[j].h)
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
if e.s[j].i > maxIndex { // check for index higher than height allows
|
|
|
|
return nil, fmt.Errorf("Node claims index %d; %d max at height %d",
|
|
|
|
e.s[j].i, maxIndex, e.s[j].h)
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
|
|
|
|
if j > 0 { // check that node heights are descending
|
|
|
|
if e.s[j-1].h < e.s[j].h {
|
|
|
|
return nil, fmt.Errorf("Node heights out of order")
|
2016-01-19 07:15:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
return &e, nil
|
2016-01-19 05:24:34 +03:00
|
|
|
}
|
2016-06-30 02:54:24 +03:00
|
|
|
|
2016-06-30 21:34:05 +03:00
|
|
|
// ToBytes returns the root of the elkrem sender tree as a byte slice. This
|
|
|
|
// function is in place to allow one to export the root of the tree. However,
|
|
|
|
// node that if one uses a deterministic procedure to generate the root, then
|
|
|
|
// serialization isn't necessary as it can simply be re-derived on the fly.
|
|
|
|
func (e *ElkremSender) ToBytes() []byte {
|
|
|
|
return e.root[:]
|
|
|
|
}
|