std.crypto.pcurves.secp256k1.AffineCoordinates
容器
仿射坐标中的点。
字段
std.crypto.pcurves.secp256k1.AffineCoordinates.cMov
函数
fn cMov(p: *AffineCoordinates, a: AffineCoordinates, c: u1) void
参数
std.crypto.pcurves.secp256k1.AffineCoordinates.identityElement
常量
以仿射坐标表示的单位元。
std.crypto.pcurves.secp256k1.AffineCoordinates.neg
函数
pub fn neg(p: AffineCoordinates) AffineCoordinates
参数
std.crypto.pcurves.secp256k1.crypto
别名
别名目标 std.crypto
std.crypto.pcurves.secp256k1.EncodingError
别名
std.crypto.pcurves.secp256k1.IdentityElementError
别名
std.crypto.pcurves.secp256k1.math
别名
别名目标 std.math
std.crypto.pcurves.secp256k1.mem
别名
别名目标 std.mem
std.crypto.pcurves.secp256k1.meta
别名
别名目标 std.meta
std.crypto.pcurves.secp256k1.NonCanonicalError
别名
std.crypto.pcurves.secp256k1.NotSquareError
别名
std.crypto.pcurves.secp256k1.Secp256k1
容器
secp256k1 上的群运算。
字段
FescalarbasePointidentityElementBEndomorphismrejectIdentityfromAffineCoordinatesfromSerializedAffineCoordinatesrecoverYfromSec1toCompressedSec1toUncompressedSec1randomnegdbladdMixedaddsubsubMixedaffineCoordinatesequivalentcMovpcSelectslidemulMultiPublicpcMul16precomputebasePointPcmulmulPublicmulDoubleBasePublic
std.crypto.pcurves.secp256k1.Secp256k1.add
函数
将 secp256k1 点相加。
pub fn add(p: Secp256k1, q: Secp256k1) Secp256k1
参数
std.crypto.pcurves.secp256k1.Secp256k1.addMixed
函数
相加两个 secp256k1 点,第二个点使用仿射坐标指定。
pub fn addMixed(p: Secp256k1, q: AffineCoordinates) Secp256k1
参数
p: Secp256k1
q: AffineCoordinates
std.crypto.pcurves.secp256k1.Secp256k1.affineCoordinates
函数
返回仿射坐标。
pub fn affineCoordinates(p: Secp256k1) AffineCoordinates
参数
p: Secp256k1
std.crypto.pcurves.secp256k1.Secp256k1.B
常量
std.crypto.pcurves.secp256k1.Secp256k1.basePoint
常量
secp256k1 基点。
std.crypto.pcurves.secp256k1.Secp256k1.basePointPc
常量
std.crypto.pcurves.secp256k1.Secp256k1.cMov
函数
fn cMov(p: *Secp256k1, a: Secp256k1, c: u1) void
参数
std.crypto.pcurves.secp256k1.Secp256k1.dbl
函数
对 secp256k1 点执行倍点。
pub fn dbl(p: Secp256k1) Secp256k1
参数
p: Secp256k1
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism
命名空间
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.b1_neg
常量
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.b2_neg
常量
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.beta
常量
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.lambda
常量
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.scalarFromInt
函数
fn scalarFromInt(x: u256) scalar.Scalar
参数
x: u256
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.splitScalar
函数
计算 r1 与 r2,使 k = r1 + r2*lambda (mod L)。 两个输出均使用所请求的字节序。
pub fn splitScalar(s: [32]u8, endian: std.lang.Endian) NonCanonicalError!SplitScalar
参数
错误
- NonCanonical NonCanonicalError
std.crypto.pcurves.secp256k1.Secp256k1.Endomorphism.SplitScalar
容器
字段
r1: [32]u8
r2: [32]u8
std.crypto.pcurves.secp256k1.Secp256k1.equivalent
函数
两组坐标表示同一点时返回 true。
pub fn equivalent(a: Secp256k1, b: Secp256k1) bool
参数
std.crypto.pcurves.secp256k1.Secp256k1.Fe
别名
底层素数域。
std.crypto.pcurves.secp256k1.Secp256k1.fromAffineCoordinates
函数
检查仿射坐标满足曲线方程后,由其创建点。
pub fn fromAffineCoordinates(p: AffineCoordinates) EncodingError!Secp256k1
参数
错误
- InvalidEncoding EncodingError
std.crypto.pcurves.secp256k1.Secp256k1.fromSec1
函数
反序列化 SEC1 编码的点。
pub fn fromSec1(s: []const u8) (EncodingError || NotSquareError || NonCanonicalError)!Secp256k1
参数
s: []const u8
std.crypto.pcurves.secp256k1.Secp256k1.fromSerializedAffineCoordinates
函数
从序列化的仿射坐标创建点。
pub fn fromSerializedAffineCoordinates(xs: [32]u8, ys: [32]u8, endian: std.lang.Endian) (NonCanonicalError || EncodingError)!Secp256k1
参数
std.crypto.pcurves.secp256k1.Secp256k1.identityElement
常量
secp256k1 的单位元。
std.crypto.pcurves.secp256k1.Secp256k1.mul
函数
将椭圆曲线点乘以标量。 若结果为单位元,返回 error.IdentityElement。
pub fn mul(p: Secp256k1, s_: [32]u8, endian: std.lang.Endian) IdentityElementError!Secp256k1
参数
错误
- IdentityElement IdentityElementError
std.crypto.pcurves.secp256k1.Secp256k1.mulDoubleBasePublic
函数
对公开参数执行双基点乘法:以可变时间计算 (p1s1)+(p2s2)。 可用于签名验证。
pub fn mulDoubleBasePublic(p1: Secp256k1, s1: [32]u8, p2: Secp256k1, s2: [32]u8, endian: std.lang.Endian) IdentityElementError!Secp256k1
参数
错误
- IdentityElement IdentityElementError
std.crypto.pcurves.secp256k1.Secp256k1.mulMultiPublic
函数
fn mulMultiPublic(comptime n: usize, p: [n]Secp256k1, s: [n][32]u8, endian: std.lang.Endian) IdentityElementError!Secp256k1
参数
错误
- IdentityElement IdentityElementError
std.crypto.pcurves.secp256k1.Secp256k1.mulPublic
函数
以可变时间将椭圆曲线点乘以公开标量。 可用于签名验证。
pub fn mulPublic(p: Secp256k1, s: [32]u8, endian: std.lang.Endian) (IdentityElementError || NonCanonicalError)!Secp256k1
参数
std.crypto.pcurves.secp256k1.Secp256k1.neg
函数
翻转 X 坐标的符号。
pub fn neg(p: Secp256k1) Secp256k1
参数
p: Secp256k1
std.crypto.pcurves.secp256k1.Secp256k1.pcMul16
函数
fn pcMul16(pc: *const [16]Secp256k1, s: [32]u8, comptime vartime: bool) IdentityElementError!Secp256k1
参数
错误
- IdentityElement IdentityElementError
std.crypto.pcurves.secp256k1.Secp256k1.pcSelect
函数
fn pcSelect(comptime n: usize, pc: *const [n]Secp256k1, b: u8) Secp256k1
参数
std.crypto.pcurves.secp256k1.Secp256k1.precompute
函数
fn precompute(p: Secp256k1, comptime count: usize) [1 + count]Secp256k1
参数
p: Secp256k1
count: usize
std.crypto.pcurves.secp256k1.Secp256k1.random
函数
返回随机点。
pub fn random(io: std.Io) Secp256k1
参数
std.crypto.pcurves.secp256k1.Secp256k1.recoverY
函数
由 X 坐标恢复 Y 坐标。
pub fn recoverY(x: Fe, is_odd: bool) NotSquareError!Fe
参数
x: Fe
is_odd: bool
错误
- NotSquare NotSquareError
std.crypto.pcurves.secp256k1.Secp256k1.rejectIdentity
函数
拒绝单位元。
pub fn rejectIdentity(p: Secp256k1) IdentityElementError!void
参数
p: Secp256k1
错误
- IdentityElement IdentityElementError
std.crypto.pcurves.secp256k1.Secp256k1.scalar
别名
模主子群阶的域算术。
std.crypto.pcurves.secp256k1.Secp256k1.slide
函数
fn slide(s: [32]u8) [2 * 32 + 1]i8
参数
s: [32]u8
std.crypto.pcurves.secp256k1.Secp256k1.sub
函数
计算 secp256k1 点相减。
pub fn sub(p: Secp256k1, q: Secp256k1) Secp256k1
参数
std.crypto.pcurves.secp256k1.Secp256k1.subMixed
函数
对 secp256k1 点执行减法,第二个点以仿射坐标给出。
pub fn subMixed(p: Secp256k1, q: AffineCoordinates) Secp256k1
参数
p: Secp256k1
q: AffineCoordinates
std.crypto.pcurves.secp256k1.Secp256k1.toCompressedSec1
函数
使用压缩 SEC-1 格式序列化点。
pub fn toCompressedSec1(p: Secp256k1) [33]u8
参数
p: Secp256k1
std.crypto.pcurves.secp256k1.Secp256k1.toUncompressedSec1
函数
使用未压缩 SEC-1 格式序列化点。
pub fn toUncompressedSec1(p: Secp256k1) [65]u8
参数
p: Secp256k1
std.crypto.pcurves.secp256k1.std
别名
别名目标 std
源码
std/crypto/pcurves/secp256k1.zig
const std = @import("std");
const crypto = std.crypto;
const math = std.math;
const mem = std.mem;
const meta = std.meta;
const EncodingError = crypto.errors.EncodingError;
const IdentityElementError = crypto.errors.IdentityElementError;
const NonCanonicalError = crypto.errors.NonCanonicalError;
const NotSquareError = crypto.errors.NotSquareError;
/// secp256k1 上的群运算。
pub const Secp256k1 = struct {
/// 底层素数域。
pub const Fe = @import("secp256k1/field.zig").Fe;
/// 模主子群阶的域算术。
pub const scalar = @import("secp256k1/scalar.zig");
x: Fe,
y: Fe,
z: Fe = Fe.one,
is_base: bool = false,
/// secp256k1 基点。
pub const basePoint = Secp256k1{
.x = Fe.fromInt(55066263022277343669578718895168534326250603453777594175500187360389116729240) catch unreachable,
.y = Fe.fromInt(32670510020758816978083085130507043184471273380659243275938904335757337482424) catch unreachable,
.z = Fe.one,
.is_base = true,
};
/// secp256k1 的单位元。
pub const identityElement = Secp256k1{ .x = Fe.zero, .y = Fe.one, .z = Fe.zero };
pub const B = Fe.fromInt(7) catch unreachable;
pub const Endomorphism = struct {
const lambda = scalarFromInt(37718080363155996902926221483475020450927657555482586988616620542887997980018);
const beta = Fe.fromInt(55594575648329892869085402983802832744385952214688224221778511981742606582254) catch unreachable;
const b1_neg = scalarFromInt(303414439467246543595250775667605759171);
const b2_neg = scalarFromInt(scalar.field_order - 64502973549206556628585045361533709077);
fn scalarFromInt(x: u256) scalar.Scalar {
var buf: [32]u8 = undefined;
mem.writeInt(u256, &buf, x, .little);
return scalar.Scalar.fromBytes(buf, .little) catch unreachable;
}
pub const SplitScalar = struct {
r1: [32]u8,
r2: [32]u8,
};
/// 计算 r1 与 r2,使 k = r1 + r2*lambda (mod L)。
/// 两个输出均使用所请求的字节序。
pub fn splitScalar(s: [32]u8, endian: std.lang.Endian) NonCanonicalError!SplitScalar {
const k = try scalar.Scalar.fromBytes(s, endian);
const k_int = mem.readInt(u256, &s, endian);
const t1 = math.mulWide(u256, k_int, 21949224512762693861512883645436906316123769664773102907882521278123970637873);
const t2 = math.mulWide(u256, k_int, 103246583619904461035481197785446227098457807945486720222659797044629401272177);
const c1 = @as(u128, @truncate(t1 >> 384)) + @as(u1, @truncate(t1 >> 383));
const c2 = @as(u128, @truncate(t2 >> 384)) + @as(u1, @truncate(t2 >> 383));
const r2 = scalarFromInt(c1).mul(b1_neg).add(scalarFromInt(c2).mul(b2_neg));
const r1 = k.sub(r2.mul(lambda));
return .{ .r1 = r1.toBytes(endian), .r2 = r2.toBytes(endian) };
}
};
/// 拒绝单位元。
pub fn rejectIdentity(p: Secp256k1) IdentityElementError!void {
const affine_0 = @intFromBool(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@intFromBool(p.y.isZero()) | @intFromBool(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @intFromBool(p.z.isZero()) | affine_0;
if (is_identity != 0) {
return error.IdentityElement;
}
}
/// 检查仿射坐标满足曲线方程后,由其创建点。
pub fn fromAffineCoordinates(p: AffineCoordinates) EncodingError!Secp256k1 {
const x = p.x;
const y = p.y;
const x3B = x.sq().mul(x).add(B);
const yy = y.sq();
if (!x3B.equivalent(yy)) {
return error.InvalidEncoding;
}
return .{ .x = x, .y = y, .z = Fe.one };
}
/// 从序列化的仿射坐标创建点。
pub fn fromSerializedAffineCoordinates(xs: [32]u8, ys: [32]u8, endian: std.lang.Endian) (NonCanonicalError || EncodingError)!Secp256k1 {
const x = try Fe.fromBytes(xs, endian);
const y = try Fe.fromBytes(ys, endian);
return fromAffineCoordinates(.{ .x = x, .y = y });
}
/// 由 X 坐标恢复 Y 坐标。
pub fn recoverY(x: Fe, is_odd: bool) NotSquareError!Fe {
const x3B = x.sq().mul(x).add(B);
var y = try x3B.sqrt();
const yn = y.neg();
y.cMov(yn, @intFromBool(is_odd) ^ @intFromBool(y.isOdd()));
return y;
}
/// 反序列化 SEC1 编码的点。
pub fn fromSec1(s: []const u8) (EncodingError || NotSquareError || NonCanonicalError)!Secp256k1 {
if (s.len < 1) return error.InvalidEncoding;
const encoding_type = s[0];
const encoded = s[1..];
switch (encoding_type) {
0 => {
if (encoded.len != 0) return error.InvalidEncoding;
return Secp256k1.identityElement;
},
2, 3 => {
if (encoded.len != 32) return error.InvalidEncoding;
const x = try Fe.fromBytes(encoded[0..32].*, .big);
const y_is_odd = (encoding_type == 3);
const y = try recoverY(x, y_is_odd);
return Secp256k1{ .x = x, .y = y };
},
4 => {
if (encoded.len != 64) return error.InvalidEncoding;
const x = try Fe.fromBytes(encoded[0..32].*, .big);
const y = try Fe.fromBytes(encoded[32..64].*, .big);
return Secp256k1.fromAffineCoordinates(.{ .x = x, .y = y });
},
else => return error.InvalidEncoding,
}
}
/// 使用压缩 SEC-1 格式序列化点。
pub fn toCompressedSec1(p: Secp256k1) [33]u8 {
var out: [33]u8 = undefined;
const xy = p.affineCoordinates();
out[0] = if (xy.y.isOdd()) 3 else 2;
out[1..].* = xy.x.toBytes(.big);
return out;
}
/// 使用未压缩 SEC-1 格式序列化点。
pub fn toUncompressedSec1(p: Secp256k1) [65]u8 {
var out: [65]u8 = undefined;
out[0] = 4;
const xy = p.affineCoordinates();
out[1..33].* = xy.x.toBytes(.big);
out[33..65].* = xy.y.toBytes(.big);
return out;
}
/// 返回随机点。
pub fn random(io: std.Io) Secp256k1 {
const n = scalar.random(io, .little);
return basePoint.mul(n, .little) catch unreachable;
}
/// 翻转 X 坐标的符号。
pub fn neg(p: Secp256k1) Secp256k1 {
return .{ .x = p.x, .y = p.y.neg(), .z = p.z };
}
/// 对 secp256k1 点执行倍点。
// 算法 9 来自 https://eprint.iacr.org/2015/1060.pdf
pub fn dbl(p: Secp256k1) Secp256k1 {
var t0 = p.y.sq();
var Z3 = t0.dbl();
Z3 = Z3.dbl();
Z3 = Z3.dbl();
var t1 = p.y.mul(p.z);
var t2 = p.z.sq();
// b3 = (2^2)^2 + 2^2 + 1
const t2_4 = t2.dbl().dbl();
t2 = t2_4.dbl().dbl().add(t2_4).add(t2);
var X3 = t2.mul(Z3);
var Y3 = t0.add(t2);
Z3 = t1.mul(Z3);
t1 = t2.dbl();
t2 = t1.add(t2);
t0 = t0.sub(t2);
Y3 = t0.mul(Y3);
Y3 = X3.add(Y3);
t1 = p.x.mul(p.y);
X3 = t0.mul(t1);
X3 = X3.dbl();
return .{
.x = X3,
.y = Y3,
.z = Z3,
};
}
/// 相加两个 secp256k1 点,第二个点使用仿射坐标指定。
// 来自 https://eprint.iacr.org/2015/1060.pdf 的算法 8
pub fn addMixed(p: Secp256k1, q: AffineCoordinates) Secp256k1 {
var t0 = p.x.mul(q.x);
var t1 = p.y.mul(q.y);
var t3 = q.x.add(q.y);
var t4 = p.x.add(p.y);
t3 = t3.mul(t4);
t4 = t0.add(t1);
t3 = t3.sub(t4);
t4 = q.y.mul(p.z);
t4 = t4.add(p.y);
var Y3 = q.x.mul(p.z);
Y3 = Y3.add(p.x);
var X3 = t0.dbl();
t0 = X3.add(t0);
// b3 = (2^2)^2 + 2^2 + 1
const t2_4 = p.z.dbl().dbl();
var t2 = t2_4.dbl().dbl().add(t2_4).add(p.z);
var Z3 = t1.add(t2);
t1 = t1.sub(t2);
const Y3_4 = Y3.dbl().dbl();
Y3 = Y3_4.dbl().dbl().add(Y3_4).add(Y3);
X3 = t4.mul(Y3);
t2 = t3.mul(t1);
X3 = t2.sub(X3);
Y3 = Y3.mul(t0);
t1 = t1.mul(Z3);
Y3 = t1.add(Y3);
t0 = t0.mul(t3);
Z3 = Z3.mul(t4);
Z3 = Z3.add(t0);
var ret = Secp256k1{
.x = X3,
.y = Y3,
.z = Z3,
};
ret.cMov(p, @intFromBool(q.x.isZero()));
return ret;
}
/// 将 secp256k1 点相加。
// https://eprint.iacr.org/2015/1060.pdf 中的算法 7。
pub fn add(p: Secp256k1, q: Secp256k1) Secp256k1 {
var t0 = p.x.mul(q.x);
var t1 = p.y.mul(q.y);
var t2 = p.z.mul(q.z);
var t3 = p.x.add(p.y);
var t4 = q.x.add(q.y);
t3 = t3.mul(t4);
t4 = t0.add(t1);
t3 = t3.sub(t4);
t4 = p.y.add(p.z);
var X3 = q.y.add(q.z);
t4 = t4.mul(X3);
X3 = t1.add(t2);
t4 = t4.sub(X3);
X3 = p.x.add(p.z);
var Y3 = q.x.add(q.z);
X3 = X3.mul(Y3);
Y3 = t0.add(t2);
Y3 = X3.sub(Y3);
X3 = t0.dbl();
t0 = X3.add(t0);
// b3 = (2^2)^2 + 2^2 + 1
const t2_4 = t2.dbl().dbl();
t2 = t2_4.dbl().dbl().add(t2_4).add(t2);
var Z3 = t1.add(t2);
t1 = t1.sub(t2);
const Y3_4 = Y3.dbl().dbl();
Y3 = Y3_4.dbl().dbl().add(Y3_4).add(Y3);
X3 = t4.mul(Y3);
t2 = t3.mul(t1);
X3 = t2.sub(X3);
Y3 = Y3.mul(t0);
t1 = t1.mul(Z3);
Y3 = t1.add(Y3);
t0 = t0.mul(t3);
Z3 = Z3.mul(t4);
Z3 = Z3.add(t0);
return .{
.x = X3,
.y = Y3,
.z = Z3,
};
}
/// 计算 secp256k1 点相减。
pub fn sub(p: Secp256k1, q: Secp256k1) Secp256k1 {
return p.add(q.neg());
}
/// 对 secp256k1 点执行减法,第二个点以仿射坐标给出。
pub fn subMixed(p: Secp256k1, q: AffineCoordinates) Secp256k1 {
return p.addMixed(q.neg());
}
/// 返回仿射坐标。
pub fn affineCoordinates(p: Secp256k1) AffineCoordinates {
const affine_0 = @intFromBool(p.x.equivalent(AffineCoordinates.identityElement.x)) & (@intFromBool(p.y.isZero()) | @intFromBool(p.y.equivalent(AffineCoordinates.identityElement.y)));
const is_identity = @intFromBool(p.z.isZero()) | affine_0;
const zinv = p.z.invert();
var ret = AffineCoordinates{
.x = p.x.mul(zinv),
.y = p.y.mul(zinv),
};
ret.cMov(AffineCoordinates.identityElement, is_identity);
return ret;
}
/// 两组坐标表示同一点时返回 true。
pub fn equivalent(a: Secp256k1, b: Secp256k1) bool {
if (a.sub(b).rejectIdentity()) {
return false;
} else |_| {
return true;
}
}
fn cMov(p: *Secp256k1, a: Secp256k1, c: u1) void {
p.x.cMov(a.x, c);
p.y.cMov(a.y, c);
p.z.cMov(a.z, c);
}
fn pcSelect(comptime n: usize, pc: *const [n]Secp256k1, b: u8) Secp256k1 {
var t = Secp256k1.identityElement;
comptime var i: u8 = 1;
inline while (i < pc.len) : (i += 1) {
t.cMov(pc[i], @as(u1, @truncate((@as(usize, b ^ i) -% 1) >> 8)));
}
return t;
}
fn slide(s: [32]u8) [2 * 32 + 1]i8 {
var e: [2 * 32 + 1]i8 = undefined;
for (s, 0..) |x, i| {
e[i * 2 + 0] = @as(i8, @as(u4, @truncate(x)));
e[i * 2 + 1] = @as(i8, @as(u4, @truncate(x >> 4)));
}
// 此时 e[0..63] 位于 0 到 15 之间,e[63] 位于 0 到 7 之间
var carry: i8 = 0;
for (e[0..64]) |*x| {
x.* += carry;
carry = (x.* + 8) >> 4;
x.* -= carry * 16;
std.debug.assert(x.* >= -8 and x.* <= 8);
}
e[64] = carry;
// 现在 e[*] 均位于 -8 到 8 之间,包括 e[64]。
std.debug.assert(carry >= -8 and carry <= 8);
return e;
}
// 以*可变时间*计算 s[i]*p[i] 之和。标量可以大于群阶。
fn mulMultiPublic(comptime n: usize, p: [n]Secp256k1, s: [n][32]u8, endian: std.lang.Endian) IdentityElementError!Secp256k1 {
var pcs: [2 * n][9]Secp256k1 = undefined;
var es: [2 * n][2 * 32 + 1]i8 = undefined;
for (p, s, 0..) |point, k, i| {
pcs[2 * i] = if (point.is_base) basePointPc[0..9].* else precompute(point, 8);
// lambda*P
for (&pcs[2 * i + 1], pcs[2 * i]) |*pc_lambda, pc| {
pc_lambda.* = .{ .x = pc.x.mul(Endomorphism.beta), .y = pc.y, .z = pc.z };
}
var s48: [48]u8 = undefined;
mem.writeInt(u384, &s48, mem.readInt(u256, &k, endian), .little);
const split = Endomorphism.splitScalar(scalar.reduce48(s48, .little), .little) catch unreachable;
for ([_][32]u8{ split.r1, split.r2 }, es[2 * i ..][0..2]) |r, *e| {
const x = mem.readInt(u256, &r, .little);
const negate = x > scalar.field_order / 2;
var r_abs: [32]u8 = undefined;
mem.writeInt(u256, &r_abs, if (negate) scalar.field_order - x else x, .little);
e.* = slide(r_abs);
if (negate) {
for (e) |*d| d.* = -d.*;
}
}
}
var pos: usize = 0;
for (&es) |*e| pos = @max(pos, mem.findLastNone(i8, e, &.{0}) orelse 0);
var q = Secp256k1.identityElement;
while (true) : (pos -= 1) {
for (&pcs, &es) |*pc, *e| {
const slot = e[pos];
if (slot > 0) {
q = q.add(pc[@as(usize, @intCast(slot))]);
} else if (slot < 0) {
q = q.sub(pc[@as(usize, @intCast(-slot))]);
}
}
if (pos == 0) break;
q = q.dbl().dbl().dbl().dbl();
}
try q.rejectIdentity();
return q;
}
fn pcMul16(pc: *const [16]Secp256k1, s: [32]u8, comptime vartime: bool) IdentityElementError!Secp256k1 {
var q = Secp256k1.identityElement;
var pos: usize = 252;
while (true) : (pos -= 4) {
const slot = @as(u4, @truncate((s[pos >> 3] >> @as(u3, @truncate(pos)))));
if (vartime) {
if (slot != 0) {
q = q.add(pc[slot]);
}
} else {
q = q.add(pcSelect(16, pc, slot));
}
if (pos == 0) break;
q = q.dbl().dbl().dbl().dbl();
}
try q.rejectIdentity();
return q;
}
fn precompute(p: Secp256k1, comptime count: usize) [1 + count]Secp256k1 {
var pc: [1 + count]Secp256k1 = undefined;
pc[0] = Secp256k1.identityElement;
pc[1] = p;
var i: usize = 2;
while (i <= count) : (i += 1) {
pc[i] = if (i % 2 == 0) pc[i / 2].dbl() else pc[i - 1].add(p);
}
return pc;
}
const basePointPc = pc: {
@setEvalBranchQuota(50000);
break :pc precompute(Secp256k1.basePoint, 15);
};
/// 将椭圆曲线点乘以标量。
/// 若结果为单位元,返回 error.IdentityElement。
pub fn mul(p: Secp256k1, s_: [32]u8, endian: std.lang.Endian) IdentityElementError!Secp256k1 {
const s = if (endian == .little) s_ else Fe.orderSwap(s_);
if (p.is_base) {
return pcMul16(&basePointPc, s, false);
}
try p.rejectIdentity();
const pc = precompute(p, 15);
return pcMul16(&pc, s, false);
}
/// *以可变时间*将椭圆曲线点乘以*公开*标量。
/// 可用于签名验证。
pub fn mulPublic(p: Secp256k1, s: [32]u8, endian: std.lang.Endian) (IdentityElementError || NonCanonicalError)!Secp256k1 {
try scalar.rejectNonCanonical(s, endian);
return mulMultiPublic(1, .{p}, .{s}, endian);
}
/// 对公开参数执行双基点乘法:*以可变时间*计算 (p1*s1)+(p2*s2)。
/// 可用于签名验证。
pub fn mulDoubleBasePublic(p1: Secp256k1, s1: [32]u8, p2: Secp256k1, s2: [32]u8, endian: std.lang.Endian) IdentityElementError!Secp256k1 {
try p1.rejectIdentity();
try p2.rejectIdentity();
return mulMultiPublic(2, .{ p1, p2 }, .{ s1, s2 }, endian);
}
};
/// 仿射坐标中的点。
pub const AffineCoordinates = struct {
x: Secp256k1.Fe,
y: Secp256k1.Fe,
/// 以仿射坐标表示的单位元。
pub const identityElement = AffineCoordinates{ .x = Secp256k1.identityElement.x, .y = Secp256k1.identityElement.y };
pub fn neg(p: AffineCoordinates) AffineCoordinates {
return .{ .x = p.x, .y = p.y.neg() };
}
fn cMov(p: *AffineCoordinates, a: AffineCoordinates, c: u1) void {
p.x.cMov(a.x, c);
p.y.cMov(a.y, c);
}
};
test {
_ = @import("tests/secp256k1.zig");
}