Is there any code or command for doubling a point ?
이전 댓글 표시
I have an elliptic curve y*2=x*3+148x+225 mod 5003 I took G=(1355,2421) as the shared key I want to find points as (G,2G,3G,4G,......5003G)
채택된 답변
추가 답변 (4개)
Bruno Luong
2018년 10월 26일
EL = struct('a', 148, 'b', 225, 'p', 5003);
% Point
G = [1355,2421];
% Compute C*G for C=1,2,...,maxC
maxC = 5003;
maxk = nextpow2(maxC);
CG = zeros(maxC,2);
j = 1;
CG(j,:) = G;
G2k = G;
% precompute the inverse of 1...p-1, and stores in table itab
p = EL.p;
itab = p_inverse(1:p-1, p);
for k=1:maxk
for i=1:j-1
j = j+1;
CG(j,:) = EL_add(G2k,CG(i,:),EL,itab);
if j == maxC
break
end
end
if j == maxC
break
end
G2k = EL_add(G2k,G2k,EL,itab);
j = j+1;
CG(j,:) = G2k;
end
CG
function ia = p_inverse(a, p)
[~,ia] = gcd(a,p);
end
function R = EL_add(P,Q,EL,itab)
% R = ELadd(P,Q,EL,itab)
% Perform addition: R = P + Q on elliptic curve
% P, Q, R are (1x2) arrays of integers in [0,p) or [Inf,Inf] (null element)
% (EL) is a structure with scalar fields a, b, p.
% Together they represent the elliptic curve y^2 = x^3 + a*x + b on Z/pZ
% p is prime number
% itab is array of length p-1, inverse of 1,....,p-1 in Z/pZ
% WARNING: no overflow check, work on reasonable small p only
if ELiszero(P)
R = Q;
elseif ELiszero(Q)
R = P;
else
p = EL.p;
xp = P(1);
yp = P(2);
xq = Q(1);
yq = Q(2);
d = xq-xp;
if d ~= 0
n = yq-yp;
else
if yp == yq
d = 2*yp;
n = 3*xp*xp + EL.a;
else % P == -Q
R = [Inf,Inf];
return
end
end
invd = itab(mod(d,p)); % [~,invd,~] = gcd(d,p);
lambda = mod(n*invd,p); % slope
xr = lambda*lambda - xp - xq;
yr = lambda*(xp-xr) - yp;
R = mod([xr, yr],p);
end
end
function b = ELiszero(P)
% Check if the EL point is null-element
b = any(~isfinite(P));
end
댓글 수: 11
Maria Hameed
2018년 10월 26일
Bruno Luong
2018년 10월 26일
You seem using older MATLAB release.
Then save the 3 functions p_inverse, EL_add, ELiszero in separate mfiles.
Maria Hameed
2018년 10월 26일
Bruno Luong
2018년 10월 26일
Not at all. I don't know why and where you get an idea to put this curly bracket.
Do you know what is an mfile? A MATLAB script? A function? Have you ever working with MATLAB? Please read the Doc if it's not clear for you.
Maria Hameed
2018년 10월 26일
Bruno Luong
2018년 10월 26일
편집: Bruno Luong
2018년 10월 26일
And function? Do you know how to put a function to an mfile?
Maria Hameed
2018년 10월 27일
Bruno Luong
2018년 10월 27일
편집: Bruno Luong
2018년 10월 27일
Open MATLAB editor (type "edit" in command line)
Use the mouse copy one of the function above (from function ... to ... end closing the body) and past to the editor (the "Untitle" tab).
Click on [Save] button then when asked give the same name than the function name.
Do this for the three functions p_inverse, EL_add, ELiszero I instruct you.
Cut the functions text to keep just the calling commands in the script.
If you still have problem ask someone who knows MATLAB around you.
Ammy
2022년 2월 21일
Dear @Bruno Luong I have tried the above code for some larger p as compared to above defined p=5003,
I have tried the following
for p=100019, a=0 , b=2, the above code generates all the point correctly, there is no issue.
But in any of the following I couldn't generate the correct points,
- p=957221, a=0 , b=2, its generator G=(762404,61090)
- p=997247, a=0 , b=2, its generator G=(386850,53128)
May I request for your help in this regard?
Bruno Luong
2022년 2월 21일
As stated in my code, for illustration only, there is no careful check for overflow of calculation. This code is more robust but still not bulet-proof
EL = struct('a', 0, 'b', 2, 'p', 957221);
% Point
G = [762404,61090];
% Compute C*G for C=1,2,...,maxC
maxC = 5003;
maxk = nextpow2(maxC);
CG = zeros(maxC,2);
j = 1;
CG(j,:) = G;
G2k = G;
% precompute the inverse of 1...p-1, and stores in table itab
p = EL.p;
itab = p_inverse(1:p-1, p);
for k=1:maxk
for i=1:j-1
j = j+1;
CG(j,:) = EL_add(G2k,CG(i,:),EL,itab);
if j == maxC
break
end
end
if j == maxC
break
end
G2k = EL_add(G2k,G2k,EL,itab);
j = j+1;
CG(j,:) = G2k;
end
CG
function ia = p_inverse(a, p)
[~,ia] = gcd(a,p);
end
function R = EL_add(P,Q,EL,itab)
% R = ELadd(P,Q,EL,itab)
% Perform addition: R = P + Q on elliptic curve
% P, Q, R are (1x2) arrays of integers in [0,p) or [Inf,Inf] (null element)
% (EL) is a structure with scalar fields a, b, p.
% Together they represent the elliptic curve y^2 = x^3 + a*x + b on Z/pZ
% p is prime number
% itab is array of length p-1, inverse of 1,....,p-1 in Z/pZ
% WARNING: no overflow check, work on reasonable small p only
if ELiszero(P)
R = Q;
elseif ELiszero(Q)
R = P;
else
p = EL.p;
xp = P(1);
yp = P(2);
xq = Q(1);
yq = Q(2);
d = xq-xp;
if d ~= 0
n = yq-yp;
else
if yp == yq
d = 2*yp;
n = 3*xp*xp + EL.a;
else % P == -Q
R = [Inf,Inf];
return
end
end
d = mod(d,p);
n = mod(n,p);
invd = itab(d); % [~,invd,~] = gcd(d,p);
lambda = mod(n*invd,p); % slope
xr = lambda*lambda - xp - xq;
xr = mod(xr,p);
yr = lambda*(xp-xr) - yp;
yr = mod(yr,p);
R = [xr, yr];
end
end
function b = ELiszero(P)
% Check if the EL point is null-element
b = any(~isfinite(P));
end
Ammy
2022년 2월 21일
KSSV
2018년 10월 23일
G=[1355,2421] ;
P = 1:1:5003 ;
Q = P'.*G ;
댓글 수: 8
Maria Hameed
2018년 10월 23일
KSSV
2018년 10월 23일
It is not showing any error in my pc.
KSSV
2018년 10월 23일
G=[1355,2421] ;
P = 1:1:5003 ;
Q = zeros(numel(P),2) ;
for i = 1:numel(P)
Q(i,:) = P(i)*G ;
end
Maria Hameed
2018년 10월 24일
Maria Hameed
2018년 10월 24일
Maria Hameed
2018년 10월 24일
Walter Roberson
2018년 10월 24일
Should the definition of s really divide by 2 and multiply the results by y, or should it be dividing by (2*y)?
Maria Hameed
2018년 10월 24일
Bruno Luong
2018년 10월 23일
0 개 추천
I reiterate my answer previously, you need first to program the "+" operator for EL, then doubling point 2*Q is simply Q "+" Q.
카테고리
도움말 센터 및 File Exchange에서 Special Values에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!