Skip to content

Commit be17a69

Browse files
committed
Remove Sighash
1 parent bf96f52 commit be17a69

File tree

8 files changed

+146
-37
lines changed

8 files changed

+146
-37
lines changed

Cargo.lock

Lines changed: 117 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ byteorder = "1.4.3"
2929
group = "0.13"
3030
rand_core = "0.6.4"
3131
jubjub = "0.10.0"
32-
zcash_primitives = { git="https://github.com/PIVX-Project/librustzcash", rev="e7662b23d16c38595ef9ad1f3ae683b1f54dc732" }
33-
zcash_proofs = { git="https://github.com/PIVX-Project/librustzcash", rev="e7662b23d16c38595ef9ad1f3ae683b1f54dc732" }
32+
zcash_primitives = { git="https://github.com/Duddino/librustzcash", branch="shielstake" }
33+
zcash_proofs = { git="https://github.com/Duddino/librustzcash", branch="shielstake" }
3434
zcash_note_encryption = "0.3.0"
3535

src/primitives/block.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class ShieldStakeProof
8383
CAmount amount;
8484
uint256 inputCv;
8585
uint256 rk;
86-
SpendDescription::spend_auth_sig_t spendSig;
8786
libzcash::GrothProof inputProof = {{0}};
8887

8988
uint256 outputCv;
@@ -96,7 +95,6 @@ class ShieldStakeProof
9695
{
9796
amount = 0;
9897
inputCv.SetNull();
99-
spendSig = {{0}};
10098
rk.SetNull();
10199
inputProof = {{0}};
102100
outputCv.SetNull();
@@ -110,7 +108,6 @@ class ShieldStakeProof
110108
READWRITE(obj.amount);
111109
READWRITE(obj.inputCv);
112110
READWRITE(obj.rk);
113-
READWRITE(obj.spendSig);
114111
READWRITE(obj.inputProof);
115112
READWRITE(obj.epk);
116113
READWRITE(obj.cmu);

src/rust/include/librustzcash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef LIBRUSTZCASH_INCLUDE_H_
22
#define LIBRUSTZCASH_INCLUDE_H_
33

4+
#include <cstddef>
45
#include <stdint.h>
56

67
extern "C" {

src/rust/src/rustzcash.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,12 @@ pub extern "system" fn librustzcash_sapling_check_spend(
588588
};
589589

590590
// Deserialize the signature
591-
let spend_auth_sig = match Signature::read(&(unsafe { &*spend_auth_sig })[..]) {
592-
Ok(sig) => sig,
593-
Err(_) => return false,
591+
// Spend auth sig is not needed in shield stake proofs.
592+
// See #2836 for details
593+
let spend_auth_sig = if spend_auth_sig.is_null() {
594+
None
595+
} else {
596+
Signature::read(&(unsafe { &*spend_auth_sig })[..]).ok()
594597
};
595598

596599
// Deserialize the proof
@@ -599,12 +602,18 @@ pub extern "system" fn librustzcash_sapling_check_spend(
599602
Err(_) => return false,
600603
};
601604

605+
let sighash_value = if sighash_value.is_null() {
606+
[0u8; 32]
607+
} else {
608+
unsafe { *sighash_value }
609+
};
610+
602611
unsafe { &mut *ctx }.check_spend(
603612
&cv,
604613
anchor,
605614
unsafe { &*nullifier },
606615
rk.clone(),
607-
unsafe { &*sighash_value },
616+
&sighash_value,
608617
spend_auth_sig,
609618
zkproof.clone(),
610619
unsafe { SAPLING_SPEND_VK.as_ref() }.unwrap(),
@@ -684,7 +693,15 @@ pub extern "system" fn librustzcash_sapling_final_check(
684693
Err(_) => return false,
685694
};
686695

687-
unsafe { &*ctx }.final_check(value_balance, unsafe { &*sighash_value }, binding_sig)
696+
// Sighash is not needed in Shield stake proof.
697+
// See #2836 for details.
698+
let sighash_value = if sighash_value.is_null() {
699+
[0u8; 32]
700+
} else {
701+
unsafe { *sighash_value }
702+
};
703+
704+
unsafe { &*ctx }.final_check(value_balance, &sighash_value, binding_sig)
688705
}
689706

690707
#[no_mangle]

src/rust/src/tests/notes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::librustzcash_sapling_compute_cm;
2-
use crate::librustzcash_sapling_compute_nf;
1+
use crate::{librustzcash_sapling_compute_cm, librustzcash_sapling_compute_nf};
32

43
#[test]
54
fn notes() {

src/sapling/sapling_validation.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,7 @@ bool CheckShieldStake(const CBlock& block, CValidationState& state, const CChain
240240
const auto& p = block.shieldStakeProof;
241241
const int DOS_LEVEL_BLOCK = 100;
242242

243-
uint256 dataToBeSigned;
244-
try {
245-
// TODO: write signature for shield
246-
// dataToBeSigned = SignatureHash(scriptCode, tx, NOT_AN_INPUT, SIGHASH_ALL, 0, SIGVERSION_SAPLING);
247-
} catch (const std::logic_error& ex) {
248-
// A logic error should never occur because we pass NOT_AN_INPUT and
249-
// SIGHASH_ALL to SignatureHash().
250-
return state.DoS(100, error("%s: error computing signature hash", __func__),
251-
REJECT_INVALID, "error-computing-signature-hash");
252-
}
253-
254-
if (!librustzcash_sapling_check_spend(ctx, p.inputCv.begin(), inputNote.anchor.begin(), inputNote.nullifier.begin(), p.rk.begin(), p.inputProof.begin(), p.spendSig.begin(), dataToBeSigned.begin())) {
243+
if (!librustzcash_sapling_check_spend(ctx, p.inputCv.begin(), inputNote.anchor.begin(), inputNote.nullifier.begin(), p.rk.begin(), p.inputProof.begin(), nullptr, nullptr)) {
255244
librustzcash_sapling_verification_ctx_free(ctx);
256245
return state.DoS(
257246
DOS_LEVEL_BLOCK,
@@ -265,7 +254,7 @@ bool CheckShieldStake(const CBlock& block, CValidationState& state, const CChain
265254
REJECT_INVALID, "bad-txns-sapling-output-description-invalid");
266255
}
267256

268-
if (!librustzcash_sapling_final_check(ctx, block.shieldStakeProof.amount, block.shieldStakeProof.sig.data(), dataToBeSigned.begin())) {
257+
if (!librustzcash_sapling_final_check(ctx, block.shieldStakeProof.amount, block.shieldStakeProof.sig.data(), nullptr)) {
269258
librustzcash_sapling_verification_ctx_free(ctx);
270259
return state.DoS(
271260
100,

src/sapling/saplingscriptpubkeyman.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,12 +1362,6 @@ bool SaplingScriptPubKeyMan::ComputeShieldStakeProof(CBlock& block, CStakeableSh
13621362
ss << witnesses[0]->path();
13631363
std::vector<unsigned char> witness(ss.begin(), ss.end());
13641364
assert(anchor == spendNote.anchor);
1365-
librustzcash_sapling_spend_sig(
1366-
sk.expsk.ask.begin(),
1367-
alpha.begin(),
1368-
dataToBeSigned.begin(),
1369-
block.shieldStakeProof.spendSig.data());
1370-
13711365
if (!librustzcash_sapling_spend_proof(ctx, sk.expsk.full_viewing_key().ak.begin(),
13721366
sk.expsk.nsk.begin(),
13731367
note.note.d.data(),
@@ -1410,6 +1404,7 @@ bool SaplingScriptPubKeyMan::ComputeShieldStakeProof(CBlock& block, CStakeableSh
14101404
librustzcash_sapling_proving_ctx_free(ctx);
14111405
return false;
14121406
}
1407+
14131408
librustzcash_sapling_proving_ctx_free(ctx);
14141409
block.shieldStakeProof.amount = suggestedValue;
14151410
LogPrintf("%s : Shield Stake proof generated with value %d\n", __func__, suggestedValue);

0 commit comments

Comments
 (0)