<jupyter_start><jupyter_text>ディーラ型の秘密分散<jupyter_code>import random

print("----- #0 init -----")
p = 9999991
n = 10
m = 5
print("p = {}".format(p))
print("n = {}".format(n))
print("m = {}".format(m))

print("----- #1 secret & random")
a = [0]*(m)
for i in range(m):
  a[i] = random.randint(2, p-1)
print("a = {}".format(a))
s = a[0]
print("s = {}".format(s))

print("----- #2 f(x)")
def f(x, p):
  if x == 0:
      return 0
  y = 0
  for i in range(len(a)):
      y += a[i]*pow(x, i, p)
  return y % p

print("----- #3 point")
class point:
  def __init__(self, x, y):
      self.x = x
      self.y = y

  def __str__(self):
      return "({},{})".format(self.x, self.y)



print("----- #4 dealer -> player")
u = [0]*n
for i in range(n):
  u[i] = point(i+1, f(i+1, p))
  print("{} -> player{}".format(u[i], i+1))

print("----- #5 Make secret")
for i in range(n-m):
  del u[random.randint(0, len(u)-1)]
for i in range(len(u)):
  print("player{} : {}".format(u[i].x, u[i]))

print("----- #6 Lagrange")
def Lagrange(p, u):
  s = 0
  for i in range(len(u)):
      t = u[i].y
      for j in range(len(u)):
          if i != j:
              t *= u[j].x*pow(u[j].x-u[i].x, p-2, p)
      s += t % p
      s = s % p
  return s

print("----- #7 Result")
l = Lagrange(p, u)
print("s = {}".format(l))

if s == l:
  print("Match (^^)")
else:
  print("Unmatch (TT)")<jupyter_output>----- #0 init -----
p = 9999991
n = 10
m = 5
----- #1 secret & random
a = [7640856, 7821422, 7161272, 5618458, 1629954]
s = 7640856
----- #2 f(x)
----- #3 point
----- #4 dealer -> player
(1,9871980) -> player1
(2,2955824) -> player2
(3,9281543) -> player3
(4,357269) -> player4
(5,6810012) -> player5
(6,8385759) -> player6
(7,3949411) -> player7
(8,1484792) -> player8
(9,8094658) -> player9
(10,724) -> player10
----- #5 Make secret
player1 : (1,9871980)
player6 : (6,8385759)
player8 : (8,1484792)
player9 : (9,8094658)
player10 : (10,724)
----- #6 Lagrange
----- #7 Result
s = 7640856
Match (^^)
{'fraction_non_alphanumeric': 0.26131164209456026, 'fraction_numerical': 0.11692933401118455, 'mean_word_length': 2.611009174311927, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '264525', 'license': 'MIT', 'n_tokens_mistral': 995, 'n_tokens_neox': 883, 'n_words': 227, 'path': 'SecretSharingDemo.ipynb', 'repo_name': 'tnakagawa/pyref', 'size': '1967'}