Skip to content

Commit a030f57

Browse files
committed
proof: update handling of tapscript preimages
1 parent 13f6073 commit a030f57

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

proof/records.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,14 @@ func CommitmentProofTapSiblingPreimageRecord(
224224
preimage **commitment.TapscriptPreimage) tlv.Record {
225225

226226
sizeFunc := func() uint64 {
227-
// 1 byte for the type, and then the pre-image itself.
228-
return 1 + uint64(len((*preimage).SiblingPreimage))
227+
var buf bytes.Buffer
228+
err := commitment.TapscriptPreimageEncoder(
229+
&buf, preimage, &[8]byte{},
230+
)
231+
if err != nil {
232+
panic(err)
233+
}
234+
return uint64(len(buf.Bytes()))
229235
}
230236
return tlv.MakeDynamicRecord(
231237
CommitmentProofTapSiblingPreimageType, preimage, sizeFunc,
@@ -238,10 +244,15 @@ func TapscriptProofTapPreimage1Record(
238244
preimage **commitment.TapscriptPreimage) tlv.Record {
239245

240246
sizeFunc := func() uint64 {
241-
// 1 byte for the type, and then the pre-image itself.
242-
return 1 + uint64(len((*preimage).SiblingPreimage))
247+
var buf bytes.Buffer
248+
err := commitment.TapscriptPreimageEncoder(
249+
&buf, preimage, &[8]byte{},
250+
)
251+
if err != nil {
252+
panic(err)
253+
}
254+
return uint64(len(buf.Bytes()))
243255
}
244-
245256
return tlv.MakeDynamicRecord(
246257
TapscriptProofTapPreimage1, preimage,
247258
sizeFunc, commitment.TapscriptPreimageEncoder,
@@ -253,10 +264,15 @@ func TapscriptProofTapPreimage2Record(
253264
preimage **commitment.TapscriptPreimage) tlv.Record {
254265

255266
sizeFunc := func() uint64 {
256-
// 1 byte for the type, and then the pre-image itself.
257-
return 1 + uint64(len((*preimage).SiblingPreimage))
267+
var buf bytes.Buffer
268+
err := commitment.TapscriptPreimageEncoder(
269+
&buf, preimage, &[8]byte{},
270+
)
271+
if err != nil {
272+
panic(err)
273+
}
274+
return uint64(len(buf.Bytes()))
258275
}
259-
260276
return tlv.MakeDynamicRecord(
261277
TapscriptProofTapPreimage2, preimage,
262278
sizeFunc, commitment.TapscriptPreimageEncoder,

proof/taproot.go

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ type TapscriptProof struct {
9999
// EncodeRecords returns the encoding records for TapscriptProof.
100100
func (p TapscriptProof) EncodeRecords() []tlv.Record {
101101
records := make([]tlv.Record, 0, 3)
102-
if p.TapPreimage1 != nil && len(p.TapPreimage1.SiblingPreimage) > 0 {
102+
if p.TapPreimage1 != nil && !p.TapPreimage1.IsEmpty() {
103103
records = append(records, TapscriptProofTapPreimage1Record(
104104
&p.TapPreimage1,
105105
))
106106
}
107-
if p.TapPreimage2 != nil && len(p.TapPreimage2.SiblingPreimage) > 0 {
107+
if p.TapPreimage2 != nil && !p.TapPreimage2.IsEmpty() {
108108
records = append(records, TapscriptProofTapPreimage2Record(
109109
&p.TapPreimage2,
110110
))
@@ -373,81 +373,68 @@ func (p TapscriptProof) DeriveTaprootKeys(internalKey *btcec.PublicKey) (
373373
// hashes. In this case, the tapscript tree has two elements, with both
374374
// of them being leaves.
375375
case !p.TapPreimage1.IsEmpty() && !p.TapPreimage2.IsEmpty() &&
376-
p.TapPreimage1.SiblingType == commitment.LeafPreimage &&
377-
p.TapPreimage2.SiblingType == commitment.LeafPreimage:
376+
p.TapPreimage1.Type() == commitment.LeafPreimage &&
377+
p.TapPreimage2.Type() == commitment.LeafPreimage:
378378

379-
leafHash1, err := commitment.TapLeafHash(
380-
p.TapPreimage1.SiblingPreimage,
381-
)
379+
leafHash1, err := p.TapPreimage1.TapHash()
382380
if err != nil {
383381
return nil, err
384382
}
385-
leafHash2, err := commitment.TapLeafHash(
386-
p.TapPreimage2.SiblingPreimage,
387-
)
383+
384+
leafHash2, err := p.TapPreimage2.TapHash()
388385
if err != nil {
389386
return nil, err
390387
}
391388

392-
rootHash := commitment.NewTapBranchHash(*leafHash1, *leafHash2)
389+
rootHash := asset.NewTapBranchHash(*leafHash1, *leafHash2)
393390
tapscriptRoot = rootHash[:]
394391

395392
// Two pre-images are specified, with both of the pre-images being a
396393
// branch. In this case, we don't know how many elements the tree has,
397394
// we just care that these are actually branches and the hash up
398395
// correctly.
399396
case !p.TapPreimage1.IsEmpty() && !p.TapPreimage2.IsEmpty() &&
400-
p.TapPreimage1.SiblingType == commitment.BranchPreimage &&
401-
p.TapPreimage2.SiblingType == commitment.BranchPreimage:
397+
p.TapPreimage1.Type() == commitment.BranchPreimage &&
398+
p.TapPreimage2.Type() == commitment.BranchPreimage:
402399

403-
branch1, err := commitment.TapBranchHash(
404-
p.TapPreimage1.SiblingPreimage,
405-
)
400+
branch1, err := p.TapPreimage1.TapHash()
406401
if err != nil {
407402
return nil, err
408403
}
409-
branch2, err := commitment.TapBranchHash(
410-
p.TapPreimage2.SiblingPreimage,
411-
)
404+
branch2, err := p.TapPreimage2.TapHash()
412405
if err != nil {
413406
return nil, err
414407
}
415408

416-
rootHash := commitment.NewTapBranchHash(*branch1, *branch2)
409+
rootHash := asset.NewTapBranchHash(*branch1, *branch2)
417410
tapscriptRoot = rootHash[:]
418411

419412
// Two pre-images are specified, with one of them being a leaf and the
420413
// other being a branch. In this case, we have an un-balanced tapscript
421414
// tree. We'll verify the first sibling is a leaf, and the other is
422415
// actually a branch.
423416
case !p.TapPreimage1.IsEmpty() && !p.TapPreimage2.IsEmpty() &&
424-
p.TapPreimage1.SiblingType == commitment.LeafPreimage &&
425-
p.TapPreimage2.SiblingType == commitment.BranchPreimage:
417+
p.TapPreimage1.Type() == commitment.LeafPreimage &&
418+
p.TapPreimage2.Type() == commitment.BranchPreimage:
426419

427-
leafHash, err := commitment.TapLeafHash(
428-
p.TapPreimage1.SiblingPreimage,
429-
)
420+
leafHash, err := p.TapPreimage1.TapHash()
430421
if err != nil {
431422
return nil, err
432423
}
433424

434-
branchHash, err := commitment.TapBranchHash(
435-
p.TapPreimage2.SiblingPreimage,
436-
)
425+
branchHash, err := p.TapPreimage2.TapHash()
437426
if err != nil {
438427
return nil, err
439428
}
440429

441-
rootHash := commitment.NewTapBranchHash(*leafHash, *branchHash)
430+
rootHash := asset.NewTapBranchHash(*leafHash, *branchHash)
442431
tapscriptRoot = rootHash[:]
443432

444433
// Only a single pre-image was specified, and the pre-image is a leaf.
445-
case !p.TapPreimage1.IsEmpty() &&
446-
p.TapPreimage1.SiblingType == commitment.BranchPreimage:
434+
case !p.TapPreimage1.IsEmpty() && p.TapPreimage2.IsEmpty() &&
435+
p.TapPreimage1.Type() == commitment.LeafPreimage:
447436

448-
tapHash, err := commitment.TapLeafHash(
449-
p.TapPreimage1.SiblingPreimage,
450-
)
437+
tapHash, err := p.TapPreimage1.TapHash()
451438
if err != nil {
452439
return nil, err
453440
}

0 commit comments

Comments
 (0)