L05 – Modulo Arithmetic and Square-and-Multiply Algorithm

Discrete Mathematics2026年3月18日发布 芮和
553 360
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
© 版权声明

相关文章

36 条评论

  • 哈哈镜
    哈哈镜 读者

    模幂运算这块挺绕的。

    中国云南
    回复
    • 嘎嘣棒
      嘎嘣棒 读者

      同感,需要多练几遍。

      中国吉林@ 哈哈镜
      回复
  • 兰舟轻棹
    兰舟轻棹 读者

    RSA那部分看得有点懵,再琢磨琢磨。

    中国北京
    回复
    • 秋高气爽
      秋高气爽 读者

      同感,那部分确实有点绕

      中国福建@ 兰舟轻棹
      回复
  • 反骨精
    反骨精 读者

    时间锁那个例子,真有人算三年多啊?

    美国
    回复
    • 行舟远
      行舟远 读者

      这毅力也是没谁了

      美国@ 反骨精
      回复
  • 彩虹伞
    彩虹伞 读者

    这算法名字挺酷的,平方乘。

    意大利
    回复
  • 暗紫幽兰
    暗紫幽兰 读者

    时间锁这概念有点意思,感觉像现实版时光胶囊。

    波兰
    回复
    • 嫣紫云霞
      嫣紫云霞 读者

      同感,未来感很强。

      中国宁夏@ 暗紫幽兰
      回复
  • 神经幻想
    神经幻想 读者

    平方乘算法例子算得挺清楚的

    德国
    回复
  • 微风细雨
    微风细雨 读者

    模运算这块,二进制表示看着就头大。

    中国广东
    回复
  • 辰光掠影
    辰光掠影 读者

    RSA这玩意儿,学一次忘一次😂

    中国上海
    回复
  • 废土幽灵
    废土幽灵 读者

    反复平方法原来这么早就有了啊

    罗马尼亚
    回复
  • 夜之孤独
    夜之孤独 游客

    量子计算机真来了,时间锁是不是直接变废铁?

    中国上海
    回复
  • 星核探索者
    星核探索者 游客

    commitment scheme那块构造逻辑有点绕,回头再啃。

    中国湖北
    回复
  • 焰心秘语
    焰心秘语 游客

    t设太大怕等几十年,设太小又秒破,难搞。

    中国广西
    回复
  • 暗影掠夺
    暗影掠夺 读者

    模幂运算看着简单,实际debug到凌晨😭

    澳大利亚
    回复
  • 协议破坏者
    协议破坏者 读者

    我之前调库都调吐了,自己实现根本不敢想。

    日本
    回复
  • 墨枭
    墨枭 读者

    binding性全靠大数分解撑着,有点悬。

    中国四川
    回复
  • 放鸽子专业户
    放鸽子专业户 游客

    现在谁还敢用plain RSA,不加padding等于裸奔。

    中国上海
    回复
  • 属音
    属音 游客

    除法算法里的while循环感觉容易出bug啊。

    中国湖南
    回复
  • 星骸拾荒
    星骸拾荒 游客

    那个LCS35谜题真有人跑3年多?佩服耐心😂

    中国广东
    回复
  • 星星兔
    星星兔 游客

    反复平方快是快,但缓存小的设备估计扛不住。

    中国浙江
    回复
  • 夏花绚烂
    夏花绚烂 读者

    手算验证了下例子,确实是8,中间步骤没错。

    中国
    回复
  • 山静日长
    山静日长 读者

    现在还有人真用plain RSA?不加padding太危险了

    中国北京
    回复
  • 茶香氤氲
    茶香氤氲 读者

    反复平方快是快,但缓存不够的设备怕是要卡死

    中国湖北
    回复
  • 霜羽
    霜羽 游客

    看着2^t mod φ(n)就头皮发麻,不敢想写错一步

    中国湖北
    回复
  • 百发百中
    百发百中 读者

    除法算法里那个while循环边界容易崩吧

    中国重庆
    回复
  • 空之咏叹
    空之咏叹 读者

    这commitment scheme要是p q泄露就全完了啊

    中国
    回复
  • 无畏的船长
    无畏的船长 读者

    平方乘算法还挺巧妙的。

    中国上海
    回复
  • 时光剪影
    时光剪影 游客

    这模幂运算的复杂度推导看得我头大,有没有人画个流程图?

    中国重庆
    回复