写在前面:
任何大师所发明的都是通用的规律,而不是赋予表象的技巧:
高斯的“正十七边形尺规作图法”是表象,而尺规作图的加法器、乘法器、开根器是通用规律,更核心的是如何将正多边形的角度转换成开根式。
废话不多,进入正题:
运行结果:
****************************************************
J_bob = 5 ; I_alice = 5
transfer_to_alice 1375
range_list [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Yu_list [ 1355, 1340, 1967, 32, 473, 758, 2051, 26, 883, 537]
Zu_list [ 71, 56, 41, 32, 45, 9, 18, 26, 27, 2]
list_back_to_bob [ 71, 56, 41, 32, 45, 10, 19, 27, 28, 3]
list[J_bob - 1] = list[4] = 45
G = x_random_bob % p_alice_for_bob = 45
result: Bob <= Alice
****************************************************
J_bob = 7 ; I_alice = 5
transfer_to_alice 1373
range_list [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Yu_list [ 125, 2110, 1355, 1340, 1967, 32, 473, 758, 2051, 26]
Zu_list [ 18, 77, 71, 56, 41, 32, 45, 9, 18, 26]
list_back_to_bob [ 18, 77, 71, 56, 41, 33, 46, 10, 19, 27]
list[J_bob - 1] = list[6] = 46
G = x_random_bob % p_alice_for_bob = 45
result: Bob > Alice
****************************************************
J_bob = 3 ; I_alice = 5
transfer_to_alice 1377
range_list [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Yu_list [ 1967, 32, 473, 758, 2051, 26, 883, 537, 1110, 1428]
Zu_list [ 41, 32, 45, 9, 18, 26, 27, 2, 40, 37]
list_back_to_bob [ 41, 32, 45, 9, 18, 27, 28, 3, 41, 38]
list[J_bob - 1] = list[2] = 45
G = x_random_bob % p_alice_for_bob = 45
result: Bob <= Alice
Process finished with exit code 0
实现代码:
def map_list_5_str(list_in):
ready_list = list(map(lambda x: '%5s' % str(x), list_in))
r_str = ','.join(ready_list)
return '[' + r_str + ']'
# public key of alice as e, and private key as d, total encode space is N
e_alice = 79
d_alice = 1019
N_alice = 3337
o1 = 1
o2 = 10
I_alice = 5
for J_bob in [5, 7, 3]:
print "\n****************************************************"
print "J_bob =", J_bob, "; I_alice =", I_alice
assert o1 < I_alice < o2
assert o1 < J_bob < o2
# ------------------------------
x_random_bob = 473 # bob's random number
c = (x_random_bob ** e_alice) % N_alice
transfer_to_alice = c - J_bob
print 'transfer_to_alice', transfer_to_alice
range_alice = [1, o2 - o1 + 1]
range_list = range(range_alice[0], range_alice[1] + 1)
print 'range_list ', map_list_5_str(range_list)
Yu_list = [((transfer_to_alice + u) ** d_alice) % N_alice for u in range_list]
print 'Yu_list ', map_list_5_str(Yu_list)
p_alice_for_bob = 107
assert p_alice_for_bob < N_alice
Zu_list = list(map(lambda x: x % p_alice_for_bob, Yu_list))
print 'Zu_list ', map_list_5_str(Zu_list)
list_back_to_bob = Zu_list[0:I_alice]
list_back_to_bob.extend(list(map(lambda x: x + 1, Zu_list[I_alice:])))
print 'list_back_to_bob', map_list_5_str(list_back_to_bob)
g = x_random_bob % p_alice_for_bob
g_ = list_back_to_bob[J_bob - 1]
print ' list[J_bob - 1] = list['+str(J_bob - 1)+'] =', g_
print 'G = x_random_bob % p_alice_for_bob =', g
print 'result:', 'Bob <= Alice' if g >= g_ else 'Bob > Alice'
参考:
Andrew Yao’s Millionaire’s Problem
Explain it like I’m Five: The Socialist Millionaire Problem and Secure Multi-Party Computation
YAO’S MILLIONAIRES’ PROBLEM AND PUBLIC-KEY ENCRYPTION WITHOUT COMPUTATIONAL ASSUMPTIONS
一种有意思的想法
- 先在一不透光试管中加入未知量的水
- A女士量取她年龄那么多毫升的1mol/L的HCl 加入试管
- B女士量取她年龄那么多毫升的1mol/L的NaOH 加入试管
- 用Ph试纸就可判断谁的年龄大了
密码学协议举例[4]–秘密数字的比较(百万富翁问题)|文艺数学君