-
Notifications
You must be signed in to change notification settings - Fork 372
/
ec_scalar.cpp
144 lines (116 loc) · 3.21 KB
/
ec_scalar.cpp
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
/**
* Copyright (c) 2011-2018 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/bitcoin/math/ec_scalar.hpp>
#include <bitcoin/bitcoin/math/hash.hpp>
#include <bitcoin/bitcoin/formats/base_16.hpp>
#include <bitcoin/bitcoin/utility/serializer.hpp>
namespace libbitcoin {
// TODO: implement equality and inequality for ec_scalar and ec_secret.
const ec_scalar ec_scalar::zero = null_hash;
// Null is valid but uninitialized object is invalid.
ec_scalar::ec_scalar()
: valid_(false), secret_(null_hash)
{
}
ec_scalar::ec_scalar(const ec_secret& secret)
: valid_(true), secret_(secret)
{
}
ec_scalar::ec_scalar(uint64_t value)
: valid_(true)
{
secret_ = null_hash;
auto serial = make_unsafe_serializer(secret_.end() - 4);
serial.write_4_bytes_big_endian(value);
}
// Operators.
// ----------------------------------------------------------------------------
ec_scalar ec_scalar::operator-() const
{
if (!(*this))
return *this;
auto negation = *this;
if (!ec_negate(negation.secret_))
return {};
return negation;
}
ec_scalar& ec_scalar::operator+=(const ec_scalar& scalar)
{
if (!(*this))
return *this;
*this = *this + scalar;
return *this;
}
ec_scalar& ec_scalar::operator-=(const ec_scalar& scalar)
{
if (!(*this))
return *this;
*this = *this - scalar;
return *this;
}
ec_scalar& ec_scalar::operator=(const ec_secret& secret)
{
valid_ = true;
secret_ = secret;
return *this;
}
ec_scalar operator+(ec_scalar left, const ec_scalar& right)
{
if (!left || !right)
return {};
if (!ec_add(left.secret_, right.secret_))
return {};
return left;
}
ec_scalar operator-(ec_scalar left, const ec_scalar& right)
{
if (!left || !right)
return {};
const auto negative_right = -right;
if (!negative_right)
return {};
return left + negative_right;
}
ec_scalar operator*(ec_scalar left, const ec_scalar& right)
{
if (!left || !right)
return {};
if (!ec_multiply(left.secret_, right.secret_))
return {};
return left;
}
ec_scalar::operator bool() const
{
// Caller must make both test against zero when that is required.
// Overloading bool with valid and non-zero would be very non-intuitive.
return valid_ /*&& secret_ != ec_scalar::zero*/;
}
ec_scalar::operator const ec_secret&() const
{
return secret_;
}
const ec_secret& ec_scalar::secret() const
{
return secret_;
}
ec_secret& ec_scalar::secret()
{
return secret_;
}
} // namespace libbitcoin