我一直在使用 libsodium 库来实现 Shamir 秘密共享并尝试测试由暗水晶完成的实现
https://gitlab.com/dark-crystal-javascript/key-backup-crypto/-/blob/master/example.js
实现是这样的
function encryptionKeypair () {
const keypair = {
publicKey: sodium.sodium_malloc(sodium.crypto_box_PUBLICKEYBYTES),
secretKey: sodium.sodium_malloc(sodium.crypto_box_SECRETKEYBYTES)
}
sodium.crypto_box_keypair(keypair.publicKey, keypair.secretKey)
return keypair
},
function oneWayBox (message, publicKey) {
console.log('in one way box');
const curvePublicKey = sodium.sodium_malloc(sodium.crypto_box_PUBLICKEYBYTES)
// console.log('curvePublicKey', curvePublicKey.toString('hex'));
console.log('curvePublicKey', curvePublicKey.length);
console.log('publicKey', publicKey.length);
sodium.crypto_sign_ed25519_pk_to_curve25519(curvePublicKey, publicKey)
// console.log('curvePublicKey', curvePublicKey.toString('hex'));
console.log('in one way box');
console.log('\n');
const ephemeral = this.encryptionKeypair()
const nonce = this.randomBytes(sodium.crypto_box_NONCEBYTES)
const cipherText = sodium.sodium_malloc(message.length + sodium.crypto_box_MACBYTES)
sodium.crypto_box_easy(cipherText, message, nonce, curvePublicKey, ephemeral.secretKey)
zero(ephemeral.secretKey)
zero(message)
return Buffer.concat([nonce, ephemeral.publicKey, cipherText])
},
下面是 Secret-sharing-generation.js
const secrets = require('secret-sharing')
const s = require('.')
const secret = 'My secret key'
const label = ''
console.log('Secret to share:', secret.toString('hex'))
console.log(`Packing with label: '${label}'`)
const packedSecret = s.packLabel(secret, label)
console.log(`Packed secret: ${packedSecret.toString('hex')}`)
console.log(`Length of packed secret is ${packedSecret.length} bytes.`)
const signingKeypair = s.keypair()
const encryptionKeypair = s.signingKeypairToEncryptionKeypair(signingKeypair)
const custodians = []
for (let i = 0; i < 5; i++) {
custodians.push(s.encryptionKeypair())
}
console.log('custodians', custodians);
console.log('Creating 5 shares, 3 needed to recover')
secrets.share(packedSecret, 5, 3).then((shards) => {
console.log('Shards:')
console.log(shards.map(s => s.toString('hex')))
console.log('Signed shards:')
const signedShards = s.signShards(shards, signingKeypair)
console.log(signedShards.map(s => s.toString('hex')))
const boxedShards = signedShards.map((shard, i) => {
return s.oneWayBox(shard, custodians[i].publicKey)
})
console.log('Boxed shards:')
console.log(boxedShards.map(s => s.toString('hex')))
console.log(`Length of boxed shards are ${boxedShards[0].length} bytes.`)
secrets.combine(shards.slice(2)).then((result) => {
console.log('Result of recombining 3 shares:', result.toString())
})
})
现在的问题是,当我使用encryptionKeypair
函数为其生成密钥对时,然后当我尝试crypto_sign_ed25519_sk_to_curve25519
使用此encryptionKeypair
函数中生成的密钥对生成时,我得到了
UnhandledPromiseRejectionWarning: Error: ENOMEM, Cannot allocate memory
我检查了我的交换空间它是完全免费的
total used free shared buff/cache available
Mem: 3138 83 2896 0 158 2908
Swap: 5119 0 5119
我无法理解问题所在。