/
marshal.go
166 lines (152 loc) · 4.95 KB
/
marshal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2020 Consensys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package ecdsa
import (
"crypto/subtle"
"errors"
"github.com/consensys/gnark-crypto/ecc/secp256k1/fr"
"io"
"math/big"
"github.com/consensys/gnark-crypto/ecc/secp256k1"
)
var errWrongSize = errors.New("wrong size buffer")
var errRBiggerThanRMod = errors.New("r >= r_mod")
var errSBiggerThanRMod = errors.New("s >= r_mod")
var errZero = errors.New("zero value")
// Bytes returns the binary representation of the public key
// follows https://tools.ietf.org/html/rfc8032#section-3.1
// and returns a compressed representation of the point (x,y)
//
// x, y are the coordinates of the point
// on the curve as big endian integers.
// compressed representation store x with a parity bit to recompute y
func (pk *PublicKey) Bytes() []byte {
var res [sizePublicKey]byte
pkBin := pk.A.RawBytes()
subtle.ConstantTimeCopy(1, res[:sizePublicKey], pkBin[:])
return res[:]
}
// SetBytes sets p from binary representation in buf.
// buf represents a public key as x||y where x, y are
// interpreted as big endian binary numbers corresponding
// to the coordinates of a point on the curve.
// It returns the number of bytes read from the buffer.
func (pk *PublicKey) SetBytes(buf []byte) (int, error) {
n := 0
if len(buf) < sizePublicKey {
return n, io.ErrShortBuffer
}
if _, err := pk.A.SetBytes(buf[:sizePublicKey]); err != nil {
return 0, err
}
n += sizeFp
return n, nil
}
// RecoverFrom recovers the public key from the message msg, recovery
// information v and decompose signature {r,s}. If recovery succeeded, the
// methods sets the current public key to the recovered value. Otherwise returns
// error and leaves current public key unchanged.
func (pk *PublicKey) RecoverFrom(msg []byte, v uint, r, s *big.Int) error {
if s.Cmp(fr.Modulus()) >= 0 {
return errors.New("s is larger than modulus")
}
if s.Cmp(big.NewInt(0)) <= 0 {
return errors.New("s is negative")
}
P, err := RecoverP(v, r)
if err != nil {
return err
}
z := HashToInt(msg)
rinv := new(big.Int).ModInverse(r, fr.Modulus())
u1 := new(big.Int).Mul(z, rinv)
u1.Neg(u1)
u1.Mod(u1, fr.Modulus())
u2 := new(big.Int).Mul(s, rinv)
u2.Mod(u2, fr.Modulus())
var Q secp256k1.G1Jac
Q.JointScalarMultiplicationBase(P, u1, u2)
pk.A.FromJacobian(&Q)
return nil
}
// Bytes returns the binary representation of pk,
// as byte array publicKey||scalar
// where publicKey is as publicKey.Bytes(), and
// scalar is in big endian, of size sizeFr.
func (privKey *PrivateKey) Bytes() []byte {
var res [sizePrivateKey]byte
pubkBin := privKey.PublicKey.A.RawBytes()
subtle.ConstantTimeCopy(1, res[:sizePublicKey], pubkBin[:])
subtle.ConstantTimeCopy(1, res[sizePublicKey:sizePrivateKey], privKey.scalar[:])
return res[:]
}
// SetBytes sets pk from buf, where buf is interpreted
// as publicKey||scalar
// where publicKey is as publicKey.Bytes(), and
// scalar is in big endian, of size sizeFr.
// It returns the number byte read.
func (privKey *PrivateKey) SetBytes(buf []byte) (int, error) {
n := 0
if len(buf) < sizePrivateKey {
return n, io.ErrShortBuffer
}
if _, err := privKey.PublicKey.A.SetBytes(buf[:sizePublicKey]); err != nil {
return 0, err
}
n += sizePublicKey
subtle.ConstantTimeCopy(1, privKey.scalar[:], buf[sizePublicKey:sizePrivateKey])
n += sizeFr
return n, nil
}
// Bytes returns the binary representation of sig
// as a byte array of size 2*sizeFr r||s
func (sig *Signature) Bytes() []byte {
var res [sizeSignature]byte
subtle.ConstantTimeCopy(1, res[:sizeFr], sig.R[:])
subtle.ConstantTimeCopy(1, res[sizeFr:], sig.S[:])
return res[:]
}
// SetBytes sets sig from a buffer in binary.
// buf is read interpreted as r||s
// It returns the number of bytes read from buf.
func (sig *Signature) SetBytes(buf []byte) (int, error) {
n := 0
if len(buf) != sizeSignature {
return n, errWrongSize
}
// S, R < R_mod (to avoid malleability)
frMod := fr.Modulus()
zero := big.NewInt(0)
bufBigInt := new(big.Int)
bufBigInt.SetBytes(buf[:sizeFr])
if bufBigInt.Cmp(zero) == 0 {
return 0, errZero
}
if bufBigInt.Cmp(frMod) != -1 {
return 0, errRBiggerThanRMod
}
bufBigInt.SetBytes(buf[sizeFr : 2*sizeFr])
if bufBigInt.Cmp(zero) == 0 {
return 0, errZero
}
if bufBigInt.Cmp(frMod) != -1 {
return 0, errSBiggerThanRMod
}
subtle.ConstantTimeCopy(1, sig.R[:], buf[:sizeFr])
n += sizeFr
subtle.ConstantTimeCopy(1, sig.S[:], buf[sizeFr:2*sizeFr])
n += sizeFr
return n, nil
}