当前位置:网站首页>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 .
边栏推荐
- 【BUG解决】fiftyone报AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘错误解决方法
- Solve the problem that the server cannot be connected via SSH during reinstallation
- Substrate 源码追新导读: Call调用索引化, 存储层事物化全面完成
- Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions
- 90. (cesium chapter) cesium high level listening events
- Redis-缓存问题
- SuperMap iClient3D for WebGL 加载TMS瓦片
- The realization of QT the flipping effect of QQ weather forecast window
- Some commonly used hardware information of the server (constantly updated)
- MySQL判断执行条件为NULL时,返回0,出错问题解决 Incorrect parameter count in the call to native function ‘ISNULL‘,
猜你喜欢
随机推荐
Understanding and learning of MySQL indexing and optimization
Splitting e-commerce systems into micro services
JMeter之事务控制器
SuperMap iDesktop 常见倾斜数据处理全流程解析
SuperMap iServer11i新功能----图例的发布和使用
海思3559萬能平臺搭建:獲取數據幀修改後編碼
【一天学awk】正则匹配
SuperMap 3D SDKs_Unity插件开发——连接数据服务进行SQL查询
Redis - problèmes de cache
Flink sql控制台,不识别group_concat函数吗?
Beego development blog system learning (II)
Redis6 learning notes - Chapter 2 - Basic redis6 operations
Map collection
grep匹配查找
剑指 Offer 05. 替换空格: 把字符串 s 中的每个空格替换成“%20“
“\“id\“ contains an invalid value“
【一天学awk】基础中的基础
Redis-缓存问题
SuperMap iClient3D for WebGL 加载TMS瓦片
The website with id 0 that was requested wasn‘t found. Verify the website and try again









![移除无效的括号[用数组模拟栈]](/img/df/0a2ae5ae40adb833d52b2dddea291b.png)