Mental Poker

トランプのカードを配るときに不正をなくすみたいなやつ

https://link.springer.com/content/pdf/10.1007%2F3-540-39799-X_10.pdf

Coppersmith1986_Chapter_CheatingAtMentalPoker.pdf

Protocol A

from Crypto.Util.number import getPrime, inverse
from math import gcd
import random

p = getPrime(512)

while True:
    a = random.randint(2, p-1) # Alice's secret
    if gcd(a, p-1) == 1:
        break

while True:
    b = random.randint(2, p-1) # Bob's secret
    if gcd(b, p-1) == 1:
        break

ainv = inverse(a, p-1)
binv = inverse(b, p-1)

cards = [2+i for i in range(52)]

# Alice encrypts cards
cards = [pow(c, a, p) for c in cards]

# Alice sends cards to Bob

# Bob choices alice's hand and Bob's hand
hands = random.sample(cards, k=10)
alice_hand, bob_hand = hands[:5], hands[5:]

# Bob encrypts bob_hand
bob_hand = [pow(c, b, p) for c in bob_hand]

# Bob sends alice_hand and bob_hand to alice

# Alice decrypts both hands
alice_hand = [pow(c, ainv, p) for c in alice_hand]
bob_hand = [pow(c, ainv, p) for c in bob_hand]

# Alice sends bob_hand to Bob


# Bob decrypt his hand
bob_hand = [pow(c, binv, p) for c in bob_hand]

print(alice_hand)
print(bob_hand)