-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
129 lines (101 loc) · 3.32 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
// Copyright (c) 2022 Cymony Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package eccgroup
import (
"github.com/cymony/cryptomony/eccgroup/internal"
)
// Element represents an element on the curve of the prime-order group.
type Element struct {
internal.Element
}
func newPoint(p internal.Element) *Element {
return &Element{p}
}
// Base sets the receiver to the group's base point a.k.a. canonical generator, and returns the receiver.
func (e *Element) Base() *Element {
return &Element{e.Element.Base()}
}
// Identity sets the receiver to the point at infinity of the Group's underlying curve, and returns the reveiver.
func (e *Element) Identity() *Element {
return &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 1 if the receiver and input element are equivalent, and 0 otherwise.
func (e *Element) Equal(element *Element) int {
if element == nil {
return 0
}
return e.Element.Equal(element.Element)
}
// IsIdentity returns whether the receiver 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 {
e.Element.Set(element.Element)
return e
}
// Copy returns a copy of the receiver.
func (e *Element) Copy() *Element {
return &Element{e.Element.Copy()}
}
// Encode returns the compressed byte encoding of the element.
func (e *Element) Encode() []byte {
return e.Element.Encode()
}
// Decode sets the receiver to a decoding of the input data, and returns an error on failure.
func (e *Element) Decode(data []byte) error {
return e.Element.Decode(data)
}
// MarshalBinary returns the compressed byte encoding of the element.
func (e *Element) MarshalBinary() ([]byte, error) {
return e.Element.MarshalBinary()
}
// UnmarshalBinary sets e to the decoding of the byte encoded element.
func (e *Element) UnmarshalBinary(data []byte) error {
return e.Element.UnmarshalBinary(data)
}
// MarshalText implements the encoding.MarshalText interface.
func (e *Element) MarshalText() (text []byte, err error) {
return e.Element.MarshalText()
}
// UnmarshalText implements the encoding.UnmarshalText interface.
func (e *Element) UnmarshalText(text []byte) error {
return e.Element.UnmarshalText(text)
}