-
Hi, from mpyc.runtime import mpc
secint = mpc.SecInt(32)
secfld = mpc.SecFld(2**8)
###### Approach 1 ######
a = secint(3)
b = await mpc.output(a) # I want to avoid this reveal step
c = secfld(b)
d = await mpc.output(c)
print(d) # prints `x+1` as expected
###### Approach 2 ######
p = secint(3)
q = mpc.convert(p, secfld)
r = await mpc.output(q)
print(r) # prints some random value like `x^7+x^6+x^5+x^2+x` ?! Am I using Also, is using Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
Yes, that's right, a conversion from a secint to a secfld value, where the target secfld is not a prime field is not supported. It is possible to do such a conversion securely. But it is also important to check if and why you would need such a conversion precisely? |
Beta Was this translation helpful? Give feedback.
-
Right, those things won't work. It's a bit more complicated as we need to use random bits that "live" both as a Here's some starter code that does the job. It may be sufficiently efficient for your case? import secrets
from mpyc.runtime import mpc
secint = mpc.SecInt()
secfld256 = mpc.SecFld(256)
@mpc.coroutine
async def to(x):
n = len(x)
await mpc.returnType(secfld256, n)
r = [secrets.randbits(1) for _ in range(n)]
r_src = [secint.field(1-2*a) for a in r]
r_src = mpc.input([secint(a) for a in r_src])
r_src = list(map(list, zip(*r_src)))
r_src = [mpc.prod(a) for a in r_src]
r_src = [(1-a)/2 for a in r_src]
c = mpc.vector_add(x, r_src)
c = [mpc.lsb(a) for a in c]
c = await mpc.output(c)
r_tgt = [secfld256.field(a) for a in r]
r_tgt = mpc.input([secfld256(a) for a in r_tgt])
r_tgt = list(map(sum, zip(*r_tgt)))
r_tgt = [a + b for a,b in zip(c, r_tgt)]
return r_tgt
mpc.run(mpc.start())
a = secint(3)
x = mpc.to_bits(a)
x = to(x)
b = 0
for xi in reversed(x[1:]):
b += xi
b *= secfld256.field(2)
b += x[0]
print(mpc.run(mpc.output(b)))
mpc.run(mpc.shutdown()) |
Beta Was this translation helpful? Give feedback.
-
Using the above solution, I can perform the transformations for the Is there a way to convert that into an So, I came up with the following approach: a = [[secfld(1), secfld(2)],
[secfld(3), secfld(4)]]
b = mpc.np_fromlist(sum(a, [])) # first, flatten the list, then apply `np_fromlist`
c = mpc.np_reshape(b, (2, 2)) # now, reshape to get back the original shape
print(c) # `ArraySecFld8(GF(2^8))`
print(c.shape) # (2, 2) Am I doing it the correct way? |
Beta Was this translation helpful? Give feedback.
-
Yes, that's the right way to do it, given the current API. This is to keep the implementation of |
Beta Was this translation helpful? Give feedback.
-
Hi @lschoe, I have a followup question. How can I extract bits securely from I am trying to create some hash commitment using sha3-256 which accepts a list of My original item is a When I tried to directly extract all the bits of So, looking for an alternative approach for the conversion, which is more practical in terms of time consumption. |
Beta Was this translation helpful? Give feedback.
-
Hi @abraj, maybe there is an easy way out in this case. The point is that instead of using Line 35 in 15f747c you can also use |
Beta Was this translation helpful? Give feedback.
Right, those things won't work. It's a bit more complicated as we need to use random bits that "live" both as a
secint
and as asecfld256
.Here's some starter code that does the job. It may be sufficiently efficient for your case?