Skip to content

Commit e5ad763

Browse files
authored
version 6.1.0 (#15)
1 parent 4fa0930 commit e5ad763

File tree

13 files changed

+247
-124
lines changed

13 files changed

+247
-124
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
cmake_minimum_required( VERSION 3.14 )
1818

19-
project( scl VERSION 6.0.0 DESCRIPTION "Secure Computation Library" )
19+
project( scl VERSION 6.1.0 DESCRIPTION "Secure Computation Library" )
2020

2121
if(NOT CMAKE_BUILD_TYPE)
2222
set(CMAKE_BUILD_TYPE Release)

RELEASE.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
6.1.0: Extend serialization functionality
2+
- Make Write methods return the number of bytes written.
3+
- Make it possible to serialize vectors with arbitrary content.
4+
15
6.0.0: Improvements to serialization and Channels.
26
- Added a Serializer type that can be specialized in order to specify how
37
various objects are converted to bytes.

include/scl/math/ec.h

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "scl/math/ec_ops.h"
2424
#include "scl/math/ff.h"
2525
#include "scl/math/number.h"
26+
#include "scl/math/ops.h"
2627

2728
namespace scl::math {
2829

@@ -36,7 +37,7 @@ namespace scl::math {
3637
* @see Secp256k1
3738
*/
3839
template <typename Curve>
39-
class EC {
40+
class EC final : Add<EC<Curve>>, Eq<EC<Curve>>, Print<EC<Curve>> {
4041
public:
4142
/**
4243
* @brief The field that this curve is defined over.
@@ -121,17 +122,6 @@ class EC {
121122
return *this;
122123
}
123124

124-
/**
125-
* @brief Add two points.
126-
* @param lhs the left hand point
127-
* @param rhs the right hand point
128-
* @return the sum of two EC points.
129-
*/
130-
friend EC operator+(const EC& lhs, const EC& rhs) {
131-
EC copy(lhs);
132-
return copy += rhs;
133-
}
134-
135125
/**
136126
* @brief Double this point.
137127
* @return this after doubling.
@@ -160,17 +150,6 @@ class EC {
160150
return *this;
161151
}
162152

163-
/**
164-
* @brief Subtract two EC points.
165-
* @param lhs the left hand point
166-
* @param rhs the right hand point
167-
* @return the difference of two EC points.
168-
*/
169-
friend EC operator-(const EC& lhs, const EC& rhs) {
170-
EC copy(lhs);
171-
return copy -= rhs;
172-
}
173-
174153
/**
175154
* @brief Perform a scalar multiplication.
176155
* @param scalar the scalar
@@ -243,15 +222,6 @@ class EC {
243222
return *this;
244223
}
245224

246-
/**
247-
* @brief Compute the negation of this EC point.
248-
* @return the negation of this EC point.
249-
*/
250-
EC operator-() {
251-
EC copy(*this);
252-
return copy.Negate();
253-
}
254-
255225
/**
256226
* @brief Check if this EC point is equal to another EC point.
257227
* @param other the other EC point
@@ -261,20 +231,6 @@ class EC {
261231
return CurveEqual<Curve>(m_value, other.m_value);
262232
}
263233

264-
/**
265-
* @brief Equality operator for EC points.
266-
*/
267-
friend bool operator==(const EC& lhs, const EC& rhs) {
268-
return lhs.Equal(rhs);
269-
}
270-
271-
/**
272-
* @brief In-equality operator for EC points.
273-
*/
274-
friend bool operator!=(const EC& lhs, const EC& rhs) {
275-
return !(lhs == rhs);
276-
}
277-
278234
/**
279235
* @brief Check if this point is equal to the point at inifity.
280236
* @return true if this point is equal to the point at inifity.
@@ -298,14 +254,6 @@ class EC {
298254
return CurveToString<Curve>(m_value);
299255
}
300256

301-
/**
302-
* @brief Write the curve point to a STL output stream.
303-
* @see EC::ToString.
304-
*/
305-
friend std::ostream& operator<<(std::ostream& os, const EC& point) {
306-
return os << point.ToString();
307-
}
308-
309257
/**
310258
* @brief Write this point to a buffer.
311259
* @param dest the destination

include/scl/math/ff.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <type_traits>
2424

2525
#include "scl/math/ff_ops.h"
26-
#include "scl/math/ring.h"
26+
#include "scl/math/ops.h"
2727
#include "scl/util/prg.h"
2828

2929
namespace scl::math {
@@ -35,7 +35,10 @@ namespace scl::math {
3535
* @see Mersenne127
3636
*/
3737
template <typename Field>
38-
class FF final : Ring<FF<Field>> {
38+
class FF final : Add<FF<Field>>,
39+
Mul<FF<Field>>,
40+
Eq<FF<Field>>,
41+
Print<FF<Field>> {
3942
public:
4043
/**
4144
* @brief Size in bytes of a field element.

include/scl/math/mat.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "scl/math/ff.h"
3131
#include "scl/math/lagrange.h"
32+
#include "scl/math/ops.h"
3233
#include "scl/math/vec.h"
3334
#include "scl/util/prg.h"
3435
#include "scl/util/traits.h"
@@ -42,7 +43,7 @@ class Vec;
4243
* @brief Matrix.
4344
*/
4445
template <typename Elem>
45-
class Mat {
46+
class Mat : Print<Mat<Elem>> {
4647
public:
4748
/**
4849
* @brief The type of the matrix elements.
@@ -385,13 +386,6 @@ class Mat {
385386
*/
386387
std::string ToString() const;
387388

388-
/**
389-
* @brief Write a string representation of this vector to a stream.
390-
*/
391-
friend std::ostream& operator<<(std::ostream& os, const Mat<Elem>& v) {
392-
return os << v.ToString();
393-
}
394-
395389
/**
396390
* @brief Test if this matrix is equal to another.
397391
*/

include/scl/math/number.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323

2424
#include <gmp.h>
2525

26+
#include "scl/math/ops.h"
2627
#include "scl/util/prg.h"
2728

2829
namespace scl::math {
2930

3031
/**
3132
* @brief Arbitrary precision integer.
3233
*/
33-
class Number {
34+
class Number final : Print<Number> {
3435
public:
3536
/**
3637
* @brief Generate a random Number.
@@ -355,13 +356,6 @@ class Number {
355356
*/
356357
std::string ToString() const;
357358

358-
/**
359-
* @brief << overload for STL ostream objects.
360-
*/
361-
friend std::ostream& operator<<(std::ostream& os, const Number& number) {
362-
return os << number.ToString();
363-
};
364-
365359
/**
366360
* @brief STL swap implementation for Number.
367361
*/

include/scl/math/ops.h

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* SCL --- Secure Computation Library
2+
* Copyright (C) 2023 Anders Dalskov
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef SCL_MATH_OPS_H
19+
#define SCL_MATH_OPS_H
20+
21+
#include <ostream>
22+
23+
namespace scl::math {
24+
25+
/**
26+
* @brief Provides binary <code>+</code> and <code>-</code>, and unary
27+
* <code>-</code> operators.
28+
*
29+
* Requires that the type \p T implements the <code>+=</code> and
30+
* <code>-=</code> operators, and a <code>Negate</code> function.
31+
*/
32+
template <typename T>
33+
struct Add {
34+
/**
35+
* @brief Add two elements and return their sum.
36+
*/
37+
friend T operator+(const T& lhs, const T& rhs) {
38+
T temp(lhs);
39+
return temp += rhs;
40+
}
41+
42+
/**
43+
* @brief Subtract two elements and return their difference.
44+
*/
45+
friend T operator-(const T& lhs, const T& rhs) {
46+
T temp(lhs);
47+
return temp -= rhs;
48+
}
49+
50+
/**
51+
* @brief Return the negation of an element.
52+
*/
53+
friend T operator-(const T& elem) {
54+
T temp(elem);
55+
return temp.Negate();
56+
}
57+
};
58+
59+
/**
60+
* @brief Provides <code>*</code> and <code>/</code> operators.
61+
*
62+
* Requires that \p T implements the <code>*=</code> and <code>/=</code>
63+
* operators.
64+
*/
65+
template <typename T>
66+
struct Mul {
67+
/**
68+
* @brief Multiply two elements and return their product.
69+
*/
70+
friend T operator*(const T& lhs, const T& rhs) {
71+
T temp(lhs);
72+
return temp *= rhs;
73+
}
74+
75+
/**
76+
* @brief Divide two elements and return their quotient.
77+
*/
78+
friend T operator/(const T& lhs, const T& rhs) {
79+
T temp(lhs);
80+
return temp /= rhs;
81+
}
82+
};
83+
84+
/**
85+
* @brief Provides <code>==</code> and <code>!=</code> operators.
86+
*
87+
* Requires that \p implements an <code>Equal(T)</code> function.
88+
*/
89+
template <typename T>
90+
struct Eq {
91+
/**
92+
* @brief Compare two elements for equality.
93+
*/
94+
friend bool operator==(const T& lhs, const T& rhs) {
95+
return lhs.Equal(rhs);
96+
}
97+
98+
/**
99+
* @brief Compare two elements for inequality.
100+
*/
101+
friend bool operator!=(const T& lhs, const T& rhs) {
102+
return !(lhs == rhs);
103+
}
104+
};
105+
106+
/**
107+
* @brief Provides <code><<</code> syntax for printing to a string.
108+
*
109+
* Requires that \p implements a <code>ToString()</code> function.
110+
*/
111+
template <typename T>
112+
struct Print {
113+
/**
114+
* @brief Write a string representation of an element to a stream.
115+
*/
116+
friend std::ostream& operator<<(std::ostream& os, const T& r) {
117+
return os << r.ToString();
118+
}
119+
};
120+
121+
} // namespace scl::math
122+
123+
#endif // SCL_MATH_OPS_H

include/scl/math/ring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace scl::math {
2525

2626
/**
2727
* @brief Derives some basic operations on Ring elements via. CRTP.
28+
* @deprecated
2829
*/
2930
template <typename T>
3031
struct Ring {

include/scl/math/z2k.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include <stdexcept>
2222

23-
#include "scl/math/ring.h"
23+
#include "scl/math/ops.h"
2424
#include "scl/math/z2k_ops.h"
2525
#include "scl/util/prg.h"
2626

@@ -36,7 +36,10 @@ namespace scl::math {
3636
* byte (so Z2k<6> and Z2k<8> take up the same amount of space).
3737
*/
3838
template <std::size_t Bits>
39-
class Z2k final : public Ring<Z2k<Bits>> {
39+
class Z2k final : Add<Z2k<Bits>>,
40+
Mul<Z2k<Bits>>,
41+
Eq<Z2k<Bits>>,
42+
Print<Z2k<Bits>> {
4043
public:
4144
/**
4245
* @brief The raw type of a Z2k element.

0 commit comments

Comments
 (0)