floCrypto improvement

moving non canon functions to util property
This commit is contained in:
sairajzero 2019-12-19 20:25:14 +05:30
parent 7a60c9f134
commit d09ada8261

View File

@ -5610,6 +5610,7 @@
/* FLO Crypto Operators*/ /* FLO Crypto Operators*/
const floCrypto = { const floCrypto = {
util:{
p: BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16), p: BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16),
ecparams: EllipticCurve.getSECCurveByName("secp256k1"), ecparams: EllipticCurve.getSECCurveByName("secp256k1"),
@ -5624,30 +5625,8 @@
// x is x value of public key in BigInteger format without 02 or 03 or 04 prefix // x is x value of public key in BigInteger format without 02 or 03 or 04 prefix
return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p) return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p)
}, },
//generate a random Interger within range
randInt: function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
},
//generate a random String within length (options : alphaNumeric chars only)
randString: function (length, alphaNumeric = false) {
var result = '';
if(alphaNumeric)
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
else
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
for ( var i = 0; i < length; i++ )
result += characters.charAt(Math.floor(Math.random() * characters.length));
return result;
},
getUncompressedPublicKey: function (compressedPublicKey) { getUncompressedPublicKey: function (compressedPublicKey) {
const p = this.p; const p = this.p;
// Fetch x from compressedPublicKey // Fetch x from compressedPublicKey
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey); let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
const prefix = pubKeyBytes.shift() // remove prefix const prefix = pubKeyBytes.shift() // remove prefix
@ -5655,20 +5634,15 @@
pubKeyBytes.unshift(0) // add prefix 0 pubKeyBytes.unshift(0) // add prefix 0
let x = new BigInteger(pubKeyBytes) let x = new BigInteger(pubKeyBytes)
let xDecimalValue = x.toString() let xDecimalValue = x.toString()
// Fetch y // Fetch y
let y = this.calculateY(x); let y = this.calculateY(x);
let yDecimalValue = y.toString(); let yDecimalValue = y.toString();
// verify y value // verify y value
let resultBigInt = y.mod(BigInteger("2")); let resultBigInt = y.mod(BigInteger("2"));
let check = resultBigInt.toString() % 2; let check = resultBigInt.toString() % 2;
if (prefix_modulus !== check) { if (prefix_modulus !== check) {
yDecimalValue = y.negate().mod(p).toString(); yDecimalValue = y.negate().mod(p).toString();
} }
return { return {
x: xDecimalValue, x: xDecimalValue,
y: yDecimalValue y: yDecimalValue
@ -5728,12 +5702,32 @@
privateKeyDecimal: privateKeyDecimal, privateKeyDecimal: privateKeyDecimal,
privateKeyHex: privateKeyHex privateKeyHex: privateKeyHex
} }
}
},
//generate a random Interger within range
randInt: function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
},
//generate a random String within length (options : alphaNumeric chars only)
randString: function (length, alphaNumeric = false) {
var result = '';
if(alphaNumeric)
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
else
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
for ( var i = 0; i < length; i++ )
result += characters.charAt(Math.floor(Math.random() * characters.length));
return result;
}, },
//Encrypt Data using public-key //Encrypt Data using public-key
encryptData: function (data, receiverCompressedPublicKey) { encryptData: function (data, receiverCompressedPublicKey) {
var senderECKeyData = this.getSenderPublicKeyString(); var senderECKeyData = this.util.getSenderPublicKeyString();
var senderDerivedKey = this.deriveSharedKeySender(receiverCompressedPublicKey, senderECKeyData.privateKey); var senderDerivedKey = this.util.deriveSharedKeySender(receiverCompressedPublicKey, senderECKeyData.privateKey);
let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue; let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue;
let secret = Crypto.AES.encrypt(data, senderKey); let secret = Crypto.AES.encrypt(data, senderKey);
return { return {
@ -5747,12 +5741,12 @@
var receiverECKeyData = {}; var receiverECKeyData = {};
if (typeof myPrivateKey !== "string") throw new Error("No private key found."); if (typeof myPrivateKey !== "string") throw new Error("No private key found.");
let privateKey = this.wifToDecimal(myPrivateKey, true); let privateKey = this.util.wifToDecimal(myPrivateKey, true);
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error( if (typeof privateKey.privateKeyDecimal !== "string") throw new Error(
"Failed to detremine your private key."); "Failed to detremine your private key.");
receiverECKeyData.privateKey = privateKey.privateKeyDecimal; receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
var receiverDerivedKey = this.deriveReceiverSharedKey(data.senderPublicKeyString, receiverECKeyData var receiverDerivedKey = this.util.deriveReceiverSharedKey(data.senderPublicKeyString, receiverECKeyData
.privateKey); .privateKey);
let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue; let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue;
@ -5784,7 +5778,7 @@
var sigBytes = Crypto.util.hexToBytes(signatureHex); var sigBytes = Crypto.util.hexToBytes(signatureHex);
var signature = Bitcoin.ECDSA.parseSig(sigBytes); var signature = Bitcoin.ECDSA.parseSig(sigBytes);
var publicKeyPoint = this.ecparams.getCurve().decodePointHex(publicKeyHex); var publicKeyPoint = this.util.ecparams.getCurve().decodePointHex(publicKeyHex);
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s, var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger, signature.r, signature.s,
publicKeyPoint); publicKeyPoint);
@ -5853,6 +5847,7 @@
} }
}, },
//Split the str using shamir's Secret and Returns the shares
createShamirsSecretShares: function (str, total_shares, threshold_limit) { createShamirsSecretShares: function (str, total_shares, threshold_limit) {
try{ try{
if (str.length > 0) { if (str.length > 0) {
@ -5866,10 +5861,12 @@
} }
}, },
//Verifies the shares and str
verifyShamirsSecret: function (sharesArray, str) { verifyShamirsSecret: function (sharesArray, str) {
return (str && this.retrieveShamirSecret(sharesArray) === str) return (str && this.retrieveShamirSecret(sharesArray) === str)
}, },
//Returns the retrived secret by combining the shamirs shares
retrieveShamirSecret: function (sharesArray) { retrieveShamirSecret: function (sharesArray) {
try{ try{
if (sharesArray.length > 0) { if (sharesArray.length > 0) {