当前位置:网站首页>Efficient elliptic curve point addition and multiplication in scrypt
Efficient elliptic curve point addition and multiplication in scrypt
2022-06-30 12:35:00 【Scrypt smart contract】
We propose a novel and effective method , Used to calculate point addition and scalar multiplication on elliptic curve in bitcoin script . For points add , We will surpass 1MB Script size reduced to ~400 byte .

Point addition
For each i, Each point Pi By two coordinates (xi, yj) Express . For calculation P3 = P1 + P2, We use the following formula

Add some formula
If P1 != P2,

otherwise

A simple implementation requires computing the reciprocal of the modulus , Apply extended Euclidean algorithm . However , This will cause the script size to be too large , Because the exact number of cycles in the algorithm is unknown in advance , And must use a large conservative upper limit .
Efficient solutions
We don't just add points , But by passing the expected point in the unlock script P3 To solve this problem . We only verify in the script P3 = P1 + P2. To avoid modulo reciprocal in verification , We convert the formula into the following equivalent form .
When P1 != P2

When P1 == P2

And P3 equally ,λ It is also pre calculated under the chain , And pass... In the unlock script , As shown below . This produces a very compact script , The size is only ~400B.
static function isSumHelper(Point p1, Point p2, int lambda, Point p) : bool {
// check lambda is indeed gradient
bool lambdaOK = (p1 == p2) ?
(2 * lambda * p1.y - 3 * p1.x * p1.x) % P == 0 :
(lambda * (p2.x - p1.x) - (p2.y - p1.y)) % P == 0;
// also check p = p1 + p2
return lambdaOK && (lambda * lambda - p1.x - p2.x - p.x) % P == 0 &&
(lambda * (p1.x - p.x) - p1.y - p.y) % P == 0;
}
// return true if lambda is the gradient of the line between p1 and p2
// and p = p1 + p2
static function isSum(Point p1, Point p2, int lambda, Point p) : bool {
// special handling of point ZERO
bool ret = p1 == ZERO ? p2 == p : (p2 == ZERO ? p1 == p : (p1.x == p2.x && (p1.y + p2.y) % P == 0) ? p == ZERO : true);
return ret && isSumHelper(p1, p2, lambda, p);
}
x * P = (x0 + x1 * 2 + x2 * 4 + x3 * 8 + … + x255 * 2²⁵⁵) * P
= x0 * P + x1 * (2P) + x2 * (4P) + x3 * (8P) + … + x255 * (2²⁵⁵P)
x0, x1, x2, …, x255 It's scalar x The bit of "s" means , From least significant bit to most significant bit . We calculate in advance 2P, 4P, 8P, …, 2²⁵⁵P Chain them down and pass them to the unlock script , These are verified in the locking script , As shown in the following paragraph 21-24 Line .
// return true iff p * x == r
static function isMul(Point p, int x, Point r, Point[EC.N] pMultiples, Point[EC.N] qs, int[EC.N1] lambdas1, int[EC.N1] lambdas2) : bool {
// validate pMultiples = [p, 2p, 4p, 8p, ...]
loop (N) : i {
require(i == 0 ? pMultiples[i] == p : isSum(pMultiples[i - 1], pMultiples[i - 1], lambdas1[i - 1], pMultiples[i]));
}
// // x * p = x0 * p + x1 *(2p) + x2 * (4p) + x3 * (8p) + ...
// // xi is the i-th bit of x
Point P0 = ZERO;
loop (N) : i {
Point P = x % 2 ? pMultiples[i] : ZERO;
// right shift by 1
x /= 2;
if (i == 0) {
P0 = P;
} else if (i == 1) {
// first
require(isSum(P0, P, lambdas2[i - 1], qs[i - 1]));
} else {
// rest
require(isSum(qs[i - 1], P, lambdas2[i - 1], i < N1 ? qs[i] : r));
}
}
return true;
}
thank
This article is based on Craig Wright and Owen Vaughan The job of , As well as from nChain Of Enrique Larraia and Owen Vaughan Valuable feedback from .
边栏推荐
猜你喜欢

STM32 porting the fish component of RT thread Standard Edition

Hannaiping of Qilin software: the construction of Digital China needs its own open source root community

edusoho企培版纯内网部署教程(解决播放器,上传,后台卡顿问题)

SuperMap iClient3D for WebGL 加载TMS瓦片

实现多方数据安全共享,解决普惠金融信息不对称难题

AGCO AI frontier promotion (6.30)

【LeetCode】15、三数之和

How to select an OLAP database engine?

Biological network analysis using deep learning

Redis的配置文件及新数据类型
随机推荐
Joplin实现样式更改
Subtrate 源码追新导读-5月上旬: XCM 正式启用
浅谈 JMeter 运行原理
Instructions for legend use in SuperMap iclient3d 11i for cesium 3D scene
Four Misunderstandings of Internet Marketing
海思3559开发常识储备:相关名词全解
How difficult is data governance and data innovation?
Embedded sig | multi OS hybrid deployment framework
ES6新特性介绍
Idea has a new artifact, a set of code to adapt to multiple terminals!
Tencent two sides: @bean and @component are used on the same class. What happens?
90. (cesium chapter) cesium high level listening events
SuperMap iClient3D 11i for Cesium三维场景中图例使用说明
SuperMap iClient3D for WebGL 加载TMS瓦片
60 divine vs Code plug-ins!!
Sword finger offer 05 Replace spaces: replace each space in the string s with "%20"“
8253 counter introduction
SuperMap 3D SDKs_Unity插件开发——连接数据服务进行SQL查询
Beego development blog system learning (II)
SuperMap iclient3d for webgl loading TMS tiles