-
Notifications
You must be signed in to change notification settings - Fork 151
/
marshal.go
108 lines (99 loc) · 3.36 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
// 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"
"io"
)
// 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.Bytes()
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
}
// 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.Bytes()
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, io.ErrShortBuffer
}
subtle.ConstantTimeCopy(1, sig.R[:], buf[:sizeFr])
n += sizeFr
subtle.ConstantTimeCopy(1, sig.S[:], buf[sizeFr:2*sizeFr])
n += sizeFr
return n, nil
}