简体   繁体   中英

ECDSA secp256k1 keypair signing on Swift

Here, i have created private key by some dummy prvData, then created 2 dummy data objects and trying to sign data1 and data2 object

NSData *prvData = [NSData hexStringToData:@"e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35"];

NSData *data1 = [NSData hexStringToData:@"0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"];
NSData *data2 = [NSData hexStringToData:@"0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c20339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"];


NSData *sig1 = [CKSecp256k1 compactSignData:data1 withPrivateKey:prvData];
NSData *sig2 = [CKSecp256k1 compactSignData:data2 withPrivateKey:prvData];

Here, both sig1 and sign2 object values are coming same, but it shouldn't happen. So Please if anybody has done this signing problem, please tell me, how to do?

These functions directly call unmanaged (C) code. The input value is considered to be the same size as the key size: 256 bits or 32 bytes. If you deliver more than that then only the first 32 bytes are taken. The reason is easy: the data is simply send using a single pointer without specifying the length .

So basically this code will only work if you send it a hash value of 32 bytes. I think you can only make this work for 256 bit hashes or hashes that are smaller than that. For larger hashes you may have to perform modular reduction over the order of the curve. Smaller hashes can be left-padded with zero's up to 32 bytes to make them work (ECDSA is big endian).

You may have noticed that the hash mechanism is missing within the call to CKSecp256k1 . If you take a look at the code it will certainly not perform it internally, even if the input is called "message". This is a low level API that is used specifically for BitCoin, it seeems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM