代码高尔夫:Diffie-Hellman 密钥交换

发布于 2024-07-11 09:13:59 字数 620 浏览 8 评论 0原文

早在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-07-18 09:13:59

对于那些不熟悉 Diffie-Hellman 或 dc(或 Perl)的人:如果您将其作为“program gx m”运行,则该程序的所有功能都是输出 g x(mod m),其中 g、x 和 m 以十六进制给出。 例如,

./dh.pl 10 2 9
4

因为 10 是十六,而 102 是二百五十六,即 4 mod 9。

dc 命令 16dio??? |p 表示:

  • 十六压入堆栈,
  • d复制它,
  • i输入基数(基数)设置为结果出栈(16,十六进制),
  • o输出基数设置为出栈结果(16),
  • 获取三行输入并执行它们(因此,如果三行是三个数字 g ,x,m,它们被推入堆栈),
  • 进行求幂 gx(mod m),
  • 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.

./dh.pl 10 2 9
4

because 10 is sixteen and 102 is two-hundred-and-fifty-six, which is 4 mod 9.

And the dc command 16dio???|p says:

  • push sixteen onto the stack,
  • duplicate it,
  • set input radix (base) to the result of popping the stack (16, hex),
  • set output radix to the result of popping the stack (16),
  • get three lines of input and execute them (so if the three lines are three numbers g, x, m, they get pushed onto the stack),
  • do the exponentiation gx(mod m),
  • print it.

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 the dc I have here), so it can be improved to something that can handle them all on the same line :-)

dc -e '16dio?|p'

贱贱哒 2024-07-18 09:13:59

我非常怀疑有什么能超越现代 DC 版本! 这是Python:

def f(g,x,m):
 def h(n):return int(`n`,16)
 return h(g)**h(x)%h(m)

它在Python 3.0中不起作用,因为我们已经 逐步淘汰反向引号

I very much doubt anything will top the modern dc version! Here's Python:

def f(g,x,m):
 def h(n):return int(`n`,16)
 return h(g)**h(x)%h(m)

It won't work in Python 3.0 as we've phased out reverse quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文