Why is this Elgamal Digital Signature Algorithm implemented in MATLAB not working properly? - public-key

%%%CODE:
clc;
disp('Implementation of ELGAMAL Digital Signature');
clear all;
close all;
%%Hardcoded values (vpi stands for VariablePrecisionIntegers, and are used to store large values)
g = vpi(5) %alpha in example
k = vpi(9) %Random number 1<k<p-1 and gcd(k, p − 1) = 1.
p = vpi(23) %Prime Number
x = vpi(3) %Secret Key 1 < x < p − 1
m = vpi(7) %Message
y = vpi(2)
r = vpi(2)
s = vpi(2)
%%Key Generation
y = powermod(g,x,p) %y = g^x mod p
%%Signature Generation
r = powermod(g,k,p) %r = g^k mod p
multinver = mulinv(9,23) %Generates multiplicative inverse k^-1 mod p
s = mod(((multinver)*(m-x*r)),p-1) %s = (k^-1)*(m-x*r) mod p-1
%%Verification
zvg = vpi(2);
zvg = powermod (g,m,p) %zvg = g^m mod p
zvyr = vpi(2);
zvyr = mod(((y^r)*(r^s)),p) %zvyr = y^r * r^s mod p
Output:
Implementation of ELGAMAL Digital Signature
g = 5
k = 9
p = 23
x = 3
m = 7
y = 2
r = 2
s = 2
y = 10
r = 11
multinver = 18
s = 16
zvg = 17
zvyr = 5
zvg should be equal to zvyr, but it's coming out wrong.

You are computing multinver wrong
It should be
multinver = mulinv(k,p-1) %Generates multiplicative inverse k^-1 mod (p-1)
Notice that we are computing the inverse mod (p-1) not mod (p).

Related

The dependency graph in MATLAB

I've got the following function:
phi0 = [0; 0]; %initial values
[T,PHI] = ode45(#eqn,[0, 10], phi0,odeset('RelTol',2e-13,'AbsTol',1e-100));
plot(T, PHI(:,1),'-b',T, PHI(:,2),'-g');
title('\it d = 0.1')
w1 = PHI(end,1)/(10000*2*pi) %the limit frequency for phi1
w2 = PHI(end,2)/(10000*2*pi) %the limit frequency for phi2
delta_w = w2 - w1
phi1_at_t_10k = PHI(end,1) %the value phi1(t=10000)
phi2_at_t_10k = PHI(end,2)
function dy_dt = eqn(t,phi)
d = 0.1; %synchronization parameter
n = 3;
g = [ 1.01; 1.02];
f = g-sin(phi/n);
exch = [d;-d]*sin(phi(2)-phi(1));
dy_dt = f+exch;
end
The w is calculated by the formula: w_i = (1/2pi)(lim((phi(t)-phi(0))/t) where t->infinity (here it's equal to 10000).
The question is how to plot the dependence of delta_w on different values of d (from d=0 to d=5 with step = 0.1)?
To collect summarize my comments:
First make the parameter d explicit in the ODE function
function dy_dt = eqn(t,phi,d)
n = 3;
g = [ 1.01; 1.02];
f = g-sin(phi/n);
exch = [d;-d]*sin(phi(2)-phi(1));
dy_dt = f+exch;
end
Then put the ODE integration and evaluation of the result in its own procedure
function delta_w = f(d)
phi0 = [0; 0]; %initial values
opts = odeset('RelTol',2e-13,'AbsTol',1e-100);
[T,PHI] = ode45(#(t,y)eqn(t,y,d), [0, 10], phi0, opts);
w1 = PHI(end,1)/(10000*2*pi); %the limit frequency for phi1
w2 = PHI(end,2)/(10000*2*pi); %the limit frequency for phi2
delta_w = w2 - w1;
end
And finally evaluate for the list of d values under consideration
d = [0:0.1:5];
delta_w = arrayfun(#(x)f(x),d);
plot(d,delta_w);
This should give a result. If it is not the expected one, further research into assumptions, equations and code is necessary.

Simulating waves in disc or circle form

I am getting an error when I run this code while selecting disc view or circle view option for wave simulation. The code and error are attached. I think there is some problem in this part of code typically in fzero function. Any help would be great.
Code:
function z = bjzeros(n,k)
% BJZEROS Zeros of the Bessel function.
% z = bjzeros(n,k) is the first k zeros of besselj(n,x)
% delta must be chosen so that the linear search can take
% steps as large as possible
delta = .99*pi;
Jsubn = inline('besselj(n,x)''x','n');
a = n+1;
fa = besselj(n,a);
z = zeros(1,k);
j = 0;
while j < k
b = a + delta;
fb = besselj(n,b);
if sign(fb) ~= sign(fa)
j = j+1;
z(j) = fzerotx(Jsubn,[a b],n);
end
a = b;
fa = fb;
end
Error:
Undefined function 'fzerotx' for input arguments of type 'inline'.
Error in waves>bjzeros (line 292)
z(j) = fzerotx(Jsubn,[a b],n);
Error in waves (line 137)
mu = [bjzeros(0,2) bjzeros(1,2)];
Function Declarations and Syntax
The fzerotx() function may not be declared. You can follow the file structure below to create the required M-files/functions in the same directory. Another small error may be caused by a missing comma, I got rid of the error by changing the line:
Jsubn = inline('besselj(n,x)''x','n');
to
Jsubn = inline('besselj(n,x)','x','n');
File 1: Main File/Function Call → [main.m]
mu = [bjzeros(0,2) bjzeros(1,2)];
File 2: bjzeros() Function → [bjzeros.m]
function z = bjzeros(n,k)
% BJZEROS Zeros of the Bessel function.
% z = bjzeros(n,k) is the first k zeros of besselj(n,x)
% delta must be chosen so that the linear search can take
% steps as large as possible
delta = .99*pi;
Jsubn = inline('besselj(n,x)','x','n');
a = n+1;
fa = besselj(n,a);
z = zeros(1,k);
j = 0;
while j < k
b = a + delta;
fb = besselj(n,b);
if sign(fb) ~= sign(fa)
j = j+1;
z(j) = fzerotx(Jsubn,[a b],n);
end
a = b;
fa = fb;
end
end
File 3: fzerotx() Function → [fzerotx.m]
Function Reference: MATLAB: Textbook version of FZERO
function b = fzerotx(F,ab,varargin)
%FZEROTX Textbook version of FZERO.
% x = fzerotx(F,[a,b]) tries to find a zero of F(x) between a and b.
% F(a) and F(b) must have opposite signs. fzerotx returns one
% end point of a small subinterval of [a,b] where F changes sign.
% Arguments beyond the first two, fzerotx(F,[a,b],p1,p2,...),
% are passed on, F(x,p1,p2,..).
%
% Examples:
% fzerotx(#sin,[1,4])
% F = #(x) sin(x); fzerotx(F,[1,4])
% Copyright 2014 Cleve Moler
% Copyright 2014 The MathWorks, Inc.
% Initialize.
a = ab(1);
b = ab(2);
fa = F(a,varargin{:});
fb = F(b,varargin{:});
if sign(fa) == sign(fb)
error('Function must change sign on the interval')
end
c = a;
fc = fa;
d = b - c;
e = d;
% Main loop, exit from middle of the loop
while fb ~= 0
% The three current points, a, b, and c, satisfy:
% f(x) changes sign between a and b.
% abs(f(b)) <= abs(f(a)).
% c = previous b, so c might = a.
% The next point is chosen from
% Bisection point, (a+b)/2.
% Secant point determined by b and c.
% Inverse quadratic interpolation point determined
% by a, b, and c if they are distinct.
if sign(fa) == sign(fb)
a = c; fa = fc;
d = b - c; e = d;
end
if abs(fa) < abs(fb)
c = b; b = a; a = c;
fc = fb; fb = fa; fa = fc;
end
% Convergence test and possible exit
m = 0.5*(a - b);
tol = 2.0*eps*max(abs(b),1.0);
if (abs(m) <= tol) | (fb == 0.0)
break
end
% Choose bisection or interpolation
if (abs(e) < tol) | (abs(fc) <= abs(fb))
% Bisection
d = m;
e = m;
else
% Interpolation
s = fb/fc;
if (a == c)
% Linear interpolation (secant)
p = 2.0*m*s;
q = 1.0 - s;
else
% Inverse quadratic interpolation
q = fc/fa;
r = fb/fa;
p = s*(2.0*m*q*(q - r) - (b - c)*(r - 1.0));
q = (q - 1.0)*(r - 1.0)*(s - 1.0);
end;
if p > 0, q = -q; else p = -p; end;
% Is interpolated point acceptable
if (2.0*p < 3.0*m*q - abs(tol*q)) & (p < abs(0.5*e*q))
e = d;
d = p/q;
else
d = m;
e = m;
end;
end
% Next point
c = b;
fc = fb;
if abs(d) > tol
b = b + d;
else
b = b - sign(b-a)*tol;
end
fb = F(b,varargin{:});
end
Ran using MATLAB R2019b

Implementing Euler's Method in GNU Octave

I am reading "Numerical Methods for Engineers" by Chapra and Canale. In it, they've provided pseudocode for the implementation of Euler's method (for solving ordinary differential equations). Here is the pseucode:
Pseucode for implementing Euler's method
I tried implementing this code in GNU Octave, but depending on the input values, I am getting one of two errors:
The program doesn't give any output at all. I have to press 'Ctrl + C' in order to break execution.
The program gives this message:
error: 'ynew' undefined near line 5 column 21
error: called from
Integrator at line 5 column 9
main at line 18 column 7
I would be very grateful if you could get this program to work for me. I am actually an amateur in GNU Octave. Thank you.
Edit 1: Here is my code. For main.m:
%prompt user
y = input('Initial value of y:');
xi = input('Initial value of x:');
xf = input('Final value of x:');
dx = input('Step size:');
xout = input('Output interval:');
x = xi;
m = 0;
xpm = x;
ypm = y;
while(1)
xend = x + xout;
if xend > xf
xend = xf;
h = dx;
Integrator(x,y,h,xend);
m = m + 1;
xpm = x;
ypm = y;
if x >= xf
break;
endif
endif
end
For Integrator.m:
function Integrator(x,y,h,xend)
while(1)
if xend - x < h
h = xend - x;
Euler(x,y,h,ynew);
y = ynew;
if x >= xend
break;
endif
endif
end
endfunction
For Euler.m:
function Euler(x,y,h,ynew)
Derivs(x,y,dydx);
ynew = y + dydx * h;
x = x + h;
endfunction
For Derivs.m:
function Derivs(x,y,dydx)
dydx = -2 * x^3 + 12 * x^2 - 20 * x + 8.5;
endfunction
Edit 2: I shoud mention that the differential equation which Chapra and Canale have given as an example is:
y'(x) = -2 * x^3 + 12 * x^2 - 20 * x + 8.5
That is why the 'Derivs.m' script shows dydx to be this particular polynomial.
Here is my final code. It has four different M-files:
main.m
%prompt the user
y = input('Initial value of y:');
x = input('Initial value of x:');
xf = input('Final value of x:');
dx = input('Step size:');
xout = dx;
%boring calculations
m = 1;
xp = [x];
yp = [y];
while x < xf
[x,y] = Integrator(x,y,dx,min(xf, x+xout));
m = m+1;
xp(m) = x;
yp(m) = y;
end
%plot the final result
plot(xp,yp);
title('Solution using Euler Method');
ylabel('Dependent variable (y)');
xlabel('Independent variable (x)');
grid on;
Integrator.m
%This function takes in 4 inputs (x,y,h,xend) and returns 2 outputs [x,y]
function [x,y] = Integrator(x,y,h,xend)
while x < xend
h = min(h, xend-x);
[x,y] = Euler(x,y,h);
end
endfunction
Euler.m
%This function takes in 3 inputs (x,y,h) and returns 2 outputs [x,ynew]
function [x,ynew] = Euler(x,y,h)
dydx = Derivs(x,y);
ynew = y + dydx * h;
x = x + h;
endfunction
Derivs.m
%This function takes in 2 inputs (x,y) and returns 1 output [dydx]
function [dydx] = Derivs(x,y)
dydx = -2 * x^3 + 12 * x^2 - 20 * x + 8.5;
endfunction
Your functions should look like
function [x, y] = Integrator(x,y,h,xend)
while x < xend
h = min(h, xend-x)
[x,y] = Euler(x,y,h);
end%while
end%function
as an example. Depending on what you want to do with the result, your main loop might need to collect all the results from the single steps. One variant for that is
m = 1;
xp = [x];
yp = [y];
while x < xf
[x,y] = Integrator(x,y,dx,min(xf, x+xout));
m = m+1;
xp(m) = x;
yp(m) = y;
end%while

Surface plot of equally spaced grid of overlapping Gaussians not plotting correctly?

I am attempting to make a surface plot in Octave based on a bunch of overlapping, offset curves. the function should be treating x and y identically but I am not seeing that in the final plot, rather it has ridges running along one axis. I cant tell if this is a plotting error or something else wrong with my code. am hoping you can provide some help/insight.
Plot with ridges following one axis
Thanks
clear;
graphics_toolkit gnuplot
V = 2500; %scan speed in mm
rr = 200000; %rep rate in Hz
Qsp = 1; %pulse energy
pi = 3.14159;
ns = 1; %scan number
w = 0.0125; %Gaussian radius in mm
dp = 0.0125; %lateral pulse distance in mm
dh = 0.0125; %hatch pitch in mm
xmax = 0.2; %ablation area x in mm
ymax = 0.2; %ablation area y in mm
np = round(xmax/dp);
nh = round(ymax/dh);
points = 50;
cof = ns*2*Qsp/(pi*w^2);
N = [0:1:np];
M = rot90([0:1:nh]);
for i = 1:points
for j = 1:points
x = (i-1)*xmax/(points-1);
y = (j-1)*ymax/(points-1);
k = exp(-(2*(x-N*dp).^2+(y-M*dh).^2)/(w^2));
H(i,j) = cof*sum(sum(k));
endfor
endfor
ii = [1:1:points];
jj = ii;
xx = (ii-1)*xmax/(points-1);
yy = (jj-1)*ymax/(points-1);
surf(xx,yy,H);
I find it helpful to space out components of complex expressions.
k = exp(-(2*(x-N*dp).^2+(y-M*dh).^2)/(w^2));
It's too hard to read with so many parentheses.
k = exp(
-(
2*(x-N*dp).^2 + (y-M*dh).^2
) / (w^2)
);
Do you see it already?
k = exp(
-(
2 * (x-N*dp).^2
+
(y-M*dh).^2
) / (w^2)
);
The x component gets multiplied by two, but the y component doesn't.

Fourier coefficients using matlab numerical integration

I have been trying to display the an and bn fourier coefficients in matlab but no success, I was able to display the a0 because that is not part of the iteration.
I will highly appreciate your help, below is my code
syms an;
syms n;
syms t;
y = sym(0);
L = 0.0005;
inc = 0.00001; % equally sample space of 100 points
an = int(3*t^2*cos(n*pi*t/L),t,-L,L)*(1/L);
bn = int(3*t^2*sin(n*pi*t/L),t,-L,L)*(1/L);
a0 = int(3*t^2,t,-L,L)*(1/L);
a0 = .5 *a0;
a0=a0
for i=1:5
y = subs(an, n, i)*cos(i*pi*t/0.0005)
z = subs(bn, n, i)*sin(i*pi*t/0.0005)
end
If everything you stated within your question is correct, I would tend to solve it this way:
clc, clear all,close all
L = 0.0005;
n = 5;
an = zeros(1,n);
bn = zeros(1,n);
for i = 1:5
f1 = #(t) 3.*(t.^2).*cos(i.*pi.*t./L);
f2 = #(t) 3.*(t.^2).*sin(i.*pi.*t./L);
an(i) = quad(f1,-L,L).*(1./L);
bn(i) = quad(f2,-L,L).*(1./L);
a0 = .5.*quad(#(t) 3.*t.^2,-L,L).*(1./L);
end
I hope this helps.