3 提供大数相关功能,用于非对称加密算法

前置条件:NA

场景:

  1. 可支持大数基本的运算。

约束:主要用于非对称算法

性能: 支持版本几何性能持平

可靠性: NA

3.1 BIGNUM

大数一般指的是位数很多的数。计算机表示的数的大小是有限的,精度也是有限的,它不能支持大数运算。密码学中采用了很多大数计算,主要用于非对称算法。

支持大数初始化函数、计算类函数、随机函数、与字符/位相关的函数、上下文结构的功能

3.1.1 全局函数

初始化函数
/*
 * 创建一个空的大数对象。
 *
 * 返回值 CPointer<BIGNUM>
 */
public func bnNew(): CPointer<BIGNUM>

/*
 * 将 a 中所有项均赋值为 0,但是内存并没有释放。
 *
 * 参数 a 
 * 返回值 Unit
 */
public func bnClear(a: CPointer<BIGNUM>): Unit

/*
 * 释放大数对象,每个创建的大数对象都需要释放。
 * 不能对同一个大数进行连续内存释放操作
 * 
 * 参数 a - 大数对象
 * 返回值 Unit
 */
public func bnFree(a: CPointer<BIGNUM>): Unit

/*
 * 相当与将 bnFree 和 bnClear 综合,要不就赋值 0,要不就释放空间。
 * 不能对同一个大数进行连续内存释放操作
 * 
 * 参数 a - 大数
 *
 * 返回值 Unit
 */
public func bnClearFree(a: CPointer<BIGNUM>): Unit
计算类函数
/*
 * 将整数设置给大数对象。
 * 
 * 参数 a - 大数对象
 * 参数 w - 整数
 * 返回值 Unit
 */
public func bnSetWord(a: CPointer<BIGNUM>, w: UInt64): Unit

/*
 * 从大数中提取整数。
 * 
 * 参数 a - 大数
 * 返回值 UInt64 - 结果
 */
public func bnGetWord(a: CPointer<BIGNUM>): UInt64

/*
 * 比较两个大数
 * 
 * 参数 a - 大数
 * 参数 b - 大数
 * 返回值 Int32 -  a < b return -1 
 *                a == b return 0 
 *                a > b return 1
 */
public func bnCmp(a: CPointer<BIGNUM>, b: CPointer<BIGNUM>): Int32

/*
 * 从大数提取 10 进制字符串。
 * 
 * 参数 a - 大数
 * 返回值 String
 */
public func bnBn2dec(a: CPointer<BIGNUM>): String

/*
 * 将 b 复制给 a ,正确返回值 a。
 * 
 * 参数 a - 大数
 * 参数 b - 大数
 * 返回值 CPointer<BIGNUM> - success a , error null
 */
public func bnCopy(a: CPointer<BIGNUM>, b: CPointer<BIGNUM>): CPointer<BIGNUM>

/*
 * 新建一个 BIGNUM 结构,将 a 复制给新建结构返回。
 * 
 * 参数 a - 大数
 * 返回值 CPointer<BIGNUM>
 */
public func bnDup(a: CPointer<BIGNUM>): CPointer<BIGNUM>

/*
 * 交换 a,b。
 * 
 * 参数 a - 大数
 * 参数 b - 大数
 * 返回值 Unit
 */
public func bnSwap(a: CPointer<BIGNUM>, b: CPointer<BIGNUM>): Unit

/*
 * 返回值 a 占用的比特数。
 * 
 * 参数 a - 大数
 * 返回值 Int32
 */
public func bnNumBits(a: CPointer<BIGNUM>): Int32

/*
 * 他返回有意义比特的位数,例如 0x00000111 为 9。
 * 
 * 参数 a
 * 返回值 Int32
 */
public func bnNumBitsWord(a: UInt64): Int32

/*
 * 返回一个为 1 的大数。
 *
 * 返回值 CPointer<BIGNUM>
 */
public func bnValueOne(): CPointer<BIGNUM>

/*
 * 判断 a 与 b 的绝对值是否相等。
 * 
 * 参数 a - 大数
 * 参数 b - 大数
 * 返回值 Int32 - |a| < |b| return -1 
 *                 |a| == |b| return 0 
 *                 |a| > |b| return 1
 */
public func bnUcmp(a: CPointer<BIGNUM>, b: CPointer<BIGNUM>): Int32

/*
 * 判断 a 是不是为 0。
 * 
 * 参数 a - 大数
 * 返回值 Bool
 */
public func bnIsZero(a: CPointer<BIGNUM>): Bool

/*
 * 判断 a 是不是 1。
 * 
 * 参数 a - 大数
 * 返回值 Bool
 */
public func bnIsOne(a: CPointer<BIGNUM>): Bool

/*
 * 判断 a 是不是值 w。
 * 
 * 参数 a - 大数
 * 参数 w
 * 返回值 Bool
 */
public func bnIsWord(a: CPointer<BIGNUM>, w: UInt64): Bool

/*
 * 对大数执行整数加法操作。
 * 
 * 参数 r  - 和
 * 参数 a - 加数
 * 参数 b - 加数
 * 返回值 Unit
 */
public func bnAdd(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>): Unit

/*
 * 判断a是不是一个奇数
 * 
 * 参数 a - large numbers
 * 返回值 Bool
 */
public func bnIsOdd(a: CPointer<BIGNUM>): Bool

/*
 * 计算a与b的差,值储存在r中, r = a - b
 * 
 * 参数 r  - difference
 * 参数 a - large numbers
 * 参数 b - large numbers
 * 返回值 Unit
 */
public func bnSub(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>): Unit

/*
 * 大数 a 加上 w,值储存在 a 中,a = a + w
 * 
 * 参数 a - large numbers
 * 参数 w       
 * 返回值 Unit 
 */
public func bnAddWord(a: CPointer<BIGNUM>, w: UInt64): Unit

/*
 * 大数 a 减去 w,值储存在 a 中,a = a - w
 * 
 * 参数 a - large numbers
 * 参数 w          
 * 返回值 Unit
 */
public func bnSubWord(a: CPointer<BIGNUM>, w: UInt64): Unit

/*
 * 大数 a 乘以 w,值储存在 a 中,a = a * w 
 * 
 * 参数 a: CPointer<BIGNUM> - large numbers
 * 参数 w: UInt64          
 * 返回值 Unit
 */
public func bnMulWord(a: CPointer<BIGNUM>, w: UInt64): Unit

/*
 * 大数 a 除以 w,值储存在 a 中,返回余数 a = a / w
 * 
 * 参数 a - large numbers
 * 参数 w
 * 返回值 Unit
 */
public func bnDivWord(a: CPointer<BIGNUM>, w: UInt64): Unit

/*
 * 大数 a 模 w,返回余数 a = a % w 
 * 
 * 参数 a - large numbers
 * 参数 w          
 * 返回值 Unit
 */
public func bnModWord(a: CPointer<BIGNUM>, w: UInt64): UInt64

/*
 * 计算a与b的积,值储存在r中 r = a * b;
 * 
 * 参数 r  - product
 * 参数 a - bignum
 * 参数 b - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnMul(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a的平方,值储存在r中,r = a * a;
 * 
 * 参数 r  - square
 * 参数 a - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnSqr(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算m与d的商,值储存在dv中,余数储存在rem中, dv = m / d , rem = m % d
 * 
 * 参数 dv - merchant
 * 参数 rem - remainder
 * 参数 m - bignum
 * 参数 d - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnDiv(dv: CPointer<BIGNUM>, rem: CPointer<BIGNUM>, m: CPointer<BIGNUM>, d: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a与m的模,并且结果如果小于0,就加上m,值储存在r中, r = ( (a % m) + m) % m
 * 
 * 参数 r  - value
 * 参数 a - bignum
 * 参数 m - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnNnmod(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, m: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a与b的和,再模m,值储存在r中, r = (a + b) % m
 * 
 * 参数 r  - value
 * 参数 a  - bignum
 * 参数 b - bignum
 * 参数 m - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnModAdd(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>, m: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a与b的差,再模m,值储存在r中, r = (a - b) % m
 * 
 * 参数 r - value
 * 参数 a - bignum
 * 参数 b - bignum
 * 参数 m - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnModSub(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>, m: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a与b的积,再模m,值储存在r中,  r =(a * b) % m
 * 
 * 参数 r - value
 * 参数 a - bignum
 * 参数 b - bignum
 * 参数 m - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnModMul(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>, m: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a的平方,再模m,值储存在r中, r = (a * a) % m
 * 
 * 参数 r - value
 * 参数 a - bignum
 * 参数 m - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnModSqr(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, m: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a的p次方,值储存在r中, r = a^p
 * 
 * 参数 r - value
 * 参数 a - bignum
 * 参数 p - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnExp(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, p: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a的p次方,再模m,值储存在r中, r = (a ^ p) % m
 * 
 * 参数 r - value
 * 参数 a - bignum
 * 参数 p - bignum
 * 参数 m - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnModExp(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, p: CPointer<BIGNUM>, m: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit

/*
 * 计算a与b的最大公约数,值储存在r中, r = gcd(a,b)
 * 
 * 参数 r - value
 * 参数 a - bignum
 * 参数 b - bignum
 * 参数 ctx - BN_CTX
 * 返回值 Unit
 */
public func bnGcd(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, b: CPointer<BIGNUM>, ctx: CPointer<BN_CTX>): Unit
随机函数
/*
 * 生成指定位的强随机大数。
 * 
 * 参数 rnd - 随机大数
 * 参数 bits - 指定位数 ( > 0 )
 * 参数 top - 头部
 *      BN_RAND_TOP_ANY = -1  最高位为0 
 *      BN_RAND_TOP_ONE  = 0  最高位为1 
 *      BN_RAND_TOP_TWO  = 1  最高位和次高位为1
 * 参数 bottom - 尾部
 *      BN_RAND_BOTTOM_ANY = 0 尾部位随机
 *      BN_RAND_BOTTOM_ODD  = 1 尾部位置1,表示奇数
 * 返回值 Unit
 */
public func bnRand(rnd: CPointer<BIGNUM>, bits: Int32, top: Int32, bottom: Int32): Unit

/*
 * 生成0 ~ range范围的大数
 * 
 * 参数 rnd - 生成的随机大数
 * 参数 range - 范围( 0 ~ range)
 * 返回值 Unit
 */
public func bnRandRange(rnd: CPointer<BIGNUM>, range: CPointer<BIGNUM>): Unit

/*
 * 产生一个伪随机数。
 * 
 * 参数 rnd - 随机大数
 * 参数 bits - 指定位数 ( > 0 )
 * 参数 top - 头部
 *      BN_RAND_TOP_ANY = -1  最高位为0 
 *      BN_RAND_TOP_ONE  = 0  最高位为1 
 *      BN_RAND_TOP_TWO  = 1  最高位和次高位为1
 * 参数 bottom - 尾部
 *      BN_RAND_BOTTOM_ANY = 0 尾部位随机
 *      BN_RAND_BOTTOM_ODD  = 1 尾部位置1,表示奇数
 * 返回值 Unit
 */
public func bnPseudoRand(rnd: CPointer<BIGNUM>, bits: Int32, top: Int32, bottom: Int32): Unit

/*
 * 生成0 ~ range范围的大数
 * 
 * 参数 rnd - 生成的随机大数
 * 参数 range - 范围( 0 ~ range)
 * 返回值 Unit
 */
public func bnPseudoRandRange(rnd: CPointer<BIGNUM>, range: CPointer<BIGNUM>): Unit
与 字符/位 相关的函数
/*
 * 将 |a| 转化为 ASCLL 返回,并返回它的长度
 * 
 * 参数 a - large number
 * 返回值 String - ASCLL
 * 返回值 Int32 - length
 */
public func bnBn2bin(a: CPointer<BIGNUM>): (String, Int32)

/*
 * 将 s 中前 len 位的正整数转化为大数存在 ret
 * 
 * 参数 s
 * 参数 len
 * 参数 ret
 * 返回值 Unit
 */
public func bnBin2bin(s: String, len: Int32, ret: CPointer<BIGNUM>): Unit

/*
 * 将大数转化为十六进制的字符串返回
 * 
 * 参数 a
 * 返回值 String
 */
public func bnBn2hex(a: CPointer<BIGNUM>): String

/*
 * 将十六进制字符串转换为大数
 * 
 * 参数 a
 * 参数 str
 * 返回值 Unit
 */
public func bnHex2bn(a: CPointer<BIGNUM>, str: String): Unit

/*
 * 将十进制字符串转换为大数
 * 
 * 参数 a
 * 参数 str
 * 返回值 Unit
 */
public func bnDec2bn(a: CPointer<BIGNUM>, str: String): Unit

/*
 * 将a中的第n位设置为1,假如a小于n位将扩展
 * 
 * 参数 a
 * 参数 n
 * 返回值 Unit
 */
public func bnSetBit(a: CPointer<BIGNUM>, n: Int32): Unit

/*
 * 将 a 中的第 n 为设置为 0,假如 a 小于 n 位,a 保持不变
 * 
 * 参数 a
 * 参数 n
 * 返回值 Unit
 */
public func bnClearBit(a: CPointer<BIGNUM>, n: Int32): Unit

/*
 * 测试a中的第n位是否已经设置
 * 
 * 参数 a
 * 参数 n
 * 返回值 Bool - true: 设置
 *             false: 未设置
 */
public func bnIsBit(a: CPointer<BIGNUM>, n: Int32): Bool

/*
 * 将a截断至n位,假如a小于n位将出错
 * 
 * 参数 a
 * 参数 n
 * 返回值 Unit
 */
public func bnMaskBit(a: CPointer<BIGNUM>, n: Int32): Unit

/*
 * a左移1位,结果存于r
 * 
 * 参数 r
 * 参数 a
 * 返回值 Unit
 */
public func bnLshift1(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>): Unit

/*
 * a右移1位,结果存于r
 * 
 * 参数 r
 * 参数 a
 * 返回值 Unit
 */
public func bnRshift1(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>): Unit

/*
 * a左移n位,结果存于r
 * 
 * 参数 r
 * 参数 a: CPointer<BIGNUM>
 * 返回值 Unit
 */
public func bnLshift(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, n: Int32): Unit

/*
 * a右移n位,结果存于r
 * 
 * 参数 r
 * 参数 a
 * 返回值 Unit
 */
public func bnRshift(r: CPointer<BIGNUM>, a: CPointer<BIGNUM>, n: Int32): Unit
上下文结构
/*
 * 申请一个新的上下文结构
 *
 * 返回值 CPointer<BN_CTX>
 */
public func bnCtxNew(): CPointer<BN_CTX>

/*
 * 释放上下文结构
 * 
 * 参数 c - CPointer<BN_CTX>
 *
 * 返回值 Unit
 */
public func bnCtxFree(c: CPointer<BN_CTX>): Unit

3.1.2 其他接口

与 c 互操作的结构体,使用方式为 CPointer CPointer

public struct BIGNUM
public struct BN_CTX

https://gitee.com/cangjie-tpc/crypto4cj/blob/master/doc/feature_api.md#313-%E7%A4%BA%E4%BE%8B3.1.3 示例

from crypto4cj import bignumcj.*
from std import collection.*

main() {    
    var pBNa: CPointer<BIGNUM> = bnNew()
    var pBNb: CPointer<BIGNUM> = bnNew()
    var pBNr: CPointer<BIGNUM> = bnNew()
    bnSetWord(pBNa, 1)
    bnSetWord(pBNb, 2)
    bnAdd(pBNr, pBNa, pBNb)
    var res = bnGetWord(pBNr)
    var ret = bnBn2dec(pBNr)
    bnFree(pBNa)
    bnFree(pBNb)
    bnFree(pBNr)
    if(res != 3) {
        return -1
    }
    return 0
}

运行结果如下:

0

img

在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
更多鸿蒙最新技术知识点,请关注作者博客:https://gitee.com/li-shizhen-skin/zhihu/blob/master/README.md

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐