sign inputs

This commit is contained in:
poma 2021-10-30 20:35:35 +01:00
parent 397942f94d
commit 3fed6e5410
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
4 changed files with 35 additions and 4 deletions

View File

@ -8,4 +8,17 @@ template Keypair() {
component hasher = Poseidon(1);
hasher.inputs[0] <== privateKey;
publicKey <== hasher.out;
}
}
template Signature() {
signal input privateKey;
signal input commitment;
signal input merklePath;
signal output out;
component hasher = Poseidon(3);
hasher.inputs[0] <== privateKey;
hasher.inputs[1] <== commitment;
hasher.inputs[2] <== merklePath;
out <== hasher.out;
}

View File

@ -11,7 +11,7 @@ Utxo structure:
}
commitment = hash(amount, pubKey, blinding)
nullifier = hash(commitment, merklePath, privKey)
nullifier = hash(commitment, merklePath, sign(commitment + merklePath, privKey))
*/
// Universal JoinSplit transaction with nIns inputs and 2 outputs
@ -38,6 +38,7 @@ template Transaction(levels, nIns, nOuts, zeroLeaf) {
signal private input outBlinding[nOuts];
component inKeypair[nIns];
component inSignature[nIns];
component inUtxoHasher[nIns];
component nullifierHasher[nIns];
component tree[nIns];
@ -54,10 +55,15 @@ template Transaction(levels, nIns, nOuts, zeroLeaf) {
inUtxoHasher[tx].inputs[1] <== inKeypair[tx].publicKey;
inUtxoHasher[tx].inputs[2] <== inBlinding[tx];
inSignature[tx] = Signature();
inSignature[tx].privateKey <== inPrivateKey[tx];
inSignature[tx].commitment <== inUtxoHasher[tx].out;
inSignature[tx].merklePath <== inPathIndices[tx];
nullifierHasher[tx] = Poseidon(3);
nullifierHasher[tx].inputs[0] <== inUtxoHasher[tx].out;
nullifierHasher[tx].inputs[1] <== inPathIndices[tx];
nullifierHasher[tx].inputs[2] <== inPrivateKey[tx];
nullifierHasher[tx].inputs[2] <== inSignature[tx].out;
nullifierHasher[tx].out === inputNullifier[tx];
tree[tx] = MerkleProof(levels);

View File

@ -78,6 +78,17 @@ class Keypair {
})
}
/**
* Sign a message using keypair private key
*
* @param {string|number|BigNumber} commitment a hex string with commitment
* @param {string|number|BigNumber} merklePath a hex string with merkle path
* @returns {BigNumber} a hex string with signature
*/
sign(commitment, merklePath) {
return poseidonHash([this.privkey, commitment, merklePath])
}
/**
* Encrypt data using keypair encryption key
*

View File

@ -46,7 +46,8 @@ class Utxo {
) {
throw new Error('Can not compute nullifier without utxo index or private key')
}
this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, this.keypair.privkey || 0])
const signature = this.keypair.privkey ? this.keypair.sign(this.getCommitment(), this.index || 0) : 0
this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, signature])
}
return this._nullifier
}