This commit is contained in:
poma 2021-06-09 14:19:22 +03:00
parent cb2a587540
commit a19a226277
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
3 changed files with 42 additions and 25 deletions

View File

@ -66,7 +66,7 @@ async function getProof({ inputs, outputs, tree, extAmount, fee, recipient, rela
// data for 2 transaction inputs // data for 2 transaction inputs
inAmount: inputs.map((x) => x.amount), inAmount: inputs.map((x) => x.amount),
inPrivateKey: inputs.map((x) => x.privkey), inPrivateKey: inputs.map((x) => x.keypair.privkey),
inBlinding: inputs.map((x) => x.blinding), inBlinding: inputs.map((x) => x.blinding),
inPathIndices: inputMerklePathIndices, inPathIndices: inputMerklePathIndices,
inPathElements: inputMerklePathElements, inPathElements: inputMerklePathElements,
@ -74,7 +74,7 @@ async function getProof({ inputs, outputs, tree, extAmount, fee, recipient, rela
// data for 2 transaction outputs // data for 2 transaction outputs
outAmount: outputs.map((x) => x.amount), outAmount: outputs.map((x) => x.amount),
outBlinding: outputs.map((x) => x.blinding), outBlinding: outputs.map((x) => x.blinding),
outPubkey: outputs.map((x) => x.pubkey), outPubkey: outputs.map((x) => x.keypair.pubkey),
outPathIndices: outputIndex >> Math.log2(outputs.length), outPathIndices: outputIndex >> Math.log2(outputs.length),
outPathElements: outputPath.slice(Math.log2(outputs.length)), outPathElements: outputPath.slice(Math.log2(outputs.length)),
} }
@ -130,7 +130,7 @@ async function transact({ tornadoPool, utxo }) {
const inputs = [utxo, new Utxo()] const inputs = [utxo, new Utxo()]
const outputs = [ const outputs = [
new Utxo({ amount: utxo.amount / 4 }), new Utxo({ amount: utxo.amount / 4 }),
new Utxo({ amount: (utxo.amount * 3) / 4, privkey: utxo.privkey }), new Utxo({ amount: (utxo.amount * 3) / 4, keypair: utxo.keypair }),
] ]
const { proof, args } = await getProof({ const { proof, args } = await getProof({

31
src/kaypair.js Normal file
View File

@ -0,0 +1,31 @@
const { ethers } = require('hardhat')
const { BigNumber } = ethers
const { randomBN, poseidonHash, toFixedHex } = require('./utils')
class Keypair {
constructor(privkey = randomBN()) {
this.privkey = privkey
this.pubkey = poseidonHash([this.privkey])
this.encryptionKey = 0 // todo
}
toString() {
return toFixedHex(this.pubkey) + toFixedHex(this.encryptionKey).slice(2)
}
static fromString(str) {
if (str.length === 130) {
str = str.slice(2)
}
if (str.length !== 128) {
throw new Error('Invalid key length')
}
return Object.assign(new Keypair(), {
privkey: null,
pubkey: BigNumber.from('0x' + str.slice(0, 64)),
encryptionKey: BigNumber.from('0x' + str.slice(64, 128)),
})
}
}
module.exports = Keypair

View File

@ -1,43 +1,29 @@
const { ethers } = require('hardhat') const { ethers } = require('hardhat')
const { BigNumber } = ethers const { BigNumber } = ethers
const { randomBN, poseidonHash } = require('./utils') const { randomBN, poseidonHash } = require('./utils')
const Keypair = require('./kaypair')
function fromPrivkey(privkey) {
return {
privkey,
pubkey: poseidonHash([privkey]),
}
}
class Utxo { class Utxo {
constructor({ amount, pubkey, privkey, blinding, index } = {}) { constructor({ amount = 0, keypair = new Keypair(), blinding = randomBN(), index } = {}) {
if (!pubkey) { this.amount = BigNumber.from(amount)
if (privkey) { this.blinding = BigNumber.from(blinding)
pubkey = fromPrivkey(privkey).pubkey this.keypair = keypair
} else {
;({ pubkey, privkey } = fromPrivkey(randomBN()))
}
}
this.amount = BigNumber.from(amount || 0)
this.blinding = blinding || randomBN()
this.pubkey = pubkey
this.privkey = privkey
this.index = index this.index = index
} }
getCommitment() { getCommitment() {
if (!this._commitment) { if (!this._commitment) {
this._commitment = poseidonHash([this.amount, this.blinding, this.pubkey]) this._commitment = poseidonHash([this.amount, this.blinding, this.keypair.pubkey])
} }
return this._commitment return this._commitment
} }
getNullifier() { getNullifier() {
if (!this._nullifier) { if (!this._nullifier) {
if (this.amount > 0 && (this.index === undefined || !this.privkey === undefined)) { if (this.amount > 0 && (this.index === undefined || this.keypair.privkey === undefined || this.keypair.privkey === null)) {
throw new Error('Can not compute nullifier without utxo index or private key') throw new Error('Can not compute nullifier without utxo index or private key')
} }
this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, this.privkey || 0]) this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, this.keypair.privkey || 0])
} }
return this._nullifier return this._nullifier
} }