Skip to content

Commit

Permalink
Merge pull request #16 from ERussel/feature/sr25519-new-methods
Browse files Browse the repository at this point in the history
add ed25519 secret key representation support
  • Loading branch information
ERussel authored Aug 7, 2020
2 parents e5bad8c + 92c3409 commit 0449cc6
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Example/IrohaCryptoExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
842D1E5C24CB851D00C30A7A /* Secp256k1SigningTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 842D1E5B24CB851D00C30A7A /* Secp256k1SigningTests.m */; };
842D1E5F24CD7DDE00C30A7A /* EDSignatureTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 842D1E5E24CD7DDE00C30A7A /* EDSignatureTests.m */; };
842D1E6224CD89AA00C30A7A /* EDKeypairFactoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 842D1E6124CD89AA00C30A7A /* EDKeypairFactoryTests.m */; };
843C49D624DD369600B71DDA /* SNPrivateKeyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 843C49D524DD369600B71DDA /* SNPrivateKeyTests.m */; };
8490138924A60F23008F705E /* substrateTestVectors.json in Resources */ = {isa = PBXBuildFile; fileRef = 8490138824A60F23008F705E /* substrateTestVectors.json */; };
8490139324A62340008F705E /* SNBIP39SeedTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8490139124A62340008F705E /* SNBIP39SeedTests.m */; };
8490139424A62340008F705E /* SNKeyFactoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8490139224A62340008F705E /* SNKeyFactoryTests.m */; };
Expand Down Expand Up @@ -74,6 +75,7 @@
842D1E5E24CD7DDE00C30A7A /* EDSignatureTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EDSignatureTests.m; sourceTree = "<group>"; };
842D1E6024CD830100C30A7A /* EDTestConstants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EDTestConstants.h; sourceTree = "<group>"; };
842D1E6124CD89AA00C30A7A /* EDKeypairFactoryTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EDKeypairFactoryTests.m; sourceTree = "<group>"; };
843C49D524DD369600B71DDA /* SNPrivateKeyTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SNPrivateKeyTests.m; sourceTree = "<group>"; };
8490138824A60F23008F705E /* substrateTestVectors.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = substrateTestVectors.json; sourceTree = "<group>"; };
8490139124A62340008F705E /* SNBIP39SeedTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SNBIP39SeedTests.m; sourceTree = "<group>"; };
8490139224A62340008F705E /* SNKeyFactoryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SNKeyFactoryTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -228,6 +230,7 @@
8490139C24A645CB008F705E /* SNAccountTestData+Load.h */,
8490139D24A645CB008F705E /* SNAccountTestData+Load.m */,
8490154824ACB360008F705E /* SS58AddressFactoryTests.m */,
843C49D524DD369600B71DDA /* SNPrivateKeyTests.m */,
);
name = SR25519;
path = ../../Tests/SR25519;
Expand Down Expand Up @@ -490,6 +493,7 @@
841F1B3C2407D4830018E14D /* IRBIP39TestData+Load.m in Sources */,
842D1E5724CB707B00C30A7A /* Blake2sTests.m in Sources */,
8490139B24A644E8008F705E /* SNAccountTestData.m in Sources */,
843C49D624DD369600B71DDA /* SNPrivateKeyTests.m in Sources */,
02C85057217F679800C6A4E7 /* IRIrohaSignatureTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
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.6.2'
s.version = '0.7.0'
s.summary = 'Provides object oriented wrappers for C/C++ crypto functions used by blockchains.'

s.homepage = 'https://github.com/soramitsu'
Expand Down
5 changes: 5 additions & 0 deletions IrohaCrypto/Classes/sr25519/SNPrivateKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@

@interface SNPrivateKey : NSObject<IRPrivateKeyProtocol>

- (nullable instancetype)initWithFromEd25519:(nonnull NSData*)data
error:(NSError*_Nullable*_Nullable)error;

- (nonnull NSData*)toEd25519Data;

@end
31 changes: 31 additions & 0 deletions IrohaCrypto/Classes/sr25519/SNPrivateKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,35 @@ - (nullable instancetype)initWithRawData:(nonnull NSData *)data
return self;
}

- (nullable instancetype)initWithFromEd25519:(nonnull NSData*)data
error:(NSError*_Nullable*_Nullable)error {
if ([data length] != SR25519_SECRET_SIZE) {
if (error) {
NSString *message = [NSString stringWithFormat:@"Invalid raw data length %@ but expected %@",
@([data length]), @(SR25519_SECRET_SIZE)];
*error = [NSError errorWithDomain:NSStringFromClass([self class])
code:IRCryptoKeyErrorInvalidRawData
userInfo:@{NSLocalizedDescriptionKey: message}];
}

return nil;
}

if (self = [super init]) {
uint8_t secret_out[SR25519_SECRET_SIZE];
sr25519_from_ed25519_bytes(secret_out, data.bytes);

self.rawData = [NSData dataWithBytes:secret_out length:SR25519_SECRET_SIZE];
}

return self;
}

- (nonnull NSData*)toEd25519Data {
uint8_t secret_out[SR25519_SECRET_SIZE];
sr25519_to_ed25519_bytes(secret_out, _rawData.bytes);

return [NSData dataWithBytes:secret_out length:SR25519_SECRET_SIZE];
}

@end
47 changes: 47 additions & 0 deletions Tests/SR25519/SNPrivateKeyTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// SNPrivateKeyTests.m
// IrohaCryptoTests
//
// Created by Ruslan Rezin on 07.08.2020.
// Copyright © 2020 Ruslan Rezin. All rights reserved.
//

#import <XCTest/XCTest.h>
@import IrohaCrypto;

static const int KEYS_COUNT = 1;

static NSString * const SECRET_KEYS1[] = {
@"70e1ddf7edfc4a98423a4cdfdd51e4529d228840e6e30e25f1d3502250a8055c7677a385ccf0bddfb2bafbdec086bcd2475dc46aeafad822d27e1f901eb9b278",
};

static NSString * const SECRET_KEYS2[] = {
@"2ebcfbbe9d5f09534887e9bb3b8a5caa530411c87cdca1247e1a4a040ab5800b7677a385ccf0bddfb2bafbdec086bcd2475dc46aeafad822d27e1f901eb9b278"
};

@interface SNPrivateKeyTests : XCTestCase

@end

@implementation SNPrivateKeyTests

- (void)testInitWithNoneEd25519 {
for (NSUInteger index = 0; index < KEYS_COUNT; index++) {
NSError *error = nil;

NSData *expectedEd25519Data = [[NSData alloc] initWithHexString:SECRET_KEYS1[index]
error:&error];

SNPrivateKey *privateKey = [[SNPrivateKey alloc] initWithFromEd25519:expectedEd25519Data
error:&error];

XCTAssertEqualObjects(privateKey.rawData.toHexString,
SECRET_KEYS2[index]);

NSData *ed25519Data = privateKey.toEd25519Data;

XCTAssertEqualObjects(expectedEd25519Data, ed25519Data);
}
}

@end
22 changes: 21 additions & 1 deletion sr25519Imp/include/sr25519.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __SR25519_INCLUDE_GUARD_H__
#define __SR25519_INCLUDE_GUARD_H__

/* Generated with cbindgen:0.14.2 */
/* Generated with cbindgen:0.14.3 */

/* THIS FILE WAS AUTOMATICALLY GENERATED. DO NOT EDIT. Ref: https://github.com/Warchant/sr25519-crust */

Expand Down Expand Up @@ -112,6 +112,16 @@ void sr25519_derive_public_soft(uint8_t *pubkey_out,
const uint8_t *public_ptr,
const uint8_t *cc_ptr);

/**
* Retrives secret key from ed25519 representation.
*
* * secret_out: 64 bytes, pre-allocated output buffer of SR25519_SECRET_SIZE bytes
* * secret_ptr: generation seed - input buffer of SR25519_SECRET_SIZE bytes
*
*/
void sr25519_from_ed25519_bytes(uint8_t *secret_out,
const uint8_t *secret_ptr);

/**
* Generate a key pair.
*
Expand Down Expand Up @@ -141,6 +151,16 @@ void sr25519_sign(uint8_t *signature_out,
const uint8_t *message_ptr,
unsigned long message_length);

/**
* Converts secret key to ed25519 representation.
*
* * secret_out: 64 bytes, pre-allocated output buffer of SR25519_SECRET_SIZE bytes
* * secret_ptr: generation seed - input buffer of SR25519_SECRET_SIZE bytes
*
*/
void sr25519_to_ed25519_bytes(uint8_t *secret_out,
const uint8_t *secret_ptr);

/**
* Verify a message and its corresponding against a public key;
*
Expand Down
Binary file modified sr25519Imp/libsr25519crust.a
Binary file not shown.

0 comments on commit 0449cc6

Please sign in to comment.