|
| 1 | +// MARK: - Generated file, do NOT edit |
| 2 | +// any edits of this file WILL be overwritten and thus discarded |
| 3 | +// see section `gyb` in `README` for details. |
| 4 | + |
| 5 | +import Foundation |
| 6 | +@testable import K1 |
| 7 | +import XCTest |
| 8 | + |
| 9 | +private extension XCTestCase { |
| 10 | + func doTest<PubKey: _K1PublicKeyProtocol & Equatable, Encoding: Equatable>( |
| 11 | + original makeOriginal: @autoclosure () -> PubKey, |
| 12 | + serialize: KeyPath<PubKey, Encoding>, |
| 13 | + deserialize: (Encoding) throws -> PubKey |
| 14 | + ) throws { |
| 15 | + try doTestSerializationRoundtrip( |
| 16 | + original: makeOriginal(), |
| 17 | + serialize: serialize, |
| 18 | + deserialize: deserialize |
| 19 | + ) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +public func doTestSerializationRoundtrip<T, Enc>( |
| 24 | + original makeOriginal: @autoclosure () -> T, |
| 25 | + serialize: KeyPath<T, Enc>, |
| 26 | + deserialize: (Enc) throws -> T |
| 27 | +) throws where T: Hashable, Enc: Equatable { |
| 28 | + let count = 100 |
| 29 | + var unique = Set<T>() |
| 30 | + for _ in 0 ..< count { |
| 31 | + let original = makeOriginal() |
| 32 | + unique.insert(original) |
| 33 | + let serialized = original[keyPath: serialize] |
| 34 | + let deserialized = try deserialize(serialized) |
| 35 | + XCTAssertEqual(deserialized, original) |
| 36 | + let reserialized = deserialized[keyPath: serialize] |
| 37 | + XCTAssertEqual(reserialized, serialized) |
| 38 | + } |
| 39 | + XCTAssertEqual(unique.count, count) |
| 40 | +} |
| 41 | + |
| 42 | +extension K1.Schnorr.PublicKey { |
| 43 | + init() { |
| 44 | + let pubKey = K1.Schnorr.PrivateKey().publicKey |
| 45 | + try! self.init(compressedRepresentation: pubKey.compressedRepresentation) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// MARK: - SchnorrPublicKeyEncodingDecodingRoundtripTests |
| 50 | +final class SchnorrPublicKeyEncodingDecodingRoundtripTests: XCTestCase { |
| 51 | + func test_pubkey_raw_is_x963_minus_prefix() throws { |
| 52 | + let privateKey = K1.Schnorr.PrivateKey() |
| 53 | + let publicKey = privateKey.publicKey |
| 54 | + |
| 55 | + XCTAssertEqual(publicKey.rawRepresentation.hex, Data(publicKey.x963Representation.dropFirst()).hex) |
| 56 | + } |
| 57 | + |
| 58 | + func testRawRoundtrip() throws { |
| 59 | + try doTest( |
| 60 | + original: K1.Schnorr.PublicKey(), |
| 61 | + serialize: \.rawRepresentation, |
| 62 | + deserialize: K1.Schnorr.PublicKey.init(rawRepresentation:) |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + func testCompressedRoundtrip() throws { |
| 67 | + try doTest( |
| 68 | + original: K1.Schnorr.PublicKey(), |
| 69 | + serialize: \.compressedRepresentation, |
| 70 | + deserialize: K1.Schnorr.PublicKey.init(compressedRepresentation:) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + func testx963Roundtrip() throws { |
| 75 | + try doTest( |
| 76 | + original: K1.Schnorr.PublicKey(), |
| 77 | + serialize: \.x963Representation, |
| 78 | + deserialize: K1.Schnorr.PublicKey.init(x963Representation:) |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + func testDERRoundtrip() throws { |
| 83 | + try doTest( |
| 84 | + original: K1.Schnorr.PublicKey(), |
| 85 | + serialize: \.derRepresentation, |
| 86 | + deserialize: K1.Schnorr.PublicKey.init(derRepresentation:) |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + func testPEMRoundtrip() throws { |
| 91 | + try doTest( |
| 92 | + original: K1.Schnorr.PublicKey(), |
| 93 | + serialize: \.pemRepresentation, |
| 94 | + deserialize: K1.Schnorr.PublicKey.init(pemRepresentation:) |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +extension K1.ECDSA.NonRecoverable.PublicKey { |
| 100 | + init() { |
| 101 | + let pubKey = K1.ECDSA.NonRecoverable.PrivateKey().publicKey |
| 102 | + try! self.init(compressedRepresentation: pubKey.compressedRepresentation) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// MARK: - ECDSANonRecoverablePublicKeyEncodingDecodingRoundtripTests |
| 107 | +final class ECDSANonRecoverablePublicKeyEncodingDecodingRoundtripTests: XCTestCase { |
| 108 | + func test_pubkey_raw_is_x963_minus_prefix() throws { |
| 109 | + let privateKey = K1.ECDSA.NonRecoverable.PrivateKey() |
| 110 | + let publicKey = privateKey.publicKey |
| 111 | + |
| 112 | + XCTAssertEqual(publicKey.rawRepresentation.hex, Data(publicKey.x963Representation.dropFirst()).hex) |
| 113 | + } |
| 114 | + |
| 115 | + func testRawRoundtrip() throws { |
| 116 | + try doTest( |
| 117 | + original: K1.ECDSA.NonRecoverable.PublicKey(), |
| 118 | + serialize: \.rawRepresentation, |
| 119 | + deserialize: K1.ECDSA.NonRecoverable.PublicKey.init(rawRepresentation:) |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + func testCompressedRoundtrip() throws { |
| 124 | + try doTest( |
| 125 | + original: K1.ECDSA.NonRecoverable.PublicKey(), |
| 126 | + serialize: \.compressedRepresentation, |
| 127 | + deserialize: K1.ECDSA.NonRecoverable.PublicKey.init(compressedRepresentation:) |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + func testx963Roundtrip() throws { |
| 132 | + try doTest( |
| 133 | + original: K1.ECDSA.NonRecoverable.PublicKey(), |
| 134 | + serialize: \.x963Representation, |
| 135 | + deserialize: K1.ECDSA.NonRecoverable.PublicKey.init(x963Representation:) |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + func testDERRoundtrip() throws { |
| 140 | + try doTest( |
| 141 | + original: K1.ECDSA.NonRecoverable.PublicKey(), |
| 142 | + serialize: \.derRepresentation, |
| 143 | + deserialize: K1.ECDSA.NonRecoverable.PublicKey.init(derRepresentation:) |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + func testPEMRoundtrip() throws { |
| 148 | + try doTest( |
| 149 | + original: K1.ECDSA.NonRecoverable.PublicKey(), |
| 150 | + serialize: \.pemRepresentation, |
| 151 | + deserialize: K1.ECDSA.NonRecoverable.PublicKey.init(pemRepresentation:) |
| 152 | + ) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +extension K1.ECDSA.Recoverable.PublicKey { |
| 157 | + init() { |
| 158 | + let pubKey = K1.ECDSA.Recoverable.PrivateKey().publicKey |
| 159 | + try! self.init(compressedRepresentation: pubKey.compressedRepresentation) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// MARK: - ECDSARecoverablePublicKeyEncodingDecodingRoundtripTests |
| 164 | +final class ECDSARecoverablePublicKeyEncodingDecodingRoundtripTests: XCTestCase { |
| 165 | + func test_pubkey_raw_is_x963_minus_prefix() throws { |
| 166 | + let privateKey = K1.ECDSA.Recoverable.PrivateKey() |
| 167 | + let publicKey = privateKey.publicKey |
| 168 | + |
| 169 | + XCTAssertEqual(publicKey.rawRepresentation.hex, Data(publicKey.x963Representation.dropFirst()).hex) |
| 170 | + } |
| 171 | + |
| 172 | + func testRawRoundtrip() throws { |
| 173 | + try doTest( |
| 174 | + original: K1.ECDSA.Recoverable.PublicKey(), |
| 175 | + serialize: \.rawRepresentation, |
| 176 | + deserialize: K1.ECDSA.Recoverable.PublicKey.init(rawRepresentation:) |
| 177 | + ) |
| 178 | + } |
| 179 | + |
| 180 | + func testCompressedRoundtrip() throws { |
| 181 | + try doTest( |
| 182 | + original: K1.ECDSA.Recoverable.PublicKey(), |
| 183 | + serialize: \.compressedRepresentation, |
| 184 | + deserialize: K1.ECDSA.Recoverable.PublicKey.init(compressedRepresentation:) |
| 185 | + ) |
| 186 | + } |
| 187 | + |
| 188 | + func testx963Roundtrip() throws { |
| 189 | + try doTest( |
| 190 | + original: K1.ECDSA.Recoverable.PublicKey(), |
| 191 | + serialize: \.x963Representation, |
| 192 | + deserialize: K1.ECDSA.Recoverable.PublicKey.init(x963Representation:) |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + func testDERRoundtrip() throws { |
| 197 | + try doTest( |
| 198 | + original: K1.ECDSA.Recoverable.PublicKey(), |
| 199 | + serialize: \.derRepresentation, |
| 200 | + deserialize: K1.ECDSA.Recoverable.PublicKey.init(derRepresentation:) |
| 201 | + ) |
| 202 | + } |
| 203 | + |
| 204 | + func testPEMRoundtrip() throws { |
| 205 | + try doTest( |
| 206 | + original: K1.ECDSA.Recoverable.PublicKey(), |
| 207 | + serialize: \.pemRepresentation, |
| 208 | + deserialize: K1.ECDSA.Recoverable.PublicKey.init(pemRepresentation:) |
| 209 | + ) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +extension K1.KeyAgreement.PublicKey { |
| 214 | + init() { |
| 215 | + let pubKey = K1.KeyAgreement.PrivateKey().publicKey |
| 216 | + try! self.init(compressedRepresentation: pubKey.compressedRepresentation) |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +// MARK: - KeyAgreementPublicKeyEncodingDecodingRoundtripTests |
| 221 | +final class KeyAgreementPublicKeyEncodingDecodingRoundtripTests: XCTestCase { |
| 222 | + func test_pubkey_raw_is_x963_minus_prefix() throws { |
| 223 | + let privateKey = K1.KeyAgreement.PrivateKey() |
| 224 | + let publicKey = privateKey.publicKey |
| 225 | + |
| 226 | + XCTAssertEqual(publicKey.rawRepresentation.hex, Data(publicKey.x963Representation.dropFirst()).hex) |
| 227 | + } |
| 228 | + |
| 229 | + func testRawRoundtrip() throws { |
| 230 | + try doTest( |
| 231 | + original: K1.KeyAgreement.PublicKey(), |
| 232 | + serialize: \.rawRepresentation, |
| 233 | + deserialize: K1.KeyAgreement.PublicKey.init(rawRepresentation:) |
| 234 | + ) |
| 235 | + } |
| 236 | + |
| 237 | + func testCompressedRoundtrip() throws { |
| 238 | + try doTest( |
| 239 | + original: K1.KeyAgreement.PublicKey(), |
| 240 | + serialize: \.compressedRepresentation, |
| 241 | + deserialize: K1.KeyAgreement.PublicKey.init(compressedRepresentation:) |
| 242 | + ) |
| 243 | + } |
| 244 | + |
| 245 | + func testx963Roundtrip() throws { |
| 246 | + try doTest( |
| 247 | + original: K1.KeyAgreement.PublicKey(), |
| 248 | + serialize: \.x963Representation, |
| 249 | + deserialize: K1.KeyAgreement.PublicKey.init(x963Representation:) |
| 250 | + ) |
| 251 | + } |
| 252 | + |
| 253 | + func testDERRoundtrip() throws { |
| 254 | + try doTest( |
| 255 | + original: K1.KeyAgreement.PublicKey(), |
| 256 | + serialize: \.derRepresentation, |
| 257 | + deserialize: K1.KeyAgreement.PublicKey.init(derRepresentation:) |
| 258 | + ) |
| 259 | + } |
| 260 | + |
| 261 | + func testPEMRoundtrip() throws { |
| 262 | + try doTest( |
| 263 | + original: K1.KeyAgreement.PublicKey(), |
| 264 | + serialize: \.pemRepresentation, |
| 265 | + deserialize: K1.KeyAgreement.PublicKey.init(pemRepresentation:) |
| 266 | + ) |
| 267 | + } |
| 268 | +} |
0 commit comments