代码高尔夫:Diffie-Hellman 密钥交换
早在 ITAR 时代,就有一个流行的签名执行 Diffie-Hellman 密钥交换:
#!/usr/bin/perl -- -export-a-crypto-system-sig Diffie-Hellman-2-lines
($g,$e,$m)=@ARGV,$m||die"$0 gen exp mod\n";print`echo "16dio1[d2%Sa2/d0<X+d
*La1=z\U$m%0]SX$e"[$g*]\EszlXx+p|dc`
使用现代 dc,这可以大大简化为:
dc -e '16dio???|p'
虽然带有模幂命令的现代 dc 形式(“|”通过有效的指数加倍计算 g^e % m)可能是无与伦比的,但也许APL,原始形式可以改进吗? 请记住,e 和 m 值将非常大; 为了加密安全,它们的大小均为 1024 位。
Back in the ITAR era, there was a popular sig that performed Diffie-Hellman key exchange:
#!/usr/bin/perl -- -export-a-crypto-system-sig Diffie-Hellman-2-lines
($g,$e,$m)=@ARGV,$m||die"$0 gen exp mod\n";print`echo "16dio1[d2%Sa2/d0<X+d
*La1=z\U$m%0]SX$e"[$g*]\EszlXx+p|dc`
With a modern dc, this can be reduced quite a bit to:
dc -e '16dio???|p'
While the modern dc form with the modular exponentiation command ('|' computes g^e % m via efficient exponential doubling) is likely unbeatable other than perhaps APL, can the original form be improved upon? Keep in mind that the e and m values will be very large; they will both be on the order of 1024 bits each for cryptographic security.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于那些不熟悉 Diffie-Hellman 或
dc
(或 Perl)的人:如果您将其作为“program gx m
”运行,则该程序的所有功能都是输出 g x(mod m),其中 g、x 和 m 以十六进制给出。 例如,因为 10 是十六,而 102 是二百五十六,即 4 mod 9。
而
dc
命令16dio??? |p
表示:鉴于 dc 有一个单字符命令“
|
”用于计算“gx(mod m)”,这正是问题所在,我发现它不太可能在任何编程语言中得到改进。dc
恰好是解决这个问题的工具; 将编程语言与正确的工具进行比较是没有争议的。 (例如,任何常见的编程语言都需要两个以上的字符来列出目录中的文件,而“ls
”仅为 2。)也就是说,我注意到
dc -e '16dio? ??|p'
似乎希望我在三个不同的行中输入数字(至少在我这里的dc
上),所以它可以改进 到可以在同一行处理所有这些的东西:-)dc -e '16dio?|p'
For those unfamiliar with Diffie-Hellman or
dc
(or Perl): all the program does, if you run it as "program g x m
", is output gx(mod m), where g, x, and m are given in hexadecimal. E.g.because 10 is sixteen and 102 is two-hundred-and-fifty-six, which is 4 mod 9.
And the
dc
command16dio???|p
says:Given that
dc
has a one-character command "|
" for computing "gx(mod m)" which is precisely the problem, I find it highly unlikely that it can be improved upon in any programming language.dc
just happens to be a tool for exactly this problem; it's no contest comparing a programming language to the right tool. (E.g. any common programming language will take more than two characters to list files in a directory, while "ls
" is only 2.)That said, I note that
dc -e '16dio???|p'
seems to want me to input the numbers in three different lines (at least on thedc
I have here), so it can be improved to something that can handle them all on the same line :-)dc -e '16dio?|p'
我非常怀疑有什么能超越现代 DC 版本! 这是Python:
它在Python 3.0中不起作用,因为我们已经 逐步淘汰反向引号。
I very much doubt anything will top the modern dc version! Here's Python:
It won't work in Python 3.0 as we've phased out reverse quotes.