ECKCDSA von JavaScript zu VB.NET

  • VB.NET

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von exc-jdbi.

    ECKCDSA von JavaScript zu VB.NET

    Hallo,
    nachdem ich jetzt eine funktionierende Curve25519 habe brauche ich noch einmal eure Unterstützung. Ich möchte nun messages mit dem EC-KCDSA (Elliptic Curve Korean Certificate-based Digital Signature Algorithm) Verfahren signieren und verifizieren. Das Signieren läuft bereits bestens nur das Verifizieren noch nicht so ganz. Ich finde dafür nun allerdings auch keine passenden Quellcodes, die man nahezu 1zu1 übersetzen kann. Lediglich eine JavaScript-Variante die wohl leider keine Longs hat und stattdessen alles mit UInt16Arrays rechnet:

    Spoiler anzeigen

    Quellcode

    1. /* Signature verification primitive, calculates Y = vP + hG
    2. * v [in] signature value
    3. * h [in] signature hash
    4. * P [in] public key
    5. * Returns signature public key
    6. */
    7. public static verify(v, h, P) {
    8. /* Y = v abs(P) + h G */
    9. let d = new Array(32);
    10. let p = [Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray()];
    11. let s = [Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray()];
    12. let yx = [Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray()];
    13. let yz = [Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray()];
    14. let t1 = [Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray()];
    15. let t2 = [Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray(), Curve25519.createUnpackedArray()];
    16. let vi = 0, hi = 0, di = 0, nvh = 0, i, j, k;
    17. /* set p[0] to G and p[1] to P */
    18. Curve25519.set(p[0], 9);
    19. Curve25519.unpack(p[1], P);
    20. /* set s[0] to P+G and s[1] to P-G */
    21. /* s[0] = (Py^2 + Gy^2 - 2 Py Gy)/(Px - Gx)^2 - Px - Gx - 486662 */
    22. /* s[1] = (Py^2 + Gy^2 + 2 Py Gy)/(Px - Gx)^2 - Px - Gx - 486662 */
    23. Curve25519.x_to_y2(t1[0], t2[0], p[1]); /* t2[0] = Py^2 */
    24. Curve25519.sqrt(t1[0], t2[0]); /* t1[0] = Py or -Py */
    25. j = Curve25519.is_negative(t1[0]); /* ... check which */
    26. Curve25519.add(t2[0], t2[0], Curve25519.C39420360); /* t2[0] = Py^2 + Gy^2 */
    27. Curve25519.mul(t2[1], Curve25519.BASE_2Y, t1[0]); /* t2[1] = 2 Py Gy or -2 Py Gy */
    28. Curve25519.sub(t1[j], t2[0], t2[1]); /* t1[0] = Py^2 + Gy^2 - 2 Py Gy */
    29. Curve25519.add(t1[1 - j], t2[0], t2[1]); /* t1[1] = Py^2 + Gy^2 + 2 Py Gy */
    30. Curve25519.cpy(t2[0], p[1]); /* t2[0] = Px */
    31. Curve25519.sub(t2[0], t2[0], Curve25519.C9); /* t2[0] = Px - Gx */
    32. Curve25519.sqr(t2[1], t2[0]); /* t2[1] = (Px - Gx)^2 */
    33. Curve25519.recip(t2[0], t2[1], 0); /* t2[0] = 1/(Px - Gx)^2 */
    34. Curve25519.mul(s[0], t1[0], t2[0]); /* s[0] = t1[0]/(Px - Gx)^2 */
    35. Curve25519.sub(s[0], s[0], p[1]); /* s[0] = t1[0]/(Px - Gx)^2 - Px */
    36. Curve25519.sub(s[0], s[0], Curve25519.C486671); /* s[0] = X(P+G) */
    37. Curve25519.mul(s[1], t1[1], t2[0]); /* s[1] = t1[1]/(Px - Gx)^2 */
    38. Curve25519.sub(s[1], s[1], p[1]); /* s[1] = t1[1]/(Px - Gx)^2 - Px */
    39. Curve25519.sub(s[1], s[1], Curve25519.C486671); /* s[1] = X(P-G) */
    40. Curve25519.mul_small(s[0], s[0], 1); /* reduce s[0] */
    41. Curve25519.mul_small(s[1], s[1], 1); /* reduce s[1] */
    42. /* prepare the chain */
    43. for (i = 0; i < 32; i++) {
    44. vi = (vi >> 8) ^ (v[i] & 0xFF) ^ ((v[i] & 0xFF) << 1);
    45. hi = (hi >> 8) ^ (h[i] & 0xFF) ^ ((h[i] & 0xFF) << 1);
    46. nvh = ~(vi ^ hi);
    47. di = (nvh & (di & 0x80) >> 7) ^ vi;
    48. di ^= nvh & (di & 0x01) << 1;
    49. di ^= nvh & (di & 0x02) << 1;
    50. di ^= nvh & (di & 0x04) << 1;
    51. di ^= nvh & (di & 0x08) << 1;
    52. di ^= nvh & (di & 0x10) << 1;
    53. di ^= nvh & (di & 0x20) << 1;
    54. di ^= nvh & (di & 0x40) << 1;
    55. d[i] = di & 0xFF;
    56. }
    57. di = ((nvh & (di & 0x80) << 1) ^ vi) >> 8;
    58. /* initialize state */
    59. Curve25519.set(yx[0], 1);
    60. Curve25519.cpy(yx[1], p[di]);
    61. Curve25519.cpy(yx[2], s[0]);
    62. Curve25519.set(yz[0], 0);
    63. Curve25519.set(yz[1], 1);
    64. Curve25519.set(yz[2], 1);
    65. /* y[0] is (even)P + (even)G
    66. * y[1] is (even)P + (odd)G if current d-bit is 0
    67. * y[1] is (odd)P + (even)G if current d-bit is 1
    68. * y[2] is (odd)P + (odd)G
    69. */
    70. vi = 0;
    71. hi = 0;
    72. /* and go for it! */
    73. for (i = 32; i-- !== 0;) {
    74. vi = (vi << 8) | (v[i] & 0xFF);
    75. hi = (hi << 8) | (h[i] & 0xFF);
    76. di = (di << 8) | (d[i] & 0xFF);
    77. for (j = 8; j-- !== 0;) {
    78. Curve25519.mont_prep(t1[0], t2[0], yx[0], yz[0]);
    79. Curve25519.mont_prep(t1[1], t2[1], yx[1], yz[1]);
    80. Curve25519.mont_prep(t1[2], t2[2], yx[2], yz[2]);
    81. k = ((vi ^ vi >> 1) >> j & 1)
    82. + ((hi ^ hi >> 1) >> j & 1);
    83. Curve25519.mont_dbl(yx[2], yz[2], t1[k], t2[k], yx[0], yz[0]);
    84. k = (di >> j & 2) ^ ((di >> j & 1) << 1);
    85. Curve25519.mont_add(t1[1], t2[1], t1[k], t2[k], yx[1], yz[1],
    86. p[di >> j & 1]);
    87. Curve25519.mont_add(t1[2], t2[2], t1[0], t2[0], yx[2], yz[2],
    88. s[((vi ^ hi) >> j & 2) >> 1]);
    89. }
    90. }
    91. k = (vi & 1) + (hi & 1);
    92. Curve25519.recip(t1[0], yz[k], 0);
    93. Curve25519.mul(t1[1], yx[k], t1[0]);
    94. let Y = [];
    95. Curve25519.pack(t1[1], Y);
    96. return Y;
    97. }


    Bis Zeile 50. /* prepare the chain */ bin ich durch... nur dann fallen mir die unterschiedlichen Arrays auf die Füße. Denn in der VB/C#-Variante wird alles mit 10 Long-Arrays gelöst und hier eben alles mit UInt16Arrays. Hat von euch jmd vllt. eine Idee wie ich dieses Problem lösen kann? Vllt. einen Arrays-Converter oder so?

    Hier noch der VB-Code:
    Spoiler anzeigen

    Quellcode

    1. Public Shared Function verify(ByVal v As Byte(), ByVal h As Byte(), ByVal pk As Byte()) As Byte()
    2. '/* Y = v abs(P) + h G */
    3. Dim Input_vStr As String = ByteAry2HEX(v) 'v = bed2c0bd00f2e5002c7376632361a55b2ae6a1796d21a64741a9c020b8d95404
    4. Dim Input_hStr As String = ByteAry2HEX(h) 'h = 200fb965e1230bec6a9dc576d1bfa2875c3a6521fa9ced997f79ac608da939f6
    5. Dim Input_pkStr As String = ByteAry2HEX(pk) 'pk = bdb5594cce5dcadb6a4c3af20384a7ad3b8ce045b66535bf0188fdfe7a3b6479
    6. Dim d(31) As Byte
    7. Dim p() As RefCurve25519.Long10 = {New RefCurve25519.Long10, New RefCurve25519.Long10}
    8. Dim s = {New RefCurve25519.Long10, New RefCurve25519.Long10}
    9. Dim yx = {New RefCurve25519.Long10, New RefCurve25519.Long10, New RefCurve25519.Long10}
    10. Dim yz = {New RefCurve25519.Long10, New RefCurve25519.Long10, New RefCurve25519.Long10}
    11. Dim t1 = {New RefCurve25519.Long10, New RefCurve25519.Long10, New RefCurve25519.Long10}
    12. Dim t2 = {New RefCurve25519.Long10, New RefCurve25519.Long10, New RefCurve25519.Long10}
    13. Dim vi = 0, hi = 0, di = 0, nvh = 0, jj, k As Integer
    14. Dim TempPack(31) As Byte
    15. Dim TempPackStr As String = ""
    16. '/* set p[0] to G And p[1] to P */
    17. RefCurve25519.Set(p(0), 9)
    18. RefCurve25519.Unpack(p(1), pk)
    19. '/* set s[0] to P+G And s[1] to P-G */
    20. '/* s[0] = (Py^2 + Gy^2 - 2 Py Gy)/(Px - Gx)^2 - Px - Gx - 486662 */
    21. '/* s[1] = (Py^2 + Gy^2 + 2 Py Gy)/(Px - Gx)^2 - Px - Gx - 486662 */
    22. RefCurve25519.CurveEquationInline(t2(0), p(1), t1(0)) '/* t2[0] = Py ^ 2 */
    23. '#debug#
    24. RefCurve25519.Pack(p(1), TempPack)
    25. TempPackStr = ByteAry2HEX(TempPack) 't1(0) = 78d91ac1f1ae1a9e4200a42ac4a9001fa24b44be8e2b58cd123843684b90235f
    26. ' t2(0) = e34bdb568e732b26e4d8400e4c428751868406323388e0228cb7f8b309ddcd43
    27. ' p(1) = bdb5594cce5dcadb6a4c3af20384a7ad3b8ce045b66535bf0188fdfe7a3b6479
    28. RefCurve25519.sqrt(t1(0), t2(0)) '/* t1[0] = Py Or -Py */
    29. '#debug#
    30. RefCurve25519.Pack(t1(0), TempPack)
    31. TempPackStr = ByteAry2HEX(TempPack) 't1(0) = "3e88da5e05657c0d44b02f88e89bc1d5914c2d597d2832e3dcc1c3e6291ca727"
    32. jj = RefCurve25519.IsNegative(t1(0)) '/* ... check which */
    33. RefCurve25519.Add(t2(0), t2(0), New RefCurve25519.Long10(RefCurve25519.C39420360, 0, 0, 0, 0, 0, 0, 0, 0, 0)) '/* t2[0] = Py ^ 2 + Gy ^ 2 */
    34. '#debug#
    35. RefCurve25519.Pack(t2(0), TempPack)
    36. TempPackStr = ByteAry2HEX(TempPack) 't2(0) = abcd34598e732b26e4d8400e4c428751868406323388e0228cb7f8b309ddcd43
    37. '3b586202bb742cac9b3c0725036585db665d6e11a745c23f96f2be8ebccca33e
    38. 'Dim test As Byte() = HEXStr2ByteAry("3b586202bb742cac9b3c0725036585db665d6e11a745c23f96f2be8ebccca33e")
    39. 'Dim test2 As RefCurve25519.Long10 = New RefCurve25519.Long10
    40. 'RefCurve25519.Unpack(test2, test)
    41. RefCurve25519.Multiply(t2(1), RefCurve25519.Base2Y, t1(0)) '/* t2[1] = 2 Py Gy Or -2 Py Gy */
    42. '#debug#
    43. RefCurve25519.Pack(t2(1), TempPack)
    44. TempPackStr = ByteAry2HEX(TempPack) 't2(1) = "2823ace230af25bebb3175a816310c9bb0f1f387f808cc20e719b6c8ca0a7d17"
    45. RefCurve25519.Sub(t1(jj), t2(0), t2(1)) '/* t1[0] = Py ^ 2 + Gy ^ 2 - 2 Py Gy */
    46. '#debug#
    47. RefCurve25519.Pack(t1(jj), TempPack)
    48. TempPackStr = ByteAry2HEX(TempPack) 't1(jj = 0) = "83aa88765dc4056828a7cb6535117bb6d59212aa3a7f1402a59d42eb3ed2502c"
    49. ' t2(0) = "abcd34598e732b26e4d8400e4c428751868406323388e0228cb7f8b309ddcd43"
    50. ' t2(1) = "2823ace230af25bebb3175a816310c9bb0f1f387f808cc20e719b6c8ca0a7d17"
    51. 'Dim test1 As Byte() = HEXStr2ByteAry("0000000000000000000000000000000000000000000000000000000000000000")
    52. 'Dim test1a As RefCurve25519.Long10 = New RefCurve25519.Long10
    53. 'RefCurve25519.Unpack(test1a, test1)
    54. 'Dim test2 As Byte() = HEXStr2ByteAry("70aa88765dc4056828a7cb6535117bb6d59212aa3a7f1402a59d42eb3ed250ac")
    55. 'Dim test2a As RefCurve25519.Long10 = New RefCurve25519.Long10
    56. 'RefCurve25519.Unpack(test2a, test2)
    57. 'Dim test3 As Byte() = HEXStr2ByteAry("83aa88765dc4056828a7cb6535117bb6d59212aa3a7f1402a59d42eb3ed2502c")
    58. 'Dim test3a As RefCurve25519.Long10 = New RefCurve25519.Long10
    59. 'RefCurve25519.Unpack(test3a, test3)
    60. 'Dim testresult As RefCurve25519.Long10 = New RefCurve25519.Long10
    61. 'RefCurve25519.Sub(testresult, test3a, test2a) 'test1a + test2a = (same as ec-kcdsa.js) = 2e78bdcfeb37318e0c800c74815302085c1719dc6d07f52431553b9f48af1e70
    62. '' test1a + test3a = test2a = abcd34598e732b26e4d8400e4c428751868406323388e0228cb7f8b309ddcd43
    63. '' test3a - test2a = (same as ec-kcdsa.js) = 6a557789a23bfa97d758349acaee84492a6ded55c580ebfd5a62bd14c12daf53
    64. '' test2a - test3a = ? = 83aa88765dc4056828a7cb6535117bb6d59212aa3a7f1402a59d42eb3ed2502c
    65. 'RefCurve25519.Pack(testresult, TempPack)
    66. 'TempPackStr = ByteAry2HEX(TempPack)
    67. RefCurve25519.Add(t1(1 - jj), t2(0), t2(1)) '/* t1[1] = Py ^ 2 + Gy ^ 2 + 2 Py Gy */
    68. '#debug#
    69. RefCurve25519.Pack(t1(1 - jj), TempPack)
    70. TempPackStr = ByteAry2HEX(TempPack) '"d3f0e03bbf2251e49f0ab6b6627393ec3676fab92b91ac4373d1ae7cd4e74a5b"
    71. RefCurve25519.Copy(t2(0), p(1)) '/* t2[0] = Px */
    72. '#debug#
    73. RefCurve25519.Pack(t2(0), TempPack)
    74. TempPackStr = ByteAry2HEX(TempPack) 't2(0) = "bdb5594cce5dcadb6a4c3af20384a7ad3b8ce045b66535bf0188fdfe7a3b6479"
    75. RefCurve25519.Sub(t2(0), t2(0), New RefCurve25519.Long10(9, 0, 0, 0, 0, 0, 0, 0, 0, 0)) '/* t2[0] = Px - Gx */
    76. '#debug#
    77. RefCurve25519.Pack(t2(0), TempPack)
    78. TempPackStr = ByteAry2HEX(TempPack) 't2(0) = "b4b5594cce5dcadb6a4c3af20384a7ad3b8ce045b66535bf0188fdfe7a3b6479"
    79. RefCurve25519.Square(t2(1), t2(0)) '/* t2[1] = (Px - Gx) ^ 2 */
    80. '#debug#
    81. RefCurve25519.Pack(t2(1), TempPack)
    82. TempPackStr = ByteAry2HEX(TempPack) 't2(1) = 2d879f6da4f68c482054b78baafbfae784fddfbf931d0c5c097f89d542a50d30
    83. RefCurve25519.Reciprocal(t2(0), t2(1), 0) '/* t2[0] = 1/(Px - Gx)^2 */
    84. '#debug#
    85. RefCurve25519.Pack(t2(0), TempPack)
    86. TempPackStr = ByteAry2HEX(TempPack) ' t2(0) = cc0db7db039091eeeb2147167228f62106a0bd3a913dd67d93d676537d609b37
    87. RefCurve25519.Multiply(s(0), t1(0), t2(0)) '/* s[0] = t1[0]/(Px - Gx)^2 */
    88. '#debug#
    89. RefCurve25519.Pack(s(0), TempPack)
    90. TempPackStr = ByteAry2HEX(TempPack) 's(0) = ad4ad72c0f2e272c5c30dc9aa22f927b654cc5105cf42a385f83e5257031ff13
    91. RefCurve25519.Sub(s(0), s(0), p(1)) '/* s[0] = t1[0]/(Px - Gx)^2 - Px */
    92. '#debug#
    93. RefCurve25519.Pack(s(0), TempPack)
    94. TempPackStr = ByteAry2HEX(TempPack) 's(0) = dd947de040d05c50f1e3a1a89eabeacd29c0e4caa58ef5785dfbe726f5f59a1a
    95. RefCurve25519.Sub(s(0), s(0), New RefCurve25519.Long10(RefCurve25519.C486671, 0, 0, 0, 0, 0, 0, 0, 0, 0)) '/* s[0] = X(P + G) */
    96. '#debug#
    97. RefCurve25519.Pack(s(0), TempPack)
    98. TempPackStr = ByteAry2HEX(TempPack) ' s(0) = ce2776e040d05c50f1e3a1a89eabeacd29c0e4caa58ef5785dfbe726f5f59a1a
    99. RefCurve25519.Multiply(s(1), t1(1), t2(0)) '/* s[1] = t1[1]/(Px - Gx)^2 */
    100. '#debug#
    101. RefCurve25519.Pack(s(1), TempPack)
    102. TempPackStr = ByteAry2HEX(TempPack) 's(1) = c687944436aed57827e1842031e8c99b282beaa0810d21715e8e673c051bde5a
    103. RefCurve25519.Sub(s(1), s(1), p(1)) '/* s[1] = t1[1]/(Px - Gx)^2 - Px */
    104. '#debug#
    105. RefCurve25519.Pack(s(1), TempPack)
    106. TempPackStr = ByteAry2HEX(TempPack) 's(1) = f6d13af867500b9dbc944a2e2d6422eeec9e095bcba7ebb15c066a3d8adf7961
    107. RefCurve25519.Sub(s(1), s(1), New RefCurve25519.Long10(RefCurve25519.C486671, 0, 0, 0, 0, 0, 0, 0, 0, 0)) '/* s[1] = X(P - G) */
    108. '#debug#
    109. RefCurve25519.Pack(s(1), TempPack)
    110. TempPackStr = ByteAry2HEX(TempPack) 's(1) = e76433f867500b9dbc944a2e2d6422eeec9e095bcba7ebb15c066a3d8adf7961
    111. RefCurve25519.MulSmall(s(0), s(0), 1) '/* reduce s[0] */
    112. '#debug#
    113. RefCurve25519.Pack(s(0), TempPack)
    114. TempPackStr = ByteAry2HEX(TempPack) 's(0) = ce2776e040d05c50f1e3a1a89eabeacd29c0e4caa58ef5785dfbe726f5f59a1a
    115. RefCurve25519.MulSmall(s(1), s(1), 1) '/* reduce s[1] */
    116. '#debug#
    117. RefCurve25519.Pack(s(1), TempPack)
    118. TempPackStr = ByteAry2HEX(TempPack) 's(1) = e76433f867500b9dbc944a2e2d6422eeec9e095bcba7ebb15c066a3d8adf7961
    119. #Region "Tracestop 1"
    120. 'p(0) =
    121. 'N0 9 Long
    122. 'N1 0 Long
    123. 'N2 0 Long
    124. 'N3 0 Long
    125. 'N4 0 Long
    126. 'N5 0 Long
    127. 'N6 0 Long
    128. 'N7 0 Long
    129. 'N8 0 Long
    130. 'N9 0 Long
    131. 'p(1) =
    132. 'N0 5879229 Long
    133. 'N1 9925523 Long
    134. 'N2 26041209 Long
    135. 'N3 2068946 Long
    136. 'N4 45522448 Long
    137. 'N5 31493179 Long
    138. 'N6 45275938 Long
    139. 'N7 16791526 Long
    140. 'N8 61861848 Long
    141. 'N9 31822061 Long
    142. 's(0) =
    143. 'N0 27192331 Long
    144. 'N1 24318579 Long
    145. 'N2 17573660 Long
    146. 'N3 19711439 Long
    147. 'N4 5413017 Long
    148. 'N5 30672297 Long
    149. 'N6 15276307 Long
    150. 'N7 12279728 Long
    151. 'N8 5320169 Long
    152. 'N9 29115157 Long
    153. 's(1) =
    154. 'N0 41529697 Long
    155. 'N1 11249298 Long
    156. 'N2 3399453 Long
    157. 'N3 34898 Long
    158. 'N4 29919506 Long
    159. 'N5 19270466 Long
    160. 'N6 25013351 Long
    161. 'N7 17315350 Long
    162. 'N8 935140 Long
    163. 'N9 18909289 Long
    164. 't1(0) =
    165. 'N0 97122056 Long
    166. 'N1 19501385 Long
    167. 'N2 97391928 Long
    168. 'N3 9915679 Long
    169. 'N4 45240380 Long
    170. 'N5 29422681 Long
    171. 'N6 9838121 Long
    172. 'N7 46452542 Long
    173. 'N8 39555765 Long
    174. 'N9 35130938 Long
    175. 't1(1) =
    176. 'N0 -1203122 Long
    177. 'N1 9452179 Long
    178. 'N2 7009362 Long
    179. 'N3 2725613 Long
    180. 'N4 -2496042 Long
    181. 'N5 -28568397 Long
    182. 'N6 -912119 Long
    183. 'N7 -14796422 Long
    184. 'N8 -19207071 Long
    185. 'N9 417966 Long
    186. 't1(2) =
    187. 'N0 0 Long
    188. 'N1 0 Long
    189. 'N2 0 Long
    190. 'N3 0 Long
    191. 'N4 0 Long
    192. 'N5 0 Long
    193. 'N6 0 Long
    194. 'N7 0 Long
    195. 'N8 0 Long
    196. 'N9 0 Long
    197. 't2(0)=
    198. 'N0 62328268 Long
    199. 'N1 6553846 Long
    200. 'N2 4029906 Long
    201. 'N3 26260025 Long
    202. 'N4 8902817 Long
    203. 'N5 12427270 Long
    204. 'N6 52349085 Long
    205. 'N7 13791162 Long
    206. 'N8 64305005 Long
    207. 'N9 14577025 Long
    208. 't2(1) =
    209. 'N0 27232045 Long
    210. 'N1 20818203 Long
    211. 'N2 42207505 Long
    212. 'N3 22306234 Long
    213. 'N4 60812270 Long
    214. 'N5 31456644 Long
    215. 'N6 34523615 Long
    216. 'N7 31533953 Long
    217. 'N8 2971799 Long
    218. 'N9 12596885 Long
    219. 't2(2) =
    220. 'N0 0 Long
    221. 'N1 0 Long
    222. 'N2 0 Long
    223. 'N3 0 Long
    224. 'N4 0 Long
    225. 'N5 0 Long
    226. 'N6 0 Long
    227. 'N7 0 Long
    228. 'N8 0 Long
    229. 'N9 0 Long
    230. #End Region
    231. '/* prepare the chain */
    232. For i As Integer = 0 To 31
    233. vi = (vi >> 8) Xor (v(i) And &HFF) Xor ((v(i) And &HFF) << 1)
    234. hi = (hi >> 8) Xor (h(i) And &HFF) Xor ((h(i) And &HFF) << 1)
    235. nvh = Not (vi Xor hi)
    236. di = (nvh And (di And &H80) >> 7) Xor vi
    237. di = di Xor nvh And (di And &H1) << 1
    238. di = di Xor nvh And (di And &H2) << 1
    239. di = di Xor nvh And (di And &H4) << 1
    240. di = di Xor nvh And (di And &H8) << 1
    241. di = di Xor nvh And (di And &H10) << 1
    242. di = di Xor nvh And (di And &H20) << 1
    243. di = di Xor nvh And (di And &H40) << 1
    244. d(i) = di And &HFF
    245. Next
    246. di = ((nvh And (di And &H80) << 1) Xor vi) >> 8
    247. #Region "Tracestop 2"
    248. 'd = deee40420112ea03549db9642dbf5a645a3a629ef562ea597f79435f4826b505
    249. 'hi = 282
    250. 'vi = 12
    251. 'di = 0
    252. #End Region
    253. '/* initialize state */
    254. RefCurve25519.Set(yx(0), 1)
    255. RefCurve25519.Copy(yx(1), p(di))
    256. RefCurve25519.Copy(yx(2), s(0))
    257. RefCurve25519.Set(yz(0), 0)
    258. RefCurve25519.Set(yz(1), 1)
    259. RefCurve25519.Set(yz(2), 1)
    260. #Region "Tracestop 3"
    261. 'yx(0) =
    262. 'N0 1 Long
    263. 'N1 0 Long
    264. 'N2 0 Long
    265. 'N3 0 Long
    266. 'N4 0 Long
    267. 'N5 0 Long
    268. 'N6 0 Long
    269. 'N7 0 Long
    270. 'N8 0 Long
    271. 'N9 0 Long
    272. 'yx(1) =
    273. 'N0 9 Long
    274. 'N1 0 Long
    275. 'N2 0 Long
    276. 'N3 0 Long
    277. 'N4 0 Long
    278. 'N5 0 Long
    279. 'N6 0 Long
    280. 'N7 0 Long
    281. 'N8 0 Long
    282. 'N9 0 Long
    283. 'yx(2) =
    284. 'N0 27192331 Long
    285. 'N1 24318579 Long
    286. 'N2 17573660 Long
    287. 'N3 19711439 Long
    288. 'N4 5413017 Long
    289. 'N5 30672297 Long
    290. 'N6 15276307 Long
    291. 'N7 12279728 Long
    292. 'N8 5320169 Long
    293. 'N9 29115157 Long
    294. 'yz(0) =
    295. 'N0 0 Long
    296. 'N1 0 Long
    297. 'N2 0 Long
    298. 'N3 0 Long
    299. 'N4 0 Long
    300. 'N5 0 Long
    301. 'N6 0 Long
    302. 'N7 0 Long
    303. 'N8 0 Long
    304. 'N9 0 Long
    305. 'yz(1) =
    306. 'N0 1 Long
    307. 'N1 0 Long
    308. 'N2 0 Long
    309. 'N3 0 Long
    310. 'N4 0 Long
    311. 'N5 0 Long
    312. 'N6 0 Long
    313. 'N7 0 Long
    314. 'N8 0 Long
    315. 'N9 0 Long
    316. 'yz(2) =
    317. 'N0 1 Long
    318. 'N1 0 Long
    319. 'N2 0 Long
    320. 'N3 0 Long
    321. 'N4 0 Long
    322. 'N5 0 Long
    323. 'N6 0 Long
    324. 'N7 0 Long
    325. 'N8 0 Long
    326. 'N9 0 Long
    327. #End Region
    328. '/* y[0] Is (even)P + (even)G
    329. ' * y[1] Is (even)P + (odd)G if current d-bit Is 0
    330. ' * y[1] Is (odd)P + (even)G if current d-bit Is 1
    331. ' * y[2] Is (odd)P + (odd)G
    332. ' */
    333. vi = 0
    334. hi = 0
    335. 'And go for it!
    336. For i As Integer = 31 To 0 Step -1
    337. vi = (vi << 8) Or (v(i) And &HFF)
    338. hi = (hi << 8) Or (h(i) And &HFF)
    339. di = (di << 8) Or (d(i) And &HFF)
    340. For j As Integer = 8 To 0 Step -1
    341. RefCurve25519.MontyPrepare(t1(0), t2(0), yx(0), yz(0))
    342. RefCurve25519.MontyPrepare(t1(1), t2(1), yx(1), yz(1))
    343. RefCurve25519.MontyPrepare(t1(2), t2(2), yx(2), yz(2))
    344. k = ((vi Xor vi >> 1) >> j And 1) + ((hi Xor hi >> 1) >> j And 1)
    345. RefCurve25519.MontyDouble(yx(2), yz(2), t1(k), t2(k), yx(0), yz(0))
    346. k = (di >> j And 2) Or ((di >> j And 1) << 1)
    347. RefCurve25519.MontyAdd(t1(1), t2(1), t1(k), t2(k), yx(1), yz(1), p(di >> j And 1))
    348. RefCurve25519.MontyAdd(t1(2), t2(2), t1(0), t2(0), yx(2), yz(2), s(((vi Xor hi) >> j And 2) >> 1))
    349. Next
    350. Next
    351. #Region "Tracestop 4"
    352. 'vi = -1111436610 Integer
    353. 'hi = 1706626848 Integer
    354. 'di = 1111551710 Integer
    355. 'k = 2
    356. 't1(0) =
    357. 'N0 80652174 Long
    358. 'N1 52304140 Long
    359. 'N2 30594347 Long
    360. 'N3 33419878 Long
    361. 'N4 58886342 Long
    362. 'N5 46170530 Long
    363. 'N6 82841748 Long
    364. 'N7 40879862 Long
    365. 'N8 78485571 Long
    366. 'N9 37669871 Long
    367. 't1(1) =
    368. 'N0 59101306 Long
    369. 'N1 14212684 Long
    370. 'N2 22378684 Long
    371. 'N3 8174658 Long
    372. 'N4 18242175 Long
    373. 'N5 32162007 Long
    374. 'N6 3725737 Long
    375. 'N7 6610879 Long
    376. 'N8 32011420 Long
    377. 'N9 27005631 Long
    378. 't1(2) =
    379. 'N0 43433296 Long
    380. 'N1 28535350 Long
    381. 'N2 54616526 Long
    382. 'N3 3286957 Long
    383. 'N4 44009088 Long
    384. 'N5 7814553 Long
    385. 'N6 21404851 Long
    386. 'N7 29710982 Long
    387. 'N8 61374206 Long
    388. 'N9 29792398 Long
    389. 't2(0) =
    390. 'N0 -27974162 Long
    391. 'N1 10880740 Long
    392. 'N2 29979591 Long
    393. 'N3 -522078 Long
    394. 'N4 -10540188 Long
    395. 'N5 19609484 Long
    396. 'N6 18140918 Long
    397. 'N7 23191440 Long
    398. 'N8 20903125 Long
    399. 'N9 17901149 Long
    400. 't2(1) =
    401. 'N0 30577412 Long
    402. 'N1 -8385638 Long
    403. 'N2 -23853868 Long
    404. 'N3 17400612 Long
    405. 'N4 -19780918 Long
    406. 'N5 -15733788 Long
    407. 'N6 28290292 Long
    408. 'N7 -3007844 Long
    409. 'N8 -23177790 Long
    410. 'N9 -4652416 Long
    411. 't2(2) =
    412. 'N0 -33490657 Long
    413. 'N1 3984873 Long
    414. 'N2 11859928 Long
    415. 'N3 -4786581 Long
    416. 'N4 -6301271 Long
    417. 'N5 -10071233 Long
    418. 'N6 -27201046 Long
    419. 'N7 10991415 Long
    420. 'N8 25635918 Long
    421. 'N9 -26723488 Long
    422. 'yx(0) =
    423. 'N0 59150063 Long
    424. 'N1 4071334 Long
    425. 'N2 13453948 Long
    426. 'N3 33019915 Long
    427. 'N4 30137134 Long
    428. 'N5 23265735 Long
    429. 'N6 61416867 Long
    430. 'N7 3096186 Long
    431. 'N8 60250062 Long
    432. 'N9 25932033 Long
    433. 'yx(1) =
    434. 'N0 39142553 Long
    435. 'N1 18963241 Long
    436. 'N2 20228824 Long
    437. 'N3 14668816 Long
    438. 'N4 56781996 Long
    439. 'N5 9273275 Long
    440. 'N6 24755442 Long
    441. 'N7 30808233 Long
    442. 'N8 44088001 Long
    443. 'N9 23338255 Long
    444. 'yx(2) =
    445. 'N0 54853063 Long
    446. 'N1 10187929 Long
    447. 'N2 19951159 Long
    448. 'N3 7346679 Long
    449. 'N4 62539145 Long
    450. 'N5 15066087 Long
    451. 'N6 16871919 Long
    452. 'N7 22734774 Long
    453. 'N8 39982547 Long
    454. 'N9 6378161 Long
    455. 'yz(0) =
    456. 'N0 28785620 Long
    457. 'N1 22601994 Long
    458. 'N2 23262302 Long
    459. 'N3 16422086 Long
    460. 'N4 60172685 Long
    461. 'N5 32369462 Long
    462. 'N6 48153093 Long
    463. 'N7 24551075 Long
    464. 'N8 55043262 Long
    465. 'N9 4839025 Long
    466. 'yz(1) =
    467. 'N0 62149839 Long
    468. 'N1 27250867 Long
    469. 'N2 81567 Long
    470. 'N3 6463061 Long
    471. 'N4 29961849 Long
    472. 'N5 21022609 Long
    473. 'N6 33531641 Long
    474. 'N7 25943479 Long
    475. 'N8 19667325 Long
    476. 'N9 8169659 Long
    477. 'yz(2) =
    478. 'N0 45499790 Long
    479. 'N1 8293683 Long
    480. 'N2 32672976 Long
    481. 'N3 26337680 Long
    482. 'N4 50195045 Long
    483. 'N5 9503606 Long
    484. 'N6 25823197 Long
    485. 'N7 21171840 Long
    486. 'N8 25301773 Long
    487. 'N9 20206989 Long
    488. #End Region
    489. k = (vi And 1) + (hi And 1)
    490. 'k = 0
    491. RefCurve25519.Reciprocal(t1(0), yz(k), 0)
    492. RefCurve25519.Multiply(t1(1), yx(k), t1(0))
    493. #Region "Tracestop 5"
    494. 't1(0) =
    495. 'N0 45872088 Long
    496. 'N1 33140773 Long
    497. 'N2 14911662 Long
    498. 'N3 23898813 Long
    499. 'N4 62412525 Long
    500. 'N5 30064514 Long
    501. 'N6 35380908 Long
    502. 'N7 14749202 Long
    503. 'N8 57081376 Long
    504. 'N9 1274365 Long
    505. 't1(1) =
    506. 'N0 4071327 Long
    507. 'N1 27121515 Long
    508. 'N2 3684926 Long
    509. 'N3 13986023 Long
    510. 'N4 63714629 Long
    511. 'N5 32417481 Long
    512. 'N6 65506912 Long
    513. 'N7 22562823 Long
    514. 'N8 34259780 Long
    515. 'N9 2110758 Long
    516. 't1(2) =
    517. 'N0 43433296 Long
    518. 'N1 28535350 Long
    519. 'N2 54616526 Long
    520. 'N3 3286957 Long
    521. 'N4 44009088 Long
    522. 'N5 7814553 Long
    523. 'N6 21404851 Long
    524. 'N7 29710982 Long
    525. 'N8 61374206 Long
    526. 'N9 29792398 Long
    527. #End Region
    528. Dim Y(31) As Byte
    529. RefCurve25519.Pack(t1(1), Y)
    530. ' Soll = d65c6cc6d1841a3deec249841d7bf1ddeb65625efa70f8d98867a3175a1be526
    531. Dim HEXHEX As String = ByteAry2HEX(Y) 'Y = 0145e6685f5ee3bd65af2821351b2548e7f95fb56d3d368a05c9fc7211481360
    532. Return Y
    533. End Function


    Vielen Dank schon mal
    Ist von hier und bitte Lizenz durchlesen.
    code.google.com/archive/p/curve25519-java/



    Hab es kurz übersetzt, finde aber jetzt nicht gerade den Fehler den er hat in der Funktion Test_Equal

    Freundliche Grüsse

    exc-jdbi


    Spoiler anzeigen

    Quellcode

    1. Option Strict On
    2. Option Explicit On
    3. Imports System.Threading
    4. Public Module Module1
    5. Public Sub Main()
    6. Dim tk As Double = 0, tv As Double = 0
    7. Console.WriteLine(vbLf & "--- Diffie Hellman (ECDH) ---" & vbLf)
    8. Benchmark("Key agreement", EBench.BENCH_AGREE, 4, 0)
    9. Test_Equal(K, Check1)
    10. Console.WriteLine(" Keypair generation : same" & vbLf)
    11. Console.WriteLine(vbLf & "--- Digital Signatures (EC-KCDSA) ---" & vbLf)
    12. tv = Benchmark("Verification", EBench.BENCH_VERIFY, 4, 0)
    13. Test_Equal(K, Check2)
    14. tk = Benchmark("Keypair generation", EBench.BENCH_KEYGEN, 2, tv)
    15. Test_Equal(E1k, Check3)
    16. Test_Equal(E2k, Check4)
    17. Benchmark("Signing", EBench.BENCH_SIGN, 1, tk + tv)
    18. Test_Equal(K, Check5)
    19. Console.WriteLine(vbLf & "OK")
    20. End Sub
    21. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    22. Private ReadOnly E1 As Byte() = {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    23. Private ReadOnly E2 As Byte() = {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    24. Private ReadOnly K As Byte() = {9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    25. Private ReadOnly E1k As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    26. Private ReadOnly E2k As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    27. Private ReadOnly E1e2k As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    28. Private ReadOnly E2e1k As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    29. Private ReadOnly E1s As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    30. Private ReadOnly E2s As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    31. Private Sub Xor25519(d As Byte(), s As Byte())
    32. Dim i As Int32
    33. For i = 0 To 32 - 1
    34. d(i) = d(i) Xor s(i)
    35. Next
    36. End Sub
    37. Private Sub Test_Equal(a As Byte(), b As Byte())
    38. Dim i As Int32
    39. For i = 0 To 32 - 1
    40. If a(i) <> b(i) Then
    41. Throw New ArgumentException(vbLf & "*** consistency check failed ***" & vbLf)
    42. End If
    43. Next
    44. End Sub
    45. Private Sub State_Update()
    46. Test_Equal(E1e2k, E2e1k)
    47. Xor25519(E1, E2k)
    48. Xor25519(E2, E1k)
    49. Xor25519(K, E1e2k)
    50. End Sub
    51. Private Enum EBench
    52. BENCH_NOP = 0
    53. BENCH_AGREE = 1
    54. BENCH_VERIFY = 2
    55. BENCH_KEYGEN = 3
    56. BENCH_SIGN = 4
    57. End Enum
    58. Private Function Time_Bench(bench As Int32, count As Int32) As Long
    59. Dim sw = Stopwatch.StartNew()
    60. Dim start As Long = sw.ElapsedTicks
    61. While count <> 0
    62. Select Case bench
    63. Case EBench.BENCH_NOP
    64. Call State_Update()
    65. Exit Select
    66. Case EBench.BENCH_AGREE
    67. Curve25519.Curve(E1k, E1, K)
    68. Curve25519.Curve(E2e1k, E2, E1k)
    69. Curve25519.Curve(E2k, E2, K)
    70. Curve25519.Curve(E1e2k, E1, E2k)
    71. Call State_Update()
    72. Exit Select
    73. Case EBench.BENCH_VERIFY
    74. Curve25519.Verify(E1k, E1, Curve25519.ZERO, K)
    75. Curve25519.Verify(E2e1k, E2, Curve25519.ZERO, E1k)
    76. Curve25519.Verify(E2k, E2, Curve25519.ZERO, K)
    77. Curve25519.Verify(E1e2k, E1, Curve25519.ZERO, E2k)
    78. Call State_Update()
    79. Exit Select
    80. Case EBench.BENCH_KEYGEN
    81. Curve25519.KeyGen(E1k, E1s, E1)
    82. Curve25519.KeyGen(E2k, E2s, E2)
    83. Curve25519.Verify(E1e2k, E1s, K, E1k)
    84. Curve25519.Verify(E2e1k, E2s, K, E2k)
    85. Call State_Update()
    86. Exit Select
    87. Case EBench.BENCH_SIGN
    88. Curve25519.KeyGen(E1k, E1, E1)
    89. Curve25519.KeyGen(E1e2k, Nothing, E2)
    90. Curve25519.Sign(E2k, E1e2k, E2, E1)
    91. Curve25519.Verify(E2e1k, E2k, E1e2k, E1k)
    92. Call State_Update()
    93. Exit Select
    94. End Select
    95. count -= 1
    96. End While
    97. Return (sw.ElapsedTicks - start) \ 1000L
    98. End Function
    99. Private Function Benchmark(what As String, bench As Int32, div As Int32, offset As Double) As Double
    100. Dim TRIES As Int32 = 3
    101. Dim COUNT As Int32 = 2000
    102. Dim i As Int32
    103. Dim time As Double, leasttime As Double = 10000000000.0
    104. Console.WriteLine(what)
    105. 'System.out.printf(" %-18s : ", what);
    106. i = TRIES
    107. While Math.Max(Interlocked.Decrement(i), i + 1) <> 0
    108. time = Time_Bench(bench, COUNT \ div)
    109. time -= Time_Bench(EBench.BENCH_NOP, COUNT \ div)
    110. time = time / (COUNT * 1000) - offset
    111. If time < leasttime Then leasttime = time
    112. Console.WriteLine(time.ToString())
    113. 'System.out.printf("%.3f ", time);
    114. End While
    115. Console.WriteLine("ms")
    116. 'System.out.println("ms");
    117. Return leasttime
    118. End Function
    119. Private ReadOnly Check1 As Byte() = {255, 153, 2, 78, 126, 231, 146, 145, 26, 255, 202, 198, 120, 154, 239, 219, 81, 85, 90, 245, 200, 21, 212, 168, 212, 173, 200, 134, 193, 134, 40, 59}
    120. Private ReadOnly Check2 As Byte() = {4, 104, 164, 208, 209, 140, 151, 93, 72, 158, 222, 60, 125, 144, 106, 156, 92, 147, 23, 242, 55, 205, 177, 40, 247, 214, 178, 151, 252, 74, 150, 25}
    121. Private ReadOnly Check3 As Byte() = {102, 104, 149, 19, 117, 243, 84, 43, 51, 192, 17, 93, 58, 3, 64, 149, 11, 231, 126, 17, 36, 194, 137, 145, 86, 189, 235, 42, 147, 13, 202, 36}
    122. Private ReadOnly Check4 As Byte() = {9, 207, 229, 5, 75, 70, 10, 63, 222, 112, 123, 118, 148, 64, 234, 30, 4, 222, 173, 25, 192, 20, 77, 125, 133, 130, 244, 103, 99, 200, 173, 102}
    123. Private ReadOnly Check5 As Byte() = {71, 17, 254, 189, 183, 208, 95, 116, 185, 63, 163, 50, 130, 44, 231, 155, 150, 39, 72, 139, 42, 211, 82, 0, 249, 172, 10, 191, 147, 50, 100, 101}
    124. End Module
    125. ' Ported from C to Java by Dmitry Skiba [sahn0], 23/02/08.
    126. ' Original: http://cds.xs4all.nl:8081/ecdh/
    127. '
    128. ' Generic 64-bit int32 implementation of Curve25519 ECDH
    129. ' Written by Matthijs van Duin, 200608242056
    130. ' Public domain.
    131. '
    132. ' Based on work by Daniel J Bernstein, http://cr.yp.to/ecdh.html
    133. '
    134. Public Class Curve25519
    135. ' key size
    136. Public Shared ReadOnly KEY_SIZE As Int32 = 32
    137. ' 0
    138. Public Shared ZERO As Byte() = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    139. ' the prime 2^255-19
    140. Public Shared PRIME As Byte() = {237, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127}
    141. ' group order (a prime near 2^252+2^124)
    142. Public Shared ORDER As Byte() = {237, 211, 245, 92, 26, 99, 18, 88, 214, 156, 247, 162, 222, 249, 222, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16}
    143. ' ******* KEY AGREEMENT ********
    144. ' Private key clamping
    145. ' * k [out] your private key for key agreement
    146. ' * k [in] 32 random bytes
    147. '
    148. Public Shared Sub Clamp(k As Byte())
    149. k(31) = Convert.ToByte(k(31) And &H7F)
    150. k(31) = Convert.ToByte(k(31) Or &H40)
    151. k(0) = Convert.ToByte(k(0) And &HF8)
    152. End Sub
    153. ' Key-pair generation
    154. ' * P [out] your public key
    155. ' * s [out] your private key for signing
    156. ' * k [out] your private key for key agreement
    157. ' * k [in] 32 random bytes
    158. ' * s may be NULL if you don't care
    159. ' *
    160. ' * WARNING: if s is not NULL, this function has data-dependent timing
    161. Public Shared Sub KeyGen(P As Byte(), s As Byte(), k As Byte())
    162. Clamp(k)
    163. Core(P, s, k, Nothing)
    164. End Sub
    165. ' Key agreement
    166. ' * Z [out] shared secret (needs hashing before use)
    167. ' * k [in] your private key for key agreement
    168. ' * P [in] peer's public key
    169. '
    170. Public Shared Sub Curve(Z As Byte(), k As Byte(), P As Byte())
    171. Core(Z, Nothing, k, P)
    172. End Sub
    173. ' ******* DIGITAL SIGNATURES ********
    174. ' deterministic EC-KCDSA
    175. ' *
    176. ' * s is the private key for signing
    177. ' * P is the corresponding public key
    178. ' * Z is the context data (signer public key or certificate, etc)
    179. ' *
    180. ' * signing:
    181. ' *
    182. ' * m = hash(Z, message)
    183. ' * x = hash(m, s)
    184. ' * keygen25519(Y, NULL, x);
    185. ' * r = hash(Y);
    186. ' * h = m XOR r
    187. ' * sign25519(v, h, x, s);
    188. ' *
    189. ' * output (v,r) as the signature
    190. ' *
    191. ' * verification:
    192. ' *
    193. ' * m = hash(Z, message);
    194. ' * h = m XOR r
    195. ' * verify25519(Y, v, h, P)
    196. ' *
    197. ' * confirm r == hash(Y)
    198. ' *
    199. ' * It would seem to me that it would be simpler to have the signer directly do
    200. ' * h = hash(m, Y) and send that to the recipient instead of r, who can verify
    201. ' * the signature by checking h == hash(m, Y). If there are any problems with
    202. ' * such a scheme, please let me know.
    203. ' *
    204. ' * Also, EC-KCDSA (like most DS algorithms) picks x random, which is a waste of
    205. ' * perfectly good entropy, but does allow Y to be calculated in advance of (or
    206. ' * parallel to) hashing the message.
    207. '
    208. ' Signature generation primitive, calculates (x-h)s mod q
    209. ' * v [out] signature value
    210. ' * h [in] signature hash (of message, signature pub key, and context data)
    211. ' * x [in] signature private key
    212. ' * s [in] private key for signing
    213. ' * returns true on success, false on failure (use different x or h)
    214. '
    215. Public Shared Function Sign(v As Byte(), h As Byte(), x As Byte(), s As Byte()) As Boolean
    216. ' v = (x - h) s mod q
    217. Dim tmp1 = New Byte(64) {}
    218. Dim tmp2 = New Byte(32) {}
    219. Dim w, i As Int32
    220. For i = 0 To 32 - 1
    221. v(i) = 0
    222. Next
    223. i = Mula_Small(v, x, 0, h, 32, -1)
    224. Mula_Small(v, v, 0, ORDER, 32, (15 - v(31)) \ 16)
    225. Mula32(tmp1, v, s, 32, 1)
    226. DivMod(tmp2, tmp1, 64, ORDER, 32)
    227. w = 0
    228. i = 0
    229. While i < 32
    230. v(i) = tmp1(i)
    231. w = w Or v(i)
    232. 'w = w Or CSharpImpl.__Assign(v(i), tmp1(i))
    233. i += 1
    234. End While
    235. Return w <> 0
    236. End Function
    237. ' Signature verification primitive, calculates Y = vP + hG
    238. ' * Y [out] signature public key
    239. ' * v [in] signature value
    240. ' * h [in] signature hash
    241. ' * P [in] public key
    242. '
    243. Public Shared Sub Verify(Yk As Byte(), v As Byte(), h As Byte(), Pk As Byte())
    244. ' Y = v abs(P) + h G
    245. Dim d = New Byte(31) {}
    246. Dim p As Long10() = New Long10() {New Long10(), New Long10()}, s As Long10() = New Long10() {New Long10(), New Long10()}, yx As Long10() = New Long10() {New Long10(), New Long10(), New Long10()}, yz As Long10() = New Long10() {New Long10(), New Long10(), New Long10()}, t1 As Long10() = New Long10() {New Long10(), New Long10(), New Long10()}, t2 As Long10() = New Long10() {New Long10(), New Long10(), New Long10()}
    247. Dim i, j, k As Int32, vi As Int32 = 0, hi As Int32 = 0, di As Int32 = 0, nvh As Int32 = 0
    248. ' set p[0] to G and p[1] to P
    249. [Set](p(0), 9)
    250. Unpack(p(1), Pk)
    251. ' set s[0] to P+G and s[1] to P-G
    252. ' s[0] = (Py^2 + Gy^2 - 2 Py Gy)/(Px - Gx)^2 - Px - Gx - 486662
    253. ' s[1] = (Py^2 + Gy^2 + 2 Py Gy)/(Px - Gx)^2 - Px - Gx - 486662
    254. X_To_Y2(t1(0), t2(0), p(1)) ' t2[0] = Py^2
    255. Sqrt(t1(0), t2(0)) ' t1[0] = Py or -Py
    256. j = Is_Negative(t1(0)) ' ... check which
    257. t2(0)._0 += 39420360 ' t2[0] = Py^2 + Gy^2
    258. Mul(t2(1), BASE_2Y, t1(0)) ' t2[1] = 2 Py Gy or -2 Py Gy
    259. [Sub](t1(j), t2(0), t2(1)) ' t1[0] = Py^2 + Gy^2 - 2 Py Gy
    260. Add(t1(1 - j), t2(0), t2(1)) ' t1[1] = Py^2 + Gy^2 + 2 Py Gy
    261. Cpy(t2(0), p(1)) ' t2[0] = Px
    262. t2(0)._0 -= 9 ' t2[0] = Px - Gx
    263. Sqr(t2(1), t2(0)) ' t2[1] = (Px - Gx)^2
    264. Recip(t2(0), t2(1), 0) ' t2[0] = 1/(Px - Gx)^2
    265. Mul(s(0), t1(0), t2(0)) ' s[0] = t1[0]/(Px - Gx)^2
    266. [Sub](s(0), s(0), p(1)) ' s[0] = t1[0]/(Px - Gx)^2 - Px
    267. s(0)._0 -= 9 + 486662 ' s[0] = X(P+G)
    268. Mul(s(1), t1(1), t2(0)) ' s[1] = t1[1]/(Px - Gx)^2
    269. [Sub](s(1), s(1), p(1)) ' s[1] = t1[1]/(Px - Gx)^2 - Px
    270. s(1)._0 -= 9 + 486662 ' s[1] = X(P-G)
    271. Mul_Small(s(0), s(0), 1) ' reduce s[0]
    272. Mul_Small(s(1), s(1), 1) ' reduce s[1]
    273. ' prepare the chain
    274. For i = 0 To 32 - 1
    275. vi = vi >> 8 Xor v(i) And &HFF Xor (v(i) And &HFF) << 1
    276. hi = hi >> 8 Xor h(i) And &HFF Xor (h(i) And &HFF) << 1
    277. nvh = Not (vi Xor hi)
    278. di = nvh And (di And &H80) >> 7 Xor vi
    279. di = di Xor nvh And (di And &H1) << 1
    280. di = di Xor nvh And (di And &H2) << 1
    281. di = di Xor nvh And (di And &H4) << 1
    282. di = di Xor nvh And (di And &H8) << 1
    283. di = di Xor nvh And (di And &H10) << 1
    284. di = di Xor nvh And (di And &H20) << 1
    285. di = di Xor nvh And (di And &H40) << 1
    286. d(i) = Convert.ToByte(di And &HFF)
    287. Next
    288. di = (nvh And (di And &H80) << 1 Xor vi) >> 8
    289. ' initialize state
    290. [Set](yx(0), 1)
    291. Cpy(yx(1), p(di))
    292. Cpy(yx(2), s(0))
    293. [Set](yz(0), 0)
    294. [Set](yz(1), 1)
    295. [Set](yz(2), 1)
    296. ' y[0] is (even)P + (even)G
    297. ' * y[1] is (even)P + (odd)G if current d-bit is 0
    298. ' * y[1] is (odd)P + (even)G if current d-bit is 1
    299. ' * y[2] is (odd)P + (odd)G
    300. '
    301. vi = 0
    302. hi = 0
    303. ' and go for it!
    304. i = 32
    305. While Math.Max(Interlocked.Decrement(i), i + 1) <> 0
    306. vi = vi << 8 Or v(i) And &HFF
    307. hi = hi << 8 Or h(i) And &HFF
    308. di = di << 8 Or d(i) And &HFF
    309. j = 8
    310. While Math.Max(Interlocked.Decrement(j), j + 1) <> 0
    311. Mont_Prep(t1(0), t2(0), yx(0), yz(0))
    312. Mont_Prep(t1(1), t2(1), yx(1), yz(1))
    313. Mont_Prep(t1(2), t2(2), yx(2), yz(2))
    314. k = ((vi Xor vi >> 1) >> j And 1) + ((hi Xor hi >> 1) >> j And 1)
    315. Mont_Dbl(yx(2), yz(2), t1(k), t2(k), yx(0), yz(0))
    316. k = di >> j And 2 Xor (di >> j And 1) << 1
    317. Mont_Add(t1(1), t2(1), t1(k), t2(k), yx(1), yz(1), p(di >> j And 1))
    318. Mont_Add(t1(2), t2(2), t1(0), t2(0), yx(2), yz(2), s(((vi Xor hi) >> j And 2) >> 1))
    319. End While
    320. End While
    321. k = (vi And 1) + (hi And 1)
    322. Recip(t1(0), yz(k), 0)
    323. Mul(t1(1), yx(k), t1(0))
    324. Pack(t1(1), Yk)
    325. End Sub
    326. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    327. ' sahn0:
    328. ' * Using this class instead of long[10] to avoid bounds checks.
    329. Private Class Long10
    330. Public Sub New()
    331. End Sub
    332. Public Sub New(_0 As Long, _1 As Long, _2 As Long, _3 As Long, _4 As Long, _5 As Long, _6 As Long, _7 As Long, _8 As Long, _9 As Long)
    333. Me._0 = _0 : Me._1 = _1 : Me._2 = _2 : Me._3 = _3 : Me._4 = _4
    334. Me._5 = _5 : Me._6 = _6 : Me._7 = _7 : Me._8 = _8 : Me._9 = _9
    335. End Sub
    336. Public _0, _1, _2, _3, _4, _5, _6, _7, _8, _9 As Long
    337. End Class
    338. ' ******************* radix 2^8 math ********************
    339. Private Shared Sub Cpy32(d As Byte(), s As Byte())
    340. Dim i As Int32
    341. For i = 0 To 32 - 1
    342. d(i) = s(i)
    343. Next
    344. End Sub
    345. ' p[m..n+m-1] = q[m..n+m-1] + z * x
    346. ' n is the size of x
    347. ' n+m is the size of p and q
    348. Private Shared Function Mula_Small(p As Byte(), q As Byte(), m As Int32, x As Byte(), n As Int32, z As Int32) As Int32
    349. Dim v As Int32 = 0, i As Int32 = 0
    350. While i < n
    351. v += (q(i + m) And &HFF) + z * (x(i) And &HFF)
    352. p(i + m) = Convert.ToByte(v And &HFF)
    353. v >>= 8
    354. i += 1
    355. End While
    356. Return v
    357. End Function
    358. ' p += x * y * z where z is a small int32
    359. ' * x is size 32, y is size t, p is size 32+t
    360. ' * y is allowed to overlap with p+32 if you don't care about the upper half
    361. Private Shared Function Mula32(p As Byte(), x As Byte(), y As Byte(), t As Int32, z As Int32) As Int32
    362. Dim n As Int32 = 31
    363. Dim w As Int32 = 0
    364. Dim i As Int32 = 0
    365. While i < t
    366. Dim zy As Int32 = z * (y(i) And &HFF)
    367. w += Mula_Small(p, p, i, x, n, zy) + (p(i + n) And &HFF) + zy * (x(n) And &HFF)
    368. p(i + n) = Convert.ToByte(w And &HFF)
    369. w >>= 8
    370. i += 1
    371. End While
    372. p(i + n) = Convert.ToByte((w + (p(i + n) And &HFF)) And &HFF)
    373. Return w >> 8
    374. End Function
    375. ' divide r (size n) by d (size t), returning quotient q and remainder r
    376. ' * quotient is size n-t+1, remainder is size t
    377. ' * requires t > 0 && d[t-1] != 0
    378. ' * requires that r[-1] and d[-1] are valid memory locations
    379. ' * q may overlap with r+t
    380. Private Shared Sub DivMod(q As Byte(), r As Byte(), n As Int32, d As Byte(), t As Int32)
    381. Dim rn As Int32 = 0
    382. Dim dt As Int32 = (d(t - 1) And &HFF) << 8
    383. If t > 1 Then
    384. dt = dt Or d(t - 2) And &HFF
    385. End If
    386. While Math.Max(Interlocked.Decrement(n), n + 1) >= t
    387. Dim z As Int32 = rn << 16 Or (r(n) And &HFF) << 8
    388. If n > 0 Then
    389. z = z Or r(n - 1) And &HFF
    390. End If
    391. z \= dt
    392. rn += Mula_Small(r, r, n - t + 1, d, t, -z)
    393. q(n - t + 1) = Convert.ToByte(z + rn And &HFF) ' rn is 0 or -1 (underflow)
    394. Mula_Small(r, r, n - t + 1, d, t, -rn)
    395. rn = r(n) And &HFF
    396. r(n) = 0
    397. End While
    398. r(t - 1) = Convert.ToByte(rn)
    399. End Sub
    400. Private Shared Function NumSize(x As Byte(), n As Int32) As Int32
    401. While Math.Max(Interlocked.Decrement(n), n + 1) <> 0 AndAlso x(n) = 0
    402. End While
    403. Return n + 1
    404. End Function
    405. ' Returns x if a contains the gcd, y if b.
    406. ' * Also, the returned buffer contains the inverse of a mod b,
    407. ' * as 32-byte signed.
    408. ' * x and y must have 64 bytes space for temporary use.
    409. ' * requires that a[-1] and b[-1] are valid memory locations
    410. Private Shared Function Egcd32(x As Byte(), y As Byte(), a As Byte(), b As Byte()) As Byte()
    411. Dim an, qn, i As Int32, bn As Int32 = 32
    412. For i = 0 To 32 - 1
    413. y(i) = 0 : x(i) = 0
    414. Next
    415. x(0) = 1
    416. an = NumSize(a, 32)
    417. If an = 0 Then Return y ' division by zero
    418. Dim temp As Byte() = New Byte(31) {}
    419. While True
    420. qn = bn - an + 1
    421. DivMod(temp, b, bn, a, an)
    422. bn = NumSize(b, bn)
    423. If bn = 0 Then Return x
    424. Mula32(y, x, temp, qn, -1)
    425. qn = an - bn + 1
    426. DivMod(temp, a, an, b, bn)
    427. an = NumSize(a, an)
    428. If an = 0 Then Return y
    429. Mula32(x, y, temp, qn, -1)
    430. End While
    431. Throw New Exception()
    432. End Function
    433. ' ******************* radix 2^25.5 GF(2^255-19) math ********************
    434. Private Shared ReadOnly P25 As Int32 = 33554431 ' (1 << 25) - 1
    435. Private Shared ReadOnly P26 As Int32 = 67108863 ' (1 << 26) - 1
    436. ' Convert to internal format from little-endian byte format
    437. Private Shared Sub Unpack(x As Long10, m As Byte())
    438. x._0 = m(0) And &HFF Or (m(1) And &HFF) << 8 Or (m(2) And &HFF) << 16 Or (m(3) And &HFF And 3) << 24
    439. x._1 = (m(3) And &HFF And Not 3) >> 2 Or (m(4) And &HFF) << 6 Or (m(5) And &HFF) << 14 Or (m(6) And &HFF And 7) << 22
    440. x._2 = (m(6) And &HFF And Not 7) >> 3 Or (m(7) And &HFF) << 5 Or (m(8) And &HFF) << 13 Or (m(9) And &HFF And 31) << 21
    441. x._3 = (m(9) And &HFF And Not 31) >> 5 Or (m(10) And &HFF) << 3 Or (m(11) And &HFF) << 11 Or (m(12) And &HFF And 63) << 19
    442. x._4 = (m(12) And &HFF And Not 63) >> 6 Or (m(13) And &HFF) << 2 Or (m(14) And &HFF) << 10 Or (m(15) And &HFF) << 18
    443. x._5 = m(16) And &HFF Or (m(17) And &HFF) << 8 Or (m(18) And &HFF) << 16 Or (m(19) And &HFF And 1) << 24
    444. x._6 = (m(19) And &HFF And Not 1) >> 1 Or (m(20) And &HFF) << 7 Or (m(21) And &HFF) << 15 Or (m(22) And &HFF And 7) << 23
    445. x._7 = (m(22) And &HFF And Not 7) >> 3 Or (m(23) And &HFF) << 5 Or (m(24) And &HFF) << 13 Or (m(25) And &HFF And 15) << 21
    446. x._8 = (m(25) And &HFF And Not 15) >> 4 Or (m(26) And &HFF) << 4 Or (m(27) And &HFF) << 12 Or (m(28) And &HFF And 63) << 20
    447. x._9 = (m(28) And &HFF And Not 63) >> 6 Or (m(29) And &HFF) << 2 Or (m(30) And &HFF) << 10 Or (m(31) And &HFF) << 18
    448. End Sub
    449. ' Check if reduced-form input >= 2^255-19
    450. Private Shared Function Is_Overflow(x As Long10) As Boolean
    451. Return x._0 > P26 - 19 AndAlso (x._1 And x._3 And x._5 And x._7 And x._9) = P25 AndAlso (x._2 And x._4 And x._6 And x._8) = P26 OrElse x._9 > P25
    452. End Function
    453. ' Convert from internal format to little-endian byte format. The
    454. ' * number must be in a reduced form which is output by the following ops:
    455. ' * unpack, mul, sqr
    456. ' * set -- if input in range 0 .. P25
    457. ' * If you're unsure if the number is reduced, first multiply it by 1.
    458. Private Shared Sub Pack(x As Long10, m As Byte())
    459. Dim ld As Int32 = 0, ud As Int32 = 0
    460. Dim t As Long
    461. ld = If(Is_Overflow(x), 1, 0) - If(x._9 < 0, 1, 0)
    462. ud = ld * -(P25 + 1)
    463. ld *= 19
    464. t = ld + x._0 + (x._1 << 26)
    465. m(0) = Convert.ToByte(t And &HFF)
    466. m(1) = Convert.ToByte((t >> 8) And &HFF)
    467. m(2) = Convert.ToByte((t >> 16) And &HFF)
    468. m(3) = Convert.ToByte((t >> 24) And &HFF)
    469. t = (t >> 32) + (x._2 << 19)
    470. m(4) = Convert.ToByte(t And &HFF)
    471. m(5) = Convert.ToByte((t >> 8) And &HFF)
    472. m(6) = Convert.ToByte((t >> 16) And &HFF)
    473. m(7) = Convert.ToByte((t >> 24) And &HFF)
    474. t = (t >> 32) + (x._3 << 13)
    475. m(8) = Convert.ToByte(t And &HFF)
    476. m(9) = Convert.ToByte((t >> 8) And &HFF)
    477. m(10) = Convert.ToByte((t >> 16) And &HFF)
    478. m(11) = Convert.ToByte((t >> 24) And &HFF)
    479. t = (t >> 32) + (x._4 << 6)
    480. m(12) = Convert.ToByte(t And &HFF)
    481. m(13) = Convert.ToByte((t >> 8) And &HFF)
    482. m(14) = Convert.ToByte((t >> 16) And &HFF)
    483. m(15) = Convert.ToByte((t >> 24) And &HFF)
    484. t = (t >> 32) + x._5 + (x._6 << 25)
    485. m(16) = Convert.ToByte(t And &HFF)
    486. m(17) = Convert.ToByte((t >> 8) And &HFF)
    487. m(18) = Convert.ToByte((t >> 16) And &HFF)
    488. m(19) = Convert.ToByte((t >> 24) And &HFF)
    489. t = (t >> 32) + (x._7 << 19)
    490. m(20) = Convert.ToByte(t And &HFF)
    491. m(21) = Convert.ToByte((t >> 8) And &HFF)
    492. m(22) = Convert.ToByte((t >> 16) And &HFF)
    493. m(23) = Convert.ToByte((t >> 24) And &HFF)
    494. t = (t >> 32) + (x._8 << 12)
    495. m(24) = Convert.ToByte(t And &HFF)
    496. m(25) = Convert.ToByte((t >> 8) And &HFF)
    497. m(26) = Convert.ToByte((t >> 16) And &HFF)
    498. m(27) = Convert.ToByte((t >> 24) And &HFF)
    499. t = (t >> 32) + (x._9 + ud << 6)
    500. m(28) = Convert.ToByte(t And &HFF)
    501. m(29) = Convert.ToByte((t >> 8) And &HFF)
    502. m(30) = Convert.ToByte((t >> 16) And &HFF)
    503. m(31) = Convert.ToByte((t >> 24) And &HFF)
    504. End Sub
    505. ' Copy a number
    506. Private Shared Sub Cpy(_out As Long10, _in As Long10)
    507. _out._0 = _in._0
    508. _out._1 = _in._1
    509. _out._2 = _in._2
    510. _out._3 = _in._3
    511. _out._4 = _in._4
    512. _out._5 = _in._5
    513. _out._6 = _in._6
    514. _out._7 = _in._7
    515. _out._8 = _in._8
    516. _out._9 = _in._9
    517. End Sub
    518. ' Set a number to value, which must be in range -185861411 .. 185861411
    519. Private Shared Sub [Set](_out As Long10, _in As Int32)
    520. _out._0 = _in
    521. _out._1 = 0
    522. _out._2 = 0
    523. _out._3 = 0
    524. _out._4 = 0
    525. _out._5 = 0
    526. _out._6 = 0
    527. _out._7 = 0
    528. _out._8 = 0
    529. _out._9 = 0
    530. End Sub
    531. ' Add/subtract two numbers. The inputs must be in reduced form, and the
    532. ' * output isn't, so to do another addition or subtraction on the output,
    533. ' * first multiply it by one to reduce it.
    534. Private Shared Sub Add(xy As Long10, x As Long10, y As Long10)
    535. xy._0 = x._0 + y._0
    536. xy._1 = x._1 + y._1
    537. xy._2 = x._2 + y._2
    538. xy._3 = x._3 + y._3
    539. xy._4 = x._4 + y._4
    540. xy._5 = x._5 + y._5
    541. xy._6 = x._6 + y._6
    542. xy._7 = x._7 + y._7
    543. xy._8 = x._8 + y._8
    544. xy._9 = x._9 + y._9
    545. End Sub
    546. Private Shared Sub [Sub](xy As Long10, x As Long10, y As Long10)
    547. xy._0 = x._0 - y._0
    548. xy._1 = x._1 - y._1
    549. xy._2 = x._2 - y._2
    550. xy._3 = x._3 - y._3
    551. xy._4 = x._4 - y._4
    552. xy._5 = x._5 - y._5
    553. xy._6 = x._6 - y._6
    554. xy._7 = x._7 - y._7
    555. xy._8 = x._8 - y._8
    556. xy._9 = x._9 - y._9
    557. End Sub
    558. ' Multiply a number by a small int32 in range -185861411 .. 185861411.
    559. ' * The output is in reduced form, the input x need not be. x and xy may point
    560. ' * to the same buffer.
    561. Private Shared Function Mul_Small(xy As Long10, x As Long10, y As Long) As Long10
    562. Dim t As Long
    563. t = x._8 * y
    564. xy._8 = t And (1 << 26) - 1
    565. t = (t >> 26) + x._9 * y
    566. xy._9 = t And (1 << 25) - 1
    567. t = 19 * (t >> 25) + x._0 * y
    568. xy._0 = t And (1 << 26) - 1
    569. t = (t >> 26) + x._1 * y
    570. xy._1 = t And (1 << 25) - 1
    571. t = (t >> 25) + x._2 * y
    572. xy._2 = t And (1 << 26) - 1
    573. t = (t >> 26) + x._3 * y
    574. xy._3 = t And (1 << 25) - 1
    575. t = (t >> 25) + x._4 * y
    576. xy._4 = t And (1 << 26) - 1
    577. t = (t >> 26) + x._5 * y
    578. xy._5 = t And (1 << 25) - 1
    579. t = (t >> 25) + x._6 * y
    580. xy._6 = t And (1 << 26) - 1
    581. t = (t >> 26) + x._7 * y
    582. xy._7 = t And (1 << 25) - 1
    583. t = (t >> 25) + xy._8
    584. xy._8 = t And (1 << 26) - 1
    585. xy._9 += t >> 26
    586. Return xy
    587. End Function
    588. ' Multiply two numbers. The output is in reduced form, the inputs need not
    589. ' * be.
    590. Private Shared Function Mul(xy As Long10, x As Long10, y As Long10) As Long10
    591. ' sahn0:
    592. ' * Using local variables to avoid class access.
    593. ' * This seem to improve performance a bit...
    594. '
    595. Dim x_0 As Long = x._0, x_1 As Long = x._1, x_2 As Long = x._2, x_3 As Long = x._3, x_4 As Long = x._4, x_5 As Long = x._5, x_6 As Long = x._6, x_7 As Long = x._7, x_8 As Long = x._8, x_9 As Long = x._9
    596. Dim y_0 As Long = y._0, y_1 As Long = y._1, y_2 As Long = y._2, y_3 As Long = y._3, y_4 As Long = y._4, y_5 As Long = y._5, y_6 As Long = y._6, y_7 As Long = y._7, y_8 As Long = y._8, y_9 As Long = y._9
    597. Dim t As Long
    598. t = x_0 * y_8 + x_2 * y_6 + x_4 * y_4 + x_6 * y_2 + x_8 * y_0 + 2 * (x_1 * y_7 + x_3 * y_5 + x_5 * y_3 + x_7 * y_1) + 38 * (x_9 * y_9)
    599. xy._8 = t And (1 << 26) - 1
    600. t = (t >> 26) + x_0 * y_9 + x_1 * y_8 + x_2 * y_7 + x_3 * y_6 + x_4 * y_5 + x_5 * y_4 + x_6 * y_3 + x_7 * y_2 + x_8 * y_1 + x_9 * y_0
    601. xy._9 = t And (1 << 25) - 1
    602. t = x_0 * y_0 + 19 * ((t >> 25) + x_2 * y_8 + x_4 * y_6 + x_6 * y_4 + x_8 * y_2) + 38 * (x_1 * y_9 + x_3 * y_7 + x_5 * y_5 + x_7 * y_3 + x_9 * y_1)
    603. xy._0 = t And (1 << 26) - 1
    604. t = (t >> 26) + x_0 * y_1 + x_1 * y_0 + 19 * (x_2 * y_9 + x_3 * y_8 + x_4 * y_7 + x_5 * y_6 + x_6 * y_5 + x_7 * y_4 + x_8 * y_3 + x_9 * y_2)
    605. xy._1 = t And (1 << 25) - 1
    606. t = (t >> 25) + x_0 * y_2 + x_2 * y_0 + 19 * (x_4 * y_8 + x_6 * y_6 + x_8 * y_4) + 2 * (x_1 * y_1) + 38 * (x_3 * y_9 + x_5 * y_7 + x_7 * y_5 + x_9 * y_3)
    607. xy._2 = t And (1 << 26) - 1
    608. t = (t >> 26) + x_0 * y_3 + x_1 * y_2 + x_2 * y_1 + x_3 * y_0 + 19 * (x_4 * y_9 + x_5 * y_8 + x_6 * y_7 + x_7 * y_6 + x_8 * y_5 + x_9 * y_4)
    609. xy._3 = t And (1 << 25) - 1
    610. t = (t >> 25) + x_0 * y_4 + x_2 * y_2 + x_4 * y_0 + 19 * (x_6 * y_8 + x_8 * y_6) + 2 * (x_1 * y_3 + x_3 * y_1) + 38 * (x_5 * y_9 + x_7 * y_7 + x_9 * y_5)
    611. xy._4 = t And (1 << 26) - 1
    612. t = (t >> 26) + x_0 * y_5 + x_1 * y_4 + x_2 * y_3 + x_3 * y_2 + x_4 * y_1 + x_5 * y_0 + 19 * (x_6 * y_9 + x_7 * y_8 + x_8 * y_7 + x_9 * y_6)
    613. xy._5 = t And (1 << 25) - 1
    614. t = (t >> 25) + x_0 * y_6 + x_2 * y_4 + x_4 * y_2 + x_6 * y_0 + 19 * (x_8 * y_8) + 2 * (x_1 * y_5 + x_3 * y_3 + x_5 * y_1) + 38 * (x_7 * y_9 + x_9 * y_7)
    615. xy._6 = t And (1 << 26) - 1
    616. t = (t >> 26) + x_0 * y_7 + x_1 * y_6 + x_2 * y_5 + x_3 * y_4 + x_4 * y_3 + x_5 * y_2 + x_6 * y_1 + x_7 * y_0 + 19 * (x_8 * y_9 + x_9 * y_8)
    617. xy._7 = t And (1 << 25) - 1
    618. t = (t >> 25) + xy._8
    619. xy._8 = t And (1 << 26) - 1
    620. xy._9 += t >> 26
    621. Return xy
    622. End Function
    623. ' Square a number. Optimization of mul25519(x2, x, x)
    624. Private Shared Function Sqr(x2 As Long10, x As Long10) As Long10
    625. Dim x_0 As Long = x._0, x_1 As Long = x._1, x_2 As Long = x._2, x_3 As Long = x._3, x_4 As Long = x._4, x_5 As Long = x._5, x_6 As Long = x._6, x_7 As Long = x._7, x_8 As Long = x._8, x_9 As Long = x._9
    626. Dim t As Long
    627. t = x_4 * x_4 + 2 * (x_0 * x_8 + x_2 * x_6) + 38 * (x_9 * x_9) + 4 * (x_1 * x_7 + x_3 * x_5)
    628. x2._8 = t And (1 << 26) - 1
    629. t = (t >> 26) + 2 * (x_0 * x_9 + x_1 * x_8 + x_2 * x_7 + x_3 * x_6 + x_4 * x_5)
    630. x2._9 = t And (1 << 25) - 1
    631. t = 19 * (t >> 25) + x_0 * x_0 + 38 * (x_2 * x_8 + x_4 * x_6 + x_5 * x_5) + 76 * (x_1 * x_9 + x_3 * x_7)
    632. x2._0 = t And (1 << 26) - 1
    633. t = (t >> 26) + 2 * (x_0 * x_1) + 38 * (x_2 * x_9 + x_3 * x_8 + x_4 * x_7 + x_5 * x_6)
    634. x2._1 = t And (1 << 25) - 1
    635. t = (t >> 25) + 19 * (x_6 * x_6) + 2 * (x_0 * x_2 + x_1 * x_1) + 38 * (x_4 * x_8) + 76 * (x_3 * x_9 + x_5 * x_7)
    636. x2._2 = t And (1 << 26) - 1
    637. t = (t >> 26) + 2 * (x_0 * x_3 + x_1 * x_2) + 38 * (x_4 * x_9 + x_5 * x_8 + x_6 * x_7)
    638. x2._3 = t And (1 << 25) - 1
    639. t = (t >> 25) + x_2 * x_2 + 2 * (x_0 * x_4) + 38 * (x_6 * x_8 + x_7 * x_7) + 4 * (x_1 * x_3) + 76 * (x_5 * x_9)
    640. x2._4 = t And (1 << 26) - 1
    641. t = (t >> 26) + 2 * (x_0 * x_5 + x_1 * x_4 + x_2 * x_3) + 38 * (x_6 * x_9 + x_7 * x_8)
    642. x2._5 = t And (1 << 25) - 1
    643. t = (t >> 25) + 19 * (x_8 * x_8) + 2 * (x_0 * x_6 + x_2 * x_4 + x_3 * x_3) + 4 * (x_1 * x_5) + 76 * (x_7 * x_9)
    644. x2._6 = t And (1 << 26) - 1
    645. t = (t >> 26) + 2 * (x_0 * x_7 + x_1 * x_6 + x_2 * x_5 + x_3 * x_4) + 38 * (x_8 * x_9)
    646. x2._7 = t And (1 << 25) - 1
    647. t = (t >> 25) + x2._8
    648. x2._8 = t And (1 << 26) - 1
    649. x2._9 += t >> 26
    650. Return x2
    651. End Function
    652. ' Calculates a reciprocal. The output is in reduced form, the inputs need not
    653. ' * be. Simply calculates y = x^(p-2) so it's not too fast.
    654. ' When sqrtassist is true, it instead calculates y = x^((p-5)/8)
    655. Private Shared Sub Recip(y As Long10, x As Long10, sqrtassist As Int32)
    656. Dim t0 As Long10 = New Long10(), t1 As Long10 = New Long10(), t2 As Long10 = New Long10(), t3 As Long10 = New Long10(), t4 As Long10 = New Long10()
    657. Dim i As Int32
    658. ' the chain for x^(2^255-21) is straight from djb's implementation
    659. Sqr(t1, x) ' 2 == 2 * 1
    660. Sqr(t2, t1) ' 4 == 2 * 2
    661. Sqr(t0, t2) ' 8 == 2 * 4
    662. Mul(t2, t0, x) ' 9 == 8 + 1
    663. Mul(t0, t2, t1) ' 11 == 9 + 2
    664. Sqr(t1, t0) ' 22 == 2 * 11
    665. Mul(t3, t1, t2) ' 31 == 22 + 9 == 2^5 - 2^0
    666. Sqr(t1, t3) ' 2^6 - 2^1
    667. Sqr(t2, t1) ' 2^7 - 2^2
    668. Sqr(t1, t2) ' 2^8 - 2^3
    669. Sqr(t2, t1) ' 2^9 - 2^4
    670. Sqr(t1, t2) ' 2^10 - 2^5
    671. Mul(t2, t1, t3) ' 2^10 - 2^0
    672. Sqr(t1, t2) ' 2^11 - 2^1
    673. Sqr(t3, t1) ' 2^12 - 2^2
    674. For i = 1 To 5 - 1
    675. Sqr(t1, t3)
    676. Sqr(t3, t1)
    677. Next ' t3
    678. ' 2^20 - 2^10
    679. Mul(t1, t3, t2) ' 2^20 - 2^0
    680. Sqr(t3, t1) ' 2^21 - 2^1
    681. Sqr(t4, t3) ' 2^22 - 2^2
    682. For i = 1 To 10 - 1
    683. Sqr(t3, t4)
    684. Sqr(t4, t3)
    685. Next ' t4
    686. ' 2^40 - 2^20
    687. Mul(t3, t4, t1) ' 2^40 - 2^0
    688. For i = 0 To 5 - 1
    689. Sqr(t1, t3)
    690. Sqr(t3, t1)
    691. Next ' t3
    692. ' 2^50 - 2^10
    693. Mul(t1, t3, t2) ' 2^50 - 2^0
    694. Sqr(t2, t1) ' 2^51 - 2^1
    695. Sqr(t3, t2) ' 2^52 - 2^2
    696. For i = 1 To 25 - 1
    697. Sqr(t2, t3)
    698. Sqr(t3, t2)
    699. Next ' t3
    700. ' 2^100 - 2^50
    701. Mul(t2, t3, t1) ' 2^100 - 2^0
    702. Sqr(t3, t2) ' 2^101 - 2^1
    703. Sqr(t4, t3) ' 2^102 - 2^2
    704. For i = 1 To 50 - 1
    705. Sqr(t3, t4)
    706. Sqr(t4, t3)
    707. Next ' t4
    708. ' 2^200 - 2^100
    709. Mul(t3, t4, t2) ' 2^200 - 2^0
    710. For i = 0 To 25 - 1
    711. Sqr(t4, t3)
    712. Sqr(t3, t4)
    713. Next ' t3
    714. ' 2^250 - 2^50
    715. Mul(t2, t3, t1) ' 2^250 - 2^0
    716. Sqr(t1, t2) ' 2^251 - 2^1
    717. Sqr(t2, t1) ' 2^252 - 2^2
    718. If sqrtassist <> 0 Then
    719. Mul(y, x, t2) ' 2^252 - 3
    720. Else
    721. Sqr(t1, t2) ' 2^253 - 2^3
    722. Sqr(t2, t1) ' 2^254 - 2^4
    723. Sqr(t1, t2) ' 2^255 - 2^5
    724. Mul(y, t1, t0) ' 2^255 - 21
    725. End If
    726. End Sub
    727. ' checks if x is "negative", requires reduced input
    728. Private Shared Function Is_Negative(x As Long10) As Int32
    729. Return Convert.ToInt32(If(Is_Overflow(x) OrElse x._9 < 0, 1, 0) Xor (x._0 And 1))
    730. End Function
    731. ' a square root
    732. Private Shared Sub Sqrt(x As Long10, u As Long10)
    733. Dim v As Long10 = New Long10(), t1 As Long10 = New Long10(), t2 As Long10 = New Long10()
    734. Add(t1, u, u) ' t1 = 2u
    735. Recip(v, t1, 1) ' v = (2u)^((p-5)/8)
    736. Sqr(x, v) ' x = v^2
    737. Mul(t2, t1, x) ' t2 = 2uv^2
    738. t2._0 -= 1 ' t2 = 2uv^2-1
    739. Mul(t1, v, t2) ' t1 = v(2uv^2-1)
    740. Mul(x, u, t1) ' x = uv(2uv^2-1)
    741. End Sub
    742. ' ******************* Elliptic curve ********************
    743. ' y^2 = x^3 + 486662 x^2 + x over GF(2^255-19)
    744. ' t1 = ax + az
    745. ' * t2 = ax - az
    746. Private Shared Sub Mont_Prep(t1 As Long10, t2 As Long10, ax As Long10, az As Long10)
    747. Add(t1, ax, az)
    748. [Sub](t2, ax, az)
    749. End Sub
    750. ' A = P + Q where
    751. ' * X(A) = ax/az
    752. ' * X(P) = (t1+t2)/(t1-t2)
    753. ' * X(Q) = (t3+t4)/(t3-t4)
    754. ' * X(P-Q) = dx
    755. ' * clobbers t1 and t2, preserves t3 and t4
    756. Private Shared Sub Mont_Add(t1 As Long10, t2 As Long10, t3 As Long10, t4 As Long10, ax As Long10, az As Long10, dx As Long10)
    757. Mul(ax, t2, t3)
    758. Mul(az, t1, t4)
    759. Add(t1, ax, az)
    760. [Sub](t2, ax, az)
    761. Sqr(ax, t1)
    762. Sqr(t1, t2)
    763. Mul(az, t1, dx)
    764. End Sub
    765. ' B = 2 * Q where
    766. ' * X(B) = bx/bz
    767. ' * X(Q) = (t3+t4)/(t3-t4)
    768. ' * clobbers t1 and t2, preserves t3 and t4
    769. Private Shared Sub Mont_Dbl(t1 As Long10, t2 As Long10, t3 As Long10, t4 As Long10, bx As Long10, bz As Long10)
    770. Sqr(t1, t3)
    771. Sqr(t2, t4)
    772. Mul(bx, t1, t2)
    773. [Sub](t2, t1, t2)
    774. Mul_Small(bz, t2, 121665)
    775. Add(t1, t1, bz)
    776. Mul(bz, t1, t2)
    777. End Sub
    778. ' Y^2 = X^3 + 486662 X^2 + X
    779. ' * t is a temporary
    780. Private Shared Sub X_To_Y2(t As Long10, y2 As Long10, x As Long10)
    781. Sqr(t, x)
    782. Mul_Small(y2, x, 486662)
    783. Add(t, t, y2)
    784. t._0 += 1
    785. Mul(y2, t, x)
    786. End Sub
    787. ' P = kG and s = sign(P)/k
    788. Private Shared Sub Core(Px As Byte(), s As Byte(), k As Byte(), Gx As Byte())
    789. Dim dx As Long10 = New Long10(), t1 As Long10 = New Long10(), t2 As Long10 = New Long10(), t3 As Long10 = New Long10(), t4 As Long10 = New Long10()
    790. Dim x As Long10() = New Long10() {New Long10(), New Long10()}, z As Long10() = New Long10() {New Long10(), New Long10()}
    791. Dim i, j As Int32
    792. ' unpack the base
    793. If Gx IsNot Nothing Then
    794. Unpack(dx, Gx)
    795. Else
    796. [Set](dx, 9)
    797. End If
    798. ' 0G = point-at-infinity
    799. [Set](x(0), 1)
    800. [Set](z(0), 0)
    801. ' 1G = G
    802. Cpy(x(1), dx)
    803. [Set](z(1), 1)
    804. i = 32
    805. While Math.Max(Interlocked.Decrement(i), i + 1) <> 0
    806. If i = 0 Then
    807. i = 0
    808. End If
    809. j = 8
    810. While Math.Max(Interlocked.Decrement(j), j + 1) <> 0
    811. ' swap arguments depending on bit
    812. Dim bit1 As Int32 = (k(i) And &HFF) >> j And 1
    813. Dim bit0 As Int32 = Not (k(i) And &HFF) >> j And 1
    814. Dim ax As Long10 = x(bit0)
    815. Dim az As Long10 = z(bit0)
    816. Dim bx As Long10 = x(bit1)
    817. Dim bz As Long10 = z(bit1)
    818. ' a' = a + b
    819. ' b' = 2 b
    820. Mont_Prep(t1, t2, ax, az)
    821. Mont_Prep(t3, t4, bx, bz)
    822. Mont_Add(t1, t2, t3, t4, ax, az, dx)
    823. Mont_Dbl(t1, t2, t3, t4, bx, bz)
    824. End While
    825. End While
    826. Recip(t1, z(0), 0)
    827. Mul(dx, x(0), t1)
    828. Pack(dx, Px)
    829. ' calculate s such that s abs(P) = G .. assumes G is std base point
    830. If s IsNot Nothing Then
    831. X_To_Y2(t2, t1, dx) ' t1 = Py^2
    832. Recip(t3, z(1), 0) ' where Q=P+G ...
    833. Mul(t2, x(1), t3) ' t2 = Qx
    834. Add(t2, t2, dx) ' t2 = Qx + Px
    835. t2._0 += 9 + 486662 ' t2 = Qx + Px + Gx + 486662
    836. dx._0 -= 9 ' dx = Px - Gx
    837. Sqr(t3, dx) ' t3 = (Px - Gx)^2
    838. Mul(dx, t2, t3) ' dx = t2 (Px - Gx)^2
    839. [Sub](dx, dx, t1) ' dx = t2 (Px - Gx)^2 - Py^2
    840. dx._0 -= 39420360 ' dx = t2 (Px - Gx)^2 - Py^2 - Gy^2
    841. Mul(t1, dx, BASE_R2Y) ' t1 = -Py
    842. If Is_Negative(t1) <> 0 Then ' sign is 1, so just copy
    843. Cpy32(s, k) ' sign is -1, so negate
    844. Else
    845. Mula_Small(s, ORDER_TIMES_8, 0, k, 32, -1)
    846. End If
    847. ' reduce s mod q
    848. ' * (is this needed? do it just in case, it's fast anyway)
    849. 'divmod((dstptr) t1, s, 32, order25519, 32);
    850. ' take reciprocal of s mod q
    851. Dim temp1 = New Byte(31) {}
    852. Dim temp2 = New Byte(63) {}
    853. Dim temp3 = New Byte(63) {}
    854. Cpy32(temp1, ORDER)
    855. Cpy32(s, Egcd32(temp2, temp3, s, temp1))
    856. If (s(31) And &H80) <> 0 Then Mula_Small(s, s, 0, ORDER, 32, 1)
    857. End If
    858. End Sub
    859. ' smallest multiple of the order that's >= 2^255
    860. Private Shared ReadOnly ORDER_TIMES_8 As Byte() = {104, 159, 174, 231, 210, 24, 147, 192, 178, 230, 188, 23, 245, 206, 247, 166, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}
    861. ' constants 2Gy and 1/(2Gy)
    862. Private Shared ReadOnly BASE_2Y As Long10 = New Long10(39999547, 18689728, 59995525, 1648697, 57546132, 24010086, 19059592, 5425144, 63499247, 16420658)
    863. Private Shared ReadOnly BASE_R2Y As Long10 = New Long10(5744, 8160848, 4790893, 13779497, 35730846, 12541209, 49101323, 30047407, 40071253, 6226132)
    864. End Class
    Ich brech ins Essen! Da kommt er mit einem voll funktionsfähigem Komplettcode um die Ecke und vernichtet mal eben so meine monatelange Arbeit daran mit einem Fingerschnippen und dem Vermerk: "hi, da war jemand schneller als du, lass es lieber ganz bleiben mit dem programmieren und werde Jurist und befasse dich mit Lizenzen..." NEIN, einfach NEIN :D ! Zum Einen finde ich das Saustark :thumbsup: ! Zum anderen jedoch echt uncool :thumbdown: . Aber gut gegoogelt ist halb gecodet und deswegen geht der Punkt an dich!

    Dankeschön exc-jdbi ;)
    @evolver

    Sorry, wollte ganz bestimmt nicht deinen Unmut steigern. Vielleicht wäre es auch sinnvoller gewesen, dir den Hinweis per PM zu kommen zu lassen. (Bin auch zufällig darüber gestolpert)

    Abgesehen von der Lizenz, darf man sicher mal den Code testen (darüber steht nicht einmal was) und schauen, was es mit der ECKCDSA auf sich hat und das mit den Infos von Wikipedia vergleichen. en.wikipedia.org/wiki/KCDSA

    Ausserdem ist der Code zum Teil "hundslausig" übersetzt, und jedem Vb.Net Profi würden gleich die Tränen in die Augen schiessen.

    Das Ganze (der GrundCode) lässt sich mMn um einiges verschönern, indem mehr mit den Bordmittel von .Net gearbeitet wird. (Async/Await, ActionOfT, FuncOfT, Anonyme Methoden, Klassen, Extentions, etc.)

    Was nun genau der Fehler (Test_Equal ungleiche Arrays) auf sich hat in der Signierung, bin ich noch nicht dahinter gekommen wo das Problem liegt, da ich mich mit dem Code und der Mathematik noch nicht beschäftigt habe. Es ist aber anscheinend nur die Signierung die diesen Fehler hervorruft.

    Daher meine Intension: Bitte nicht aufgeben und dran bleiben, und deine Arbeit fortsetzen, denn schlussendlich lernt man ja auch viel über die Umsetzung, das mathematisches Vorgehen, Konzeption, und überhaupt alles, was nicht sofort ersichtlich und offensichtlich etc. ist.

    Freundliche Grüsse

    exc-jdbi
    hallo,
    ich schrieb ja bereits, dass das Signieren schon läuft. Tausche einfach mal die "Sign"-Methode durch folgende aus und es funzt (zumind. bei mir), wenn du noch dran interessiert bist:

    Spoiler anzeigen

    Quellcode

    1. Public Shared Function Sign(ByVal h As Byte(), ByVal x As Byte(), ByVal s As Byte()) As Byte()
    2. ' v = (x - h) s Mod q
    3. Dim h1(31) As Byte
    4. Dim x1(31) As Byte
    5. Dim Dummy1(31) As Byte
    6. Dim Dummy2(31) As Byte
    7. Dim Temp_v(63) As Byte
    8. Dim Dummy3(63) As Byte
    9. ' Don't clobber the arguments, be nice!
    10. h1 = h.ToArray 'h1 = h
    11. x1 = x.ToArray 'x1 = x
    12. ' Reduce modulo group order
    13. DivMod(Dummy1, h1, 32, ORDER, 32)
    14. DivMod(Dummy2, x1, 32, ORDER, 32)
    15. ' v = x1 - h1
    16. ' If v Is negative, add the group order to it to become positive.
    17. ' If v was already positive we don't have to worry about overflow
    18. ' when adding the order because v < ORDER And 2*ORDER < 2^256
    19. Dim v(31) As Byte
    20. Mula_Small(v, x1, 0, h1, 32, -1)
    21. Mula_Small(v, v, 0, ORDER, 32, 1)
    22. ' Temp_v = (x-h)*s Mod q
    23. Mula32(Temp_v, v, s, 32, 1)
    24. DivMod(Dummy3, Temp_v, 64, ORDER, 32)
    25. Dim w As Boolean = False
    26. For i As Integer = 0 To 31
    27. v(i) = Temp_v(i)
    28. w = w Or CBool(v(i))
    29. Next
    30. If w <> False Then
    31. Return v
    32. Else
    33. Return Nothing
    34. End If
    35. End Function


    Beste Grüße
    Sieht so auf den ersten Blick gut aus :thumbsup:

    Habs jetzt einfach schnell umgeschrieben, hoffe das es kein Fehler hat.

    VB.NET-Quellcode

    1. Public Shared Function Sign(v As Byte(), h As Byte(), x As Byte(), s As Byte()) As Boolean
    2. ' v = (x - h) s mod q
    3. Dim w, i As Int32
    4. Array.Clear(v, 0, v.Length)
    5. Dim hh = h.ToArray 'h1 = h
    6. Dim xx = x.ToArray 'x1 = x
    7. ' Reduce modulo group order
    8. Dim Dummy1 = New Byte(31) {}
    9. Dim Dummy2 = New Byte(31) {}
    10. DivMod(Dummy1, hh, 32, ORDER, 32)
    11. DivMod(Dummy2, xx, 32, ORDER, 32)
    12. ' v = x1 - h1
    13. ' If v Is negative, add the group order to it to become positive.
    14. ' If v was already positive we don't have to worry about overflow
    15. ' when adding the order because v < ORDER And 2*ORDER < 2^256
    16. Dim tmp1 = New Byte(63) {}
    17. Dim tmp2 = New Byte(63) {}
    18. i = Mula_Small(v, xx, 0, hh, 32, -1)
    19. 'Mula_Small(v, v, 0, ORDER, 32, (15 - v(31)) \ 16)
    20. Mula_Small(v, v, 0, ORDER, 32, 1)
    21. Mula32(tmp1, v, s, 32, 1)
    22. DivMod(tmp2, tmp1, 64, ORDER, 32)
    23. w = 0
    24. i = 0
    25. While i < 32
    26. v(i) = tmp1(i)
    27. w = w Or v(i)
    28. i += 1
    29. End While
    30. Return w <> 0
    31. End Function
    Der Code funzt nun. Aber er sieht noch echt aus wie unterm Sofa hervorgeholt. Zumal das EC-KCDSA nun einfach mit in der Curve25519 mit hinzugekritzelt ist (börgs). Das muss jetzt noch schön gemacht werden. Und wenn das fertig ist, dann können wir uns noch mal genauer mit der Lizenz auseinander setzen.

    Stay Tuned...
    Ich finde man darf ruhig zeigen, dass es unterschiedliche Programmiersprachen gibt, die Codes auch unterschiedlich interpretieren.

    Mein Java-Programm ist schon länger nicht mehr auf meinem System, auch wenn es eine sehr gute Sprache ist. Dafür habe ich angefangen mit C#, und bis jetzt bin ich sehr zufrieden mit dieser Sprache.
    Ob der Code auf dem oben erwähnten Link funktioniert in Java, kann ich leider nicht beurteilen. Fakt ist aber, dass es in C# nicht funktioniert, und zum Glück gibt es viele andere User wie dich, die sich gerne mit der Materie beschäftigen.

    Klar muss man Lizenzen beachten. Aber solange man sich nur mit der Technik und der Mathematik beschäftigt (sofern erlaubt), und keinem mutwillig einen Schaden unterjubelt gibts auch hier genug Freiraum der genutzt werden darf, um die Interessen zu dieser Materie im gegenseitigen Interesse aller zu wahren.

    Freundliche Grüsse

    exc-jdbi