From c32e55d87071ee2e098071e60534090eb69e9c0f Mon Sep 17 00:00:00 2001 From: Russel Date: Tue, 12 Oct 2021 14:23:23 +0300 Subject: [PATCH] add support for uncompressed public key --- .../project.pbxproj | 10 ++++ Example/Podfile.lock | 43 +++++++------- IrohaCrypto.podspec | 2 +- IrohaCrypto/Classes/secp256k1/SECPublicKey.h | 4 ++ IrohaCrypto/Classes/secp256k1/SECPublicKey.m | 24 ++++++++ Tests/secp256k1/Secp256k1PublicKeyTests.m | 57 +++++++++++++++++++ 6 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 Tests/secp256k1/Secp256k1PublicKeyTests.m diff --git a/Example/IrohaCryptoExample.xcodeproj/project.pbxproj b/Example/IrohaCryptoExample.xcodeproj/project.pbxproj index f8cd275..3138e29 100644 --- a/Example/IrohaCryptoExample.xcodeproj/project.pbxproj +++ b/Example/IrohaCryptoExample.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ 8490153624AC84A6008F705E /* IRBase58Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8490153524AC84A6008F705E /* IRBase58Tests.m */; }; 8490153924AC855F008F705E /* IRBase58TestData.m in Sources */ = {isa = PBXBuildFile; fileRef = 8490153824AC855F008F705E /* IRBase58TestData.m */; }; 8490153C24AC8609008F705E /* IRBase58TestData+Load.m in Sources */ = {isa = PBXBuildFile; fileRef = 8490153B24AC8609008F705E /* IRBase58TestData+Load.m */; }; + 849E16E82715A1C50065B305 /* Secp256k1PublicKeyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 849E16E72715A1C50065B305 /* Secp256k1PublicKeyTests.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -91,6 +92,7 @@ 8490153824AC855F008F705E /* IRBase58TestData.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IRBase58TestData.m; sourceTree = ""; }; 8490153A24AC8609008F705E /* IRBase58TestData+Load.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "IRBase58TestData+Load.h"; sourceTree = ""; }; 8490153B24AC8609008F705E /* IRBase58TestData+Load.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "IRBase58TestData+Load.m"; sourceTree = ""; }; + 849E16E72715A1C50065B305 /* Secp256k1PublicKeyTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Secp256k1PublicKeyTests.m; sourceTree = ""; }; C86A7B671D6593C871CD90E3 /* Pods_IrohaCrypto_IrohaCryptoTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_IrohaCrypto_IrohaCryptoTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; DDFBE092955D311B59246789 /* Pods-IrohaCrypto-IrohaCryptoTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-IrohaCrypto-IrohaCryptoTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-IrohaCrypto-IrohaCryptoTests/Pods-IrohaCrypto-IrohaCryptoTests.release.xcconfig"; sourceTree = ""; }; F9891E598602CB1FE1A23F9A /* Pods-IrohaCrypto-IrohaCryptoTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-IrohaCrypto-IrohaCryptoTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-IrohaCrypto-IrohaCryptoTests/Pods-IrohaCrypto-IrohaCryptoTests.debug.xcconfig"; sourceTree = ""; }; @@ -204,6 +206,7 @@ children = ( 842D1E5924CB850200C30A7A /* Secp256k1KeypairTests.m */, 842D1E5B24CB851D00C30A7A /* Secp256k1SigningTests.m */, + 849E16E72715A1C50065B305 /* Secp256k1PublicKeyTests.m */, ); name = secp256k1; path = ../../Tests/secp256k1; @@ -346,6 +349,7 @@ TargetAttributes = { 02C85048217F678200C6A4E7 = { CreatedOnToolsVersion = 10.0; + LastSwiftMigration = 1250; }; 02D61B8621771F400047F7E7 = { CreatedOnToolsVersion = 10.0; @@ -501,6 +505,7 @@ 8488100926988B570076BC48 /* EDKeypairFactoryTests.m in Sources */, 8490153624AC84A6008F705E /* IRBase58Tests.m in Sources */, 841F1B3C2407D4830018E14D /* IRBIP39TestData+Load.m in Sources */, + 849E16E82715A1C50065B305 /* Secp256k1PublicKeyTests.m in Sources */, 842D1E5724CB707B00C30A7A /* Blake2sTests.m in Sources */, 8490139B24A644E8008F705E /* SNAccountTestData.m in Sources */, 843C49D624DD369600B71DDA /* SNPrivateKeyTests.m in Sources */, @@ -543,6 +548,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = F9891E598602CB1FE1A23F9A /* Pods-IrohaCrypto-IrohaCryptoTests.debug.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = ANAULAA354; INFOPLIST_FILE = IrohaCryptoTests/Info.plist; @@ -554,6 +560,8 @@ ); PRODUCT_BUNDLE_IDENTIFIER = co.jp.soramitsu.IrohaCryptoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -562,6 +570,7 @@ isa = XCBuildConfiguration; baseConfigurationReference = DDFBE092955D311B59246789 /* Pods-IrohaCrypto-IrohaCryptoTests.release.xcconfig */; buildSettings = { + CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = ANAULAA354; INFOPLIST_FILE = IrohaCryptoTests/Info.plist; @@ -573,6 +582,7 @@ ); PRODUCT_BUNDLE_IDENTIFIER = co.jp.soramitsu.IrohaCryptoTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 9d1b492..a25383f 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,33 +1,34 @@ PODS: - - IrohaCrypto (0.8.0): - - IrohaCrypto/BIP39 (= 0.8.0) - - IrohaCrypto/blake2 (= 0.8.0) - - IrohaCrypto/Common (= 0.8.0) - - IrohaCrypto/ed25519 (= 0.8.0) - - IrohaCrypto/Iroha (= 0.8.0) - - IrohaCrypto/Scrypt (= 0.8.0) - - IrohaCrypto/secp256k1 (= 0.8.0) - - IrohaCrypto/sr25519 (= 0.8.0) - - IrohaCrypto/ss58 (= 0.8.0) - - IrohaCrypto/BIP39 (0.8.0): + - IrohaCrypto (0.8.2): + - IrohaCrypto/BIP39 (= 0.8.2) + - IrohaCrypto/blake2 (= 0.8.2) + - IrohaCrypto/Common (= 0.8.2) + - IrohaCrypto/ed25519 (= 0.8.2) + - IrohaCrypto/Iroha (= 0.8.2) + - IrohaCrypto/Scrypt (= 0.8.2) + - IrohaCrypto/secp256k1 (= 0.8.2) + - IrohaCrypto/sr25519 (= 0.8.2) + - IrohaCrypto/ss58 (= 0.8.2) + - IrohaCrypto/BIP39 (0.8.2): - IrohaCrypto/Common - - IrohaCrypto/blake2 (0.8.0) - - IrohaCrypto/Common (0.8.0) - - IrohaCrypto/ed25519 (0.8.0): + - IrohaCrypto/blake2 (0.8.2) + - IrohaCrypto/Common (0.8.2) + - IrohaCrypto/ed25519 (0.8.2): - IrohaCrypto/Common - - IrohaCrypto/Iroha (0.8.0): + - IrohaCrypto/Iroha (0.8.2): - IrohaCrypto/Common - - IrohaCrypto/Scrypt (0.8.0): + - IrohaCrypto/Scrypt (0.8.2): - IrohaCrypto/Common - scrypt.c (~> 0.1) - - IrohaCrypto/secp256k1 (0.8.0): + - IrohaCrypto/secp256k1 (0.8.2): - IrohaCrypto/Common - secp256k1.c (~> 0.1) - - IrohaCrypto/sr25519 (0.8.0): + - IrohaCrypto/sr25519 (0.8.2): - IrohaCrypto/BIP39 - IrohaCrypto/blake2 - IrohaCrypto/Common - - IrohaCrypto/ss58 (0.8.0): + - IrohaCrypto/ss58 (0.8.2): + - IrohaCrypto/blake2 - IrohaCrypto/Common - scrypt.c (0.1.1) - secp256k1.c (0.1.2) @@ -45,10 +46,10 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - IrohaCrypto: c2e54b873bc858daf7afbd414865582f63cacdea + IrohaCrypto: 2b7585e71eff4fbd7e7adc2d592ad5c22610524b scrypt.c: b42ae06183251329d2b2c620c226fb541a4a3592 secp256k1.c: db47b726585d80f027423682eb369729e61b3b20 PODFILE CHECKSUM: 090cdf3e5819729438de794e136ffaa6b24d3abc -COCOAPODS: 1.10.1 +COCOAPODS: 1.10.2 diff --git a/IrohaCrypto.podspec b/IrohaCrypto.podspec index cee3e55..613f060 100644 --- a/IrohaCrypto.podspec +++ b/IrohaCrypto.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'IrohaCrypto' - s.version = '0.8.1' + s.version = '0.8.2' s.summary = 'Provides object oriented wrappers for C/C++ crypto functions used by blockchains.' s.homepage = 'https://github.com/soramitsu' diff --git a/IrohaCrypto/Classes/secp256k1/SECPublicKey.h b/IrohaCrypto/Classes/secp256k1/SECPublicKey.h index 00a50dc..b90c658 100644 --- a/IrohaCrypto/Classes/secp256k1/SECPublicKey.h +++ b/IrohaCrypto/Classes/secp256k1/SECPublicKey.h @@ -12,4 +12,8 @@ + (NSUInteger)length; ++ (NSUInteger)uncompressedLength; + +- (nonnull NSData*)uncompressed; + @end diff --git a/IrohaCrypto/Classes/secp256k1/SECPublicKey.m b/IrohaCrypto/Classes/secp256k1/SECPublicKey.m index 4f27301..d57fe05 100644 --- a/IrohaCrypto/Classes/secp256k1/SECPublicKey.m +++ b/IrohaCrypto/Classes/secp256k1/SECPublicKey.m @@ -6,6 +6,7 @@ // #import "SECPublicKey.h" +#import @interface SECPublicKey() @@ -19,6 +20,10 @@ + (NSUInteger)length { return 33; } ++ (NSUInteger)uncompressedLength { + return 65; +} + - (nullable instancetype)initWithRawData:(nonnull NSData*)data error:(NSError*_Nullable*_Nullable)error { if (data.length != [[self class] length]) { if (error) { @@ -39,4 +44,23 @@ - (nullable instancetype)initWithRawData:(nonnull NSData*)data error:(NSError*_N return self; } +- (nonnull NSData*)uncompressed { + secp256k1_pubkey rawPublicKey; + + secp256k1_context *context = secp256k1_context_create(SECP256K1_CONTEXT_NONE); + + int status = secp256k1_ec_pubkey_parse(context, &rawPublicKey, _rawData.bytes, [_rawData length]); + + size_t uncompressedLength = [SECPublicKey uncompressedLength]; + unsigned char uncompresedPubkey[uncompressedLength]; + + secp256k1_ec_pubkey_serialize(context, + uncompresedPubkey, + &uncompressedLength, + &rawPublicKey, + SECP256K1_EC_UNCOMPRESSED); + + return [NSData dataWithBytes:uncompresedPubkey length:uncompressedLength]; +} + @end diff --git a/Tests/secp256k1/Secp256k1PublicKeyTests.m b/Tests/secp256k1/Secp256k1PublicKeyTests.m new file mode 100644 index 0000000..8f8f273 --- /dev/null +++ b/Tests/secp256k1/Secp256k1PublicKeyTests.m @@ -0,0 +1,57 @@ +// +// Secp256k1PublicKeyTests.m +// IrohaCryptoTests +// +// Created by Ruslan Rezin on 12.10.2021. +// Copyright © 2021 Ruslan Rezin. All rights reserved. +// + +#import +@import IrohaCrypto; + +static const int KEYS_COUNT = 1; + +static NSString * const PUBLIC_KEYS[] = { + @"0376698beebe8ee5c74d8cc50ab84ac301ee8f10af6f28d0ffd6adf4d6d3b9b762" +}; + +static NSString * const UNCOMPRESSED[] = { + @"0476698beebe8ee5c74d8cc50ab84ac301ee8f10af6f28d0ffd6adf4d6d3b9b762d46ca56d3dad2ce13213a6f42278dabbb53259f2d92681ea6a0b98197a719be3" +}; + +@interface Secp256k1PublicKeyTests : XCTestCase + +@end + +@implementation Secp256k1PublicKeyTests + +- (void)testPublicKeyUncompression { + for (NSUInteger index = 0; index < KEYS_COUNT; index++) { + NSError *error = nil; + NSData *compressedPublicKey = [[NSData alloc] initWithHexString:PUBLIC_KEYS[index] error:&error]; + + if (error) { + XCTFail(@"Did receive error: %@", [error localizedDescription]); + return; + } + + SECPublicKey *publicKey = [[SECPublicKey alloc] initWithRawData:compressedPublicKey error:&error]; + + if (error) { + XCTFail(@"Did receive error: %@", [error localizedDescription]); + return; + } + + NSData *actualUncompressed = [publicKey uncompressed]; + NSData *expectedUncompressed = [[NSData alloc] initWithHexString:UNCOMPRESSED[index] error:&error]; + + if (error) { + XCTFail(@"Did receive error: %@", [error localizedDescription]); + return; + } + + XCTAssertEqualObjects(actualUncompressed, expectedUncompressed); + } +} + +@end