码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Go语言-big.Int


    文章目录

      • Go 语言 big.Int
      • 应用场景:大整数位运算
      • 使用举例: go sdk中crypto/ecdsa 椭圆曲线生成私钥相关结构中就有使用

    Go 语言 big.Int

    Go 语言 big.Int
    参考URL: https://blog.csdn.net/wzygis/article/details/82867793

    math/big 作为 Go 语言提供的进行大数操作的官方库。

    big.Int 用于表示 大整数。

    应用场景:大整数位运算

    在密码学、加密算法或者需要处理大数字的领域中,使用大整数进行位操作是非常常见的。

    实战demo:来自cosmos的 bip39.go

      ...
     
    	// Break entropy up into sentenceLength chunks of 11 bits
    	// For each word AND mask the rightmost 11 bits and find the word at that index
    	// Then bitshift entropy 11 bits right and repeat
    	// Add to the last empty slot so we can work with LSBs instead of MSB
    
    	// Entropy as an int so we can bitmask without worrying about bytes slices
    	entropyInt := new(big.Int).SetBytes(entropy)
    
    	// Slice to hold words in
    	words := make([]string, sentenceLength)
    
    	// Throw away big int for AND masking
    	word := big.NewInt(0)
    
    	for i := sentenceLength - 1; i >= 0; i-- {
    		// Get 11 right most bits and bitshift 11 to the right for next time
    		word.And(entropyInt, Last11BitsMask)
    		entropyInt.Div(entropyInt, RightShift11BitsDivider)
    
    		// Get the bytes representing the 11 bits as a 2 byte slice
    		wordBytes := padByteSlice(word.Bytes(), 2)
    
    		// Convert bytes to an index and add that word to the list
    		words[i] = WordList[binary.BigEndian.Uint16(wordBytes)]
    	}
    
    	return strings.Join(words, " "), nil
    }
    

    代码解析:

    entropyInt := new(big.Int).SetBytes(entropy) 这行代码的作用是将字节切片 entropy 转换为大整数。

    将这些字节数据转换为大整数可以方便进行位操作、数学运算等操作,同时也能保持精度和范围。因此,将字节转换为大整数是一种常见的做法

    • 将熵(entropy)分成长度为 sentenceLength 的 11 位比特。
    • 对于每个单词,将最右边的 11 位进行按位与(AND)运算,并找到该索引位置的单词。
    • 然后将熵向右移动 11 位,重复上述操作。

    使用举例: go sdk中crypto/ecdsa 椭圆曲线生成私钥相关结构中就有使用

    举例:
    比如 go sdk中crypto/ecdsa 椭圆曲线生成私钥相关结构中就有使用到,demo如下:
    key, err := ecdsa.GenerateKey(secp256k1.S256(), seed)

    // PublicKey represents an ECDSA public key.
    type PublicKey struct {
    	elliptic.Curve
    	X, Y *big.Int
    }
    
    // PrivateKey represents an ECDSA private key.
    type PrivateKey struct {
    	PublicKey
    	D *big.Int
    }
    
  • 相关阅读:
    Quartz重启服务会执行但再次执行前Trigger状态为Error的问题。Quartz不同环境的注意事项
    PlantUML基础使用教程
    目标检测YOLO实战应用案例100讲-基于改进YOLOv3的目标检测模型研究与应用(下)
    k8s 读书笔记 - 深入掌握 Pod 扩缩容
    Node.js 是如何做 GC (垃圾回收)的?
    MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
    文件存储服务 实时通信服务 HTTP通信协议
    【Spring Boot 3】的安全防线:整合 【Spring Security 6】
    基于通用单片机(久齐) 半导体制冷制热的控制 手机散热器按摩仪器中冷热头的控制
    【UE 材质】常用向量运算节点——点积、叉积、归一化
  • 原文地址:https://blog.csdn.net/inthat/article/details/107615761
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号