github.com/incognitochain/go-incognito-sdk@v1.0.1/privacy/curve25519/crypto_test.go (about) 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 17 package curve25519 18 19 //import "fmt" 20 import "testing" 21 import "bufio" 22 import "log" 23 import "os" 24 import "strings" 25 import "strconv" 26 import "encoding/hex" 27 28 // these tests, specifically "tests_data.txt" are available in the monero project and used here to verify whether we implement everything 29 30 func Test_MultiScalarMul(t *testing.T) { 31 32 } 33 34 func Test_Crypto(t *testing.T) { 35 36 file, err := os.Open("tests_data.txt") 37 if err != nil { 38 log.Fatalf("Test file tests_data is missing, err %s ", err) 39 } 40 defer file.Close() 41 42 scanner := bufio.NewScanner(file) 43 for scanner.Scan() { 44 // parse the line 45 line := scanner.Text() 46 words := strings.Fields(line) 47 48 if len(words) < 2 { 49 continue 50 } 51 switch words[0] { 52 case "check_scalar": 53 scalar := HexToKey(words[1]) 54 expected := "true" == words[2] 55 actual := ScValid(&scalar) 56 if actual != expected { 57 t.Fatalf("Failed %s: Expected %v, got %v.", words[0], expected, actual) 58 } 59 case "check_key": 60 public_key := HexToKey(words[1]) 61 expected := "true" == words[2] 62 actual := public_key.Public_Key_Valid() 63 if actual != expected { 64 t.Fatalf("Failed %s: Expected %v, got %v %s", words[0], expected, actual, public_key) 65 } 66 67 case "random_scalar": // ignore them 68 case "hash_to_scalar": 69 data, _ := hex.DecodeString(words[1]) 70 expected := HexToKey(words[2]) 71 actual := HashToScalar(data) 72 if *actual != expected { 73 t.Fatalf("Failed %s: Expected %v, got %v.", words[0], expected, actual) 74 } 75 //t.Logf("executing %s\n", expected) 76 case "generate_keys": // this test is meant to test RNG ?? 77 key_secret := HexToKey(words[2]) 78 key_public := HexToKey(words[1]) 79 80 if key_public != *(key_secret.PublicKey()) { 81 t.Errorf("Failed %s key generation testing failed %s ", words[0], key_secret) 82 } 83 case "secret_key_to_public_key": 84 key_secret := HexToKey(words[1]) 85 86 expected := "true" == words[2] 87 actual := key_secret.Private_Key_Valid() 88 89 if expected != actual { 90 t.Fatalf("Failed %s: Expected %v, got %v. %s", words[0], expected, actual, key_secret) 91 } 92 93 if actual { // test only if required 94 key_public := HexToKey(words[3]) 95 if key_public != *(key_secret.PublicKey()) { 96 t.Errorf("Failed %s key generation testing failed %s ", words[0], key_secret) 97 } 98 } 99 100 case "generate_key_derivation": 101 public_key := HexToKey(words[1]) 102 private_key := HexToKey(words[2]) 103 104 expected := "true" == words[3] 105 actual := public_key.Public_Key_Valid() 106 107 if expected != actual { 108 t.Fatalf(" Failed %s: Expected %v, got %v. %s", words[0], expected, actual, public_key) 109 } 110 111 if expected == true { // yes knowingly using the same variables 112 expected := HexToKey(words[4]) 113 actual := KeyDerivation(&public_key, &private_key) 114 115 if expected != actual { 116 t.Fatalf("1Failed %s: Expected %v, got %v. %s", words[0], expected, actual, public_key) 117 } 118 } 119 120 case "derive_public_key": 121 kd := HexToKey(words[1]) // 122 outIdx, _ := strconv.ParseUint(words[2], 10, 0) 123 base := HexToKey(words[3]) 124 var expected1, actual1 bool 125 var expected2, actual2 Key 126 expected1 = words[4] == "true" 127 if expected1 { 128 expected2 = HexToKey(words[5]) 129 } 130 131 actual1 = base.Public_Key_Valid() 132 if actual1 != expected1 { 133 t.Fatalf("%s: Expected %v, got %v.", words[0], expected1, actual1) 134 } 135 136 if expected1 { 137 actual2 = kd.KeyDerivation_To_PublicKey(outIdx, base) 138 if actual2 != expected2 { 139 t.Fatalf("%s: Expected %v, got %v.", words[0], expected2, actual2) 140 } 141 } 142 case "derive_secret_key": 143 kd := HexToKey(words[1]) // 144 outIdx, _ := strconv.ParseUint(words[2], 10, 0) 145 base := HexToKey(words[3]) 146 expected := HexToKey(words[4]) 147 148 actual := kd.KeyDerivation_To_PrivateKey(outIdx, base) 149 if actual != expected { 150 t.Fatalf("%s: Expected %v, got %v.", words[0], expected, actual) 151 } 152 153 case "hash_to_point": // this is different check than HashToPoint 154 155 hash := HexToKey(words[1]) 156 expected := HexToKey(words[2]) 157 158 var actual Key 159 var p1 ProjectiveGroupElement 160 p1.FromBytes(&hash) 161 p1.ToBytes(&actual) 162 163 if actual != expected { 164 t.Fatalf("%s: Expected %v, got %v.", words[0], expected, actual) 165 } 166 case "hash_to_ec": 167 pub := HexToKey(words[1]) 168 expected := HexToKey(words[2]) 169 170 var actual Key 171 ex := pub.HashToEC() 172 ex.ToBytes(&actual) 173 174 if actual != expected { 175 t.Fatalf("%s: Expected %s, got %s.", words[0], expected, actual) 176 } 177 178 case "generate_key_image": 179 public_key := HexToKey(words[1]) 180 private_key := HexToKey(words[2]) 181 expected := HexToKey(words[3]) 182 183 actual := GenerateKeyImage(public_key, private_key) 184 if actual != expected { 185 t.Fatalf("%s: Expected %s, got %s.", words[0], expected, actual) 186 } 187 188 // these are ignored because they are not required DERO project is based on ringct+ 189 case "generate_signature": 190 case "check_signature": 191 case "generate_ring_signature": 192 case "check_ring_signature": 193 194 default: 195 196 t.Fatalf("This test is not handled %s: ", words[0]) 197 198 } 199 } 200 201 } 202 203 // test whether H generation is alright 204 func TestH(t *testing.T) { 205 G := ScalarmultBase((d2h(1))) 206 // t.Logf("G %s \nH %s", G, H) 207 actual := G.HashToPointSimple() 208 if actual != H { 209 t.Fatalf("H generation failed Actual %s expected %s", actual, H) 210 } 211 } 212 213 214 func BenchmarkScalarMultH(b *testing.B) { 215 216 }