1 个不稳定版本
| 0.1.1 | 2021年11月30日 |
|---|---|
| 0.1.0 |
|
#57 在 #研究 中
10KB
tiny-secp256k1
此库正在开发中,并且,就像它依赖的 C 库 secp256k1(通过 secp256k1-sys Rust 包)一样,这是一个针对 bitcoinjs 生态系统最终用户确定最佳 API 的研究努力。
安装
npm
npm install tiny-secp256k1
yarn
yarn add tiny-secp256k1
WebAssembly 和 Node.js 版本
先前版本的 tiny-secp256k1 通过 C++ 扩展 实现,通过 NAN(Node.js 的原生抽象) 和 elliptic 作为后备方案,当扩展不能构建或在类似浏览器的环境中。
当前版本使用编译为 WebAssembly 的 Rust 包(它使用 C 库)。使用 Wasm,相同的代码可以在任何环境中执行。Wasm 比 elliptic 快,但比节点绑定慢(PR 中的结果 或您可以在 benches 目录中运行自己的基准测试)。
构建
要本地构建,您需要 C/C++ 工具链,Rust 版本 >=1.50.0 以及从 binaryen 的 wasm-opt。
rustup 是安装 Rust 的推荐方法。您还需要 wasm32-unknown-unknown 目标。
rustup toolchain install stable --target wasm32-unknown-unknown --component clippy --component rustfmt
使用 npm 安装开发依赖项后,您可以构建 Wasm
make build-wasm
或运行测试
make test
另一种方法是使用 Docker
% docker build -t tiny-secp256k1 .
% docker run -it --rm -v `pwd`:/tiny-secp256k1 -w /tiny-secp256k1 tiny-secp256k1
# make build
示例
tiny-secp256k1 包含两个示例。第一个是 Node.js 的简单脚本,它生成随机数据并打印参数和方法结果。第二个是 React 应用。
React 应用在每个提交到 master 分支时在 GitHub Actions 上构建,并上传到 gh-pages 分支,该分支始终可在网络上访问:https://bitcoinjs.github.io/tiny-secp256k1/
文档
isPoint (A)
isPoint :: Buffer -> Bool
如果
A未使用序列标签0x02、0x03或0x04进行编码,则返回falseA.x不在[1...p - 1]A.y不在[1...p - 1]
isPointCompressed (A)
isPointCompressed :: Buffer -> Bool
如果公钥未压缩,则返回 false
isXOnlyPoint (A)
isXOnlyPoint :: Buffer -> Bool
如果公钥不是xOnlyPubkey,则返回 false
isPrivate (d)
isPrivate :: Buffer -> Bool
如果
d不是256位,或者d不在[1..order - 1]
pointAdd (A, B[, compressed])
pointAdd :: Buffer -> Buffer [-> Bool] -> Maybe Buffer
如果结果是无穷大,则返回 null
抛出异常
- 如果
!isPoint(A),则返回Expected Point - 如果
!isPoint(B),则返回Expected Point
pointAddScalar (A, tweak[, compressed])
pointAddScalar :: Buffer -> Buffer [-> Bool] -> Maybe Buffer
如果结果是无穷大,则返回 null
抛出异常
- 如果
!isPoint(A),则返回Expected Point - 如果
tweak不在[0...order - 1],则返回Expected Tweak
pointCompress (A, compressed)
pointCompress :: Buffer -> Bool -> Buffer
抛出异常
- 如果
!isPoint(A),则返回Expected Point
pointFromScalar (d[, compressed])
pointFromScalar :: Buffer [-> Bool] -> Maybe Buffer
如果结果是无穷大,则返回 null
抛出异常
- 如果
!isPrivate(d),则返回Expected Private
xOnlyPointFromScalar (d)
xOnlyPointFromScalar :: Buffer -> Buffer
为给定的私钥返回xOnlyPubkey
抛出异常
- 如果
!isPrivate(d),则返回Expected Private
xOnlyPointFromPoint (p)
xOnlyPointFromPoint :: Buffer -> Buffer
为给定的DER公钥返回xOnlyPubkey
抛出异常
- 如果
!isPoint(p),则返回Expected Point
pointMultiply (A, tweak[, compressed])
pointMultiply :: Buffer -> Buffer [-> Bool] -> Maybe Buffer
如果结果是无穷大,则返回 null
抛出异常
- 如果
!isPoint(A),则返回Expected Point - 如果
tweak不在[0...order - 1],则返回Expected Tweak
privateAdd (d, tweak)
privateAdd :: Buffer -> Buffer -> Maybe Buffer
如果结果是等于 0,则返回 null
抛出异常
- 如果
!isPrivate(d),则返回Expected Private - 如果
tweak不在[0...order - 1],则返回Expected Tweak
privateSub (d, tweak)
privateSub :: Buffer -> Buffer -> Maybe Buffer
如果结果是等于 0,则返回 null
抛出异常
- 如果
!isPrivate(d),则返回Expected Private - 如果
tweak不在[0...order - 1],则返回Expected Tweak
xOnlyPointAddTweak (p, tweak)
xOnlyPointAddTweak :: Buffer -> Buffer -> { parity: 1 | 0; xOnlyPubkey: Buffer; }
返回调整后的xOnlyPubkey和奇偶校验位(1|0的数字类型)
抛出异常
- 如果
!isXOnlyPoint(p),则返回Expected Point - 如果
!isXOnlyPoint(tweak),则返回Expected Tweak
xOnlyPointAddTweakCheck (p1, p2, tweak[, tweakParity])
xOnlyPointAddTweakCheck :: Buffer -> Buffer -> Buffer [-> 1 | 0] -> Bool
检查调整后的公钥(p2)与原始公钥(p1)和调整。如果包含 tweakParity,则略微减慢速度,但 tweakParity 将使聚合更快。
抛出异常
- 如果
!isXOnlyPoint(p1),则返回Expected Point - 如果
!isXOnlyPoint(p2),则返回Expected Point - 如果
!isXOnlyPoint(tweak),则返回Expected Tweak 期望奇偶性如果tweakParity 不是 1 或 0
sign (h, d[, e])
sign :: Buffer -> Buffer [-> Buffer] -> Buffer
返回归一化签名,每个 (r, s) 值都保证小于 order / 2。使用 RFC6979。将 e 作为添加的熵值添加到确定性 k 生成中。
抛出异常
- 如果
!isPrivate(d),则返回Expected Private 期望标量如果h不是 256 位期望额外数据 (32 字节)如果e不是 256 位
signSchnorr (h, d[, e])
signSchnorr :: Buffer -> Buffer [-> Buffer] -> Buffer
返回归一化 schnorr 签名。使用 BIP340 随机数生成。将 e 作为添加的熵值。
抛出异常
- 如果
!isPrivate(d),则返回Expected Private 期望标量如果h不是 256 位期望额外数据 (32 字节)如果e不是 256 位
verify (h, Q, signature[, strict = false])
verify :: Buffer -> Buffer -> Buffer [-> Bool] -> Bool
如果 (r, s) 中的任何一个值等于 0,或者签名被拒绝,则返回 false。
如果 strict 为 true,则拒绝任何 (r, s) 值大于 order / 2 的有效签名。
抛出异常
期望点如果!isPoint(Q)期望签名如果signature的 (r, s) 值不在范围[0...order - 1]期望标量如果h不是 256 位
verifySchnorr (h, Q, signature)
verifySchnorr :: Buffer -> Buffer -> Buffer -> Bool
如果 (r, s) 中的任何一个值等于 0,或者签名被拒绝,则返回 false。
抛出异常
期望点如果!isPoint(Q)期望签名如果signature的 (r, s) 值不在范围[0...order - 1]期望标量如果h不是 256 位
信誉
此库通过 Rust crate secp256k1 使用 bitcoin-core 开发者提供的原生库,通过 Rust crate secp256k1-sys,包括其测试和测试向量的派生。