具体秘密共享的流程可以参考下面这个链接的内容:https://blog.csdn.net/Matrix_element/article/details/117357359
main1.m
加法方案的主函数:确认门限(代码里(5,3))以及对应的两个多项式(代码对应的多项式是y1 = 2 * x ^ 2 - 3 * x + 4, y2 = x ^ 2 + 5 x - 6,所以通过多项式分发给每个人两个值,最后通过3个人的子秘密和恢复出4 + -6 = - 2)-> 给每个人分发子秘密值 -> 通过3个人的子秘密值和使用拉格朗日恢复出-2(两个多项式的常数项和)
%%%加法方案
%%分发
% 首先确定方案门限值
a = [5,3];
% 输入多项式的参数,生成每个人相应的密钥
% coef1 = [2,-3,4;
% 1,5,-6];%自己定义多项式系数矩阵
coef1 = ceil(rand(2,a(2))*10)%随机生成多项式系数矩阵
s = Sharing(a, coef1);
add_s = sum(s,2);
%%恢复
%P = [1,3,5];
P = [1,add_s(1);
3,add_s(3);
5,add_s(5)];
y = Recover(a,P)
% %恢复加法秘密值
% ab=0;
% ab = add_s(1) * ( (0-P(2)) * (0-P(3)) ) / ( (P(1)-P(2)) * (P(1)-P(3)) ) + ...
% add_s(3) * ( (0-P(1)) * (0-P(3)) ) / ( (P(2)-P(1)) * (P(2)-P(3)) ) + ...
% add_s(5) * ( (0-P(1)) * (0-P(2)) ) / ( (P(3)-P(1)) * (P(3)-P(2)) )
% %恢复多项式
% syms x;
% y = add_s(1) * ( (x-P(2)) * (x-P(3)) ) / ( (P(1)-P(2)) * (P(1)-P(3)) ) + ...
% add_s(3) * ( (x-P(1)) * (x-P(3)) ) / ( (P(2)-P(1)) * (P(2)-P(3)) ) + ...
% add_s(5) * ( (x-P(1)) * (x-P(2)) ) / ( (P(3)-P(1)) * (P(3)-P(2)) )
% y = expand(y)
main2.m
乘法方案主函数:确认门限以及对应的两个多项式-> 给每个分发子秘密值 -> 每个人再通过分发的子秘密值乘积为常数项生成自己的多项式->用自己的多项式给每个人再次进行分发(代码里的C矩阵)->计算lamda向量(这个每个人都可以计算) ->根据计算的lamda向量和每个人手里拥有的向量求每个人的最终拥有的子秘密值-> 通过3个人的子秘密值和使用拉格朗日恢复出-24(两个多项式的常数项积)
%%%乘法
%%一、首先确定方案门限值
a = [5,3];
% 输入多项式的参数,生成每个人相应的密钥
%coef1 = ceil(rand(2,a(2))*10)%随机生成矩阵值
coef1 = [2,-3,4;
1,5,-6];%或者自定义矩阵
s = Sharing(a, coef1);
mul_s = prod(s,2);
%%二、每个P根据自己得到的d生成自己的n-1次多项式,并分发给每个参与者
%coef_H = [ceil(rand(a(1),a(2)-1)*100),mul_s]%随机生成多项式系数矩阵
coef_H = [1,-1,mul_s(1);
1,-10,mul_s(2);
1,-100,mul_s(3);
1,-200,mul_s(4);
1,-300,mul_s(5)];%或者自定义多项式系数矩阵
%计算C矩阵
CMatrix = CalculateCMatrix1(a,coef_H)
%%三、计算λ矩阵,每个人Pi根据自己得到的5个cji(1 <= j <= 5)
lamda = zeros(1,a(1));
M = zeros(a(1),a(1));
m = [1:1:a(1)]';
for i = 1:a(1)
M(:,i) = m.^(i-1)
end
% M = [1,1,1,1,1;
% 1,2,2^2,2^3,2^4;
% 1,3,3^2,3^3,3^4;
% 1,4,4^2,4^3,4^4;
% 1,5,5^2,5^3,5^4];
lamda = [1,0,0,0,0] * inv(M);
%每个人根据自己得到的Cji计算C值
C = lamda * CMatrix;
%%四、进行恢复(比如满足1,3,5人),根据这几个人计算得到的C值进行恢复
P = [1,3,5];
ab=0;
ab = C(P(1)) * ( (0-P(2)) * (0-P(3)) ) / ( (P(1)-P(2)) * (P(1)-P(3)) ) + ...
C(P(2)) * ( (0-P(1)) * (0-P(3)) ) / ( (P(2)-P(1)) * (P(2)-P(3)) ) + ...
C(P(3)) * ( (0-P(1)) * (0-P(2)) ) / ( (P(3)-P(1)) * (P(3)-P(2)) )
%恢复多项式
syms x;
y = C(P(1)) * ( (x-P(2)) * (x-P(3)) ) / ( (P(1)-P(2)) * (P(1)-P(3)) ) + ...
C(P(2)) * ( (x-P(1)) * (x-P(3)) ) / ( (P(2)-P(1)) * (P(2)-P(3)) ) + ...
C(P(3)) * ( (x-P(1)) * (x-P(2)) ) / ( (P(3)-P(1)) * (P(3)-P(2)) );
y = expand(y)
Sharing.m
function [s] = Sharing(a,b)
%计算分发给每个人的密钥
poly_num = size(b,1);%多项式个数
s = zeros(a(1),poly_num);
max = size(b,2);%每个多项式项数
for x = 1:a(1)
for i = 1:poly_num
for j = 0:max-1
s(x,i) = s(x,i) + b(i,max-j) * x^j;
end
% s(x,1) = b(1,1) * x^2 + b(1,2) * x + b(1,3);
% s(x,2) = b(2,1) * x^2 + b(2,2) * x + b(2,3);
end
end
CalculateCMatrix.m
计算c矩阵方法1
function [s] = CalculateCMatrix(a,coef_H)
% s = zeros(a(1),a(1));
% max = size(coef_H,2);
% for i = 1:a(1)
% for j = 1:a(1)
% for t = 0:max-1
% s(i,j) = s(i,j) + coef_H(i,max-t) * j^t;
% end
% end
% end
M = zeros(a(2),a(1));
m = [1:1:a(1)];
for i = a(2):-1:1
M(i,:) = m.^(a(2)-i);
end
s = coef_H * M;
end
方法2:
function [C] = CalculateCMatrix(a,coef_H)
C = zeros(a(1),a(1));
for i = 1:a(1)
for j = 1:a(1)
C(i,j) = coef_H(i,1) * j^2 + coef_H(i,2) * j + coef_H(i,3);
end
end
end
Recover.m
朗格朗日恢复
function [y] = Recover(a,P)
num = size(P,1);
y = 0;
if num
以上函数可以将整个流程进行实现。接下来使用matlab appdesigner调用上面函数进行可视化实现,整个流程更加清晰明了。
classdef SharingSecret < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ShamirLabel matlab.ui.control.Label
Panel matlab.ui.container.Panel
Button matlab.ui.control.Button
nSpinnerLabel matlab.ui.control.Label
nSpinner matlab.ui.control.Spinner
mSpinnerLabel matlab.ui.control.Label
mSpinner matlab.ui.control.Spinner
nmEditFieldLabel matlab.ui.control.Label
nmEditField matlab.ui.control.EditField
Panel_2 matlab.ui.container.Panel
ButtonGroup_2 matlab.ui.container.ButtonGroup
Button_4 matlab.ui.control.RadioButton
Button_5 matlab.ui.control.RadioButton
UITable matlab.ui.control.Table
Button_6 matlab.ui.control.Button
UITable2 matlab.ui.control.Table
Panel_3 matlab.ui.container.Panel
Label_4 matlab.ui.control.Label
UITable2_3 matlab.ui.control.Table
UITable2_4 matlab.ui.control.Table
CLabel matlab.ui.control.Label
CLabel_2 matlab.ui.control.Label
UITable2_5 matlab.ui.control.Table
ButtonGroup_3 matlab.ui.container.ButtonGroup
Button_9 matlab.ui.control.RadioButton
Button_10 matlab.ui.control.RadioButton
Button_11 matlab.ui.control.Button
Button_12 matlab.ui.control.Button
Panel_4 matlab.ui.container.Panel
TextArea matlab.ui.control.TextArea
Button_8 matlab.ui.control.Button
Button_7 matlab.ui.control.Button
EditFieldLabel matlab.ui.control.Label
iEditField2 matlab.ui.control.NumericEditField
iEditFieldLabel matlab.ui.control.Label
iEditField matlab.ui.control.NumericEditField
UITable3 matlab.ui.control.Table
Button_14 matlab.ui.control.Button
Button_13 matlab.ui.control.Button
end
properties (Access = private)
a;%门限
flag_trapdoor = 0; %门限是否设置成功
flag_share = 0; %分发是否成功
s;%给每个人分发的秘密值
s_add;%加法方案分配给每个人的值求和
s_mul;%乘法方案分配给每个人的值求积
end
methods (Access = private)
% Button pushed function: Button
function Trapdoor(app, event)
temp1 = app.nSpinner.Value;
temp2 = app.mSpinner.Value;
app.a = [temp1,temp2];
if temp2 > temp1
msgbox("解密人数>总人数,重新输入","提示");
elseif temp2 <= 1
msgbox("解密人数需大于1","提示");
else
app.nmEditField.Value = "(" + app.a(1) + "," + app.a(2) + ")";
app.flag_trapdoor = 1;
temp = char();
for i = 1:app.a(1)
temp = [temp;sprintf('第%3d个人',i)];
end
app.UITable2.ColumnName = temp;
app.UITable2_3.ColumnName = temp;
app.UITable2_4.ColumnName = temp;
app.UITable2_5.ColumnName = temp;
% c = char();
% for i = 1:app.a(2)
% if i==1
% c = [' 常数项 ';c];
% else
% c = [sprintf('%3d次项系数 ',i-1);c];
% end
%
% end
% app.UITable2_3.RowName = c;
%app.ButtonGroup_2.SelectedObject.Value = 0;
end
end
% Callback function: ButtonGroup_2, UITable2
function ButtonGroup_2SelectionChanged(app, event)
selectedButton = app.ButtonGroup_2.SelectedObject;
if app.flag_trapdoor == 0
msgbox("请先输入门限值","提示");
else
c = char();
for i = 1:app.a(2)
if i==1
c = [' 常数项 ';c];
else
c = [sprintf('%3d次项系数 ',i-1);c];
end
end
app.UITable.RowName = c;
b = zeros(app.a(2),2);
app.UITable.Data = b;
switch selectedButton.Text
case "自定义多项式系数"
%msgbox('1');
app.UITable.Data = b;
case "随机生成"
%msgbox('2');
%app.UITable.RowName = c;
%app.UITable.Data = ceil(rand(2,app.a(2))*10)';
app.UITable.Data = randi([-10 10],app.a(2),2);
app.flag_share = 1;
end
end
end
% Cell edit callback: UITable
function UITableCellEdit(app, event)
end
% Button pushed function: Button_6
function Button_6Pushed(app, event)
app.s = Sharing(app.a, app.UITable.Data');
app.UITable2.Data = app.s';
app.s
app.s_add = sum(app.s, 2);
app.s_mul = prod(app.s, 2);
end
% Button pushed function: Button_7
function Button_7Pushed(app, event)
%t;
temp = [app.iEditField.Value,app.iEditField2.Value];
app.UITable3.Data = [app.UITable3.Data;temp];
end
% Button pushed function: Button_8
function Button_8Pushed(app, event)
if size(app.UITable3.Data,1)
加法流程:
乘法流程:
(代码实现可能可以通过matlab的函数实现更简单的实现,自己实现和可视化是一天内现学的所以可能有些缺点,但是大致流程是这样的。代码包和生成的.exe文件上传在了我的资源里以及github(https://github.com/xuekema/shamirSecretSharing))



