This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scalar.go
216 lines (170 loc) · 5 KB
/
scalar.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2020-2023 Daniel Bourdrez. All Rights Reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html
// Package crypto exposes a prime-order elliptic curve groups with additional hash-to-curve operations.
package crypto
import (
"fmt"
"strings"
"github.com/bytemare/crypto/internal"
)
// Scalar represents a scalar in the prime-order group.
type Scalar struct {
_ disallowEqual
internal.Scalar
}
func newScalar(s internal.Scalar) *Scalar {
return &Scalar{Scalar: s}
}
// Group returns the group's Identifier.
func (s *Scalar) Group() Group {
return Group(s.Scalar.Group())
}
// Zero sets the scalar to 0, and returns it.
func (s *Scalar) Zero() *Scalar {
s.Scalar.Zero()
return s
}
// One sets the scalar to 1, and returns it.
func (s *Scalar) One() *Scalar {
s.Scalar.One()
return s
}
// MinusOne sets the scalar to order-1, and returns it.
func (s *Scalar) MinusOne() *Scalar {
s.Scalar.MinusOne()
return s
}
// Random sets the current scalar to a new random scalar and returns it.
// The random source is crypto/rand, and this functions is guaranteed to return a non-zero scalar.
func (s *Scalar) Random() *Scalar {
s.Scalar.Random()
return s
}
// Add sets the receiver to the sum of the input and the receiver, and returns the receiver.
func (s *Scalar) Add(scalar *Scalar) *Scalar {
if scalar == nil {
return s
}
s.Scalar.Add(scalar.Scalar)
return s
}
// Subtract subtracts the input from the receiver, and returns the receiver.
func (s *Scalar) Subtract(scalar *Scalar) *Scalar {
if scalar == nil {
return s
}
s.Scalar.Subtract(scalar.Scalar)
return s
}
// Multiply multiplies the receiver with the input, and returns the receiver.
func (s *Scalar) Multiply(scalar *Scalar) *Scalar {
if scalar == nil {
return s.Zero()
}
s.Scalar.Multiply(scalar.Scalar)
return s
}
// Pow sets s to s**scalar modulo the group order, and returns s. If scalar is nil, it returns 1.
func (s *Scalar) Pow(scalar *Scalar) *Scalar {
if scalar == nil {
return s.One()
}
s.Scalar.Pow(scalar.Scalar)
return s
}
// Invert sets the receiver to the scalar's modular inverse ( 1 / scalar ), and returns it.
func (s *Scalar) Invert() *Scalar {
s.Scalar.Invert()
return s
}
// Equal returns true if the elements are equivalent, and false otherwise.
func (s *Scalar) Equal(scalar *Scalar) bool {
if scalar == nil {
return false
}
return s.Scalar.Equal(scalar.Scalar) == 1
}
// LessOrEqual returns 1 if s <= scalar, and 0 otherwise.
func (s *Scalar) LessOrEqual(scalar *Scalar) int {
if scalar == nil {
return 0
}
return s.Scalar.LessOrEqual(scalar.Scalar)
}
// IsZero returns whether the scalar is 0.
func (s *Scalar) IsZero() bool {
return s.Scalar.IsZero()
}
// Set sets the receiver to the value of the argument scalar, and returns the receiver.
func (s *Scalar) Set(scalar *Scalar) *Scalar {
if scalar == nil {
return s.Zero()
}
s.Scalar.Set(scalar.Scalar)
return s
}
// SetUInt64 sets s to i modulo the field order, and returns an error if one occurs.
func (s *Scalar) SetUInt64(i uint64) *Scalar {
s.Scalar.SetUInt64(i)
return s
}
// UInt64 returns the uint64 representation of the scalar,
// or an error if its value is higher than the authorized limit for uint64.
func (s *Scalar) UInt64() (uint64, error) {
i, err := s.Scalar.UInt64()
if err != nil {
return 0, fmt.Errorf("%w", err)
}
return i, nil
}
// Copy returns a copy of the receiver.
func (s *Scalar) Copy() *Scalar {
return &Scalar{Scalar: s.Scalar.Copy()}
}
// Encode returns the compressed byte encoding of the scalar.
func (s *Scalar) Encode() []byte {
return s.Scalar.Encode()
}
// Decode sets the receiver to a decoding of the input data, and returns an error on failure.
func (s *Scalar) Decode(data []byte) error {
if err := s.Scalar.Decode(data); err != nil {
return fmt.Errorf("scalar Decode: %w", err)
}
return nil
}
// Hex returns the fixed-sized hexadecimal encoding of s.
func (s *Scalar) Hex() string {
return s.Scalar.Hex()
}
// DecodeHex sets s to the decoding of the hex encoded scalar.
func (s *Scalar) DecodeHex(h string) error {
if err := s.Scalar.DecodeHex(h); err != nil {
return fmt.Errorf("scalar DecodeHex: %w", err)
}
return nil
}
// MarshalJSON marshals the scalar into valid JSON.
func (s *Scalar) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", s.Hex())), nil
}
// UnmarshalJSON unmarshals the input into the scalar.
func (s *Scalar) UnmarshalJSON(data []byte) error {
j := strings.ReplaceAll(string(data), "\"", "")
return s.DecodeHex(j)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (s *Scalar) MarshalBinary() ([]byte, error) {
return s.Scalar.Encode(), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (s *Scalar) UnmarshalBinary(data []byte) error {
if err := s.Scalar.Decode(data); err != nil {
return fmt.Errorf("scalar UnmarshalBinary: %w", err)
}
return nil
}