Skip to content

Commit 9a48ca4

Browse files
jharveybguggero
authored andcommitted
proof: add AltLeaves to Proof
In this commit, we add an AltLeaves field to Proof, and update Proof en/decode to support this optional field. With a small test change, we also check that Proof.Verify() functions for Proofs that accounted for any present AltLeaves correctly.
1 parent 6c2eb13 commit 9a48ca4

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

proof/proof.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ type Proof struct {
309309
// require only a valid signature for the previously revealed group key.
310310
GroupKeyReveal asset.GroupKeyReveal
311311

312+
// AltLeaves represent data used to construct an Asset commitment, that
313+
// was inserted in the output anchor Tap commitment. These data-carrying
314+
// leaves are used for a purpose distinct from representing individual
315+
// Taproot Assets.
316+
AltLeaves []asset.AltLeaf[asset.Asset]
317+
312318
// UnknownOddTypes is a map of unknown odd types that were encountered
313319
// during decoding. This map is used to preserve unknown types that we
314320
// don't know of yet, so we can still encode them back when serializing.
@@ -329,7 +335,7 @@ func (p *Proof) OutPoint() wire.OutPoint {
329335

330336
// EncodeRecords returns the set of known TLV records to encode a Proof.
331337
func (p *Proof) EncodeRecords() []tlv.Record {
332-
records := make([]tlv.Record, 0, 15)
338+
records := make([]tlv.Record, 0, 16)
333339
records = append(records, VersionRecord(&p.Version))
334340
records = append(records, PrevOutRecord(&p.PrevOut))
335341
records = append(records, BlockHeaderRecord(&p.BlockHeader))
@@ -369,6 +375,9 @@ func (p *Proof) EncodeRecords() []tlv.Record {
369375
&p.GroupKeyReveal,
370376
))
371377
}
378+
if len(p.AltLeaves) > 0 {
379+
records = append(records, AltLeavesRecord(&p.AltLeaves))
380+
}
372381

373382
// Add any unknown odd types that were encountered during decoding.
374383
return asset.CombineRecords(records, p.UnknownOddTypes)
@@ -392,6 +401,7 @@ func (p *Proof) DecodeRecords() []tlv.Record {
392401
BlockHeightRecord(&p.BlockHeight),
393402
GenesisRevealRecord(&p.GenesisReveal),
394403
GroupKeyRevealRecord(&p.GroupKeyReveal),
404+
AltLeavesRecord(&p.AltLeaves),
395405
}
396406
}
397407

proof/proof_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ func genRandomGenesisWithProof(t testing.TB, assetType asset.Type,
283283
},
284284
)
285285
require.NoError(t, err)
286+
287+
// Include 1 or more alt leaves in the anchor output Tap commitment.
288+
// Since this is also used for generating the test vectors, we don't
289+
// actually want to have zero alt leaves.
290+
innerAltLeaves := asset.RandAltLeaves(t, true)
291+
292+
altCommitment, err := commitment.NewAssetCommitment(innerAltLeaves...)
293+
require.NoError(t, err)
294+
295+
err = tapCommitment.Upsert(altCommitment)
296+
require.NoError(t, err)
297+
altLeaves := asset.ToAltLeaves(innerAltLeaves)
286298
genesisAsset := assets[0]
287299
_, commitmentProof, err := tapCommitment.Proof(
288300
genesisAsset.TapCommitmentKey(),
@@ -355,6 +367,7 @@ func genRandomGenesisWithProof(t testing.TB, assetType asset.Type,
355367
AdditionalInputs: nil,
356368
GenesisReveal: genReveal,
357369
GroupKeyReveal: groupKeyReveal,
370+
AltLeaves: altLeaves,
358371
}, genesisPrivKey
359372
}
360373

proof/records.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
BlockHeightType tlv.Type = 22
2828
GenesisRevealType tlv.Type = 23
2929
GroupKeyRevealType tlv.Type = 25
30+
AltLeavesType tlv.Type = 27
3031

3132
TaprootProofOutputIndexType tlv.Type = 0
3233
TaprootProofInternalKeyType tlv.Type = 2
@@ -53,7 +54,7 @@ var KnownProofTypes = fn.NewSet(
5354
TxMerkleProofType, AssetLeafType, InclusionProofType,
5455
ExclusionProofsType, SplitRootProofType, MetaRevealType,
5556
AdditionalInputsType, ChallengeWitnessType, BlockHeightType,
56-
GenesisRevealType, GroupKeyRevealType,
57+
GenesisRevealType, GroupKeyRevealType, AltLeavesType,
5758
)
5859

5960
// KnownTaprootProofTypes is a set of all known Taproot proof TLV types. This
@@ -393,3 +394,18 @@ func GroupKeyRevealRecord(reveal *asset.GroupKeyReveal) tlv.Record {
393394
GroupKeyRevealDecoder,
394395
)
395396
}
397+
398+
func AltLeavesRecord(leaves *[]asset.AltLeaf[asset.Asset]) tlv.Record {
399+
sizeFunc := func() uint64 {
400+
var buf bytes.Buffer
401+
err := asset.AltLeavesEncoder(&buf, leaves, &[8]byte{})
402+
if err != nil {
403+
panic(err)
404+
}
405+
return uint64(len(buf.Bytes()))
406+
}
407+
return tlv.MakeDynamicRecord(
408+
AltLeavesType, leaves, sizeFunc, asset.AltLeavesEncoder,
409+
asset.AltLeavesDecoder,
410+
)
411+
}

0 commit comments

Comments
 (0)