File tree Expand file tree Collapse file tree 11 files changed +622
-198
lines changed
Expand file tree Collapse file tree 11 files changed +622
-198
lines changed Original file line number Diff line number Diff line change 1616
1717cmake_minimum_required ( VERSION 3.14 )
1818
19- project ( scl VERSION 2.0 .0 DESCRIPTION "Secure Computation Library" )
19+ project ( scl VERSION 2.1 .0 DESCRIPTION "Secure Computation Library" )
2020
2121if (NOT CMAKE_BUILD_TYPE )
2222 set (CMAKE_BUILD_TYPE Release)
@@ -53,8 +53,10 @@ set(SCL_SOURCE_FILES
5353
5454if (WITH_EC MATCHES ON )
5555 set (SCL_SOURCE_FILES ${SCL_SOURCE_FILES}
56+ src/scl/math/ops_gmp_ff.cc
5657 src/scl/math/secp256k1_field.cc
5758 src/scl/math/secp256k1_curve.cc
59+ src/scl/math/secp256k1_order.cc
5860 src/scl/math/number.cc)
5961endif ()
6062
Original file line number Diff line number Diff line change 1+ 2.1: More Finite Fields
2+ - Provide a FF implementation for computations modulo the order of Secp256k1
3+ - Extend EC with support for scalar multiplications with scalars from a finite
4+ field of size the order of a subgroup.
5+
162.0: Elliptic curves and finite field refactoring
27- Make it simpler to define new finite fields
38- Include optional (but enabled by default) support for elliptic curves
Original file line number Diff line number Diff line change @@ -58,6 +58,31 @@ struct Secp256k1 {
5858 constexpr static const std::size_t kBitSize = 8 * kByteSize ;
5959 };
6060
61+ /* *
62+ * @brief Finite field modulo a Secp256k1 prime order sub-group.
63+ */
64+ struct Order {
65+ /* *
66+ * @brief Internal type of elements.
67+ */
68+ using ValueType = std::array<mp_limb_t , 4 >;
69+
70+ /* *
71+ * @brief Name of the field.
72+ */
73+ constexpr static const char * kName = " secp256k1_order" ;
74+
75+ /* *
76+ * @brief Size of an element in bytes.
77+ */
78+ constexpr static const std::size_t kByteSize = 4 * sizeof (mp_limb_t );
79+
80+ /* *
81+ * @brief Size of an element in bits.
82+ */
83+ constexpr static const std::size_t kBitSize = 8 * kByteSize ;
84+ };
85+
6186 /* *
6287 * @brief Secp256k1 curve elements are stored in projective coordinates.
6388 */
Original file line number Diff line number Diff line change @@ -168,6 +168,16 @@ class EC {
168168 return *this ;
169169 };
170170
171+ /* *
172+ * @brief Perform a scalar multiplication.
173+ * @param scalar the scalar
174+ * @return this.
175+ */
176+ EC& operator *=(const FF<typename Curve::Order>& scalar) {
177+ details::CurveScalarMultiply<Curve>(mValue , scalar);
178+ return *this ;
179+ };
180+
171181 /* *
172182 * @brief Multiply a point with a scalar from the right.
173183 * @param point the point
@@ -179,6 +189,18 @@ class EC {
179189 return copy *= scalar;
180190 };
181191
192+ /* *
193+ * @brief Multiply a point with a scalar from the right.
194+ * @param point the point
195+ * @param scalar the scalar
196+ * @return the point multiplied with the scalar.
197+ */
198+ friend EC operator *(const EC& point,
199+ const FF<typename Curve::Order>& scalar) {
200+ EC copy (point);
201+ return copy *= scalar;
202+ };
203+
182204 /* *
183205 * @brief Multiply a point with a scalar from the left.
184206 * @param point the point
@@ -189,6 +211,17 @@ class EC {
189211 return point * scalar;
190212 };
191213
214+ /* *
215+ * @brief Multiply a point with a scalar from the left.
216+ * @param point the point
217+ * @param scalar the scalar
218+ * @return the point multiplied with the scalar.
219+ */
220+ friend EC operator *(const FF<typename Curve::Order>& scalar,
221+ const EC& point) {
222+ return point * scalar;
223+ };
224+
192225 /* *
193226 * @brief Negate this point.
194227 * @return this.
Original file line number Diff line number Diff line change @@ -97,6 +97,15 @@ void CurveNegate(typename C::ValueType& out);
9797template <typename C>
9898void CurveScalarMultiply (typename C::ValueType& out, const Number& scalar);
9999
100+ /* *
101+ * @brief Scalar multiply an elliptic curve point in-place.
102+ * @param out the point
103+ * @param scalar the scalar
104+ */
105+ template <typename C>
106+ void CurveScalarMultiply (typename C::ValueType& out,
107+ const FF<typename C::Order>& scalar);
108+
100109/* *
101110 * @brief Check if two elliptic curve points are equal.
102111 * @param in1 the first point
Original file line number Diff line number Diff line change 1+ /* *
2+ * @file ops_gmp_ff.cc
3+ *
4+ * SCL --- Secure Computation Library
5+ * Copyright (C) 2022 Anders Dalskov
6+ *
7+ * This program is free software: you can redistribute it and/or modify
8+ * it under the terms of the GNU Affero General Public License as published by
9+ * the Free Software Foundation, either version 3 of the License, or
10+ * (at your option) any later version.
11+ *
12+ * This program is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU Affero General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Affero General Public License
18+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19+ */
20+
21+ #include " ./ops_gmp_ff.h"
22+
23+ void scl::details::ReadLimb (mp_limb_t &lmb, const unsigned char *bytes,
24+ std::size_t bits_per_limbs) {
25+ std::size_t c = 0 ;
26+ lmb = 0 ;
27+ for (std::size_t i = 0 ; i < bits_per_limbs; i += 8 ) {
28+ lmb |= static_cast <mp_limb_t >(bytes[c++]) << i;
29+ }
30+ }
31+
32+ std::size_t scl::details::FindFirstNonZero (const std::string &s) {
33+ int n = 0 ;
34+ for (const auto c : s) {
35+ if (c != ' 0' ) {
36+ return n;
37+ }
38+ n++;
39+ }
40+ return n;
41+ }
You can’t perform that action at this time.
0 commit comments