延續上一篇,提供另一個例子
有n個人共享秘密,有k個人聚在一起就能重建秘密
(%i1)
n:7;
k:5;
(%o1) 7
(%o2) 5
設定秘密S
(%i3) S:12345;
(%o3) 12345
任意取C(n,k-1)個質數
(%i4)
found:false$
m:binomial(n,k-1)$
while found=false do
(P:create_list(next_prime(random(S)),i,1,m),
P:unique(P),
if length(P)=m then
(found:true
)
)$
P;
(%o7) [97,157,251,347,379,557,1381,1889,3217,3527,3557,3989,4111,5381,5743,6089,6217,6547,6619,6701,
6883,8443,8501,8527,8779,8893,9371,10007,10079,10357,10391,10459,11239,12097,12323]
設定質數要給哪一位使用者
P1給V1,V2,V3,V4
P2給V1,V2,V3,V5
P3給V1,V2,V3,V6
...
(%i8)
create_list(i,i,1,n)$
setify(%)$
powerset(%,k-1)$
User:full_listify(%);
(%o11) [[1,2,3,4],[1,2,3,5],[1,2,3,6],[1,2,3,7],[1,2,4,5],[1,2,4,6],[1,2,4,7],
[1,2,5,6],[1,2,5,7],[1,2,6,7],[1,3,4,5],[1,3,4,6],[1,3,4,7],[1,3,5,6],
[1,3,5,7],[1,3,6,7],[1,4,5,6],[1,4,5,7],[1,4,6,7],[1,5,6,7],[2,3,4,5],
[2,3,4,6],[2,3,4,7],[2,3,5,6],[2,3,5,7],[2,3,6,7],[2,4,5,6],[2,4,5,7],
[2,4,6,7],[2,5,6,7],[3,4,5,6],[3,4,5,7],[3,4,6,7],[3,5,6,7],[4,5,6,7]]
將分配到的質數和秘密S乘起來當作子秘密Vi
(%i12)
V:create_list(S,i,1,n)$
for i:1 thru m do
(for user in User[ i ] do
(V[user]:V[user]*P[ i ]
)
)$
V;
(%o14)
[2027405439656203402483392037741077832451907091735220768078908930690305,
329395512180772665716441437042961317833047447165674231176372701471582355,
873365187527948903078917971136893483135753791078025669159186590065120719495,
38557189090206618525408472635427757365514394529139189207988367527928662521235,
1085040646795903444837598300544056739255857225025230153016414858410402872064055,
4518571371143044485194989367493800863732546105787200385445314138550958905680685,
34041447391883330084505313814699077342455987346908316001305119644995002433655445]
其中k個人聚在一起要重組秘密S
(%i15)
random_permutation(%)$
rest(%,n-k);
(%o16)
[34041447391883330084505313814699077342455987346908316001305119644995002433655445,
873365187527948903078917971136893483135753791078025669159186590065120719495,
1085040646795903444837598300544056739255857225025230153016414858410402872064055,
329395512180772665716441437042961317833047447165674231176372701471582355,
2027405439656203402483392037741077832451907091735220768078908930690305]
計算最大公因數得到秘密S
(%i17) lreduce(lambda([x,y], gcd(x,y)), %);
(%o17) 12345