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
/
element.go
178 lines (139 loc) · 4.34 KB
/
element.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
// 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"
)
// Element represents an element on the curve of the prime-order group.
type Element struct {
_ disallowEqual
internal.Element
}
func newPoint(p internal.Element) *Element {
return &Element{Element: p}
}
// Group returns the group's Identifier.
func (e *Element) Group() Group {
return Group(e.Element.Group())
}
// Base sets the element to the group's base point a.k.a. canonical generator.
func (e *Element) Base() *Element {
return &Element{Element: e.Element.Base()}
}
// Identity sets the element to the point at infinity of the Group's underlying curve.
func (e *Element) Identity() *Element {
return &Element{Element: e.Element.Identity()}
}
// Add sets the receiver to the sum of the input and the receiver, and returns the receiver.
func (e *Element) Add(element *Element) *Element {
if element == nil {
return e
}
e.Element.Add(element.Element)
return e
}
// Double sets the receiver to its double, and returns it.
func (e *Element) Double() *Element {
e.Element.Double()
return e
}
// Negate sets the receiver to its negation, and returns it.
func (e *Element) Negate() *Element {
e.Element.Negate()
return e
}
// Subtract subtracts the input from the receiver, and returns the receiver.
func (e *Element) Subtract(element *Element) *Element {
if element == nil {
return e
}
e.Element.Subtract(element.Element)
return e
}
// Multiply sets the receiver to the scalar multiplication of the receiver with the given Scalar, and returns it.
func (e *Element) Multiply(scalar *Scalar) *Element {
if scalar == nil {
e.Element.Identity()
return e
}
e.Element.Multiply(scalar.Scalar)
return e
}
// Equal returns true if the elements are equivalent, and false otherwise.
func (e *Element) Equal(element *Element) bool {
if element == nil {
return false
}
return e.Element.Equal(element.Element) == 1
}
// IsIdentity returns whether the Element is the point at infinity of the Group's underlying curve.
func (e *Element) IsIdentity() bool {
return e.Element.IsIdentity()
}
// Set sets the receiver to the argument, and returns the receiver.
func (e *Element) Set(element *Element) *Element {
if element == nil {
e.Element.Set(nil)
return e
}
e.Element.Set(element.Element)
return e
}
// Copy returns a copy of the receiver.
func (e *Element) Copy() *Element {
return &Element{Element: e.Element.Copy()}
}
// Encode returns the compressed byte encoding of the element.
func (e *Element) Encode() []byte {
return e.Element.Encode()
}
// XCoordinate returns the encoded x coordinate of the element.
func (e *Element) XCoordinate() []byte {
return e.Element.XCoordinate()
}
// Decode sets the receiver to a decoding of the input data, and returns an error on failure.
func (e *Element) Decode(data []byte) error {
if err := e.Element.Decode(data); err != nil {
return fmt.Errorf("element Decode: %w", err)
}
return nil
}
// Hex returns the fixed-sized hexadecimal encoding of e.
func (e *Element) Hex() string {
return e.Element.Hex()
}
// DecodeHex sets e to the decoding of the hex encoded element.
func (e *Element) DecodeHex(h string) error {
if err := e.Element.DecodeHex(h); err != nil {
return fmt.Errorf("element DecodeHex: %w", err)
}
return nil
}
// MarshalJSON marshals the element into valid JSON.
func (e *Element) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", e.Hex())), nil
}
// UnmarshalJSON unmarshals the input into the element.
func (e *Element) UnmarshalJSON(data []byte) error {
j := strings.ReplaceAll(string(data), "\"", "")
return e.DecodeHex(j)
}
// MarshalBinary returns the compressed byte encoding of the element.
func (e *Element) MarshalBinary() ([]byte, error) {
return e.Element.Encode(), nil
}
// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element) UnmarshalBinary(data []byte) error {
if err := e.Element.Decode(data); err != nil {
return fmt.Errorf("element UnmarshalBinary: %w", err)
}
return nil
}