Skip to content

Commit 89b88da

Browse files
committed
updates
1 parent 613b1f9 commit 89b88da

File tree

9 files changed

+81
-49
lines changed

9 files changed

+81
-49
lines changed

include/scl/argparser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace scl {
3838
*
3939
* The interface of ProgramOptions is pretty intuitive
4040
* @code
41+
* #include <scl/argparser.h>
42+
*
4143
* ProgramOptions opts = ... //
4244
*
4345
* // check if "-foo 123" was passed in argv
@@ -58,6 +60,8 @@ namespace scl {
5860
* strings. Specialization is, of course, allowed.
5961
*
6062
* @code
63+
* #include <scl/argparser.h>
64+
*
6165
* struct FooStruct {
6266
* int x;
6367
* int y;
@@ -226,7 +230,7 @@ struct ProgramFlag {
226230
*
227231
* @code
228232
* // example.cc
229-
* #include <scl/cmdline.h>
233+
* #include <scl/argparser.h>
230234
* #include <iostream>
231235
*
232236
* using namespace scl;

include/scl/protocol.h

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#pragma once
1919

2020
#include <any>
21+
#include <stdexcept>
22+
#include <type_traits>
2123

2224
#include "scl/coro/task.h"
2325
#include "scl/net/network.h"
@@ -63,18 +65,22 @@ struct Result;
6365
*
6466
* The following simple example illustrates this idea.
6567
* @code
68+
* #include <scl/protocol.h>
69+
*
6670
* class CountdownProtocol final : public Protocol {
6771
* public:
6872
* CountdownProtocol(int current) : m_current(current) {}
73+
6974
* Task<Result> run(Env& ignored) const override {
70-
* std::cout << "countdown: " << m_current << "\n";
7175
* if (m_current == 0) {
7276
* co_return Result::done();
7377
* } else {
74-
* co_return Result::next(
75-
* std::make_unique<CountdownProtocol>(m_current - 1));
78+
* co_return Result::nextStep(
79+
* std::make_unique<CountdownProtocol>(m_current - 1),
80+
* m_current);
7681
* }
7782
* }
83+
7884
* private:
7985
* int m_current;
8086
* };
@@ -138,21 +144,41 @@ struct Result {
138144
std::any output;
139145
};
140146

147+
/**
148+
* @brief Run a protocol.
149+
*/
141150
template <typename CALLBACK>
142151
Task<void> runProtocol(std::unique_ptr<Protocol> protocol,
143152
Env& env,
144153
CALLBACK output_cb) {
145154
while (protocol) {
146155
Result result = co_await protocol->run(env);
147156

148-
if (result.next) {
149-
protocol = std::move(result.next);
150-
}
157+
protocol = std::move(result.next);
151158

152159
if (result.output.has_value()) {
153160
output_cb(result.output);
154161
}
155162
}
156163
}
157164

165+
template <typename R>
166+
Task<R> runProtocol(std::unique_ptr<Protocol> protocol, Env& env) {
167+
Result result;
168+
169+
while (protocol) {
170+
result = co_await protocol->run(env);
171+
172+
protocol = std::move(result.next);
173+
}
174+
175+
if constexpr (!std::is_void_v<R>) {
176+
if (result.output.has_value()) {
177+
co_return std::any_cast<R>(result.output);
178+
} else {
179+
throw std::runtime_error("protocol never gave output");
180+
}
181+
}
182+
}
183+
158184
} // namespace scl

include/scl/ss/feldman.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,4 @@ bool feldmanVerify(const FeldmanShare<GROUP>& share, std::size_t share_index) {
124124
return v == GROUP::generator() * share.share;
125125
}
126126

127-
/**
128-
* @brief Verify a share given a set of commitments.
129-
* @ingroup ss
130-
* @param share the share to verify.
131-
* @param commitments the commitments to verify against.
132-
* @param share_index the index (e.g., party ID) of the share.
133-
* @return true if the provided share is valid for that index, and false
134-
* otherwise.
135-
*
136-
* This function checks if a provided share is consistent with a set of
137-
* commitments.
138-
*/
139-
template <typename GROUP>
140-
bool feldmanVerify(
141-
const typename FeldmanShare<GROUP>::Field& share,
142-
const Vector<typename FeldmanShare<GROUP>::Group>& commitments,
143-
std::size_t share_index) {
144-
return feldmanVerify<GROUP>({share, commitments}, share_index);
145-
}
146-
147127
} // namespace scl

include/scl/time.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,24 @@ inline long double timeToMillis(Time::Duration time) {
6161
* time wouldn't be accurate.
6262
*/
6363
struct Clock {
64-
virtual ~Clock();
64+
virtual ~Clock() {}
6565

6666
/**
6767
* @brief Read the value of the clock.
6868
*/
6969
virtual Time::Duration read() const = 0;
7070
};
7171

72+
class RealtimeClock final : public Clock {
73+
public:
74+
RealtimeClock() : m_start(Time::now()) {}
75+
76+
Time::Duration read() const override {
77+
return Time::now() - m_start;
78+
}
79+
80+
private:
81+
Time::TimePoint m_start;
82+
};
83+
7284
} // namespace scl

src/scl/hex.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@
1818
#include "scl/hex.h"
1919

2020
#include <cstdint>
21+
#include <iomanip>
2122

2223
template <>
2324
std::string scl::details::toHexString(const __uint128_t& v) {
24-
std::string str;
25-
if (v == 0) {
26-
str = "0";
25+
auto top = static_cast<std::uint64_t>(v >> 64);
26+
auto bot = static_cast<std::uint64_t>(v);
27+
28+
std::stringstream ss;
29+
ss << std::hex << std::setfill('0');
30+
if (top) {
31+
ss << top;
32+
// have to add appropriate padding to the bottom word here
33+
ss << std::setw(16);
34+
ss << bot;
2735
} else {
28-
std::stringstream ss;
29-
auto top = static_cast<std::uint64_t>(v >> 64);
30-
auto bot = static_cast<std::uint64_t>(v);
31-
ss << std::hex;
32-
if (top > 0) {
33-
ss << top;
34-
}
3536
ss << bot;
36-
str = ss.str();
3737
}
38-
return str;
38+
39+
auto str = ss.str();
40+
// make sure the output has an even length
41+
return str.length() % 2 ? '0' + str : str;
3942
}

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ set(SCL_SOURCE_FILES_TEST
5555
scl/net/tcp/test_config.cc
5656
scl/net/tcp/test_network.cc
5757

58-
# scl/protocol/test_protocol.cc
58+
scl/test_protocol.cc
5959

6060
scl/simulation/test_event.cc
6161
# scl/simulation/test_context.cc

test/scl/math/test_mersenne127.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ TEST_CASE("Mersenne127 defs", "[math][ff]") {
3333
}
3434

3535
TEST_CASE("Mersenne127 to string", "[math][ff]") {
36-
REQUIRE(Field::zero().toString() == "0");
37-
REQUIRE(Field::one().toString() == "1");
36+
REQUIRE(Field::zero().toString() == "00");
37+
REQUIRE(Field::one().toString() == "01");
3838

3939
Field x(0x7b);
4040
REQUIRE(x.toString() == "7b");
@@ -50,6 +50,14 @@ TEST_CASE("Mersenne127 to string", "[math][ff]") {
5050
REQUIRE(ss.str() == "7b");
5151
}
5252

53+
TEST_CASE("Mersenne127 hex") {
54+
Field x = Field::fromString("1a0000000000000001");
55+
REQUIRE(x.toString() == "1a0000000000000001");
56+
57+
Field y = Field::fromString("0123");
58+
REQUIRE(y.toString() == "0123");
59+
}
60+
5361
TEST_CASE("Mersenne127 from string", "[math][ff]") {
5462
auto y = Field::fromString("7b");
5563
REQUIRE(y == Field(0x7b));

test/scl/math/test_poly.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ TEMPLATE_TEST_CASE("Polynomial evaluate", "[math][ss]", FIELD_DEFS) {
6969
REQUIRE(x5 == FF(54));
7070
}
7171

72-
TEMPLATE_TEST_CASE("Polynomial to string", "[math][ss]", FIELD_DEFS) {
72+
TEMPLATE_TEST_CASE("Polynomial to string", "[math][ss]", scl::test::Mersenne61) {
7373
using FF = TestType;
7474

7575
Vector coeff = {FF(4), FF(5), FF(1)};

test/scl/ss/test_feldman.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ TEST_CASE("Feldman", "[ss]") {
3333
std::size_t t = 4;
3434

3535
auto secret = Field(123);
36-
auto sb = feldmanSecretShare<Curve>(secret, 4, 24, prg);
36+
auto sb = createFeldmanVerifiableSharing<Curve>(secret, 4, 24, prg);
3737
REQUIRE(sb.commitments[0] == secret * Curve::generator());
3838
REQUIRE(sb.shares.size() == 24);
3939
REQUIRE(sb.commitments.size() == t + 1);
4040
REQUIRE(feldmanVerify<Curve>({secret, sb.commitments}, 0));
41-
REQUIRE(feldmanVerify<Curve>(secret, sb.commitments, 0));
42-
REQUIRE(feldmanVerify(sb.getShare(22), 23));
41+
REQUIRE(feldmanVerify<Curve>({sb.shares[22], sb.commitments}, 23));
4342
REQUIRE(shamirRecoverP(sb.shares.subVector(5)) == secret);
4443
}
4544

@@ -50,8 +49,8 @@ TEST_CASE("Feldman hom", "[ss]") {
5049
auto s0 = Field(123);
5150
auto s1 = Field(44);
5251

53-
auto ss0 = feldmanSecretShare<Curve>(s0, t, 10, prg);
54-
auto ss1 = feldmanSecretShare<Curve>(s1, t, 10, prg);
52+
auto ss0 = createFeldmanVerifiableSharing<Curve>(s0, t, 10, prg);
53+
auto ss1 = createFeldmanVerifiableSharing<Curve>(s1, t, 10, prg);
5554

5655
auto ss2 = ss0.shares.add(ss1.shares);
5756
auto com2 = ss0.commitments.add(ss1.commitments);

0 commit comments

Comments
 (0)