forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commit.cpp
91 lines (79 loc) · 2.26 KB
/
Commit.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
#include "Commit.h"
#include "random.h"
#include "int.h"
void Commit(octetStream& comm,octetStream& open,const octetStream& message, int send_player)
{
open.store(send_player);
open.append(message.get_data(), message.get_length());
open.append_random(SEED_SIZE);
comm = open.hash();
}
bool Open(octetStream& message,const octetStream& comm,const octetStream& open, int send_player)
{
octetStream h = open.hash();
octet* open_bytes = open.get_data();
// first 4 bytes are player no.
int open_player = BYTES_TO_INT(open_bytes);
if (!(h.equals(comm) && open_player == send_player))
{
return false;
}
message.reset_write_head();
message.append(open_bytes + sizeof(int), open.get_length() - SEED_SIZE - sizeof(int));
return true;
}
void Commitment::commit(const octetStream& message)
{
open.reset_write_head();
open.append_random(SEED_SIZE);
commit(message, open);
}
void Commitment::commit(const octetStream& message, const octetStream& open)
{
Hash hash;
hash.update(&send_player, sizeof(send_player));
hash.update(message);
hash.update(open);
hash.final(comm);
}
void Commitment::check(const octetStream& message, const octetStream& comm,
const octetStream& open)
{
commit(message, open);
if (!(comm == this->comm))
throw invalid_commitment();
}
void AllCommitments::commit_and_open(const octetStream& message)
{
commit(message);
open();
}
void AllCommitments::commit(const octetStream& message)
{
Commitment mine(P.my_num());
mine.commit(message);
comms[P.my_num()] = mine.comm;
opens[P.my_num()] = mine.open;
P.Broadcast_Receive(comms);
}
void AllCommitments::open()
{
P.Broadcast_Receive(opens);
}
void AllCommitments::open(const octetStream& message)
{
messages.resize(P.num_players());
messages[P.my_num()] = message;
P.Broadcast_Receive(messages);
open();
for (int i = 0; i != P.my_num(); i++)
check(i, messages[i]);
}
void AllCommitments::check(int player, const octetStream& message)
{
Commitment(player).check(message, comms[player], opens[player]);
}
void AllCommitments::check_relative(int diff, const octetStream& message)
{
check((P.my_num() + P.num_players() - diff) % P.num_players(), message);
}