{"id":445,"date":"2021-06-29T01:20:21","date_gmt":"2021-06-28T17:20:21","guid":{"rendered":"https:\/\/www.cclo.idv.tw\/blogs\/closer\/?p=445"},"modified":"2021-06-29T01:20:21","modified_gmt":"2021-06-28T17:20:21","slug":"exercismrust-diffie-hellman","status":"publish","type":"post","link":"https:\/\/blog.cclo.idv.tw\/?p=445","title":{"rendered":"[Exercism][Rust] Diffie Hellman"},"content":{"rendered":"\n<p><a href=\"https:\/\/exercism.io\/my\/solutions\/d96a834d65b94326a18e61114c974cee\" data-type=\"URL\" data-id=\"https:\/\/exercism.io\/my\/solutions\/d96a834d65b94326a18e61114c974cee\">\u984c\u76ee\u7db2\u5740<\/a>\uff08\u9700\u767b\u5165 Exercism\uff09<\/p>\n\n\n\n<p>\u9019\u500b\u984c\u76ee\u5728 Rust \u8a9e\u8a00\u65b9\u9762\uff0c\u6c92\u6709\u4ec0\u9ebc\u592a\u56f0\u96e3\u7684\u5730\u65b9\u3002\u6240\u4ee5\u91cd\u9ede\u90fd\u662f\u5728 modular arithmetic \u7684\u904b\u7b97\u4e0a\u3002<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><pre><code class=\"language-rust\">use rand::Rng;\n\npub fn private_key(p: u64) -&gt; u64 {\n    let mut rng = rand::thread_rng();\n    rng.gen_range(2..p)\n}\n\npub fn public_key(p: u64, g: u64, a: u64) -&gt; u64 {\n    modular_exp(g, a, p)\n}\n\npub fn secret(p: u64, b_pub: u64, a: u64) -&gt; u64 {\n    modular_exp(b_pub, a, p)\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<!--more-->\n\n\n\n<p>\u9996\u5148\uff0c\u662f\u8981\u8a08\u7b97 r = b<sup>e<\/sup> mod m \u7684\u7d50\u679c\uff0c\u4e5f\u5c31\u662f modular exponentiation \u904b\u7b97\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>b<sup>e<\/sup> \u5728 e \u5f88\u5927\u7684\u60c5\u6cc1\u4e0b\uff0c\u5e7e\u4e4e\u662f\u4fdd\u8b49\u6703 overflow\uff0c\u56e0\u6b64\u4e0d\u53ef\u80fd\u786c\u5e79\u3002<\/li><li>\u53e6\u4e00\u500b\u76f4\u89ba\u7684\u65b9\u6cd5\uff0c\u662f\u5728 b \u9023\u4e58 e \u6b21\u7684\u904e\u7a0b\u4e2d\uff0c\u6bcf\u4e58\u4e00\u6b21\u5c31\u53d6\u4e00\u6b21 mod m\u3002\u9019\u500b\u65b9\u6cd5\uff08\u6bd4\u8f03\uff09\u4e0d\u6703\u7206\uff0c\u4f46\u6642\u9593\u8907\u96dc\u5ea6\u662f O(e)\uff0ce \u5f88\u5927\u7684\u8a71\uff0c\u5f88\u6162\u3002<\/li><li>\u6211\u5011\u53ef\u4ee5\u5f9e bit[0] (LSB) \u81f3 bit[n-1] (MSB) \u9010\u4e00\u6aa2\u8996 e \u7684\u6bcf\u500b bit\u3002\u82e5\u7b2c k \u500b bit \u70ba 1 \u7684\u8a71\uff0c\u5c31\u8868\u793a r \u4e4b\u4e2d\u4e58\u5165\u4e86 2<sup>k<\/sup> \u500b b\u3002\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u7528 loop \u53bb\u6383 e \u7684\u6bcf\u4e00\u500b bit\uff0c\u5982\u679c\u662f 1\uff0c\u5c31\u4e58\u5165 b\uff08\u7136\u5f8c mod m\uff09\u3002<br>\u53c8\uff0cb<sup>(2 ^ k)<\/sup> * b<sup>(2 ^ k)<\/sup> = b<sup>(2 ^ (k + 1))<\/sup>\uff0c\u6240\u4ee5\u5728\u6bcf loop \u4e00\u6b21\u6642\uff0cb \u53ea\u8981\u518d\u4e58\u4e00\u6b21\u81ea\u5df1\uff08\u7136\u5f8c mod m\uff09\uff0c\u5c31\u53ef\u4ee5\u53d6\u5f97\u4e0b\u4e00\u6b21\u6240\u9700\u8981\u7684 b\u3002<br>\u9019\u500b\u4f5c\u6cd5\u53ef\u4ee5\u628a\u6642\u9593\u8907\u96dc\u5ea6\u964d\u81f3 O(log(e))\uff0c\u7576 e \u5f88\u5927\u7684\u6642\u5019\uff0c\u6548\u679c\u9a5a\u4eba\u3002<br>\uff08\u53ef\u4ee5\u53c3\u8003 Wikipedia \u4e2d\u7684 <a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E6%A8%A1%E5%B9%82\" data-type=\"URL\" data-id=\"https:\/\/zh.wikipedia.org\/wiki\/%E6%A8%A1%E5%B9%82\">\u6a21\u51aa<\/a> \u689d\u76ee\uff09<\/li><\/ul>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><pre><code class=\"language-rust\">fn modular_exp(mut b: u64, mut e: u64, m: u64) -&gt; u64 {\n    if m == 1 {\n        return 0;\n    }\n    let mut result = 1;\n    b %= m;\n    while e &gt; 0 {\n        if e % 2 == 1 {\n            result = result * b % m;\n        }\n        e &gt;&gt;= 1;\n        b = b * b % m;\n    }\n    result\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>\u89e3\u6c7a\u4e86 modular exponentiation \u7684\u554f\u984c\u5f8c\uff0c\u57fa\u672c\u7684 test cases \u61c9\u8a72\u90fd\u80fd\u5728\u5f88\u77ed\u7684\u6642\u9593\u5167\u5b8c\u6210\u4e86\u3002\u4f46\u9019\u500b\u984c\u76ee\u9084\u6709 bonus\uff1a\u842c\u4e00 m \u5f88\u5927\uff0cb \u6c92\u6709\u8fa6\u6cd5\u9760\u4e00\u958b\u59cb\u7684 mod m \u8b8a\u5c0f\uff0c\u5c0e\u81f4 b * b \u5c31 overflow \u4e86\uff0c\u600e\u9ebc\u8fa6\uff1f\u5c0d\u65bc\u5167\u5efa big integer \u7684\u7a0b\u5f0f\u8a9e\u8a00\uff08\u4f8b\u5982 Python, Haskell\uff09\u4f86\u8aaa\uff0c\u9019\u4e0d\u662f\u4ec0\u9ebc\u554f\u984c\uff08\u9802\u591a\u8a08\u7b97\u901f\u5ea6\u8b8a\u6162\uff09\uff1b\u4f46 Rust \u4e26\u4e0d\u662f\u3002\u6240\u4ee5\u5fc5\u9808\u8981\u9760\u5225\u7684\u65b9\u6cd5\u4f86\u89e3\u6c7a\u3002<\/p>\n\n\n\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u57fa\u672c\u4e0a\u662f\u8981\u9760 Montgomery modular multiplication\uff08\u4e2d\u8b6f\uff1a\u8499\u54e5\u99ac\u5229\u4e58\u6a21\uff09\u4f86\u5e6b\u5fd9\u3002\u9019\u500b\u6f14\u7b97\u6cd5\u7684\u8a73\u7d30\u89e3\u8aaa\u53ef\u4ee5\u53c3\u8003 Wikipedia \u4e0a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Montgomery_modular_multiplication\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Montgomery_modular_multiplication\">\u82f1\u6587\u7248\u7684\u689d\u76ee<\/a>\uff08\u4e2d\u6587\u7248\u8ddf\u6c92\u5beb\u6c92\u5169\u6a23\uff09\u6216\u662f\u9019\u7bc7 CSDN \u4e0a\u7684 <a href=\"https:\/\/blog.csdn.net\/zgzczzw\/article\/details\/52712980\" data-type=\"URL\" data-id=\"https:\/\/blog.csdn.net\/zgzczzw\/article\/details\/52712980\">\u7c21\u9ad4\u4e2d\u6587\u89e3\u8aaa<\/a>\u3002\u5f8c\u8005\u5176\u5be6\u5beb\u5f97\u5f88\u4e0d\u932f\u3002\u8001\u5be6\u8aaa\u6211\u90fd\u6c92\u770b\u5b8c\u3002 XDD \u4f46\u770b\u4e86 CSDN \u4e0a\u90a3\u7bc7\u7684\u300c\u9810\u5099\u77e5\u8b58\u300d\u5f8c\uff0c\u5176\u5be6\u5c31\u6709\u80fd\u529b\u63a8\u5c0e\u51fa\u9019\u500b\u7a0b\u5f0f\u6240\u9700\u8981\u7684\u6f14\u7b97\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u89e3\u91cb\u4ec0\u9ebc\u662f Zn\u3002Zn \u5176\u5be6\u5c31\u662f\u4e00\u7d44\u6b63\u6574\u6578\u7684\u96c6\u5408\uff0c\u4ee3\u8868\u4e86 mod N \u7684\u76f8\u95dc\u8a08\u7b97\u6642\u6240\u6709\u53ef\u80fd\u7684\u7d50\u679c\uff0c\u4e5f\u5c31\u662f (0, 1, 2,..., N-1)\u3002\u6240\u6709\u7684\u8a08\u7b97\uff0c\u53c3\u6578\u5fc5\u662f Zn\uff0c\u7d50\u679c\u4e5f\u4e00\u6a23\u6703\u662f Zn\u3002<\/p>\n\n\n\n<p>\u4f9d\u7167\u9019\u6a23\u7684\u6982\u5ff5\uff0c\u6211\u5011\u5148\u601d\u8003 Zn \u7684\u52a0\u6cd5\u8a72\u600e\u9ebc\u505a\uff1f\u6700\u76f4\u89ba\u7684\u60f3\u6cd5\u7576\u7136\u5c31\u662f (Zn(a) + Zn(b)) % N\u3002\u4f46\u662f\uff0c\u5047\u8a2d\u8b8a\u6578\u7684\u578b\u5225\u90fd\u662f <code>u64<\/code>\uff0c\u53ea\u8981 N &gt; 0x8000_0000_0000_0000\uff0cZn(a) + Zn(b) \u5c31\u6709\u53ef\u80fd\u6703 overflow\u3002\u6240\u4ee5\u6211\u5011\u5fc5\u9808\u5c0f\u5fc3\u8655\u7406\u3002\u5728 <code>fn zn_add()<\/code> \u4e2d\uff0c\u5229\u7528\u4e86\u79fb\u9805\u7684\u6280\u5de7\uff0c\u78ba\u4fdd\u6bcf\u4e00\u6b65\u9a5f\u90fd\u4e0d\u6703 overflow\u3002<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><pre><code class=\"language-rust\">fn zn_add(a: u64, b: u64, m: u64) -&gt; u64 {\n    let zn_a = a % m;\n    let zn_b = b % m;\n    if zn_a &gt;= m - zn_b {    \/\/ === if zn_a + zn_b &gt;= m {\n        zn_a - (m - zn_b)   \/\/ ===     zn_a + zn_b - m \n    } else {\n        zn_a + zn_b\n    }\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>\u6709\u4e86 Zn \u7684\u52a0\u6cd5\u5f8c\uff0c\u5c31\u53ef\u4ee5\u9032\u4e00\u6b65\u69cb\u7bc9 Zn \u7684\u4e58\u6cd5\u4e86\u3002\u6211\u5011\u5f9e\u5341\u9032\u4f4d\u7684\u4e58\u6cd5\u958b\u59cb\u601d\u8003\u3002\u5047\u8a2d\u6211\u5011\u8981\u8a08\u7b97 x * y\uff0c\u800c y \u662f 4 \u4f4d\u6578\uff0c\u53ef\u4ee5\u8868\u793a\u70ba (y<sub>3<\/sub> * 1000 + y<sub>2<\/sub> * 100 + y<sub>1<\/sub> * 10 + y<sub>0<\/sub> * 1)\u3002\u90a3\u9ebc x * y \u5c31\u53ef\u4ee5\u8b8a\u6210\uff1a<\/p>\n\n\n\n<p>    x * (y<sub>3<\/sub> * 1000 + y<sub>2<\/sub> * 100 + y<sub>1<\/sub> * 10 + y<sub>0<\/sub> * 1)<br>= xy<sub>3<\/sub> * 1000 + xy<sub>2<\/sub> * 100 + xy<sub>1<\/sub> * 10 + xy<sub>0<\/sub><br>= (((xy<sub>3<\/sub>) * 10 + xy<sub>2<\/sub>) * 10 + xy<sub>1<\/sub>) * 10 + xy<sub>0<\/sub><\/p>\n\n\n\n<p>\u770b\u8d77\u4f86\u5f88\u8907\u96dc\uff0c\u4f46\u82e5\u662f\u4ee5\u7a0b\u5f0f\u7684 loop \u89d2\u5ea6\u4f86\u770b\uff0c\u5176\u5be6\u5c31\u662f\u300cr \u7684\u521d\u59cb\u503c\u70ba 0\uff1by \u5f9e\u5de6\u5230\u53f3\uff0c\u6bcf\u9047\u5230\u4e00\u4f4d\u6578\u5b57\uff0cr \u5148\u4e58\u4ee5 10\uff0c\u518d\u52a0\u4e0a\u8a72\u4f4d\u6578\u4e58\u4ee5 x \u7684\u503c\uff0c\u4e00\u76f4\u5230 y \u7684\u6700\u53f3\u908a\u4e00\u4f4d\u300d\u3002<\/p>\n\n\n\n<p>\u628a 10 \u9032\u4f4d\u7684\u6982\u5ff5\u8f49\u5230 2 \u9032\u4f4d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u300cr \u5148\u4e58\u4ee5 10\u300d\u8b8a\u6210\u300cr \u5148\u4e58\u4ee5 2\u300d\uff1b\u7136\u5f8c r * 2 \u5c31\u662f r + r\uff0c\u56e0\u6b64\u5c31\u53ef\u4ee5\u5957\u7528 Zn \u52a0\u6cd5\u3002<\/li><li>\u300c\u52a0\u4e0a\u8a72\u4f4d\u6578\u4e58\u4ee5 x \u7684\u503c\u300d.... 2 \u9032\u4f4d\u53ea\u6709 0 \u6216 1\uff0c\u6240\u4ee5\u5c31\u662f\u300c\u8a72\u4f4d\u6578\u70ba 1 \u7684\u6642\u5019\uff0c\u52a0\u4e0a x\u300d\u3002\u9019\u4e5f\u53ef\u4ee5\u5957\u7528 Zn \u52a0\u6cd5\u3002<\/li><\/ul>\n\n\n\n<p>\u9019\u5c31\u662f Zn \u4e58\u6cd5\u7684\u6f14\u7b97\u6cd5\u3002<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><pre><code class=\"language-rust\">fn zn_mul(a: u64, b: u64, m: u64) -&gt; u64 {\n    let zn_a = a % m;\n    let zn_b = b % m;\n    let digits = format!(&quot;{:b}&quot;, zn_a);\n    let mut result = 0;\n    for ch in digits.chars() {\n        result = zn_add(result, result, m);\n        if ch == &#039;1&#039; {\n            result = zn_add(result, zn_b, m);\n        }\n    }\n    result\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>\u5982\u679c\u7528 iterator \u7684 <code>fn fold() <\/code>\u4f86\u5be6\u4f5c\u7684\u8a71\uff08\u6700\u8fd1\u5728\u990a\u6210\u5118\u53ef\u80fd\u5c11\u7528 <code>mut<\/code> \u7684\u7fd2\u6163\uff09\uff1a<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><pre><code class=\"language-rust\">fn zn_mul(a: u64, b: u64, m: u64) -&gt; u64 {\n    let zn_a = a % m;\n    let zn_b = b % m;\n    format!(&quot;{:b}&quot;, zn_a)\n        .chars()\n        .fold(0, |r, ch| {\n            if ch == &#039;1&#039; {\n                zn_add(zn_add(r, r, m), zn_b, m)\n            } else {\n                zn_add(r, r, m)\n            }\n        })\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>\u6700\u5f8c\u4fee\u6539 <code>fn modular_exp()<\/code>\uff0c\u7528 <code>zn_mul()<\/code> \u53d6\u4ee3\u539f\u672c\u7684\u4e58\u6cd5\u3002\u9806\u4fbf\uff08\uff1f\uff09\u518d\u6539\u7528 iterator \u6d88 <code>mut<\/code>\uff1a<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><pre><code class=\"language-rust\">fn modular_exp(b: u64, e: u64, m: u64) -&gt; u64 {\n    if m == 1 {\n        0\n    } else {\n        format!(&quot;{:b}&quot;, e)\n            .chars()\n            .rev()\n            .fold((b % m, 1), |(b, r), c| {\n                if c == &#039;1&#039; {\n                    (zn_mul(b, b, m), zn_mul(r, b, m))\n                } else {\n                    (zn_mul(b, b, m), r)\n                }\n            })\n            .1\n    }\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p>Case closed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u984c\u76ee\u7db2\u5740\uff08\u9700\u767b\u5165 Exercism\uff09 \u9019\u500b\u984c\u76ee\u5728 Rust \u8a9e\u8a00\u65b9\u9762\uff0c\u6c92\u6709\u4ec0\u9ebc\u592a\u56f0\u96e3\u7684\u5730\u65b9\u3002\u6240\u4ee5\u91cd\u9ede\u90fd\u662f\u5728 m [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[28,35,41],"table_tags":[],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category--computer-science","tag-exercism","tag-modular-arithmetic","tag-rust"],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445"}],"version-history":[{"count":0,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/blog.cclo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftable_tags&post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}