Skip to content

Commit 88d099f

Browse files
authored
version 5.2.0 (#12)
1 parent 772453b commit 88d099f

File tree

11 files changed

+157
-29
lines changed

11 files changed

+157
-29
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 5.1.2 DESCRIPTION "Secure Computation Library" )
19+
project( scl VERSION 5.2.0 DESCRIPTION "Secure Computation Library" )
2020

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

doc/DoxyConf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ INPUT = doc/mainpage.md \
1515
include/scl/math/curves \
1616
include/scl/ss \
1717
include/scl/net \
18-
include/scl/net/discovery \
1918
include/scl/simulation \
2019
include/scl/protocol
2120
FILE_PATTERNS = *.h

include/scl/protocol/base.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,46 @@ struct Protocol {
8181

8282
/**
8383
* @brief Evaluate a protocol.
84-
* @param protocol the protocol
85-
* @param network the network to evaluate the protocol with
86-
* @param output_cb a callback for consuming protocol output
84+
* @param protocol the protocol.
85+
* @param output_cb a callback for consuming protocol output.
86+
* @param env the protocol environment.
8787
*/
8888
template <typename Callback>
8989
void Evaluate(std::unique_ptr<Protocol> protocol,
90-
net::Network& network,
91-
Callback output_cb) {
90+
Callback output_cb,
91+
Env& env) {
9292
std::shared_ptr<Protocol> next = std::move(protocol);
9393
std::shared_ptr<Protocol> prev = next;
9494

95-
ProtocolEnvironment ctx{network,
96-
std::make_unique<RealTimeClock>(),
97-
std::make_unique<StlThreadContext>()};
98-
9995
while (next != nullptr) {
100-
next = next->Run(ctx);
96+
next = next->Run(env);
10197
if (prev->Output().has_value()) {
10298
output_cb(prev->Output());
10399
}
104100
prev = next;
105101
}
106102
}
107103

104+
/**
105+
* @brief Evaluate a protocol.
106+
* @param protocol the protocol.
107+
* @param network the network to evaluate the protocol with.
108+
* @param output_cb a callback for consuming protocol output.
109+
*/
110+
template <typename Callback>
111+
void Evaluate(std::unique_ptr<Protocol> protocol,
112+
net::Network& network,
113+
Callback output_cb) {
114+
Env ctx{network,
115+
std::make_unique<RealTimeClock>(),
116+
std::make_unique<StlThreadContext>()};
117+
Evaluate(std::move(protocol), output_cb, ctx);
118+
}
119+
108120
/**
109121
* @brief Evalate a protocol, discarding all outputs generated.
110-
* @param protocol the protocol to evaluate
111-
* @param network the network to use
122+
* @param protocol the protocol to evaluate.
123+
* @param network the network to use.
112124
*/
113125
inline void Evaluate(std::unique_ptr<Protocol> protocol,
114126
net::Network& network) {

include/scl/protocol/env.h

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace scl::proto {
3535
* number of useful resources, such as a network, but also the ability to know
3636
* its total running time, as well as the ability to work with threads.
3737
*/
38-
struct ProtocolEnvironment {
38+
struct Env {
3939
/**
4040
* @brief Interface for the environment's threading context.
4141
*
@@ -55,6 +55,10 @@ struct ProtocolEnvironment {
5555

5656
/**
5757
* @brief Interface for the environment's clock context.
58+
*
59+
* This interface essentially models a "stopwatch" of sorts. The idea is that
60+
* it will start ticking when a protocol starts. The protocol can check the
61+
* current elapsed time at any point, and mark checkpoints.
5862
*/
5963
struct Clock {
6064
virtual ~Clock(){};
@@ -63,6 +67,11 @@ struct ProtocolEnvironment {
6367
* @brief Read the current value of the clock.
6468
*/
6569
virtual util::Time::Duration Read() const = 0;
70+
71+
/**
72+
* @brief Record a checkpoint with an associated message.
73+
*/
74+
virtual void Checkpoint(const std::string& message) = 0;
6675
};
6776

6877
/**
@@ -82,16 +91,32 @@ struct ProtocolEnvironment {
8291
};
8392

8493
/**
85-
* @brief A protocol clock which returns the total running time.
94+
* @deprecated
95+
*/
96+
using ProtocolEnvironment = Env;
97+
98+
/**
99+
* @brief A protocol clock which operates with real time.
86100
*/
87-
class RealTimeClock final : public ProtocolEnvironment::Clock {
101+
class RealTimeClock final : public Env::Clock {
88102
public:
89-
RealTimeClock() : m_init_time(util::Time::Now()){};
90-
~RealTimeClock(){};
103+
RealTimeClock() : m_init_time(util::Time::Now()) {}
104+
~RealTimeClock() {}
91105

106+
/**
107+
* @brief Get the current time.
108+
*/
92109
util::Time::Duration Read() const {
93110
return util::Time::Now() - m_init_time;
94-
};
111+
}
112+
113+
/**
114+
* @brief Print the current time to stdout.
115+
*/
116+
void Checkpoint(const std::string& message) {
117+
auto ms = std::chrono::duration<double, std::milli>(Read()).count();
118+
std::cout << message << " @ " << ms << " ms\n";
119+
}
95120

96121
private:
97122
util::Time::TimePoint m_init_time;
@@ -100,16 +125,16 @@ class RealTimeClock final : public ProtocolEnvironment::Clock {
100125
/**
101126
* @brief A protocol thread context which uses STL thread.
102127
*/
103-
class StlThreadContext final : public ProtocolEnvironment::Thread {
128+
class StlThreadContext final : public Env::Thread {
104129
public:
105-
~StlThreadContext(){};
130+
~StlThreadContext() {}
106131

107132
/**
108133
* @brief Sleep the current thread using std::this_thread::sleep_for.
109134
*/
110135
void Sleep(std::size_t ms) override {
111136
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
112-
};
137+
}
113138
};
114139

115140
} // namespace scl::proto

include/scl/simulation/env.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "scl/protocol/env.h"
2424
#include "scl/simulation/context.h"
25+
#include "scl/simulation/event.h"
2526

2627
namespace scl::sim {
2728

@@ -35,7 +36,7 @@ class SimulatedClock final : public proto::ProtocolEnvironment::Clock {
3536
* @param ctx a simulation context. Used to read the current time of the party
3637
* @param id the ID of the party
3738
*/
38-
SimulatedClock(const SimulationContext* ctx, std::size_t id)
39+
SimulatedClock(std::shared_ptr<SimulationContext> ctx, std::size_t id)
3940
: m_ctx(ctx), m_id(id){};
4041

4142
/**
@@ -51,8 +52,15 @@ class SimulatedClock final : public proto::ProtocolEnvironment::Clock {
5152
return now - m_ctx->ReadCurrentCheckpoint() + ts;
5253
}
5354

55+
/**
56+
* @brief Mark a checkpoint.
57+
*/
58+
void Checkpoint(const std::string& message) override {
59+
m_ctx->AddEvent(m_id, std::make_shared<CheckpointEvent>(Read(), message));
60+
}
61+
5462
private:
55-
const SimulationContext* m_ctx;
63+
std::shared_ptr<SimulationContext> m_ctx;
5664
std::size_t m_id;
5765
};
5866

include/scl/simulation/event.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ class Event {
8888
/**
8989
* @brief Event made at the end of a protocol segment.
9090
*/
91-
SEGMENT_END
91+
SEGMENT_END,
92+
93+
/**
94+
* @brief A checkpoint recorded by the protocol.
95+
*/
96+
CHECKPOINT
9297
};
9398

9499
/**
@@ -235,6 +240,31 @@ class SegmentEvent final : public Event {
235240
std::string m_name;
236241
};
237242

243+
/**
244+
* @brief An event created when a protocol calls
245+
* <code>env.clock.Checkpoint()</code>.
246+
*/
247+
class CheckpointEvent final : public Event {
248+
public:
249+
/**
250+
* @brief Create a new checkpoint event.
251+
* @param timestamp the time of the event.
252+
* @param message the message of the checkpoint.
253+
*/
254+
CheckpointEvent(util::Time::Duration timestamp, const std::string& message)
255+
: Event(Event::Type::CHECKPOINT, timestamp), m_message(message) {}
256+
257+
/**
258+
* @brief Get the checkpoint message.
259+
*/
260+
std::string Message() const {
261+
return m_message;
262+
}
263+
264+
private:
265+
std::string m_message;
266+
};
267+
238268
/**
239269
* @brief Pretty print a measurement to a stream.
240270
*/

src/scl/simulation/event.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ auto EventTypeToString(Evt::Type type) {
6060
return "SEGMENT_END";
6161
}
6262

63+
if (type == Evt::Type::CHECKPOINT) {
64+
return "CHECKPOINT";
65+
}
66+
6367
// if (type == scl::Measurement::Type::CLOSE)
6468
return "CLOSE";
6569
}
@@ -85,6 +89,10 @@ void WriteSegment(std::ostream& os, const scl::sim::SegmentEvent* m) {
8589
}
8690
}
8791

92+
void WriteCheckpoint(std::ostream& os, const scl::sim::CheckpointEvent* m) {
93+
os << " [" << m->Message() << "]";
94+
}
95+
8896
} // namespace
8997

9098
std::ostream& scl::sim::operator<<(std::ostream& os, const Evt* m) {
@@ -111,5 +119,9 @@ std::ostream& scl::sim::operator<<(std::ostream& os, const Evt* m) {
111119
WriteRecv(os, dynamic_cast<const NetworkEvent*>(m));
112120
}
113121

122+
if (t == Evt::Type::CHECKPOINT) {
123+
WriteCheckpoint(os, dynamic_cast<const CheckpointEvent*>(m));
124+
}
125+
114126
return os;
115127
}

src/scl/simulation/simulator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ std::vector<scl::sim::SimulationTrace> RunSimulation(
155155

156156
scl::proto::ProtocolEnvironment env{
157157
networks[id],
158-
std::make_unique<scl::sim::SimulatedClock>(ctx.get(), id),
158+
std::make_unique<scl::sim::SimulatedClock>(ctx, id),
159159
std::make_unique<scl::sim::SimulatedThreadCtx>(ctx, id)};
160160

161161
ps[id] = Run(ctx, id, ps[id].get(), env, output_callback);

test/scl/protocol/test_protocol.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ TEST_CASE("Protocol env real-time clock", "[protocol]") {
121121
REQUIRE(d >= 100ms);
122122
}
123123

124+
TEST_CASE("Protocol env real-time clock checkpoint", "[protocol]") {
125+
// https://truong.io/posts/capturing_stdout_for_c++_unit_testing.html
126+
127+
proto::RealTimeClock clock;
128+
129+
std::stringstream buf;
130+
std::streambuf* coutbuf = std::cout.rdbuf(buf.rdbuf());
131+
132+
clock.Checkpoint("asd");
133+
134+
auto output = buf.str();
135+
136+
std::cout.rdbuf(coutbuf);
137+
138+
REQUIRE_THAT(output, Catch::Matchers::StartsWith("asd @"));
139+
}
140+
124141
TEST_CASE("Protocol env Stl thread context", "[protocol]") {
125142
proto::StlThreadContext ctx;
126143

test/scl/simulation/test_env.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "scl/simulation/config.h"
2222
#include "scl/simulation/context.h"
2323
#include "scl/simulation/env.h"
24+
#include "scl/simulation/event.h"
2425
#include "scl/simulation/mem_channel_buffer.h"
2526

2627
using namespace scl;
@@ -44,7 +45,7 @@ TEST_CASE("Simulation env clock", "[sim]") {
4445
auto ctx = sim::SimulationContext::Create<sim::MemoryBackedChannelBuffer>(
4546
5,
4647
sim::DefaultConfigCreator());
47-
sim::SimulatedClock clock(ctx.get(), 0);
48+
sim::SimulatedClock clock(ctx, 0);
4849

4950
ctx->AddEvent(0, SomeEvent());
5051
ctx->UpdateCheckpoint();
@@ -67,6 +68,25 @@ TEST_CASE("Simulation env clock", "[sim]") {
6768
REQUIRE(t2 < 1200ms);
6869
}
6970

71+
TEST_CASE("Simulation env clock checkpoint", "[sim]") {
72+
using namespace std::chrono_literals;
73+
74+
auto ctx = sim::SimulationContext::Create<sim::MemoryBackedChannelBuffer>(
75+
5,
76+
sim::DefaultConfigCreator());
77+
sim::SimulatedClock clock(ctx, 0);
78+
79+
ctx->AddEvent(0, SomeEvent(10ms));
80+
ctx->UpdateCheckpoint();
81+
clock.Checkpoint("asd");
82+
REQUIRE(ctx->Trace(0).size() == 2);
83+
REQUIRE(ctx->Trace(0).back()->EventType() == sim::Event::Type::CHECKPOINT);
84+
REQUIRE(ctx->Trace(0).back()->Timestamp() >= 10ms);
85+
86+
sim::CheckpointEvent* e = (sim::CheckpointEvent*)ctx->Trace(0).back().get();
87+
REQUIRE(e->Message() == "asd");
88+
}
89+
7090
TEST_CASE("Simulation env thread", "[sim]") {
7191
using namespace std::chrono_literals;
7292
auto ctx = sim::SimulationContext::Create<sim::MemoryBackedChannelBuffer>(
@@ -76,7 +96,7 @@ TEST_CASE("Simulation env thread", "[sim]") {
7696
ctx->UpdateCheckpoint();
7797

7898
sim::SimulatedThreadCtx thread(ctx, 0);
79-
sim::SimulatedClock clock(ctx.get(), 0);
99+
sim::SimulatedClock clock(ctx, 0);
80100

81101
ctx->AddEvent(0, SomeEvent(util::Time::Duration(1000ms)));
82102
thread.Sleep(1000000);

0 commit comments

Comments
 (0)