You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.8 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package crypto
  17. //import "fmt"
  18. import "testing"
  19. import "bufio"
  20. import "log"
  21. import "os"
  22. import "strings"
  23. import "strconv"
  24. import "encoding/hex"
  25. // these tests, specifically "tests_data.txt" are available in the monero project and used here to verify whether we implement everything
  26. func Test_Crypto(t *testing.T) {
  27. file, err := os.Open("tests_data.txt")
  28. if err != nil {
  29. log.Fatalf("Test file tests_data is missing, err %s ", err)
  30. }
  31. defer file.Close()
  32. scanner := bufio.NewScanner(file)
  33. for scanner.Scan() {
  34. // parse the line
  35. line := scanner.Text()
  36. words := strings.Fields(line)
  37. if len(words) < 2 {
  38. continue
  39. }
  40. switch words[0] {
  41. case "check_scalar":
  42. scalar := HexToKey(words[1])
  43. expected := "true" == words[2]
  44. actual := ScValid(&scalar) == true
  45. if actual != expected {
  46. t.Fatalf("Failed %s: Expected %v, got %v.", words[0], expected, actual)
  47. }
  48. case "check_key":
  49. public_key := HexToKey(words[1])
  50. expected := "true" == words[2]
  51. actual := public_key.Public_Key_Valid() == true
  52. if actual != expected {
  53. t.Logf("Failed %s: Expected %v, got %v %s", words[0], expected, actual, public_key)
  54. }
  55. case "random_scalar": // ignore them
  56. case "hash_to_scalar":
  57. data, _ := hex.DecodeString(words[1])
  58. expected := HexToKey(words[2])
  59. actual := HashToScalar(data)
  60. if *actual != expected {
  61. t.Fatalf("Failed %s: Expected %v, got %v.", words[0], expected, actual)
  62. }
  63. //t.Logf("executing %s\n", expected)
  64. case "generate_keys": // this test is meant to test RNG ??
  65. key_secret := HexToKey(words[2])
  66. key_public := HexToKey(words[1])
  67. if key_public != *(key_secret.PublicKey()) {
  68. t.Errorf("Failed %s key generation testing failed %s ", words[0], key_secret)
  69. }
  70. case "secret_key_to_public_key":
  71. key_secret := HexToKey(words[1])
  72. expected := "true" == words[2]
  73. actual := key_secret.Private_Key_Valid()
  74. if expected != actual {
  75. t.Fatalf("Failed %s: Expected %v, got %v. %s", words[0], expected, actual, key_secret)
  76. }
  77. if actual { // test only if required
  78. key_public := HexToKey(words[3])
  79. if key_public != *(key_secret.PublicKey()) {
  80. t.Errorf("Failed %s key generation testing failed %s ", words[0], key_secret)
  81. }
  82. }
  83. case "generate_key_derivation":
  84. public_key := HexToKey(words[1])
  85. private_key := HexToKey(words[2])
  86. expected := "true" == words[3]
  87. actual := public_key.Public_Key_Valid()
  88. if expected != actual {
  89. t.Fatalf(" Failed %s: Expected %v, got %v. %s", words[0], expected, actual, public_key)
  90. }
  91. if expected == true { // yes knowingly using the same variables
  92. expected := HexToKey(words[4])
  93. actual := KeyDerivation(&public_key, &private_key)
  94. if expected != actual {
  95. t.Fatalf("Failed %s: Expected %v, got %v. %s", words[0], expected, actual, public_key)
  96. }
  97. }
  98. case "derive_public_key":
  99. kd := HexToKey(words[1]) //
  100. outIdx, _ := strconv.ParseUint(words[2], 10, 0)
  101. base := HexToKey(words[3])
  102. var expected1, actual1 bool
  103. var expected2, actual2 Key
  104. expected1 = words[4] == "true"
  105. if expected1 {
  106. expected2 = HexToKey(words[5])
  107. }
  108. actual1 = base.Public_Key_Valid()
  109. if actual1 != expected1 {
  110. t.Fatalf("%s: Expected %v, got %v.", words[0], expected1, actual1)
  111. }
  112. if expected1 {
  113. actual2 = kd.KeyDerivation_To_PublicKey(outIdx, base)
  114. if actual2 != expected2 {
  115. t.Fatalf("%s: Expected %v, got %v.", words[0], expected2, actual2)
  116. }
  117. }
  118. case "derive_secret_key":
  119. kd := HexToKey(words[1]) //
  120. outIdx, _ := strconv.ParseUint(words[2], 10, 0)
  121. base := HexToKey(words[3])
  122. expected := HexToKey(words[4])
  123. actual := kd.KeyDerivation_To_PrivateKey(outIdx, base)
  124. if actual != expected {
  125. t.Fatalf("%s: Expected %v, got %v.", words[0], expected, actual)
  126. }
  127. case "hash_to_point": // this is different check than HashToPoint
  128. hash := HexToKey(words[1])
  129. expected := HexToKey(words[2])
  130. var actual Key
  131. var p1 ProjectiveGroupElement
  132. p1.FromBytes(&hash)
  133. p1.ToBytes(&actual)
  134. if actual != expected {
  135. t.Logf("%s: Expected %v, got %v.", words[0], expected, actual)
  136. }
  137. case "hash_to_ec":
  138. pub := HexToKey(words[1])
  139. expected := HexToKey(words[2])
  140. var actual Key
  141. ex := pub.HashToEC()
  142. ex.ToBytes(&actual)
  143. if actual != expected {
  144. t.Fatalf("%s: Expected %s, got %s.", words[0], expected, actual)
  145. }
  146. case "generate_key_image":
  147. public_key := HexToKey(words[1])
  148. private_key := HexToKey(words[2])
  149. expected := HexToKey(words[3])
  150. actual := GenerateKeyImage(public_key, private_key)
  151. if actual != expected {
  152. t.Fatalf("%s: Expected %s, got %s.", words[0], expected, actual)
  153. }
  154. // these are ignored because they are not required DERO project is based on ringct+
  155. case "generate_signature":
  156. case "check_signature":
  157. case "generate_ring_signature":
  158. case "check_ring_signature":
  159. default:
  160. t.Fatalf("This test is not handled %s: ", words[0])
  161. }
  162. }
  163. }