Skip to Content

7-Number Theory

2019-01-16·Problem Solving

背景知识

  • 自然数的集合论定义:
    • a+:=a∪{a}a^+:=a\cup\{a\}
    • 归纳集
      • ∅∈A\emptyset\in A
      • ∀a(a∈A→a+∈A)\forall a(a\in A\rightarrow a^+\in A)
    • N\mathbb{N} 为所有归纳集之交
  • Peano 结构
    • e∈Se\in S
    • ∀a∈S,f(a)∈S\forall a\in S,f(a)\in S
    • ∀b∈S,∀c∈S,f(b)=f(c)→b=c\forall b\in S,\forall c\in S,f(b)=f(c)\rightarrow b=c
    • ∀a∈S,f(a)≠e\forall a\in S,f(a)\not= e
    • ∀A⊆S,(e∈A∧(∀a∈A)(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,0≤r<ba=bq+r,b>0,0\leq r<b
    • 存在性证明:良序公理
    • 唯一性证明
  • 整除及其性质
    • a∣b,a∣c⇒a∣nb+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:d∈Z,d∣a,d∣b}=min⁡{ax+by:x∈Z,y∈Z,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. d∣s,s∣dd|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)⇔d∣ab[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)∼xln⁡x\pi(x)\sim\frac{x}{\ln{x}}
    • 无限质数证明
      1. P=p1p2⋯pn+1P=p_1p_2\cdots p_n+1
      2. Fermat Numer Fn=22n+1F_n=2^{2^n}+1 两两互素
        • Fn−2=∏k=1n−1FkF_n-2=\prod_{k=1}^{n-1}F_k
      3. Mersenne Number 2p−12^p-1
        • 2p≡1(modq)⇒p∣q−12^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,p≡a(modm)p,p\equiv a\pmod{m}
    • 筛法求素数
    • 算术基本定理
      • 存在性证明:良序公理(找不满足中最小的)
      • 唯一性证明:1→n1\rightarrow n
  • φ(n)=n∏p∣n(1−1p),p\varphi(n)=n\prod_{p|n}(1-\frac{1}{p}),p is prime
    • φ(p)=p−1\varphi(p)=p-1
    • φ(n)>neγln⁡ln⁡n+3ln⁡ln⁡n,γ=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: ap−1≡1(modp)a^{p-1}\equiv1\pmod{p}
    • Wilson Theorem: (p−1)!≡−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
    • p−12\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(p−1)/2(modp)(\frac{a}{p})\equiv a^{(p-1)/2}\pmod{p}

Modular linear equations

  • ax≡b(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,⋯ ,n−1x_1=x_0+i\frac{n}{d},i=0,\cdots,n-1

CRT (The Chinese remainder theorem)

  • n=n1n2⋯nkn=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 (a⋅b)(modn)↔((a1⋅b(modn1),⋯ ,(ak⋅bk)(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(mi−1(modni)))(modn)a\equiv(\sum_{i=1}^ka_im_i(m_i^{-1}\pmod{n_i}))\pmod{n}
  • 一般同余方程
    • 法一:
      • 有解:ai≡aj(mod(ni,nj))a_i\equiv a_j \pmod{(n_i,n_j)}
      • 先拆后合(相同或倍数)
      • x≡a(modni)  ⟺  x≡C(mod[n1,⋯ ,nk])x\equiv a\pmod{n_i}\iff x\equiv C\pmod{[n_1,\cdots,n_k]}
    • 法二:
      • 两两合并
  • 简化计算:
    • 2300 mod 3192^{300}\bmod 319
    • 319=11∗29319=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>b≥1a>b\geq 1 and Euclid(a,b) performs k≥1k\geq 1 recursive calls, then a≥Fk+2,b≥Fk+1a\geq F_{k+2},b\geq F_{k+1}
    • 证明:a≥b+(a mod b)a\geq b+(a\bmod b)
    • Lame's Theorem: k≥1,a>b≥1,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(lg⁡b)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: an−1≡1(modn)a^{n-1}\equiv 1\pmod{n}
    • Carmichael numbers: ∀a∈Zn∗,an−1≡1(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
    • x2≡1(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 n−12\frac{n-1}{2}
    • P(MILLER-RABIN errs)<2−sP(\text{MILLER-RABIN errs})<2^{-s}
    • P(prime∣RETURN prime)>0.5P(\text{prime}|\text{RETURN prime})>0.5 if s>lg⁡(ln⁡n−1)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 R→R2R\rightarrow R^2
  • Pollard's rho heuristic R→R4R\rightarrow R^4
    • initial: x1=random(0,n−1),y=x1,k=2x_1=\text{random}(0,n-1),y=x_1,k=2
    • Loop
      • xi=xi−12(modn)x_i=x_{i-1}^2\pmod{n}
      • if gcd⁡(y−xi,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

  • f−1f^{-1} must be difficult to compute
  • PKC
    • Self P,DP,D
    • Others P′,D′P',D'
    • Message MM
    • 公钥加密
      • encoding with P′P': 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
    • DE≡1(modφ(n))DE\equiv 1\pmod{\varphi(n)}
    • public: (n,E)(n,E)
    • secret: (n,D)(n,D)
    • encoding: y=xE mod ny=x^E\bmod n
    • decoding: x=yD mod nx=y^D\bmod n
  • DH(Deffie-Hellman)
    • aa: small prime
    • pp: large prime
    • Agent a: (Xa,Ya=aXa mod p)(X_a,Y_a=a^{X_a}\bmod p)
    • Agent b: (Xb,Yb=aXb mod p)(X_b,Y_b=a^{X_b}\bmod p)
    • secret key: K=YbXa mod p=YaXb mod pK=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),k∈Z,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