-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedScalar.php
46 lines (41 loc) · 1.21 KB
/
SharedScalar.php
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
<?php
class SharedScalar {
public array $shares;
public array $share_of;
public array $owners;
public function __construct(array $shares)
{
$this->shares = $shares;
foreach ($shares as $share) {
$this->owners[] = $share->owner;
$this->share_of[$share->owner->name] = $share;
}
}
public function add(SharedScalar $scalar): SharedScalar
{
if ($this->owners !== $scalar->owners) {
throw new Error("should have the same owners");
}
$sum_shares = [];
foreach ($this->owners as $owner) {
$sum_shares[] = $this->share_of[$owner->name]->add($scalar->share_of[$owner->name]);
}
return new SharedScalar($sum_shares);
}
public function sub(SharedScalar $scalar): SharedScalar
{
if ($this->owners !== $scalar->owners) {
throw new Error("should have the same owners");
}
$sum_shares = [];
foreach ($this->owners as $owner) {
$sum_shares[] = $this->share_of[$owner->name]->sub($scalar->share_of[$owner->name]);
}
return new SharedScalar($sum_shares);
}
public function reconstruct (VirtualMachine $owner): PrivateScalar
{
$value = n_from_shares($this->shares, $owner);
return new PrivateScalar($value, $owner);
}
}