L05 – Modulo Arithmetic and Square-and-Multiply Algorithm

Discrete Mathematics2026年3月18日发布 芮和
15.3K 920
8,689字
37–55 分钟

Summary of Lecture 4

L04 – Plain RSA, Factoring, and Commitment Schemes

Security of RSA: Without sksk, it’s difficult to learn mm from pk,cpk, c

  • “Factoring is easy” \implies “Plain RSA is not secure”
  • “Plain RSA is secure” \implies “Factoring is hard”
  • “Factoring is hard” \implies “Plain RSA is secure”? //not proved, likely true

Commitment Scheme:

  • params𝐆𝐞𝐧(1n);(α,β)𝐂𝐨𝐦(params,m);{0,1}𝐕𝐞𝐫(α,m,β)\mathrm{params}\leftarrow\mathbf{Gen}\left(1^n\right); (\alpha, \beta)\leftarrow\mathbf{Com}(\mathrm{params}, m); \{0,1\}\leftarrow\mathbf{Ver}(\alpha, m, \beta)
    • hiding:α↛m\alpha\not\to m
    • binding:𝐕𝐞𝐫(α,m,β)=𝐕𝐞𝐫(α,m,β)=1\mathbf{Ver}(\alpha, m, \beta) = \mathbf{Ver}(\alpha, m’, \beta’) = 1 and mmm \ne m’? Impossible

CONSTRUCTION: based on RSA

  • Com:α=(pk,c)=((N,e),c);β=(p,q,d)\alpha = (pk, c) = \left((N, e), c\right); \beta = (p, q, d)
  • Ver:N=pq;ed1(mod(p1)(q1));RSA.𝐃𝐞𝐜(sk,c)=m?N = pq; ed \equiv 1 \pmod{(p−1)(q−1)}; \mathrm{RSA}.\mathbf{Dec}(sk, c) = m?

RSA Implementation:

  • choose p,qp, q efficiently? compute dd efficiently? compute cc / mm efficiently?

Addition

Binary Representation of an integer aa: a 0-1 sequence / binary string (ak1a1a0)2\left(a_{k-1}\dots a_1a_0\right)_2 such that

  • a=(ak1a1a0)2a=ak12k1++a121+a020a = \left(a_{k-1} \dots a_1 a_0\right)_2 \iff a = a_{k-1} \cdot 2^{k-1} + \dots + a_1 \cdot 2^1 + a_0 \cdot 2^0

Bit Length of Integer aa: (a)={log2(|a|)+1a01a=0\ell(a) = \begin{cases} \left\lfloor \log_2\left(\left|a\right|\right) \right\rfloor + 1 & a \neq 0 \\ 1 & a = 0 \end{cases}

Algorithm for Addition:

  • Input:a=(ak1a1a0)2,b=(bk1b1b0)2a = (a_{k-1} \dots a_1 a_0)_2 ,\, b = (b_{k-1} \dots b_1 b_0)_2 //0a,b<2k0\leq a,b<2^k
  • Output:c=a+b=(ckck1c1c0)2c = a + b = (c_k c_{k-1} \dots c_1 c_0)_2 //0a+b<2k+10\leq a+b<2^{k+1}
    • carry0carry \leftarrow 0
    • for i0i \leftarrow 0 to k1k-1 do
      • tai+bi+carryt \leftarrow a_i + b_i + carry //t{0,1,2,3}t\in\{0,1,2,3\}
      • set cic_iand carrycarrysuch that t=2carry+cit = 2 \cdot carry + c_i //carry{0,1}carry\in\{0,1\}
    • ckcarryc_k \leftarrow carry
  • Complexity:O(k)O(k) bit operations

Big O notation: O(g(n))={f(n):c,n0>0 such that 0f(n)cg(n) for all nn0}O\left(g(n)\right)=\left\{f(n):\,\exists c,n_0>0\text{ such that }0\leq f(n)\leq c\cdot g(n)\text{ for all }n\geq n_0\right\}


Subtraction

Algorithm for Subtraction:

  • Input:a=(ak1a1aright)2,b=(bk1b1b0)2,aba = \left(a_{k-1} \ldots a_1 a_right\right)_2 ,\, b = \left(b_{k-1} \ldots b_1 b_0\right)_2 ,\, a \geq b //0ba<2k0\leq b\leq a<2^k
  • Output:c=ab=(ck1c1c0)2c = a – b = \left(c_{k-1} \ldots c_1 c_0\right)_2 //0ab<2k0\leq a-b<2^k
    • carry0carry \leftarrow 0
    • for i0i \leftarrow 0 to k1k-1 do
      • taibi+carryt \leftarrow a_i – b_i + carry //t{2,1,0,1}t\in\{-2,-1,0,1\}
      • set cic_iand carrycarrysuch thatt=2carry+cit = 2 \cdot carry + c_i //carry{1,0}carry\in\{-1,0\}
  • Complexity:O(k)O(k) bit operations

Multiplication

Algorithm for Multiplication:

  • Input: a=(ak1a0)2,b=(bk1b0)2a = \left(a_{k-1} \cdots a_0\right)_2 ,\, b = \left(b_{k-1} \cdots b_0\right)_2 //0a,b<2k0\leq a,b<2^k
  • Output: c=ab=(c2k1c0)2c = ab = \left(c_{2k-1} \cdots c_0\right)_2 //0ab<22k0\leq ab<2^{2k}
    • c0;xac\leftarrow 0;\,x\leftarrow a
    • for i0i\leftarrow 0 to k1k-1 do
      • if bi=1b_i = 1, then cc+xc\leftarrow c + x;
      • xx+xx\leftarrow x + x;
  • Complexity: O(k2)O\left(k^2\right) bit operations

Division

Algorithm for Division:

  • Input: a=(ak1a0)2,b=(bl1b0)2a = (a_{k-1} \cdots a_0)_2, b = (b_{l-1} \cdots b_0)_2 //0<ba,ak1=bl1=10<b\leq a,\,a_{k-1} = b_{l-1} = 1
  • Output: q=(qklq0)2q = (q_{k-l} \cdots q_0)_2 and r=(rl1r0)2r = (r_{l-1} \cdots r_0)_2 //a=bq+r,0<q<2kl+1,0r<ba = bq + r,0<q<2^{k-l+1} ,0 \le r < b
    • (rkrk1r0)2(0ak1a0)2(r_k r_{k-1} \cdots r_0)_2 \leftarrow (0 a_{k-1} \cdots a_0)_2
    • for ikli \leftarrow k – l down to 00 do
      • qi2ri+l+ri+l1;q_i \leftarrow 2r_{i+l} + r_{i+l-1}; //qi{0,1,2,3}q_i\in\{0,1,2,3\}
      • If qi2q_i \ge 2, then qi1;q_i \leftarrow 1; //(ri+l,ri+l1){(1,0),(1,1)}\left(r_{i+l},r_{i+l-1}\right)\in\left\{(1,0),(1,1)\right\} and b<(ri+lri)2<2bb<\left(r_{i+l}\cdots r_i\right)_2<2b
      • (ri+lri)2(ri+lri)2qib;(r_{i+l} \cdots r_i)_2 \leftarrow (r_{i+l} \cdots r_i)_2 – q_i \cdot b;
      • while (ri+lri)2<0(r_{i+l} \cdots r_i)_2 < 0 do
        • (ri+lri)2(ri+lri)2+b;(r_{i+l} \cdots r_i)_2 \leftarrow (r_{i+l} \cdots r_i)_2 + b;
        • qiqi1;q_i \leftarrow q_i – 1;
    • output q=(qklq0)2q = (q_{k-l} \cdots q_0)_2 and r=(rl1r0);r = (r_{l-1} \cdots r_0);
  • Complexity: O((kl+1)l)O((k – l + 1) \cdot l) bit operations

Example: (10001)2=(111)2(010)2+(11)2(10001)_2=(111)_2(010)_2+(11)_2


Arithmetic Modulo NN

THEOREM: Let a,b{0,1,,N1}a, b \in \{0, 1, …, N – 1\}. Then

  • (a±b)modN(a \pm b) \bmod N can be computed in O((N))O(\ell(N)) bit operations
    • (a),(b)(N)\ell(a), \ell(b) \leq \ell(N)
      • a±ba \pm b are computable in O((N))O(\ell(N)) bit operations
    • 0|a+b|,|ab|<2N0 \leq |a + b|, |a – b| < 2N
      • (a±b)modN(a \pm b) \mod N: computable in O(((2N)(N)+1)(N))=O((N))O((\ell(2N) – \ell(N) + 1)\ell(N)) = O(\ell(N)) bit operations
  • (ab)modN(ab) \bmod N can be computed in O((N)2)O(\ell(N)^2) bit operations
    • (a),(b)(N)\ell(a), \ell(b) \leq \ell(N)
      • abab is computable in O((N)2)O(\ell(N)^2) bit operations
    • 0|ab|<N20 \leq |ab| < N^2
      • (ab)modN(ab) \bmod N: computable in O(((N2)(N)+1)(N))=O((N)2)O((\ell(N^2) – \ell(N) + 1)\ell(N)) = O(\ell(N)^2) bit operations.

Modulo exponentiation: For 0a<N,e,aemodN=?0 \le a < N, e \in \mathbb{N}, a^e \bmod N = ?

  • Complexity? How to compute efficiently?

EXAMPLE: modulo exponentiation

  • m=m=143733911392049898163790620742447116344546040644898141520376037626365007809899615665793895112104794373551079787727363529151277801402630305742433442340983358787394193855033926469913603762712163723160462115649025
  • e=e=46310011625494823943446873944318243690297367227688331207962573871391818800156614404181253994785434292576255362553884181998492463297303466464428022018327564723810228367576715525319623371983456905064392494176785
  • N=N=245246644900278211976517663573088018467026787678332759743414451715061600830038587216952208399332071549103626827191679864079776723243005600592035631246561218465817904100131859299619933817012149335034875870551067
  • ϕ(N)=\phi(N)=245246644900278211976517663573088018467026787678332759743414451715061600830038587216952208399332071549102628322861627039184220494270313938703906283392288487724394251766892786817697178343799758481228648091667216
  • memodN=?m^e\bmod{N}=?
  • xymodNxy \bmod N
    =((xmodN)y)modN= ((x \bmod N) \cdot y) \bmod N
    =(x(ymodN))modN= (x \cdot (y \bmod N)) \bmod N
    =((xmodN)(ymodN))modN= ((x \bmod N) \cdot (y \bmod N)) \bmod N
  • a1=a=a1modNa_1 = a = a^1 \bmod N
  • a2=(a1a)modN=a2modNa_2 = (a_1 \cdot a) \bmod N = a^2 \bmod N
  • a3=(a2a)modN=a3modNa_3 = (a_2 \cdot a) \bmod N = a^3 \bmod N
  • \dots
  • ae=(ae1a)modN=aemodNa_e = (a_{e-1} \cdot a) \bmod N = a^e \bmod N
  • Complexity:O(e)O(e) multiplications modulo NN
    • e=22048e = 2^{2048}?
      –very slow

Square-and-Multiply

ALGORITHM: (200 BC) compute aemodNa^e \bmod N in polynomial time

  • Input:a{0,1,,N1}a \in \{0, 1, …, N – 1\}; e=(ek1e0)2e = (e_{k-1} \cdots e_0)_2// k=(e)k = \ell(e)
    • e=ek12k1++e121+e020e = e_{k-1} \cdot 2^{k-1} + \cdots + e_1 \cdot 2^1 + e_0 \cdot 2^0
  • Output:aemodNa^e \bmod N
    • Square: this step requires O(k)O(k) multiplications modulo NN
      • x0=ax_0 = a
      • x1=(x02modN)=(a2modN)x_1 = (x_0^2 \bmod N) = (a^2 \bmod N)
      • x2=(x12modN)=(a22modN)x_2 = (x_1^2 \bmod N) = (a^{2^2} \bmod N)
      • \dots
      • xk1=(xk22modN)=(a2k1modN)x_{k-1} = (x_{k-2}^2 \bmod N) = (a^{2^{k-1}} \bmod N)
    • Multiply: this step requires O(k)O(k) multiplications modulo NN
      • (aemodN)=(x0e0x1e1xk1ek1modN)(a^e \bmod N) = (x_0^{e_0} \cdot x_1^{e_1} \cdots x_{k-1}^{e_{k-1}} \bmod N)
  • Complexity:O(k)O(k) multiplications modulo NN
    –fast

EXAMPLE: Compute 2123mod352^{123} \bmod 35 using square-and-multiply.

  • Input:a=2;N=35;e=123=(1111011)2;k=7a = 2; N = 35; e = 123 = (1\,1\,1\,1\,0\,1\,1)_2; k = 7
  • Square:k1k – 1 multiplications modulo NN will be done
    • x0=a=2;x_0 = a = 2;
    • x1=x02modN=4x_1 = x_0^2 \bmod N = 4
    • x2=x12modN=16x_2 = x_1^2 \bmod N = 16
    • x3=x22modN=11x_3 = x_2^2 \bmod N = 11
    • x4=x32modN=16x_4 = x_3^2 \bmod N = 16
    • x5=x42modN=11x_5 = x_4^2 \bmod N = 11
    • x6=x52modN=16x_6 = x_5^2 \bmod N = 16
  • Multiply: at most k1k – 1 multiplications modulo NN will be done
    • ae=x0x1x3x4x5x6=2×4×11×16×11×168(mod35)a^e = x_0 x_1 x_3 x_4 x_5 x_6 = 2 \times 4 \times 11 \times 16 \times 11 \times 16 \equiv 8 \pmod{35}
    • (2123mod35)=8(2^{123} \bmod 35) = 8

Time-Lock Puzzle

Question: The fastest method of computing a2tmodna^{2^t}\bmod{n}? Square-and-multiply?

MIT LCS35 Time Capsule Crypto Puzzle:

  • LCS 35th Celebration (April 12-13, 1999)
  • By Ronald L. Rivest, Adi Shamir, and David A. Wagner
  • Intrinsically Sequential Computations: Can’t be sped up using parallelism
  • Creating the Puzzle:
    • Create n=pqn = pq as the product of two primes
    • Choose tt as the number of operations required
    • Publish (n,t)(n, t) as the puzzle description
    • Puzzle: compute 22tmodn2^{2^t} \bmod n
    • Method: square-and-multiply (repeated squaring)
      • a0=2modna_0 = 2 \bmod n
      • ai=ai12modn,i=1,2,,ta_i = a_{i-1}^2 \bmod n,\,i = 1, 2, \dots, t
      • Solution is ata_t

New puzzle: 2019-05-14 Expected to be solved in 2034

Modern applications: blockchain (green consensus)


Encrypting to the future

Question: Encrypt a message mm such that it can be decrypted (w/o key) in 35 years?

Encrypting a message mm to the future with TLP:

  • Create a TLP with the aforementioned scheme. Record the primes p,qp, q.
  • Compute ata_t quickly with the primes
    • ϕ(n)=(p1)(q1)\phi(n) = (p-1)(q-1)
    • s=2tmodϕ(n)s = 2^t \bmod \phi(n) // ss is much smaller that 2t=279685186856218;s<220482^t = 2^{79685186856218}; s < 2^{2048}
    • Compute at=2smodna_t = 2^s \bmod n very quickly
  • c=m⊕︎atc = m \oplus a_t // Rivest, Shamir, Wagner, 1996

Analysis:

  • With (p,q)(p, q), it is very quick to compute ata_t and decrypt cc to mm
  • With (t,n)(t, n), it may take 35 years to compute ata_t and then learn mm
    • Although Bernard Fabrot solved this with 3.5 years of computation
© 版权声明

相关文章

92 条评论

  • 红烧牛肉面
    红烧牛肉面 读者

    时间锁谜题这思路绝了,等于把计算难度当密钥用

    马来西亚
    回复
  • Violet Mirage
    Violet Mirage 读者

    区块链用这个做绿色共识,挺巧妙的

    韩国
    回复
  • 菠萝咕咾肉
    菠萝咕咾肉 读者

    3.5年就解开了,本来要35年,这也太离谱了

    中国广东
    回复
  • 紫檀香炉
    紫檀香炉 游客

    感觉平方乘算法比直接硬算快多了,这就是算法的魅力吧

    中国陕西
    回复
  • 临江王
    临江王 读者

    mod N的运算复杂度原来只跟位数有关

    英国
    回复
  • 赛博曙光
    赛博曙光 游客

    那个LCS35谜题被破解是因为因数分解有突破吗?

    中国广东
    回复
  • 寒冰之握
    寒冰之握 读者

    平方乘算法在手算大数时候太好用了

    中国河北
    回复
    • 雪糕小云
      雪糕小云 读者

      手算神器,省得开电脑了

      韩国@ 寒冰之握
      回复
  • 糖心喵喵
    糖心喵喵 读者

    这算法公元前就有了?

    中国湖南
    回复
  • 气候操控者
    气候操控者 读者

    给未来发消息这设定绝了,可惜得等这么久。

    日本
    回复
  • 憨包谷
    憨包谷 读者

    这排版公式太多,看着眼晕

    中国浙江
    回复
  • 露珠微光
    露珠微光 游客

    那个时间锁谜题居然真有人跑了3.5年?也是够闲的

    澳大利亚
    回复
  • 幽影掠夺
    幽影掠夺 读者

    模指数运算这块儿看着就头大。

    美国
    回复
    • 彩虹小马驹
      彩虹小马驹 读者

      同感,每次看到都头疼

      中国吉林@ 幽影掠夺
      回复
  • 梦境果
    梦境果 读者

    大数运算这块还是直接调库省事,自己写太容易出bug

    中国重庆
    回复
  • CaptainPancake
    CaptainPancake 读者

    大数幂取模这块儿有点绕。

    美国
    回复
    • 幻光之影
      幻光之影 读者

      同感,这块确实得花点时间理解。

      新西兰@ CaptainPancake
      回复
  • 巧克力热饮
    巧克力热饮 读者

    平方乘算法用在区块链上还挺有意思的

    加拿大
    回复
  • 烟雨阑珊
    烟雨阑珊 游客

    之前搞密码学作业就在这步卡壳,反复平方再乘确实比硬算快太多。

    日本
    回复
  • 宇宙浪子
    宇宙浪子 游客

    所以现在时间锁谜题还值得研究吗?

    印度尼西亚
    回复
  • JollyOtter
    JollyOtter 游客

    看到模运算就想起被期末考试支配的恐惧😂

    斯里兰卡
    回复
    • 量子时空
      量子时空 读者

      期末考这玩意儿的时候差点挂科,现在看到还有阴影

      中国北京@ JollyOtter
      回复
  • 豆腐西施
    豆腐西施 游客

    手算验证了下除法算法,那个while循环确实需要小心处理

    中国北京
    回复
  • 暗语使徒
    暗语使徒 游客

    binding性依赖大数分解难度,目前还算安全

    菲律宾
    回复
  • PhantomFable
    PhantomFable 读者

    时间锁被提前破解说明硬件发展超预期啊

    中国广东
    回复
  • 红香圃下
    红香圃下 游客

    实际写代码时模幂运算边界处理挺折磨人的

    韩国
    回复
  • 糯唧唧
    糯唧唧 游客

    量子计算机威胁是有,但现有密码体系没那么容易崩

    中国台湾
    回复
  • 古城过客
    古城过客 游客

    这文章排版太密了,看得眼睛疼😵

    中国四川
    回复