Skip to content

Commit

Permalink
Merge pull request #26 from ERussel/feature/sec-crypto-improvements
Browse files Browse the repository at this point in the history
add support for uncompressed public key
  • Loading branch information
ERussel authored Oct 12, 2021
2 parents 1084a86 + c32e55d commit b9df371
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 22 deletions.
10 changes: 10 additions & 0 deletions Example/IrohaCryptoExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -91,6 +92,7 @@
8490153824AC855F008F705E /* IRBase58TestData.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IRBase58TestData.m; sourceTree = "<group>"; };
8490153A24AC8609008F705E /* IRBase58TestData+Load.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "IRBase58TestData+Load.h"; sourceTree = "<group>"; };
8490153B24AC8609008F705E /* IRBase58TestData+Load.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "IRBase58TestData+Load.m"; sourceTree = "<group>"; };
849E16E72715A1C50065B305 /* Secp256k1PublicKeyTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Secp256k1PublicKeyTests.m; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -204,6 +206,7 @@
children = (
842D1E5924CB850200C30A7A /* Secp256k1KeypairTests.m */,
842D1E5B24CB851D00C30A7A /* Secp256k1SigningTests.m */,
849E16E72715A1C50065B305 /* Secp256k1PublicKeyTests.m */,
);
name = secp256k1;
path = ../../Tests/secp256k1;
Expand Down Expand Up @@ -346,6 +349,7 @@
TargetAttributes = {
02C85048217F678200C6A4E7 = {
CreatedOnToolsVersion = 10.0;
LastSwiftMigration = 1250;
};
02D61B8621771F400047F7E7 = {
CreatedOnToolsVersion = 10.0;
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
43 changes: 22 additions & 21 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion IrohaCrypto.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions IrohaCrypto/Classes/secp256k1/SECPublicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@

+ (NSUInteger)length;

+ (NSUInteger)uncompressedLength;

- (nonnull NSData*)uncompressed;

@end
24 changes: 24 additions & 0 deletions IrohaCrypto/Classes/secp256k1/SECPublicKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import "SECPublicKey.h"
#import <secp256k1/secp256k1.h>

@interface SECPublicKey()

Expand All @@ -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) {
Expand All @@ -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
57 changes: 57 additions & 0 deletions Tests/secp256k1/Secp256k1PublicKeyTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Secp256k1PublicKeyTests.m
// IrohaCryptoTests
//
// Created by Ruslan Rezin on 12.10.2021.
// Copyright © 2021 Ruslan Rezin. All rights reserved.
//

#import <XCTest/XCTest.h>
@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

0 comments on commit b9df371

Please sign in to comment.