lightninglabs / lightninglabs/taproot-assets

universe: switch to 128-bit limbs for multiverse leaves or switch to top-level counter sum

Open
#479 3 comments 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #540 by @ffranr — merged
safey security universe
Dominant language
Go
Stars
525
Forks
150
Avg merge
2d 15h
Merged PRs (30d)
31

Description

Background

Today we use 64-bit sum values. Where relevant, we also ensure that we bail out if an overflow happens.

Related to this the multiverse and universe trees. The universe tree for issuance has a sum value that's the sum of all the issued assets for a base universe/asset. This can then be extended to also track all transfers for a given asset, with the sum value now being the total units of the assets that have been exchanged. If we then combine several of those trees into a single tree, we end up with a value that's the total amount of units moved over all assets. This is where we need to address over flow concerns.

One path here would be: for the multiverse and normal universe trees, we just tally a single integer which tracks the number of transition events. This works, but then for cases where you want to keep track of the total supply of a sparse set of assets, you lose the ability to track an aggregated sum. Maybe this is ok though, as we already have the asset group system for this use case.

Assuming we want to enable tracking that aggregate sum value (still an open question), then we can move to a 128-bit integer for the root sum.

A GPT-generated type for this looks something like:

package main

import (
	"errors"
	"fmt"
)

// Limb represents a single 64-bit portion of our 128-bit number
type Limb uint64

// U128 represents a 128-bit unsigned integer
type U128 struct {
	High Limb
	Low  Limb
}

// Add performs 128-bit addition with overflow detection
func (u *U128) Add(other U128) (U128, error) {
	sumLow := u.Low + other.Low
	sumHigh := u.High + other.High

	// Detect overflow in low limb
	if sumLow < u.Low {
		sumHigh++ // Carry
	}

	// Detect overflow in high limb
	if sumHigh < u.High {
		return U128{}, errors.New("overflow detected in 128-bit addition")
	}

	return U128{High: sumHigh, Low: sumLow}, nil
}

// Mul performs 128-bit multiplication with overflow detection
// It uses "grade school" multiplication with 64-bit limbs
func (u *U128) Mul(other U128) (U128, error) {
	lowBits := (u.Low * other.Low)
	midBits1 := (u.Low * other.High)
	midBits2 := (u.High * other.Low)
	highBits := (u.High * other.High)

	// Overflow detection
	if midBits1 > (1<<64-1)-lowBits || midBits2 > (1<<64-1)-lowBits || highBits > 0 {
		return U128{}, errors.New("overflow detected in 128-bit multiplication")
	}

	resultLow := lowBits + (midBits1 << 32) + (midBits2 << 32)
	resultHigh := highBits + (midBits1 >> 32) + (midBits2 >> 32)

	return U128{High: resultHigh, Low: resultLow}, nil
}

func ToU128Uint(value uint64) (U128, error) {
	if value > 0xFFFFFFFFFFFFFFFF {
		return U128{}, errors.New("overflow: value is greater than 64 bits")
	}
	return U128{High: 0, Low: Limb(value)}, nil
}

Steps To Completion

  • Decide on if we actually need to modify the normal Universe and Multiverse sum values.
  • If so, swap the existing 64 bit sum value for this 128 bit sum value. Type parameters will likely be useful here, as then we can retain the same tree, and bind the type at compile time.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the existing 64-bit sum values used by the normal Universe and Multiverse trees. Resolve whether aggregated sums are required or whether transition-event counts are sufficient; if 128-bit sums are chosen, define the required overflow behavior and verify the affected tree operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
blockchain
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.