<commit_before>//
//  CBHDKeys.c
//  cbitcoin
//
//  Created by <NAME> on 19/10/2013.
//  Copyright (c) 2013 <NAME>
//  
//  This file is part of cbitcoin. It is subject to the license terms
//  in the LICENSE file found in the top-level directory of this
//  distribution and at http://www.cbitcoin.com/license.html. No part of
//  cbitcoin, including this file, may be copied, modified, propagated,
//  or distributed except according to the terms contained in the
//  LICENSE file.

//  SEE HEADER FILE FOR DOCUMENTATION 

#include "CBHDKeys.h"

unsigned char CB_CURVE_ORDER[32] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41};

CBHDKey * CBNewHDKey(bool priv) {

	CBHDKey * key = malloc(sizeof(*key) + (priv ? sizeof(CBKeyPair) : sizeof(CBPubKeyInfo)));
	CBInitHDKey(key);

	return key;

}

CBHDKey * CBNewHDKeyFromData(unsigned char * data) {

	CBHDKeyVersion versionBytes = CBArrayToInt32BigEndian(data, 0);
	CBHDKeyType type = CBHDKeyGetType(versionBytes);

	if (type == CB_HD_KEY_TYPE_UNKNOWN) {
		CBLogError("Unknown key type.");
		return NULL;
	}

	// Allocate memory for key
	CBHDKey * key = CBNewHDKey(type == CB_HD_KEY_TYPE_PRIVATE);

	return CBInitHDKeyFromData(key, data, versionBytes, type) ? key : (free(key), NULL);

}

CBKeyPair * CBNewKeyPair(bool priv) {

	CBKeyPair * key = malloc(priv ? sizeof(CBKeyPair) : sizeof(CBPubKeyInfo));
	CBInitKeyPair(key);

	return key;

}

void CBInitHDKey(CBHDKey * key) {

	CBInitKeyPair(key->keyPair);

}

bool CBInitHDKeyFromData(CBHDKey * key, unsigned char * data, CBHDKeyVersion versionBytes, CBHDKeyType type) {

	CBInitHDKey(key);

	// Set version bytes
	key->versionBytes = versionBytes;

	// Chain code
	memcpy(key->chainCode, data + 13, 32);

	if (type == CB_HD_KEY_TYPE_PRIVATE){
		// Private
		memcpy(key->keyPair->privkey, data + 46, 32);
		// Calculate public key
		CBKeyGetPublicKey(CBHDKeyGetPrivateKey(key), CBHDKeyGetPublicKey(key));
	}else
		// Assume public
		memcpy(CBHDKeyGetPublicKey(key), data + 45, 33);

	// Depth
	key->depth = data[4];
	if (key->depth == 0) {
		// Master
		key->childID.priv = false;
		key->childID.childNumber = 0;
		for (int x = 5; x < 9; x++)
			if (data[x] != 0) {
				CBLogError("The fingerprint of the master key is not zero.");
				return false;
			}
	}else{
		// Not master
		int childNum = CBArrayToInt32BigEndian(data, 9);
		key->childID.priv = childNum >> 31;
		key->childID.childNumber = childNum & 0x8fffffff;
	}

	return true;
}

void CBInitKeyPair(CBKeyPair * key) {

	key->pubkey.hashSet = false;

}

bool CBHDKeyDeriveChild(CBHDKey * parentKey, CBHDKeyChildID childID, CBHDKey * childKey) {

	unsigned char hash[64], inputData[37];
	CBHDKeyType type = CBHDKeyGetType(parentKey->versionBytes);

	// Add childID to inputData
	CBInt32ToArrayBigEndian(inputData, 33, CBHDKeyGetChildNumber(childID));

	if (childID.priv) {

		// Private key derivation

		// Parent must be private
		if (type != CB_HD_KEY_TYPE_PRIVATE) {
			CBLogError("Attempting to derive a public-key without public-key derivation, or the key was of an unknown type.");
			return false;<commit_msg>Fix integer literal for HDWallet child<commit_after>//
//  CBHDKeys.c
//  cbitcoin
//
//  Created by <NAME> on 19/10/2013.
//  Copyright (c) 2013 <NAME>
//  
//  This file is part of cbitcoin. It is subject to the license terms
//  in the LICENSE file found in the top-level directory of this
//  distribution and at http://www.cbitcoin.com/license.html. No part of
//  cbitcoin, including this file, may be copied, modified, propagated,
//  or distributed except according to the terms contained in the
//  LICENSE file.

//  SEE HEADER FILE FOR DOCUMENTATION 

#include "CBHDKeys.h"

unsigned char CB_CURVE_ORDER[32] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,0x8C,0xD0,0x36,0x41,0x41};

CBHDKey * CBNewHDKey(bool priv) {

	CBHDKey * key = malloc(sizeof(*key) + (priv ? sizeof(CBKeyPair) : sizeof(CBPubKeyInfo)));
	CBInitHDKey(key);

	return key;

}

CBHDKey * CBNewHDKeyFromData(unsigned char * data) {

	CBHDKeyVersion versionBytes = CBArrayToInt32BigEndian(data, 0);
	CBHDKeyType type = CBHDKeyGetType(versionBytes);

	if (type == CB_HD_KEY_TYPE_UNKNOWN) {
		CBLogError("Unknown key type.");
		return NULL;
	}

	// Allocate memory for key
	CBHDKey * key = CBNewHDKey(type == CB_HD_KEY_TYPE_PRIVATE);

	return CBInitHDKeyFromData(key, data, versionBytes, type) ? key : (free(key), NULL);

}

CBKeyPair * CBNewKeyPair(bool priv) {

	CBKeyPair * key = malloc(priv ? sizeof(CBKeyPair) : sizeof(CBPubKeyInfo));
	CBInitKeyPair(key);

	return key;

}

void CBInitHDKey(CBHDKey * key) {

	CBInitKeyPair(key->keyPair);

}

bool CBInitHDKeyFromData(CBHDKey * key, unsigned char * data, CBHDKeyVersion versionBytes, CBHDKeyType type) {

	CBInitHDKey(key);

	// Set version bytes
	key->versionBytes = versionBytes;

	// Chain code
	memcpy(key->chainCode, data + 13, 32);

	if (type == CB_HD_KEY_TYPE_PRIVATE){
		// Private
		memcpy(key->keyPair->privkey, data + 46, 32);
		// Calculate public key
		CBKeyGetPublicKey(CBHDKeyGetPrivateKey(key), CBHDKeyGetPublicKey(key));
	}else
		// Assume public
		memcpy(CBHDKeyGetPublicKey(key), data + 45, 33);

	// Depth
	key->depth = data[4];
	if (key->depth == 0) {
		// Master
		key->childID.priv = false;
		key->childID.childNumber = 0;
		for (int x = 5; x < 9; x++)
			if (data[x] != 0) {
				CBLogError("The fingerprint of the master key is not zero.");
				return false;
			}
	}else{
		// Not master
		int childNum = CBArrayToInt32BigEndian(data, 9);
		key->childID.priv = childNum >> 31;
		key->childID.childNumber = childNum & 0x7fffffff;
	}

	return true;
}

void CBInitKeyPair(CBKeyPair * key) {

	key->pubkey.hashSet = false;

}

bool CBHDKeyDeriveChild(CBHDKey * parentKey, CBHDKeyChildID childID, CBHDKey * childKey) {

	unsigned char hash[64], inputData[37];
	CBHDKeyType type = CBHDKeyGetType(parentKey->versionBytes);

	// Add childID to inputData
	CBInt32ToArrayBigEndian(inputData, 33, CBHDKeyGetChildNumber(childID));

	if (childID.priv) {

		// Private key derivation

		// Parent must be private
		if (type != CB_HD_KEY_TYPE_PRIVATE) {
			CBLogError("Attempting to derive a public-key without public-key derivation, or the key was of an unknown type.");
			return false;
{'fraction_non_alphanumeric': 0.12634534394010294, 'fraction_numerical': 0.03026048978318515, 'mean_word_length': 4.255737704918033, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5581293', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2370, 'n_words': 568}