Skip to content

Commit 1a632a3

Browse files
authored
Merge pull request bitcoin#1405 from sipa/202301_bip324_update
BIP324 reference code / test vector improvements
2 parents f277d42 + cc177ab commit 1a632a3

10 files changed

+837
-106
lines changed

bip-0324.mediawiki

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,19 @@ To find encodings of a given X coordinate ''x'', we first need the inverse of ''
247247
* ''XSwiftECInv(x, u, case)'':
248248
** If ''case & 2 = 0'':
249249
*** If ''lift_x(-x - u)'' succeeds, return ''None''.
250-
*** Let ''v = x'' if ''case & 1 = 0''; let ''v = -x - u (mod p)'' otherwise.
250+
*** Let ''v = x''.
251251
*** Let ''s = -(u<sup>3</sup> + 7)/(u<sup>2</sup> + uv + v<sup>2</sup>) (mod p)''.
252252
** If ''case & 2 = 2'':
253253
*** Let ''s = x - u (mod p)''.
254254
*** If ''s = 0'', return ''None''.
255255
*** Let ''r'' be the square root of ''-s(4(u<sup>3</sup> + 7) + 3u<sup>2</sup>s) (mod p).''<ref name="modsqrt">'''How to compute a square root mod ''p''?''' Due to the structure of ''p'', a candidate for the square root of ''a'' mod ''p'' can be computed as ''x = a<sup>(p+1)/4</sup> mod p''. If ''a'' is not a square mod ''p'', this formula returns the square root of ''-a mod p'' instead, so it is necessary to verify that ''x<sup>2</sup> mod p = a''. If that is the case ''-x mod p'' is a solution too, but we define "the" square root to be equal to that expression (the square root will therefore always be a square itself, as ''(p+1)/4'' is even). This algorithm is a specialization of the [https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm Tonelli-Shanks algorithm].</ref> Return ''None'' if it does not exist.
256-
*** If ''case & 1 = 1'':
257-
**** If ''r = 0'', return ''None''.
258-
**** let ''r = -r (mod p)''.
256+
** If ''case & 1 = 1'' and ''r = 0'', return ''None''.
259257
*** Let ''v = (-u + r/s)/2''.
260258
** Let ''w'' be the square root of ''s (mod p)''. Return ''None'' if it does not exist.
261-
** If ''case & 4 = 4'', let ''w = -w (mod p)''.
262-
** Return ''w(u(c - 1)/2 - v)''.
259+
** If ''case & 5 = 0'', return ''-w(u(1 - c)/2 + v)''.
260+
** If ''case & 5 = 1'', return ''w(u(1 + c)/2 + v)''.
261+
** If ''case & 5 = 4'', return ''w(u(1 - c)/2 + v)''.
262+
** If ''case & 5 = 5'', return ''-w(u(1 + c)/2 + v)''.
263263
264264
The overall ''XElligatorSwift'' algorithm, matching the name used in the paper, then uses this inverse to randomly''<ref name="ellswift_helps_parroting">'''Can the ElligatorSwift encoding be used to construct public key encodings that satisfy a certain structure (and not pseudorandom)?''' The algorithm chooses the first 32 bytes (i.e., the value ''u'') and then computes a corresponding ''t'' such that the mapping to the curve point holds. In general, picking ''u'' from a uniformly random distribution provides pseudorandomness. But we can also fix any of the 32 bytes in ''u'', and the algorithm will still find a corresponding ''t''. The fact that it is possible to fix the first 32 bytes, combined with the garbage bytes in the handshake, provides a limited but very simple method of parroting other protocols such as [https://tls13.xargs.org/ TLS 1.3], which can be deployed by one of the peers without explicit support from the other peer. More general methods of parroting, e.g., introduced by defining new protocol or a protocol upgrade, are not precluded.</ref> sample encodings of ''x'':
265265

@@ -586,8 +586,8 @@ Peers supporting the v2 transport protocol signal support by advertising the <co
586586
== Test Vectors ==
587587

588588
For development and testing purposes, we provide a collection of test vectors in CSV format, and a naive, highly inefficient, [[bip-0324/reference.py|reference implementation]] of the relevant algorithms. This code is for demonstration purposes only:
589-
* [[bip-0324/xelligatorswift_test_vectors.csv|XElligatorSwift vectors]] give examples of ElligatorSwift-encoded public keys, and the X coordinate they map to.
590-
* [[bip-0324/xswiftec_test_vectors.csv|XSwiftEC vectors]] give examples of ''(u, x)'' pairs, and the various ''t'' values that ''xswiftec_inv'' maps them to.
589+
* [[bip-0324/ellswift_decode_test_vectors.csv|XElligatorSwift decoding vectors]] give examples of ElligatorSwift-encoded public keys, and the X coordinate they map to.
590+
* [[bip-0324/xswiftec_inv_test_vectors.csv|XSwiftECInv vectors]] give examples of ''(u, x)'' pairs, and the various ''t'' values that ''xswiftec_inv'' maps them to.
591591
* [[bip-0324/packet_encoding_test_vectors.csv|Packet encoding vectors]] illustrate the lifecycle of the authenticated encryption scheme proposed in this document.
592592
593593
== Rationale and References ==

bip-0324/ellswift_decode_test_vectors.csv

Lines changed: 77 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)