Discrete Log Algorithms :Baby-step giant-step

离散对数的求解

1.暴力

2.Baby-step giant-step 

3.Pollard’s ρ algorithm

……

下面搬运一下Baby-step giant-step 的做法

 

这是在 https://ctf-wiki.github.io/ctf-wiki/crypto/asymmetric/discrete-log/discrete-log/ 上看到的,比较容易理解。

而且,里面的代码写得简洁明了。

写一下自己理解和自己照着写了一遍

原文代码:

def bsgs(g, y, p):m = int(ceil(sqrt(p - 1)))S = {pow(g, j, p): j for j in range(m)}   #在字典S中存放了g^j和对应的jgs = pow(g, p - 1 - m, p)                 #求解的是baby step中的值,也就是g^(-m),其实就是g^m mod p的逆元,也就可以使用egcd来求解for i in range(m):if y in S:return i * m + S[y]               #S[y]取出的是此时y对应的iy = y * gs % p                        #如果baby step的值和 giant step的值不相等,继续执行baby stepreturn None
View Code

 

照着写一遍的代码

#求解离散对数问题import math
def egcd(a,b):r0,r1,s0,s1=1,0,0,1n=bwhile(b):q,a,b=a//b,b,a%br0,r1=r1,r0-q*r1s0,s1=s1,s0-q*s1return r0%n             #拓展欧几里得返回的三个值是,a是a和b的最大公因数,r0和s0分别是ax+by=c中的一组解x和y,【此时只是选择返回一个r0,因为得到的是ax+by=gcd(a,b)中的x即可,有的时候x或者y可能为负数,在求解正数的逆元的时候负数要对n再次求模运算#求解离散对数就是,X=G^a mod P,其中给出X,G,P的值,要求解的是 a的值,此处采用的是Baby step giant step的方法def bsgs(x,g,p):                  #求解x=g^a mod p中的a,其中g是生成元m=math.ceil(math.sqrt(p-1))   #m的值是 p-1开平方后向上取整bstep={pow(g,j,p):j for j in range(m)}              #每一次 j 的增加表示 “baby-step”,一次乘上g,字典S中存了所有的g^j(j<m)以及其对应的jgstep=egcd(pow(g,m),p)           #算出了gstep的值,也就是g^-m的值for i in range(m):if x in bstep:return i*m+bstep[x]x=x*gstep%pprint(bsgs(37,3,101))
View Code

 

继续学习其他的做法

参考资料:http://zoo.cs.yale.edu/classes/cs257/ppt/all/Mac/19_DiscreteLog.ppt

 

转载于:https://www.cnblogs.com/Guhongying/p/9966875.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278912.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android添加item动画,RecyclerView基础篇-Item添加动画

Android_Banner.jpg简介本节中我们介绍下给RecyclerView中的Item添加动画。添加的动画&#xff0c;分为&#xff0c;在打开列表时有Item的展示动画&#xff0c;当滑动的时候没有动画和打开列表滑动时有动画两种实现过程实现一个列表效果如下Screenshot_2020-09-01-17-03-35-349…

geek_Ask How-To Geek:营救受感染的PC,安装无膨胀iTunes和驯服疯狂的触控板

geekYou’ve got questions and we’ve got answers. Today we highlight how to save your computer if it’s so overrun by viruses and malware you can’t work from within Windows, install iTunes without all the bloat, and tame a hyper-sensitive trackpad. 您有问…

第1课:接口测试和jmeter总结

接口测试 1. 接口的分类&#xff1a;webService和http api接口1&#xff09; webService接口&#xff1a;是按照soap协议通过http传输&#xff0c;请求报文和返回报文都是xml格式&#xff0c;一般要借助工具来测试接口&#xff1b;2&#xff09; http api接口&#xff1a;是按照…

极客时间和极客学院_极客历史记录的本周:Twitter的诞生,OS X十周年以及太空停留时间最长的时代即将结束...

极客时间和极客学院Every week we bring you interesting trivia and milestones from the archives of Geekdom. Today we’re taking a peek at the birth of Twitter, ten years of Mac OS X, and the longest space stay in history. 每周&#xff0c;我们都会为您带来有趣…

Android风格ppt,Material Design风格的快手PPT

突发奇想&#xff0c;感觉MD风格既然适合 Android 软件的界面&#xff0c;那么在一般PPT 演示中&#xff0c;效果当也是不错。于是在网上去寻了几处制作贴&#xff0c;也简单看了 MD 设计指南的几处要点。先试试一番再说。关于 MD 设计指南和几处制作贴&#xff0c;我会把链接贴…

dropbox链接过期_询问操作方法:“开始”菜单中的Dropbox,了解符号链接和翻录TV系列DVD...

dropbox链接过期This week we take a look at how to incorporate Dropbox into your Windows Start Menu, understanding and using symbolic links, and how to rip your TV series DVDs right to unique and high-quality episode files. 本周&#xff0c;我们来看看如何将D…

android listpreference 自定义,Android – 我的ListPreference中的自定义行布局

在我的Android应用程序中,我实现了从ListPreference扩展的类SubtitleColorListPreference.我需要这个,因为我需要为列表中的每个项目设置自己的布局.一切正常,它看起来像这样&#xff1a;重要的代码是onPrepareDialogBu​​ilder(AlertDialog.Builder builder)中的方法,我在其中…

火狐 增强查找工具栏_在“提示”框中:简单的IE至Firefox同步,轻松的Windows工具栏和识别USB电缆...

火狐 增强查找工具栏() Every week we tip into our mail bag and share great tips from your fellow readers. This week we’re looking at an easy way to sync your bookmarks between IE and Firefox, using simple Windows toolbars, and a clever way to ID USB cables…

Input Director使用一个键盘和鼠标即可控制多台Windows计算机

The problem is having two or more PC’s and having to go back and forth between workstation. Input Director solves the problem by allowing you to control multiple Windows systems with only one keyboard and mouse on the Master PC. 问题是拥有两台或更多台PC…

excel导入csv文件_如何将包含以0开头的列的CSV文件导入Excel

excel导入csv文件Microsoft Excel will automatically convert data columns into the format that it thinks is best when opening comma-separated data files. For those of us that don’t want our data changed, we can change that behavior. Microsoft Excel将在打开…

android 9.0新ui,SystemUI分析(Android9.0)

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;一、SystemUI组成SystemUI是Android的系统界面&#xff0c;包括状态栏statusbar、锁屏keyboard、任务列表recents等等&#xff0c;都继承于SystemUI这个类&#xf…

chromebook刷机_如何为不支持Chrome操作系统的网站欺骗Chromebook用户代理

chromebook刷机Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

cocos android-1,cocos2dx在windows下开发,编译到android上(1)

转自&#xff1a;http://www.2cto.com/kf/201205/130697.html下面我给大家介绍下&#xff0c;用vs2010开发cocos2dx&#xff0c;然后如何使其编译到android上。步骤如下&#xff1a;1、必要条件&#xff0c;你的eclipse能把代码编译到安卓手机或虚拟机上&#xff0c;如果这一步…

中药ppi网络图太杂乱_太杂乱了吗? 这是您的iPhone,iPad,Android或台式机的15张简约壁纸...

中药ppi网络图太杂乱Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墙纸图像在iPhon…

初学者java学习计划_初学者:计划在Windows 7 Media Center中录制直播电视的时间

初学者java学习计划If you’re a new user to Windows 7 Media Center you know it can act as a DVR and pause or record Live TV. You can set up a schedule for it to record your favorite TV programs as well. 如果您是Windows 7 Media Center的新用户&#xff0c;则知…

如何在Office 2007中查看关于对话框和版本信息

One of our favorite readers wrote in today asking how to tell if his Word 2007 installation was running Service Pack 1, since he couldn’t find the About dialog, which got me thinking… I bet most people don’t know where it is! 我们最喜欢的一位读者今天写信…

windows全局热键_在Windows中创建快捷方式或热键以清除剪贴板

windows全局热键Have you ever copied something to the clipboard that you don’t want to leave there in case somebody else is going to use your computer? Sure, you can copy something else to the clipboard real quick, but can’t you just make a shortcut or h…

获取outlook 会议_如何仅在Microsoft Outlook中仅获取您关注的电子邮件的通知

获取outlook 会议Some emails are more important than others. Rather than getting alerts every time an email arrives, configure Microsoft Outlook to only alert you when the important stuff hits your inbox, rather than any old email that can wait until you ch…

jq html 多一个引号,为什么jQuery模板会为某些字符串添加双引号

背景我正在使用jQuery模板,ASP.Net MVC Razor视图和Twitter.问题使用带有一些字符串的jQuery模板会自动导致这些字符串被包含在“细节我创建了一个如下所示的jQuery模板&#xff1a;before ${text.parseUserName().parseHashTag()} after${created_at}${prettyDate(created_at)…

从Windows计算机上完全删除iTunes和其他Apple软件

If you are giving up on iTunes for another music player, uninstalling it completely can be a hassle. Here we show you how to completely remove all traces of it including QuickTime, iTunes Helper, Bonjour…all of it. 如果您在iTunes上放弃了其他音乐播放器&…