Calculus: From Limits to Cryptography

Trigonometric functions, rigorous proofs, and GNU Octave labs

How to read this. Calculus is the language of change. We start with limits, build the derivative and integral, master trigonometry, then see why analysis matters even when cryptography lives in discrete worlds. Work the Octave labs as you read — computing makes the theory stick.

1. Foundations

A limit captures the idea of "arbitrarily close". Intuitively, $\lim_{x\to a} f(x)=L$ means we can make $f(x)$ as close to $L$ as we wish by taking $x$ sufficiently close (but not equal) to $a$.

Formal ($\varepsilon$-$\delta$): $$ \lim_{x\to a} f(x)=L \iff \forall \varepsilon>0\ \exists \delta>0 \text{ such that } 0<|x-a|<\delta \Rightarrow |f(x)-L|<\varepsilon. $$ This precise game is why calculus is provable, not just plausible.

Key limits you will use constantly:

  • $\displaystyle \lim_{x\to 0}\frac{\sin x}{x}=1$ (radians!)
  • $\displaystyle \lim_{x\to 0}\frac{1-\cos x}{x}=0$, and $\displaystyle \lim_{x\to 0}\frac{1-\cos x}{x^2}=\frac12$
  • $\displaystyle \lim_{x\to 0}\frac{e^x-1}{x}=1$, $\displaystyle \lim_{x\to 0}(1+x)^{1/x}=e$

Continuity. $f$ is continuous at $a$ if $\lim_{x\to a}f(x)=f(a)$. Sums, products, compositions of continuous functions stay continuous. On a closed interval, a continuous function attains a max and min (Extreme Value Theorem) and hits every intermediate value (IVT).

Proof that $\lim_{x\to0}\sin x / x = 1$

Work in radians. Consider a unit circle sector with central angle $0

  1. Small triangle $OAB$: $\tfrac12 \sin x$
  2. Sector $OAB$: $\tfrac12 x$
  3. Large triangle $OAC$: $\tfrac12 \tan x$

Geometrically, $\tfrac12\sin x < \tfrac12 x < \tfrac12\tan x$. Divide by $\tfrac12\sin x>0$: $1 < \tfrac{x}{\sin x} < \tfrac1{\cos x}$. Taking reciprocals, $\cos x < \tfrac{\sin x}{x} < 1$. As $x\to0^+$, $\cos x\to1$, so by the squeeze theorem the middle tends to 1. Evenness gives the left-hand limit too.

Why radians are forced

The sector area formula $A=x/2$ is true only when $x$ is in radians. In degrees, $A=\pi x/360$, and the limit becomes $\pi/180$. Calculus chooses the unit that makes derivatives clean.

2. The Derivative

The derivative is the instantaneous rate of change:

$$ f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}{h} $$

when the limit exists. Geometrically, it's the slope of the tangent.

Core rules (with proof sketches)

  • Linearity: $(af+bg)'=af'+bg'$. Follows directly from limit laws.
  • Product: $(fg)'=f'g+fg'$. Add and subtract $f(x+h)g(x)$ in the numerator, factor, take limits.
  • Quotient: $\left(\frac{f}{g}\right)'=\frac{f'g-fg'}{g^2}$ ($g\neq0$). Apply product to $f\cdot g^{-1}$.
  • Chain: $(f\circ g)'(x)=f'(g(x))g'(x)$. Instantaneous change multiplies: a small change $h$ in $x$ changes $g$ by $\approx g'h$, then $f$ by $\approx f'\cdot(g'h)$.
Numerical derivative in Octave (central difference):
fprime = @(f,x) (f(x+1e-8)-f(x-1e-8))/2e-8;
fprime(@sin, 1)   % ≈ cos(1)
Central differencing is $O(h^2)$ accurate; it's the workhorse before symbolic differentiation.

3. Trigonometric Functions

Define on the unit circle: for angle $\theta$ measured counterclockwise from $(1,0)$, the point is $(\cos\theta,\sin\theta)$. Then $\tan\theta=\sin\theta/\cos\theta$, $\sec=1/\cos$, etc. Periodicity: $\sin(\theta+2\pi)=\sin\theta$, $\cos$ likewise; $\tan$ has period $\pi$.

Essential identities: $\sin^2x+\cos^2x=1$, $\sin(a\pm b)=\sin a\cos b\pm\cos a\sin b$, $\cos(a\pm b)=\cos a\cos b\mp\sin a\sin b$.

Derivatives

Using $\sin(x+h)=\sin x\cos h+\cos x\sin h$:

$$ \frac{\sin(x+h)-\sin x}{h} = \sin x\frac{\cos h-1}{h} + \cos x\frac{\sin h}{h} \to \cos x $$ since $(\cos h-1)/h\to0$ and $\sin h/h\to1$. Thus $(\sin x)'=\cos x$.

Similarly $(\cos x)'=-\sin x$. Then $$ (\tan x)'=\left(\frac{\sin x}{\cos x}\right)'=\frac{\cos^2x+\sin^2x}{\cos^2x}=\sec^2x. $$ By quotient rule: $$ (\sec x)'=\sec x\tan x,\quad (\csc x)'=-\csc x\cot x,\quad (\cot x)'=-\csc^2 x. $$

Integrals

Reversing derivatives (up to constant $C$):

$$ \int \sin x\,dx=-\cos x+C,\ \int\cos x\,dx=\sin x+C,\ \int\sec^2x\,dx=\tan x+C $$ $$ \int\sec x\tan x\,dx=\sec x+C,\ \int\csc^2x\,dx=-\cot x+C $$
Function $f$Derivative $f'$Antiderivative $\int f$
$\sin x$$\cos x$$-\cos x$
$\cos x$$-\sin x$$\sin x$
$\tan x$$\sec^2 x$$-\ln|\cos x|$
$\sec x$$\sec x\tan x$$\ln|\sec x+\tan x|$
$\arcsin x$$1/\sqrt{1-x^2}$$x\arcsin x+\sqrt{1-x^2}$
$\arctan x$$1/(1+x^2)$$x\arctan x-\tfrac12\ln(1+x^2)$

4. Integration

An antiderivative $F$ satisfies $F'=f$. The definite integral is the signed area:

$$ \int_a^b f(x)\,dx = \lim_{\|P\|\to0}\sum_{i=1}^n f(x_i^*)\,\Delta x_i $$

a limit of Riemann sums.

Fundamental Theorem of Calculus

Part 1: If $f$ is continuous and $F(x)=\int_a^x f(t)dt$, then $F'(x)=f(x)$. Intuitively, the rate at which area accumulates equals the height.

Part 2: If $F'=f$, then $\int_a^b f(x)dx = F(b)-F(a)$. Proof: Part 1 says $G(x)=\int_a^x f$ is an antiderivative; any two antiderivatives differ by a constant, evaluate at endpoints.

Techniques

  • Substitution: $\int f(g(x))g'(x)dx = \int f(u)du$. Essential for trig: $\int 2x\cos(x^2)dx=\sin(x^2)+C$.
  • Integration by parts: $\int u\,dv = uv-\int v\,du$, from product rule. Example: $\int x\sin x\,dx = -x\cos x+\sin x+C$.
  • Trig integrals: Use identities. $\int \sin^m x\cos^n x\,dx$ — save a factor for substitution when an exponent is odd; use power-reduction when both even.
  • Trig substitution: $\sqrt{a^2-x^2}\to x=a\sin\theta$, $\sqrt{a^2+x^2}\to x=a\tan\theta$, $\sqrt{x^2-a^2}\to x=a\sec\theta$. This removes the root via $\sin^2+\cos^2=1$.

5. Series and Approximations

If $f$ is infinitely differentiable at $a$,

$$ f(x)=\sum_{n=0}^\infty \frac{f^{(n)}(a)}{n!}(x-a)^n + R_N(x) $$

with remainder $R_N\to0$ in the interval of convergence.

Maclaurin series ($a=0$):

$$ e^x = \sum_{n=0}^\infty \frac{x^n}{n!},\quad \sin x = \sum_{n=0}^\infty (-1)^n\frac{x^{2n+1}}{(2n+1)!}=x-\frac{x^3}{6}+\frac{x^5}{120}-\cdots $$ $$ \cos x = \sum_{n=0}^\infty (-1)^n\frac{x^{2n}}{(2n)!}=1-\frac{x^2}{2}+\frac{x^4}{24}-\cdots $$

Differentiate term-by-term: $(\sin)'$ series becomes $\cos$ series, confirming $(\sin)'=\cos$ analytically.

Euler's formula

Plug $ix$ into $e^x$ series and group even/odd terms:

$$ e^{ix}=\sum\frac{(ix)^n}{n!}= \sum (-1)^n\frac{x^{2n}}{(2n)!}+i\sum(-1)^n\frac{x^{2n+1}}{(2n+1)!}=\cos x + i\sin x. $$

This unifies exponentials and trigonometry — the beating heart of Fourier analysis and many cryptographic number-theoretic transforms.

6. Mathematics and Cryptography of Calculus

Modern cryptography (RSA, ECC, symmetric ciphers) lives in finite rings and fields, where "derivative" has no direct meaning. Yet calculus provides the analytical infrastructure: approximations, optimization, continuous models of discrete phenomena, and the geometry that inspires elliptic curve arithmetic.

a) Continuous vs Discrete

Calculus analyzes functions $\mathbb{R}\to\mathbb{R}$ with limits. RSA uses $c\equiv m^e\pmod N$ in $\mathbb{Z}_N$. You cannot differentiate $m\mapsto m^e\bmod N$ — modular reduction is discontinuous. But analysis tells us how big $m^e$ is over the integers before reduction, which explains why low-exponent RSA with small $m$ and no padding is vulnerable (the real cube root works).

b) Calculus for cryptanalysis

Newton's method. To solve $f(x)=x^e-c=0$ over reals, iterate $x_{k+1}=x_k - f/f'$. This converges quadratically because $f'$ tells the slope. For $e=3$, after a few iterations you get the integer $m$ if $m^3=c

Gradient descent and side channels. In power analysis, an attacker minimizes a loss $L(\theta)=\| \text{trace} - \text{model}(\theta)\|^2$ over key hypotheses $\theta$. The update $\theta\leftarrow\theta-\alpha\nabla L$ uses calculus to climb down the error surface, extracting bits from noisy measurements.

c) Information theory and entropy

For a continuous random variable with density $p(x)$, differential entropy is

$$ h(X)=-\int_{-\infty}^{\infty} p(x)\log_2 p(x)\,dx. $$

Measured in bits, this integral quantifies uncertainty. In cryptography we prefer min-entropy $H_\infty=-\log_2\max_x p(x)$, but calculus lets us approximate leakage when side-channels yield Gaussian noise.

d) "Differential" cryptanalysis

The name is coincidental but conceptually parallel. Instead of $f'=\lim \Delta f/\Delta x$, cryptanalysts study the propagation of discrete differences $\Delta Y = S(X\oplus\Delta)\oplus S(X)$ through S-boxes. Sensitivity to input change — the discrete analogue of a derivative — determines security.

e) Elliptic curves: calculus in plain sight

Over reals, $E: y^2 = x^3 + ax + b$. Implicit differentiation: $2y\,dy = (3x^2+a)dx$, so slope of tangent at $P=(x_1,y_1)$ is

$$ \lambda = \frac{dy}{dx} = \frac{3x_1^2 + a}{2y_1}. $$

Point doubling uses $x_{2P}=\lambda^2-2x_1$. The same algebraic formula works modulo a prime $p$ (with division meaning multiply by inverse). Calculus motivates the chord-and-tangent law; algebra makes it cryptographic.

f) Complexity analysis via calculus

Estimating factoring cost uses integrals. The harmonic sum $H_n=\sum_{k=1}^n1/k$ satisfies $H_n=\int_1^n dx/x + \gamma + o(1)=\ln n+\gamma$. The Number Field Sieve runtime $L_N[1/3,c]=\exp((c+o(1))(\ln N)^{1/3}(\ln\ln N)^{2/3})$ is found by optimizing a continuous parameter (smoothness bound) by setting a derivative to zero.

Takeaway: calculus doesn't break discrete crypto directly, but it designs, analyzes, and attacks it.

7. GNU Octave Laboratory

Copy, paste, and run. All code tested in Octave 7+.

1) Plot sin, cos, tan

% Plot trig functions
x = linspace(-2*pi, 2*pi, 1000);
plot(x, sin(x), 'b', x, cos(x), 'r', x, tan(x), 'g');
ylim([-4,4]); grid on;
legend('sin','cos','tan'); title('Trigonometric functions');

2) Numerical derivative vs analytic

% Central difference for sin' at x0
f = @(x) sin(x); fp = @(x) cos(x);
h = 1e-6; x0 = 1.0;
num = (f(x0+h)-f(x0-h))/(2*h);
printf("num=%.9f analytic=%.9f err=%.2e\n", num, fp(x0), abs(num-fp(x0)));

3) Verify limit sin(x)/x

% Approach 0 from right
x = logspace(-8, -1, 8);
y = sin(x)./x;
disp([x' y']);  % y -> 1

4) Symbolic derivative (needs symbolic pkg)

pkg load symbolic
syms x
f = sin(x)/x;
df = diff(f, x);
simplify(df)
% df = (x*cos(x)-sin(x))/x^2

5) Riemann sum for ∫0^π sin x dx

a=0; b=pi; n=1e6;
x = linspace(a,b,n);
dx = (b-a)/n;
I = sum(sin(x))*dx;
printf("Riemann ≈ %.8f (exact 2)\n", I);

6) Taylor series for sin

x = linspace(-pi, pi, 400);
s3 = x - x.^3/6;
s5 = s3 + x.^5/120;
s7 = s5 - x.^7/5040;
plot(x, sin(x), 'k', x, s3, '--', x, s5, ':', x, s7, '-.');
legend('sin','3rd','5th','7th'); grid on;

7) Newton's method for cube root (RSA e=3 attack)

% If c = m^3 < N, recover m without modulus
c = 250047*250047*250047;  % simulate small message
f = @(x) x.^3 - c;
df = @(x) 3*x.^2;
x = c^(1/3);  % initial
for k=1:5
  x = x - f(x)/df(x);
endfor
printf("m ≈ %.0f (error %.2e)\n", x, abs(x-250047));

8) Gradient descent demo

% Minimize J(x) = (x-3)^2 + sin(5x)
J = @(x) (x-3).^2 + sin(5*x);
dJ = @(x) 2*(x-3) + 5*cos(5*x);
x = 0; alpha = 0.05;
for i=1:200
  x = x - alpha*dJ(x);
endfor
printf("min near x=%.4f, J=%.4f\n", x, J(x));

9) Elliptic curve tangent slope

% Curve y^2 = x^3 + a x + b, over reals
a = -1; b = 1;
x1 = 2; y1 = sqrt(x1^3 + a*x1 + b);
lambda = (3*x1^2 + a)/(2*y1);  % derivative dy/dx
x3 = lambda^2 - 2*x1;
y3 = lambda*(x1 - x3) - y1;
printf("lambda=%.5f, 2P=(%.5f, %.5f)\n", lambda, x3, y3);

10) Numerical integration for Gaussian entropy

sigma = 1;
p = @(x) exp(-x.^2/(2*sigma^2))/(sigma*sqrt(2*pi));
H = -quadgk(@(x) p(x).*log2(p(x)), -10, 10);
theory = 0.5*log2(2*pi*exp(1)*sigma^2);
printf("h ≈ %.5f bits, theory %.5f\n", H, theory);

11) Logistic ODE (model saturation in searches)

r = 0.3; K = 1e6;
f = @(t,P) r*P*(1-P/K);
[t,P] = ode45(f, [0 50], 1e3);
plot(t,P); xlabel('t'); ylabel('Population'); title('Logistic growth');

8. Proofs Appendix

Product Rule: $(fg)'=f'g+fg'$

$$ \frac{f(x+h)g(x+h)-f(x)g(x)}{h} = f(x+h)\frac{g(x+h)-g(x)}{h}+g(x)\frac{f(x+h)-f(x)}{h} $$ Add-subtract $f(x+h)g(x)$. As $h\to0$, $f(x+h)\to f(x)$ by differentiability implying continuity. Limits give $f g' + g f'$.

Chain Rule: $(f\circ g)' = (f'\circ g)\cdot g'$

Write $f$ differentiable at $y_0=g(x_0)$ as $f(y)=f(y_0)+f'(y_0)(y-y_0)+r(y)(y-y_0)$ with $r(y)\to0$. Set $y=g(x)$. Divide by $x-x_0$, let $x\to x_0$. The error term vanishes because $g$ is continuous; remainder gives $f'(g(x_0))g'(x_0)$.

FTC Part 1: $F(x)=\int_a^x f \Rightarrow F'=f$

For $h>0$, $F(x+h)-F(x)=\int_x^{x+h} f(t)dt = f(c_h)h$ for some $c_h\in[x,x+h]$ by the Mean Value Theorem for integrals (continuity of $f$). Divide by $h$, get $F'(x)=\lim_{h\to0}f(c_h)=f(x)$.

Derivative of $\sin$: full epsilon proof

From addition formula as above, need limits $(\sin h)/h\to1$ and $(1-\cos h)/h\to0$. The first is squeeze theorem. For the second: $1-\cos h =2\sin^2(h/2)$, so $(1-\cos h)/h = (\sin(h/2)/(h/2))\cdot\sin(h/2)\to1\cdot0=0$.

Derivative of $\arcsin x$

Let $y=\arcsin x$, so $\sin y = x$, $y\in[-\pi/2,\pi/2]$. Differentiate implicitly: $\cos y \cdot y' =1$, thus $y' =1/\cos y =1/\sqrt{1-\sin^2 y}=1/\sqrt{1-x^2}$ (positive root by range).

9. Exercises

  1. Use the squeeze theorem to prove $\lim_{x\to0}x\sin(1/x)=0$.
  2. Prove $\lim_{h\to0}(1-\cos h)/h^2=1/2$ using the half-angle identity.
  3. Differentiate $f(x)=x^x$ for $x>0$ via logarithmic differentiation. Where is the minimum?
  4. Show $(\sec x)'=\sec x\tan x$ and integrate $\int \tan x\,dx$.
  5. Compute $\int_0^{\pi/2} \sin^n x\,dx$ for $n=2,4$ using power-reduction. Guess the reduction formula.
  6. Use substitution $x=a\sin\theta$ to evaluate $\int \sqrt{a^2-x^2}\,dx$.
  7. Find the 4th-degree Maclaurin polynomial for $\cos x$ and bound the error on $[-\pi/4,\pi/4]$.
  8. Crypto-flavored: Let $f(C)=C^d$ over $\mathbb{R}$. Compute $f'(C)$. Explain why this derivative gives no information about RSA decryption $C^d\bmod N$.
  9. Implement Newton's method in Octave for $e=5$, $c=32^5$. How many iterations to recover $m=32$ starting from $x_0=c^{0.2}+10$?
  10. For a Gaussian side-channel with $\sigma=0.5$, use Octave to numerically compute differential entropy. How many bits are lost if $\sigma$ doubles?
  11. Show the tangent doubling formula for $y^2=x^3-2x$ at $P=(2,2)$ gives $\lambda=5/2$ and compute $2P$.
  12. Use integration by parts twice to find $\int e^x\sin x\,dx$.
  13. Approximate $e$ by $ (1+1/n)^n$ and use $\ln(1+u)\sim u-u^2/2$ to show error $\approx e/(2n)$.
  14. Differential cryptanalysis: If an S-box has maximum differential probability $2^{-6}$, estimate expected pairs needed to see the differential once. Relate to derivative magnitude.
  15. Use L'Hôpital's rule (itself proved via Cauchy MVT) to evaluate $\lim_{x\to0}(\tan x - \sin x)/x^3$.
Tip: Do the analysis by hand, then check with Octave. The combination of proof and computation is how modern applied mathematics works.