...
1
2
3
4
5 package encoding
6
7 import (
8 "io"
9 "math/big"
10 "math/bits"
11 )
12
13
14
15
16 type MPI struct {
17 bytes []byte
18 bitLength uint16
19 }
20
21
22 func NewMPI(bytes []byte) *MPI {
23 for len(bytes) != 0 && bytes[0] == 0 {
24 bytes = bytes[1:]
25 }
26 if len(bytes) == 0 {
27 bitLength := uint16(0)
28 return &MPI{bytes, bitLength}
29 }
30 bitLength := 8*uint16(len(bytes)-1) + uint16(bits.Len8(bytes[0]))
31 return &MPI{bytes, bitLength}
32 }
33
34
35 func (m *MPI) Bytes() []byte {
36 return m.bytes
37 }
38
39
40 func (m *MPI) BitLength() uint16 {
41 return m.bitLength
42 }
43
44
45 func (m *MPI) EncodedBytes() []byte {
46 return append([]byte{byte(m.bitLength >> 8), byte(m.bitLength)}, m.bytes...)
47 }
48
49
50 func (m *MPI) EncodedLength() uint16 {
51 return uint16(2 + len(m.bytes))
52 }
53
54
55 func (m *MPI) ReadFrom(r io.Reader) (int64, error) {
56 var buf [2]byte
57 n, err := io.ReadFull(r, buf[0:])
58 if err != nil {
59 if err == io.EOF {
60 err = io.ErrUnexpectedEOF
61 }
62 return int64(n), err
63 }
64
65 m.bitLength = uint16(buf[0])<<8 | uint16(buf[1])
66 m.bytes = make([]byte, (int(m.bitLength)+7)/8)
67
68 nn, err := io.ReadFull(r, m.bytes)
69 if err == io.EOF {
70 err = io.ErrUnexpectedEOF
71 }
72
73
74
75
76
77
78
79
80
81
82
83 return int64(n) + int64(nn), err
84 }
85
86
87 func (m *MPI) SetBig(n *big.Int) *MPI {
88 m.bytes = n.Bytes()
89 m.bitLength = uint16(n.BitLen())
90 return m
91 }
92
View as plain text