当前位置:网站首页>sCrypt 中的 ECDSA 签名验证
sCrypt 中的 ECDSA 签名验证
2022-06-30 10:04:00 【sCrypt 智能合约】
我们使用 sCrypt 语言实现了 ECDSA 签名验证算法。它可以验证任意消息是否由与给定公钥对应的私钥签名,而 OP_CHECKSIG 只能在消息是当前花费交易时验证签名¹。令人惊讶的是,这是并不需要引入任何新操作码。而在 BCH 上,需要引入额外的操作码 OP_DATASIGVERIFY(又名 OP_CHECKDATASIG))完成相同的功能的。
椭圆曲线数字签名算法 (ECDSA)
ECDSA 是比特币中用于签名生成和验证的算法。下面列出了验证算法。
实现
如下所示,我们已经实现了该算法,使用我们之前发布的椭圆曲线库。
首先,我们需要从以 DER 格式编码的签名中提取 r
和 s
分量。由于它们是大端编码,我们必须转换为 小端编码,这就是数据在 Script / sCrypt 中的编码方式。
在检索到 r
和 s
后,我们只需运行标准的 ECDSA 验证算法。
import "ec.scrypt";
import "util.scrypt";
struct RSPair {
int r;
int s;
}
// ECDSA signatures verification for secp256k1, for arbitrary message @msg
contract ECDSA {
public function verify(Sig sig, PubKey pubKey, bytes msg, int invS, Point P, int lambda, Point U1, PointMulAux u1Aux, Point U2, PointMulAux u2Aux) {
// extract (r, s) from sig
RSPair rs = parseDERSig(sig);
int r = rs.r;
int s = rs.s;
// within range
require(r >= 1 && r < EC.n);
require(s >= 1 && s < EC.n);
// verify invS
require((s * invS) % EC.n == 1);
int e = unpack(sha256(msg));
int u1 = (e * invS) % EC.n;
int u2 = (r * invS) % EC.n;
// U1 = u1 * G
require(EC.isMul(EC.G, u1, U1, u1Aux));
Point Q = pubKey2Point(pubKey);
// U2 = u2 * Q
require(EC.isMul(Q, u2, U2, u2Aux));
// P == U1 + U2
require(EC.isSum(U1, U2, lambda, P));
// cannot be identify
require(P != EC.ZERO);
require((P.x - r) % EC.n == 0);
}
// parse signature in DER format to get (r, s) pair
static function parseDERSig(Sig sig) : RSPair {
int rLen = unpack(sig[3 : 4]);
int r = fromBESigned(sig[4 : 4 + rLen]);
int sLen = unpack(sig[6 + rLen : 7 + rLen]);
int s = fromBESigned(sig[7 + rLen : 7 + rLen + sLen]);
return {
r , s };
}
// r & s are signed big endian
static function fromBESigned(bytes b) : int {
// convert big-endian to little-endian: either 32 or 33 bytes
bytes bLE = len(b) == 32 ? reverseBytes(b, 32) : reverseBytes(b, 33);
return unpack(bLE);
}
// convert public key to a point, assuming it's uncompressed
static function pubKey2Point(PubKey pubKey) : Point {
require(pubKey[: 1] == b'04');
return {
unpack(pubKey[1 : 33]), unpack(pubKey[33 : 65]) };
}
}
[1] 更准确地说,它针对 sighash 验证签名。
边栏推荐
- 断路器HystrixCircuitBreaker
- Koreano essential creates a professional style
- ionic4 ion-reorder-group组件拖拽改变item顺序
- Typescript – classes in Es5, inheritance, static methods
- 我在鹅厂淘到了一波“炼丹神器”,开发者快打包
- 苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
- Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated
- [rust daily] several new libraries were released on January 23, 2021
- Skill combing [email protected] somatosensory manipulator
- MySQL advanced SQL statement of database (1)
猜你喜欢
Implementation of monitor program with assembly language
CSDN blog operation team 2022 H1 summary
记一次实习的经历,趟坑必备(一)
Yixian e-commerce released its first quarterly report: adhere to R & D and brand investment to achieve sustainable and high-quality development
[email protected] intelligent instrument teaching aids based on 51 series single chip microcomputer"/>
Skill combing [email protected] intelligent instrument teaching aids based on 51 series single chip microcomputer
MySQL index, transaction and storage engine of database (3)
The latest SCI impact factor release: the highest score of domestic journals is 46! Netizen: I understand if
The programmer was beaten.
MATLAB image histogram equalization, namely spatial filtering
Robotframework learning notes: environment installation and robotframework browser plug-in installation
随机推荐
Auto Seg-Loss: 自动损失函数设计
Go -- standard library sort package
苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
无心剑中译狄金森《灵魂择其伴侣》
Questions about cookies and sessions
马斯克推特粉丝过亿了,但他在线失联已一周
mysql数据库基础:约束、标识列
【Rust每周一库】num-bigint - 大整数
ArcGIS Pro脚本工具(6)——修复CAD图层数据源
【深度学习】深度学习检测小目标常用方法
Musk has more than 100 million twitter fans, but he has been lost online for a week
ArcGIS PRO + PS vectorized land use planning map
Gd32 RT thread ota/bootloader driver function
R language plot visualization: use plot to visualize the prediction confidence of the multi classification model, the prediction confidence of each data point of the model in the 2D grid, and the conf
Gd32 RT thread PWM drive function
Tooltips in the era of touch
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
Auto SEG loss: automatic loss function design
59 websites programmers need to know
历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并