Skip to content

rainingmaster/lua-resty-bn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

##名称

用于ngx_resty的大数运算库

##描述

当前resty仅支持最大51位整数,而是用c中的double long也仅支持64位整数,当如果需要计算一些大数运算时,将出现一些瓶颈。 本库直接使用openssl中的bignumber库进行封装,使用本库,需要在服务器上安装openssl和openssl-dev等相关库.可以使用whereis openssl查看是否有安装相关库,一般在/usr/include/openssl/下面会看到对应的头文件-bn.h

##使用

1.在代码中引用本库,并创建实例

    local bn = require "resty.bn"

2.可以直接调用库中函数,函数暂只包括大数的:

相加(add)

    local a = "998123124234634536345"
    local b = "6998123121543524234634536345"
    local r = bn.add(a, b) -- r = a + b

相减(sub)

    local a = "2998123435452423461524236552423"
    local b = "6998123121543524234634536345"
    local r = bn.sub(a, b) -- r = a - b

相乘(mul)

    local a = "2998123435452423461524236552423"
    local b = "6998123121543524234634536345"
    local r = bn.mul(a, b) -- r = a * b

相除(div,包括模)

    local a = "2998123435452423461524236552423"
    local b = "6998123121543524234634536345"
    local d, rem = bn.div(a, b) -- d = a / b, rem = a % b

乘方(pow)

    local a = "2998123435452423461524236552423"
    local b = "65"
    local r = bn.pow(a, b) -- r = a ^ b

十进制转十六机制(dec2hex)

    local a = "2998123435452423461524236552423"
    local b = bn.dec2hex(a)

十六进制转十进制(hex2dec)

    local a = "a123ef0080912d12"
    local b = bn.hex2dec(a)

位运算左移(lshift)

    local a = "2998123435452423461524236552423"
    local b = bn.lshift(a, 10)

位运算右移(rshift)

    local a = "2998123435452423461524236552423"
    local b = bn.rshift(a, 10)

About

用于ngx_lua的大数运算

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages