Skip to content

Commit 1d74966

Browse files
committed
Secret sharing output for sfix and sfloat.
1 parent 81d3b00 commit 1d74966

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Compiler/library.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def print_plain_str(ss):
8484
if len(args) != s.count('%s'):
8585
raise CompilerError('Incorrect number of arguments for string format:', s)
8686
substrings = s.split('%s')
87+
def secret_error(x):
88+
raise CompilerError(
89+
'Cannot print secret value %s, activate printing of shares with '
90+
"'print_secrets=True'" % args[i])
8791
for i,ss in enumerate(substrings):
8892
print_plain_str(ss)
8993
if i < len(args):
@@ -98,13 +102,14 @@ def print_plain_str(ss):
98102
elif print_secrets and isinstance(val, (_secret, sbits)):
99103
val.output()
100104
else:
101-
raise CompilerError(
102-
'Cannot print secret value %s, activate printing of shares with '
103-
"'print_secrets=True'" % args[i])
105+
secret_error(args[i])
104106
elif isinstance(val, cfix):
105107
val.print_plain()
106108
elif isinstance(val, sfix) or isinstance(val, sfloat):
107-
raise CompilerError('Cannot print secret value:', args[i])
109+
if print_secrets:
110+
val.output()
111+
else:
112+
secret_error()
108113
elif isinstance(val, cfloat):
109114
val.print_float_plain()
110115
elif isinstance(val, (list, tuple)):

Compiler/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,6 +5148,11 @@ def zip(cls, *parts):
51485148
def __repr__(self):
51495149
return '<sfix{f=%d,k=%d} at %s>' % (self.f, self.k, self.v)
51505150

5151+
def output(self):
5152+
library.print_str(
5153+
'<sfix{f=%d,k=%d,v=%s}>' % (self.f, self.k, '%s'), self.v,
5154+
print_secrets=True)
5155+
51515156
class unreduced_sfix(_single):
51525157
int_type = sint
51535158

@@ -5828,6 +5833,11 @@ def for_mux(self, other):
58285833
f = lambda x: type(self)(*x)
58295834
return f, sint(list(self)), sint(list(other))
58305835

5836+
def output(self):
5837+
library.print_str(
5838+
'<sfloat{v=%s,p=%s,z=%s,s=%s}>', self.v, self.p, self.z, self.s,
5839+
print_secrets=True)
5840+
58315841
class cfloat(Tape._no_truth):
58325842
""" Helper class for printing revealed sfloats. """
58335843
__slots__ = ['v', 'p', 'z', 's', 'nan']

0 commit comments

Comments
 (0)