当前位置:网站首页>在 sCrypt 中实现高效的椭圆曲线点加法和乘法
在 sCrypt 中实现高效的椭圆曲线点加法和乘法
2022-06-30 10:04:00 【sCrypt 智能合约】
我们提出了一种新颖有效的方法,用于在比特币脚本中计算椭圆曲线上的点加法和标量乘法。对于点添加,我们将超过 1MB 的脚本大小减少到 ~400 字节。

点加法
对于每个 i,每个点 Pi 由两个坐标 (xi, yj) 表示。为了计算 P3 = P1 + P2,我们使用以下公式

加点公式
如果 P1 != P2,

否则

一个简单的实现需要计算模倒数,应用扩展的欧几里德算法。然而,这会导致脚本大小过大,因为算法中的确切循环数事先未知,并且必须使用大的保守上限。
高效的解决方案
我们不是直接计算加点,而是通过在解锁脚本中传递预期点 P3 来解决这个问题。我们只在脚本中验证 P3 = P1 + P2。为了避免验证中的模倒数,我们将公式转化为以下等价形式。
当 P1 != P2

当 P1 == P2

与 P3 一样,λ 也是链下预先计算的,并在解锁脚本中传递,如下所示。这将产生非常紧凑的脚本,大小只有 ~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 是标量 x 的位表示,从最低有效位到最高有效位。我们预先计算 2P, 4P, 8P, …, 2²⁵⁵P 链下并将它们传递到解锁脚本中,这些在锁定脚本中得到验证,如下面的第 21-24 行所示。
// 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;
}
致谢
本文基于 Craig Wright 和 Owen Vaughan 的工作,以及来自 nChain 的 Enrique Larraia 和 Owen Vaughan 的宝贵反馈。
边栏推荐
- Gd32 RT thread ota/bootloader driver function
- ArcGIS Pro scripting tool (6) -- repairing CAD layer data sources
- 我在鹅厂淘到了一波“炼丹神器”,开发者快打包
- 7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
- mysql数据库基础:TCL事务控制语言
- The AOV function of R language was used for repeated measures ANOVA (one intra group factor and one inter group factor) and interaction Plot function and boxplot to visualize the interaction
- 程序员需知的 59 个网站
- Turn to cartoon learning notes
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
- Configure Yii: display MySQL extension module verification failed
猜你喜欢

透過華為軍團看科技之變(五):智慧園區

Google 辟谣放弃 TensorFlow,它还活着!
[email protected] control a dog's running on OLED"/>Skill combing [email protected] control a dog's running on OLED

ArcGIS Pro脚本工具(5)——排序后删除重复项

mysql数据库基础:视图、变量
[email protected]语音模块+stm32+nfc"/>技能梳理[email protected]语音模块+stm32+nfc

马斯克推特粉丝过亿了,但他在线失联已一周

I found a wave of "alchemy artifact" in the goose factory. The developer should pack it quickly

Android 开发面试真题进阶版(附答案解析)

Machine learning interview preparation (I) KNN
随机推荐
Gd32 RT thread RTC driver function
技能梳理[email protected]语音模块+stm32+nfc
马斯克推特粉丝过亿了,但他在线失联已一周
Skill sorting [email protected]+adxl345+ Motor vibration + serial port output
历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并
Typescript – classes in Es5, inheritance, static methods
JS FAQs
Skill combing [email protected] somatosensory manipulator
05_Node js 文件管理模块 fs
Didi open source agile test case management platform!
The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
MySQL advanced SQL statement of database (1)
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
Auto SEG loss: automatic loss function design
滴滴开源敏捷测试用例管理平台!
Musk has more than 100 million twitter fans, but he has been lost online for a week
Jinbei LT6 is powerful in the year of the tiger, making waves
半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
The performance of arm's new CPU has been improved by 22%, up to 12 cores can be combined, and the GPU is first equipped with hardware optical tracking. Netizen: the gap with apple is growing
移植完整版RT-Thread到GD32F4XX(详细)