Skip to Content
Problem Solving

7-Number Theory

2019-01-16Original-language archivelegacy assets may be incomplete

背景知识

  • 自然数的集合论定义:
    • a+:=a{a}a^+:=a\cup\{a\}
    • 归纳集
      • A\emptyset\in A
      • a(aAa+A)\forall a(a\in A\rightarrow a^+\in A)
    • N\mathbb{N} 为所有归纳集之交
  • Peano 结构
    • eSe\in S
    • aS,f(a)S\forall a\in S,f(a)\in S
    • bS,cS,f(b)=f(c)b=c\forall b\in S,\forall c\in S,f(b)=f(c)\rightarrow b=c
    • aS,f(a)e\forall a\in S,f(a)\not= e
    • AS,(eA(aA)(f(a)A))A=S\forall A\subseteq S, (e\in A\wedge(\forall a\in A)(f(a)\in A))\rightarrow A=S
  • 良序公理与数学归纳法原理等价

数论基础

  • 带余除法: a=bq+r,b>0,0r<ba=bq+r,b>0,0\leq r<b
    • 存在性证明:良序公理
    • 唯一性证明
  • 整除及其性质
    • ab,acanb+mca|b,a|c\Rightarrow a|nb+mc
  • 最大公因子 (hcf/gcd)
    • 裴蜀定理:gcd(a,b)=ar+bs\gcd(a,b)=ar+bs
      • 证明:良序公理+带余除法
    • Strong Duality for GCD: max{di:dZ,da,db}=min{ax+by:xZ,yZ,ax+by>0}\max\{d_i:d\in\mathbb{Z},d|a,d|b\}=\min\{ax+by:x\in\mathbb{Z},y\in\mathbb{Z},ax+by>0\}
      • 证明
        1. ds,sdd|s,s|d
        2. Weak Duality + \exists
      • 唯一性证明
    • lcm(a,b)gcd(a,b)=ab\text{lcm}(a,b)\gcd(a,b)=ab
      • 证明
        1. Isomorphism Theorem
        2. Unique Factorization
        3. d(a,b)dab[a,b]d|(a,b)\Leftrightarrow d|\frac{ab}{[a,b]}
      • (a,b,c)[a,b,c]=abc[(a,b),(b,c),(c,a)](a,b,c)[a,b,c]=\frac{abc}{[(a,b),(b,c),(c,a)]}
      • [a,b,c]=abc(a,b,c)(a,b)(a,c)(b,c)[a,b,c]=\frac{abc(a,b,c)}{(a,b)(a,c)(b,c)}
    • gcd(a,b,c)=gcd(gcd(a,b),c)\gcd(a,b,c)=\gcd(\gcd(a,b),c)
  • 质数
    • π(x)xlnx\pi(x)\sim\frac{x}{\ln{x}}
    • 无限质数证明
      1. P=p1p2pn+1P=p_1p_2\cdots p_n+1
      2. Fermat Numer Fn=22n+1F_n=2^{2^n}+1 两两互素
        • Fn2=k=1n1FkF_n-2=\prod_{k=1}^{n-1}F_k
      3. Mersenne Number 2p12^p-1
        • 2p1(modq)pq12^p\equiv 1\pmod{q}\Rightarrow p|q-1
      • Dirichlet's Theorem: gcd(a,m)=1\gcd(a,m)=1 then there are infinitely many primes p,pa(modm)p,p\equiv a\pmod{m}
    • 筛法求素数
    • 算术基本定理
      • 存在性证明:良序公理(找不满足中最小的)
      • 唯一性证明1n1\rightarrow n
  • φ(n)=npn(11p),p\varphi(n)=n\prod_{p|n}(1-\frac{1}{p}),p is prime
    • φ(p)=p1\varphi(p)=p-1
    • φ(n)>neγlnlnn+3lnlnn,γ=0.577\varphi(n)>\frac{n}{e^\gamma\ln\ln n+\frac{3}{\ln\ln n}},\gamma=0.577
  • Additivie group module n: (Zn,+n)(\mathbb{Z}_n,+_n)
    • a=(a,n)\langle a\rangle=\langle (a,n)\rangle
      • a=n(a,n)|\langle a\rangle|=\frac{n}{(a,n)}
  • Multiplicative group module n: (Zn,n)(\mathbb{Z}_n^*,\cdot_n)
    • ord(a)\text{ord}(a): the smallest possible integer that a(t)=ea^{(t)}=e
      • ord(a)=a\text{ord}(a)=|\langle a\rangle|
      • Primitive root: a,ordm(a)=φ(m)a,\text{ord}_m(a)=\varphi(m)
        • Number: if exists, φ(φ(m))\varphi(\varphi(m))
    • Euler's Theorem: aφ(n)1(modn)a^\varphi(n)\equiv 1\pmod{n}
      • Fermat's Theorem: ap11(modp)a^{p-1}\equiv1\pmod{p}
    • Wilson Theorem: (p1)!1(modp)    p(p-1)!\equiv -1\pmod{p}\iff p is prime
  • quadratic residue
    • aa is quadratic residue x2=a(modp)x^2=a\pmod{p} has a solution for xx
    • p12\frac{p-1}{2} quadratic residues for pp
    • Legendre symbol (ap)=1(\frac{a}{p})=1 if aa is a quadratic residue otherwise 1-1
    • (ap)a(p1)/2(modp)(\frac{a}{p})\equiv a^{(p-1)/2}\pmod{p}

Modular linear equations

  • axb(modn)ax\equiv b\pmod{n}
    • 有解:d=gcd(a,n)bd=\gcd(a,n)|b
    • 求解:
      • ax+bn=dax+bn=d
      • x0=xbdx_0=\frac{xb}{d}
      • x1=x0+ind,i=0,,n1x_1=x_0+i\frac{n}{d},i=0,\cdots,n-1

CRT (The Chinese remainder theorem)

  • n=n1n2nkn=n_1n_2\cdots n_k, where nin_i are pairwise relatively prime, then we have mapping a(a1,a2,,ak)a\leftrightarrow(a_1,a_2,\cdots,a_k), for =+//\cdot=+/-/*, we have (ab)(modn)((a1b(modn1),,(akbk)(modnk)))(a\cdot b)\pmod{n}\leftrightarrow ((a_1\cdot b\pmod{n_1},\cdots,(a_k\cdot b_k)\pmod{n_k}))
    • \rightarrow: direct
    • \leftarrow
      • mi=nnim_i=\frac{n}{n_i}
      • a(i=1kaimi(mi1(modni)))(modn)a\equiv(\sum_{i=1}^ka_im_i(m_i^{-1}\pmod{n_i}))\pmod{n}
  • 一般同余方程
    • 法一:
      • 有解:aiaj(mod(ni,nj))a_i\equiv a_j \pmod{(n_i,n_j)}
      • 先拆后合(相同或倍数)
      • xa(modni)    xC(mod[n1,,nk])x\equiv a\pmod{n_i}\iff x\equiv C\pmod{[n_1,\cdots,n_k]}
    • 法二:
      • 两两合并
  • 简化计算:
    • 2300mod3192^{300}\bmod 319
    • 319=1129319=11*29 + CRT

数论算法

  • model: multplying two β\beta-bit integers take Θ(β2)\Theta(\beta^2) bit operations

Euclid Algorithm

int ext_gcd(int a, int b, int &x, int &y){
    if (b==0) {
        x = 1; y = 0;
        return a;
    } else {
        int ret = ext_gcd(b,a%b,y,x);
        y -= x*(a/b);
        return ret;
    }
}
  • a>b1a>b\geq 1 and Euclid(a,b) performs k1k\geq 1 recursive calls, then aFk+2,bFk+1a\geq F_{k+2},b\geq F_{k+1}
    • 证明ab+(amodb)a\geq b+(a\bmod b)
    • Lame's Theorem: k1,a>b1,b<Fk+1k\geq 1,a>b\geq 1,b<F_{k+1} then Euclid makes fewer than kk recursive calls
    • Fkϕk5,ϕ=1+52F_k\sim\frac{\phi^k}{\sqrt{5}},\phi=\frac{1+\sqrt{5}}{2}
    • O(lgb)O(\lg b)

Modular Exponentiation

int fp(int a, int b, int mod){
    int c = 1;
    for (;b;b>>=1,a=1ll*a*a%mod)
        if (b&1) c=1ll*c*a%mod;
    return c%mod;
}

Primality Testing

  • trial division: Θn\Theta{\sqrt{n}}
  • Pseudoprimality testing:
    • base-aa pseudoprime: an11(modn)a^{n-1}\equiv 1\pmod{n}
    • Carmichael numbers: aZn,an11(modn)\forall a\in\mathbb{Z}_n^*,a^{n-1}\equiv 1\pmod{n}
    • Algorithm: return MODULAR-EXPONENTIATION(2,n-1,n)1(modn)\equiv 1\pmod{n}
  • Miller-Rabin randomized primality test
    • x21(modpe)x^2\equiv 1\pmod{p^e}
    • O(sβ3)O(s\beta^3)
    • nn is an odd composite number, then the number of witnesses to the compositeness of n is at least n12\frac{n-1}{2}
    • P(MILLER-RABIN errs)<2sP(\text{MILLER-RABIN errs})<2^{-s}
    • P(primeRETURN prime)>0.5P(\text{prime}|\text{RETURN prime})>0.5 if s>lg(lnn1)s>\lg(\ln n-1)
      • applicable: s=50s=50
LL multimod(LL a, LL b, LL mod) {
    a %= mod;
    b %= mod;
    LL res = 0;
    while (b) {
        if (b & 1) {
            res += a;
            if (res >= mod) res -= mod;
        }
        b >>= 1;
        a <<= 1;
        if (a >= mod) a -= mod;
    }
    return res;
}
 
bool witness(LL s, LL n) {
    LL u = n - 1, t = 0;
    while ((u & 1) == 0 && u != 0) u >>= 1, t++;
    LL x = fp(s, u, n), tmp;
    while (t--) {
        tmp = x;
        x = multimod(x, x, n);
        if (x == 1 && tmp != n - 1 && tmp != 1) return true;
    }
 
    if (x != 1)
        return true;
    else
        return false;
}
 
bool millerrabin(LL n, const int times = 3) {
    if (n == 2) return true;
    if ((n & 1) == 0 || n < 2) return false;
    int i = times;
    while (i--) {
        LL x = random(2, n - 1);
        if (witness(x, n)) return false;
    }
    return true;
}

Integer factorization

  • Trial division RR2R\rightarrow R^2
  • Pollard's rho heuristic RR4R\rightarrow R^4
    • initial: x1=random(0,n1),y=x1,k=2x_1=\text{random}(0,n-1),y=x_1,k=2
    • Loop
      • xi=xi12(modn)x_i=x_{i-1}^2\pmod{n}
      • if gcd(yxi,n)1/n\gcd(y-x_i,n)\not=1/n finded
      • if i==ki==k
        • y=xiy=x_i
        • k=2kk=2k

Crypotography

Private Key Crypotography

  • affine cryptosystem: f(p)=ap+b(modn)f(p)=ap+b\pmod{n}
  • polyalphabetic crypotosystem
    • German ADFGVX Field Cipher
  • Kerckhoffs's principle

Public Key Crypotography

  • f1f^{-1} must be difficult to compute
  • PKC
    • Self P,DP,D
    • Others P,DP',D'
    • Message MM
    • 公钥加密
      • encoding with PP': P(M)P'(M) -> send
      • receive -> decoding with D(P(M))D'(P'(M))
    • 数字签名
      • signing with DD: M=M+D(M)M'=M+D(M)
      • (encrypt and) send -> receive (and decode)
      • verifying M=P(D(M))M=P(D(M))
  • RSA
    • n=pqn=pq
    • (E,φ(n))=1(E,\varphi(n))=1
    • DE1(modφ(n))DE\equiv 1\pmod{\varphi(n)}
    • public: (n,E)(n,E)
    • secret: (n,D)(n,D)
    • encoding: y=xEmodny=x^E\bmod n
    • decoding: x=yDmodnx=y^D\bmod n
  • DH(Deffie-Hellman)
    • aa: small prime
    • pp: large prime
    • Agent a: (Xa,Ya=aXamodp)(X_a,Y_a=a^{X_a}\bmod p)
    • Agent b: (Xb,Yb=aXbmodp)(X_b,Y_b=a^{X_b}\bmod p)
    • secret key: K=YbXamodp=YaXbmodpK=Y_b^{X_a}\bmod p= Y_a^{X_b}\bmod p
  • Attack
    • Small ee Attack: e=3,de=3,d obtained
      • Result: p,qp,q leaks
      • ed=1+kφ(n),kZ,k<min{e,d}ed=1+k\varphi(n),k\in\mathbb{Z},k<\min\{e,d\}
    • Common Modulus Attack: (ei,ej)=1(e_i,e_j)=1
      • Result: if message decoded both sent, original message can be recovered
      • M=ExFy(modn),e1x+e2y=1M=E^xF^y\pmod{n},e_1x+e_2y=1