寫在前面:html
任何大師所發明的都是通用的規律,而不是賦予表象的技巧:python
高斯的「正十七邊形尺規做圖法」是表象,而尺規做圖的加法器、乘法器、開根器是通用規律,更核心的是如何將正多邊形的角度轉換成開根式。ios
廢話很少,進入正題:bash
運行結果:dom
**************************************************** 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
實現代碼:wordpress
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'
參考:spa
Andrew Yao’s Millionaire’s Problemcode
Explain it like I’m Five: The Socialist Millionaire Problem and Secure Multi-Party Computationhtm
YAO’S MILLIONAIRES’ PROBLEM AND PUBLIC-KEY ENCRYPTION WITHOUT COMPUTATIONAL ASSUMPTIONS
一種有意思的想法
- 先在一不透光試管中加入未知量的水
- A女士量取她年齡那麼多毫升的1mol/L的HCl 加入試管
- B女士量取她年齡那麼多毫升的1mol/L的NaOH 加入試管
- 用Ph試紙就可判斷誰的年齡大了