当前位置:网站首页>ECDSA signature verification in crypt
ECDSA signature verification in crypt
2022-06-30 12:35:00 【Scrypt smart contract】
We use sCrypt The language implements ECDSA Signature verification algorithm . It can verify whether any message is signed by the private key corresponding to the given public key , and OP_CHECKSIG The signature can only be verified if the message is a currently spent transaction ¹. It's amazing , There is no need to introduce any new opcodes . And in the BCH On , Additional opcodes need to be introduced OP_DATASIGVERIFY( also called OP_CHECKDATASIG)) Complete the same function .

Elliptic curve digital signature algorithm (ECDSA)
ECDSA It is an algorithm used for signature generation and verification in bitcoin . The validation algorithms are listed below .

Realization
As shown below , We have implemented the algorithm , Use the elliptic curve library we released earlier .
First , We need to start with DER Format encoded signature extraction r and s component . Because they are big end codes , We must convert to Small end coding , This is where the data Script / sCrypt How to code in .

In search of r and s after , We just need to run the standard ECDSA Verification algorithm .
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] More precisely , It's for sighash Verify the signature .
边栏推荐
- 695. maximum island area
- 【一天学awk】运算符
- edusoho企培版纯内网部署教程(解决播放器,上传,后台卡顿问题)
- Mysql中 begin..end使用遇到的坑
- 通过EF Core框架根据SQL Server数据库表生成实体类
- Reading the table data of Tencent documents in the applet
- Building a database model using power designer tools
- Tencent two sides: @bean and @component are used on the same class. What happens?
- Vscode select multiple words
- 实现多方数据安全共享,解决普惠金融信息不对称难题
猜你喜欢

SuperMap iClient3D 11i for Cesium三维场景中图例使用说明

qt msvc 安装及调试

QT implementation dynamic navigation bar

Conference Preview - Huawei 2012 lab global software technology summit - European session

Map集合
![[cloud native | kubernetes] in depth understanding of deployment (VIII)](/img/88/4eddb8e6535a12541867b027b109a1.png)
[cloud native | kubernetes] in depth understanding of deployment (VIII)

Four Misunderstandings of Internet Marketing

How to select an OLAP database engine?

Biological network analysis using deep learning

SuperMap iclient3d for webgl loading TMS tiles
随机推荐
“\“id\“ contains an invalid value“
What is the principle of spectral confocal displacement sensor? Which fields can be applied?
New function of SuperMap iserver11i -- release and use of legend
Swagger2 automatically generates API documents
Inner join and outer join of MySQL tables
pyqt5界面的布局与资源文件的载入
Two batches of pure milk are unqualified? Michael responded that he was conducting a large-scale screening and sampling inspection of products
Embedded sig | multi OS hybrid deployment framework
Statistics on the number of closed Islands
QT MSVC installation and commissioning
qt msvc 安装及调试
Lichuang EDA learning notes 10 common connector component identification and passive buzzer driving circuit
Ensemble de cartes
Redis installation on Linux system
Map集合
Biological network analysis using deep learning
90. (cesium chapter) cesium high level listening events
海思3559开发常识储备:相关名词全解
A new journey of the smart court, paperless office, escorting the green trial of the smart court
Pharmacy management system