当前位置:网站首页>在 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 的宝贵反馈。
边栏推荐
- 苹果5G芯片被曝研发失败,QQ密码bug引热议,蔚来回应做空传闻,今日更多大新闻在此...
- 安徽《合肥市装配式建筑施工图审查设计深度要求》印发;河北衡水市调整装配式建筑预售许可标准
- GeoffreyHinton:我的五十年深度学习生涯与研究心法
- Turn to cartoon learning notes
- 我的远程办公深度体验 | 社区征文
- [rust weekly database] num bigint - large integer
- mysql数据库基础:TCL事务控制语言
- 最新SCI影响因子公布:国产期刊最高破46分!网友:算是把IF玩明白了
- Leetcode question brushing (III) -- binary search (go Implementation)
- Viewing technological changes through Huawei Corps (V): smart Park
猜你喜欢
[email protected]在oled上控制一条狗的奔跑"/>技能梳理[email protected]在oled上控制一条狗的奔跑

Google 辟谣放弃 TensorFlow,它还活着!

Migrate full RT thread to gd32f4xx (detailed)

MySQL advanced SQL statement of database (2)

Harvester ch1 of CKB and HNS, connection tutorial analysis

我在鹅厂淘到了一波“炼丹神器”,开发者快打包
[email protected] voice module +stm32+nfc"/>Skill combing [email protected] voice module +stm32+nfc

Viewing technological changes through Huawei Corps (V): smart Park

Getting started with X86 - take over bare metal control

MATLAB image histogram equalization, namely spatial filtering
随机推荐
运动App如何实现端侧后台保活,让运动记录更完整?
技能梳理[email protected]+阿里云+nbiot+dht11+bh1750+土壤湿度传感器+oled
那个程序员,被打了。
IPhone address book import into Excel
ArcGIS Pro脚本工具(6)——修复CAD图层数据源
我的远程办公深度体验 | 社区征文
技能梳理[email protected]基于51系列单片机的智能仪器教具
[rust weekly database] num bigint - large integer
逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展
How to seize the opportunity of NFT's "chaos"?
Curl --- the request fails when the post request parameter is too long (more than 1024b)
Leetcode question brushing (I) -- double pointer (go Implementation)
如何解决跨域
7 大轻量易用的工具,给开发者减压提效,助力企业敏捷上云 | Techo Day 精彩回顾...
Leetcode question brushing (II) -- sorting (go Implementation)
mysql数据库基础:TCL事务控制语言
Using LVM to resize partitions
Getting started with X86 - take over bare metal control
微信推出图片大爆炸功能;苹果自研 5G 芯片或已失败;微软解决导致 Edge 停止响应的 bug|极客头条...
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