Skip to content

Commit ce5de7b

Browse files
committed
Optimize variable name
1 parent dbed507 commit ce5de7b

9 files changed

Lines changed: 42 additions & 40 deletions

File tree

doc/en/markdown/content/prikey_crypto_ecdsa.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@ import pabtc.secp256k1
3232
def sign(prikey: pabtc.secp256k1.Fr, m: pabtc.secp256k1.Fr) -> typing.Tuple[pabtc.secp256k1.Fr, pabtc.secp256k1.Fr, int]:
3333
# https://www.secg.org/sec1-v2.pdf
3434
# 4.1.3 Signing Operation
35-
for _ in itertools.repeat(0):
35+
for _ in range(64):
3636
k = pabtc.secp256k1.Fr(max(1, secrets.randbelow(pabtc.secp256k1.N)))
3737
R = pabtc.secp256k1.G * k
38-
r = pabtc.secp256k1.Fr(R.x.x)
39-
if r.x == 0:
38+
r = pabtc.secp256k1.Fr(R.x.n)
39+
if r.n == 0:
4040
continue
4141
s = (m + prikey * r) / k
42-
if s.x == 0:
42+
if s.n == 0:
4343
continue
4444
v = 0
45-
if R.y.x & 1 == 1:
45+
if R.y.n & 1 == 1:
4646
v |= 1
47-
if R.x.x >= pabtc.secp256k1.N:
47+
if R.x.n >= pabtc.secp256k1.N:
4848
v |= 2
4949
return r, s, v
50+
raise Exception('unreachable')
5051
```
5152

5253
You may notice in the code implementation that the signature function not only returns (r, s), but also returns an additional v value. This is the recovery identifier used to determine the signer's public key from the signature. It uses two bits, the lowest bit marks the parity of the y-axis coordinate of c, so that we can uniquely recover the actual value of c based on r in the signature (elliptic curves are symmetric with respect to the x-axis, and each x corresponds to two possible y-values). Another bit is used to verify that the r value has not overflowed, because the range of coordinates of points on the elliptic curve is 0 to P, but in the signature operation we convert the x-coordinate of c to a scalar, which reduces the range to 0 to N, and therefore overflow truncation may occur.
@@ -81,7 +82,7 @@ def verify(pubkey: pabtc.secp256k1.Pt, m: pabtc.secp256k1.Fr, r: pabtc.secp256k1
8182
b = r / s
8283
R = pabtc.secp256k1.G * a + pubkey * b
8384
assert R != pabtc.secp256k1.I
84-
return r == pabtc.secp256k1.Fr(R.x.x)
85+
return r == pabtc.secp256k1.Fr(R.x.n)
8586
```
8687

8788
## ECDSA Public Key Recovery

doc/en/markdown/content/prikey_crypto_eddsa.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def pt_encode(pt: pxsol.ed25519.Pt) -> bytearray:
1818
# To form the encoding of the point, copy the least significant bit of the x-coordinate to the most significant bit
1919
# of the final octet.
2020
# See https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.2
21-
n = pt.y.x | ((pt.x.x & 1) << 255)
21+
n = pt.y.n | ((pt.x.n & 1) << 255)
2222
return bytearray(n.to_bytes(32, 'little'))
2323
```
2424

doc/en/markdown/content/prikey_crypto_secp256k1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,6 @@ import pabtc
255255

256256
prikey = pabtc.secp256k1.Fr(0x5f6717883bef25f45a129c11fcac1567d74bda5a9ad4cbffc8203c0da2a1473c)
257257
pubkey = pabtc.secp256k1.G * prikey
258-
assert(pubkey.x.x == 0xfb95541bf75e809625f860758a1bc38ac3c1cf120d899096194b94a5e700e891)
259-
assert(pubkey.y.x == 0xc7b6277d32c52266ab94af215556316e31a9acde79a8b39643c6887544fdf58c)
258+
assert(pubkey.x.n == 0xfb95541bf75e809625f860758a1bc38ac3c1cf120d899096194b94a5e700e891)
259+
assert(pubkey.y.n == 0xc7b6277d32c52266ab94af215556316e31a9acde79a8b39643c6887544fdf58c)
260260
```

doc/zh/markdown/content/prikey_crypto_ecdsa.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@ import pabtc.secp256k1
3232
def sign(prikey: pabtc.secp256k1.Fr, m: pabtc.secp256k1.Fr) -> typing.Tuple[pabtc.secp256k1.Fr, pabtc.secp256k1.Fr, int]:
3333
# https://www.secg.org/sec1-v2.pdf
3434
# 4.1.3 Signing Operation
35-
for _ in itertools.repeat(0):
35+
for _ in range(64):
3636
k = pabtc.secp256k1.Fr(max(1, secrets.randbelow(pabtc.secp256k1.N)))
3737
R = pabtc.secp256k1.G * k
38-
r = pabtc.secp256k1.Fr(R.x.x)
39-
if r.x == 0:
38+
r = pabtc.secp256k1.Fr(R.x.n)
39+
if r.n == 0:
4040
continue
4141
s = (m + prikey * r) / k
42-
if s.x == 0:
42+
if s.n == 0:
4343
continue
4444
v = 0
45-
if R.y.x & 1 == 1:
45+
if R.y.n & 1 == 1:
4646
v |= 1
47-
if R.x.x >= pabtc.secp256k1.N:
47+
if R.x.n >= pabtc.secp256k1.N:
4848
v |= 2
4949
return r, s, v
50+
raise Exception('unreachable')
5051
```
5152

5253
您可能发现在代码实现上, 签名函数不但返回了 (r, s), 还额外返回了一个 v 值. 这是恢复标识符, 用于从签名中确定签名者的公钥. 它使用了两个标志位, 最低标识位标志 c 的 y 轴坐标的奇偶位, 以便我们可以根据签名中的 r 来唯一还原 c 的实际值(椭圆曲线是关于 x 轴对称的曲线, 每一个 x 都对应两个可能的 y 值). 另一个比特位用于确认 r 值是否发生过溢出, 因为椭圆曲线上的点的坐标范围是 0 到 P, 但在签名运算中我们会将 c 的 x 坐标转换为一个标量, 范围将缩小到 0 到 N, 因此可能会发生溢出截断.
@@ -81,7 +82,7 @@ def verify(pubkey: pabtc.secp256k1.Pt, m: pabtc.secp256k1.Fr, r: pabtc.secp256k1
8182
b = r / s
8283
R = pabtc.secp256k1.G * a + pubkey * b
8384
assert R != pabtc.secp256k1.I
84-
return r == pabtc.secp256k1.Fr(R.x.x)
85+
return r == pabtc.secp256k1.Fr(R.x.n)
8586
```
8687

8788
## Ecdsa 公钥恢复

doc/zh/markdown/content/prikey_crypto_eddsa.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def pt_encode(pt: pxsol.ed25519.Pt) -> bytearray:
1919
# To form the encoding of the point, copy the least significant bit of the x-coordinate to the most significant bit
2020
# of the final octet.
2121
# See https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.2
22-
n = pt.y.x | ((pt.x.x & 1) << 255)
22+
n = pt.y.n | ((pt.x.n & 1) << 255)
2323
return bytearray(n.to_bytes(32, 'little'))
2424
```
2525

doc/zh/markdown/content/prikey_crypto_secp256k1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,6 @@ import pabtc
253253

254254
prikey = pabtc.secp256k1.Fr(0x5f6717883bef25f45a129c11fcac1567d74bda5a9ad4cbffc8203c0da2a1473c)
255255
pubkey = pabtc.secp256k1.G * prikey
256-
assert(pubkey.x.x == 0xfb95541bf75e809625f860758a1bc38ac3c1cf120d899096194b94a5e700e891)
257-
assert(pubkey.y.x == 0xc7b6277d32c52266ab94af215556316e31a9acde79a8b39643c6887544fdf58c)
256+
assert(pubkey.x.n == 0xfb95541bf75e809625f860758a1bc38ac3c1cf120d899096194b94a5e700e891)
257+
assert(pubkey.y.n == 0xc7b6277d32c52266ab94af215556316e31a9acde79a8b39643c6887544fdf58c)
258258
```

pxsol/ed25519.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,43 @@ class Fp:
99

1010
p = 0
1111

12-
def __init__(self, x: int) -> None:
13-
self.x = x % self.p
12+
def __init__(self, n: int) -> None:
13+
self.n = n % self.p
1414

1515
def __add__(self, data: typing.Self) -> typing.Self:
1616
assert self.p == data.p
17-
return self.__class__(self.x + data.x)
17+
return self.__class__(self.n + data.n)
1818

1919
def __eq__(self, data: object) -> bool:
2020
assert isinstance(data, self.__class__)
2121
assert self.p == data.p
22-
return self.x == data.x
22+
return self.n == data.n
2323

2424
def __mul__(self, data: typing.Self) -> typing.Self:
2525
assert self.p == data.p
26-
return self.__class__(self.x * data.x)
26+
return self.__class__(self.n * data.n)
2727

2828
def __neg__(self) -> typing.Self:
29-
return self.__class__(self.p - self.x)
29+
return self.__class__(self.p - self.n)
3030

3131
def __repr__(self) -> str:
3232
return json.dumps(self.json())
3333

3434
def __sub__(self, data: typing.Self) -> typing.Self:
3535
assert self.p == data.p
36-
return self.__class__(self.x - data.x)
36+
return self.__class__(self.n - data.n)
3737

3838
def __truediv__(self, data: typing.Self) -> typing.Self:
3939
return self * data ** -1
4040

4141
def __pos__(self) -> typing.Self:
42-
return self.__class__(self.x)
42+
return self.__class__(self.n)
4343

4444
def __pow__(self, data: int) -> typing.Self:
45-
return self.__class__(pow(self.x, data, self.p))
45+
return self.__class__(pow(self.n, data, self.p))
4646

4747
def json(self) -> str:
48-
return f'{self.x:064x}'
48+
return f'{self.n:064x}'
4949

5050
@classmethod
5151
def nil(cls) -> typing.Self:
@@ -115,7 +115,7 @@ def __add__(self, data: typing.Self) -> typing.Self:
115115
def __mul__(self, k: Fr) -> Pt:
116116
# Point multiplication: Double-and-add
117117
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
118-
n = k.x
118+
n = k.n
119119
result = I
120120
addend = self
121121
while n:

pxsol/eddsa.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def pt_encode(pt: pxsol.ed25519.Pt) -> bytearray:
1515
# To form the encoding of the point, copy the least significant bit of the x-coordinate to the most significant bit
1616
# of the final octet.
1717
# See https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.2
18-
n = pt.y.x | ((pt.x.x & 1) << 255)
18+
n = pt.y.n | ((pt.x.n & 1) << 255)
1919
return bytearray(n.to_bytes(32, 'little'))
2020

2121

@@ -48,7 +48,7 @@ def pt_decode(pt: bytearray) -> pxsol.ed25519.Pt:
4848
# Finally, use the x_0 bit to select the right square root. If x = 0, and x_0 = 1, decoding fails. Otherwise, if
4949
# x_0 != x mod 2, set x <-- p - x. Return the decoded point (x,y).
5050
assert x != pxsol.ed25519.Fq(0) or not bit0
51-
if x.x & 1 != bit0:
51+
if x.n & 1 != bit0:
5252
x = -x
5353
return pxsol.ed25519.Pt(x, y)
5454

@@ -101,7 +101,7 @@ def sign(prikey: bytearray, m: bytearray) -> bytearray:
101101
digest = pt_encode(R)
102102
h = pxsol.ed25519.Fr(int.from_bytes(hash(digest + pubkey + m), 'little'))
103103
s = r + a * h
104-
return digest + bytearray(s.x.to_bytes(32, 'little'))
104+
return digest + bytearray(s.n.to_bytes(32, 'little'))
105105

106106

107107
def verify(pubkey: bytearray, m: bytearray, v: bytearray) -> bool:

test/test_ed25519.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
def test_g():
55
q = pxsol.ed25519.G * pxsol.ed25519.Fr(1)
6-
assert q.x.x == 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a
7-
assert q.y.x == 0x6666666666666666666666666666666666666666666666666666666666666658
6+
assert q.x.n == 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a
7+
assert q.y.n == 0x6666666666666666666666666666666666666666666666666666666666666658
88
q = pxsol.ed25519.G * pxsol.ed25519.Fr(2)
9-
assert q.x.x == 0x36ab384c9f5a046c3d043b7d1833e7ac080d8e4515d7a45f83c5a14e2843ce0e
10-
assert q.y.x == 0x2260cdf3092329c21da25ee8c9a21f5697390f51643851560e5f46ae6af8a3c9
9+
assert q.x.n == 0x36ab384c9f5a046c3d043b7d1833e7ac080d8e4515d7a45f83c5a14e2843ce0e
10+
assert q.y.n == 0x2260cdf3092329c21da25ee8c9a21f5697390f51643851560e5f46ae6af8a3c9
1111
q = pxsol.ed25519.G * pxsol.ed25519.Fr(3)
12-
assert q.x.x == 0x67ae9c4a22928f491ff4ae743edac83a6343981981624886ac62485fd3f8e25c
13-
assert q.y.x == 0x1267b1d177ee69aba126a18e60269ef79f16ec176724030402c3684878f5b4d4
12+
assert q.x.n == 0x67ae9c4a22928f491ff4ae743edac83a6343981981624886ac62485fd3f8e25c
13+
assert q.y.n == 0x1267b1d177ee69aba126a18e60269ef79f16ec176724030402c3684878f5b4d4

0 commit comments

Comments
 (0)