Skip to content

Abstract cost function - #15

Open
yuval-block wants to merge 1 commit into
mainfrom
cost-function-abstract
Open

yuval-block wants to merge 1 commit into
mainfrom
cost-function-abstract

Conversation

@yuval-block

Copy link
Copy Markdown
Collaborator

This document describes the overall approach to the cost function without going into specific/concrete terms

@bc1cindy bc1cindy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was a great to read this

some typos:

  • l.3 - "describes how score the outcomes" → "how to score"
  • l.21 - "one might considering all actions" → "one might consider"
  • l.23 - "not account for everything, that may eventually happen," → "everything that may eventually happen"
  • l.27 - "the input that will eventually spend it already known" → "is already known"
  • l.37 - "at a higher feerate than of the unconfirmed" → "than the unconfirmed"
  • l.47 - "the goal of create some output" → "of creating"
  • l.49 - "resulting a transaction" → "resulting from a transaction"
  • l.57 - "diminishes from the importance" → "diminishes the importance"
  • l.59 - "a transaction which underpay fees" → "underpays"
  • l.67 - "the total amount involve" → "involved"
  • l.85 - "a cost function for a privacy has" → "for privacy"
  • l.89 - "Metrics makes it possible" → "Metrics make"
  • l.91 - "Although are other aspects" → "Although there are"
  • l.95 - "the coins that an honest does not control" → "an honest user"
  • l.97 - "If all actions than those of the victim" → "all actions other than"
  • l.97 - "then adversary is able" → "then the adversary"
  • l.103 - "targetted" → "targeted"
  • l.125 - "incurs a cost can be shared" → "incurs a cost that can be shared"
  • l.129 - "should at the very least indifferent" → "should at the very least be indifferent"
  • l.129 - "the the actions" → "the actions"
  • l.131 - "Alice… they need to add" → "she needs"
  • l.133 - "we required that" → "we require that"
  • l.135 - "to define honest parties' as being" → "honest parties as"
  • l.135 - "the cost function … are indeed monotonically decreasing" → "is indeed"

Comment thread cost_function.md

When coins lack any sufficient privacy (e.g. when known to a counterparty), their value should be discounted by the budget, effectively devaluing non-private wallet states. As progress is made towards satisfying the thresholds on privacy metrics, some coins will be closer to their face value, an increase in subjective value. This simplifies the cost function so it only outputs a satoshi amount, allowing subjective and objective costs to be reduced to a single number, and therefore comparisons can be made between the wallet states predicted to result from taking one action vs. another.

For example, suppose Alice set her budget to 1%. She receives a coin with value $x$. Her total budget $b_0$ is $0.01x$. The subjective balance of the wallet is $x - b_0 = 0.99x$. The wallet then automatically does a CoinJoin transaction and its subjective cost $y$ is subtracted from the budget, $b_1 = b_0 - y$. This is also deducted from the subjective balance, but due to an improvement in the privacy metrics the subjective balance increases overall (or the transaction wouldn't have been made). The coin with nominal value $x$ and subjective value $x - b_0$ has been destroyed, and replaced with a coin whose nominal value is $x - y$ and whose subjective value is at least $x - b_1$ and at most $x - y$.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whose subjective value is at least $x - b_1$ and at most $x - y$

shouldn't this be $x - b_0$? (= nominal minus remaining budget, $(x - y) - b_1$)

as written, $x - b_1$ can exceed the upper bound $x - y$ whenever $y > b_0/2$

e.g. receive 100 sats with a 1 sat budget, coinjoin costs 0.6, so the bounds give [99.6, 99.4], while $x - b_0$ gives [99, 99.4]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuval-block @bc1cindy wrote:

shouldn't this be $x - b_0$? (= nominal minus remaining budget, $(x - y) - b_1$)

At ff3728c this is not reflected and has no reply yet: is it still planned, or can it be dropped?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

the replaced coin increases by some subjective quantity that is implied to be greater than y, the objective cost of the replacement via a coinjoin

the destroyed coin is discounted as 0.99x or x-b_0

b_1 is the remaining budget at that point, it is exactly b_0 - y, and x - b_1 is the lower bound, the coin could not be worth less than that or the implied cost of the change is positive, which means it wastes money for no benefit (the cost function provides a negative valuation when the objective cost exceeds the subjective benefits)

x - y is the upper bound because the privacy liability can go to 0, in which case there is no more devaluation of the coin

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind in my head i treated x as time dependent but that doesn't match what was written

Comment thread cost_function.md

An *intent* is a concrete representation of the goal of create some output (or input, in which case the goal is the destruction of some unspent output) before some deadline. The deadline can refer to the transmission of a signed transaction to the recipient out of band, broadcasting the transaction, or its confirmation in a block. For simplicity, and without loss of generality, output creation intents will be assumed to correspond to payments but that isn't necessarily the case (for example funding a lightning channel). We will assume intents are made known to the wallet some time in advance of the deadline.

The change in the balance of a wallet resulting a transaction is its *objective cost*. This can be negative, for example for the case of receiving a payment. In contrast, the utility obtained from a transaction confirming may include subjective terms as well. Subjective costs inherently depend on beliefs or preferences, and are the basis for any surplus created from transactions as any redistribution of on chain funds is necessarily zero sum.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in the balance of a wallet resulting a transaction is its objective cost.

should this be the decrease in the balance? receiving a payment increases the balance, so under "cost = change" it would come out positive

This can be negative, for example for the case of receiving a payment.

this sentence correctly treats it as negative (a benefit)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuval-block @bc1cindy wrote:

should this be the decrease in the balance? receiving a payment increases the balance, so under "cost = change" it would come out positive

At ff3728c this is not reflected and has no reply yet: is it still planned, or can it be dropped?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, change is more general and this could model expected incoming transactions as well, when notified of an interactive payment that will create an intent to receive that payment in an interactive transaction, which must be acted upon (both parties need to join the same session and negotiate the settlement within it)

@yuval-block yuval-block added this to the BRW milestone Sep 18, 2026
Comment thread cost_function.md

## Payment batching

Confirming transactions at the earliest possible time (well in advance of the deadline) is sub-optimal as it precludes any possibilities of batching payments. However, before confirmation it is possible to replace transactions by double spending at least one of its output in a transaction which is more attractive to miners (which also diminishes from the importance of introducing any CPFP liability). This is known as replace-by-fee (RBF), and multiple policies for allowing this, some defunct, have existed over the years.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RBF replaces an unconfirmed transaction by re-spending one or more of the original's inputs (BIP 125: the replacement "spends one or more of the same inputs"), not by spending its outputs.
The text's "double spending at least one of its output" describes spending unconfirmed outputs, which is a child transaction (CPFP), not a replacement.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuval-block oakagent wrote:

RBF replaces an unconfirmed transaction by re-spending one or more of the original's inputs (BIP 125: the replacement "spends one or more of the same inputs"), not by spending its outputs.

At ff3728c this is not reflected and has no reply yet: is it still planned, or can it be dropped?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BIP 125 is no longer relevant

"double spending at least one of its output[s]" is a mistake, it should say "spent outputs" as in the txos used as inputs

@oakagent

oakagent commented Oct 8, 2026

Copy link
Copy Markdown

Typos in what this pull request adds (automated):

cost_function.md

  • l.7 - "The simplest example of such outcome is" → "The simplest example of such an outcome is"
  • l.29 - "at any point in the future feerate is likely" → "at any point in the future the feerate is likely"
  • l.33 - "consumed spending all of coins available" → "consumed spending all of the coins available"
  • l.41 - "as opposed only what is due" → "as opposed to only what is due"
  • l.57 - "at least one of its output" → "at least one of its outputs"
  • l.65 - "depends on the harms from the loss of privacy outweigh" → "depends on whether the harms from the loss of privacy outweigh"
  • l.101 - "We might turning cluster analysis" → "We might turn cluster analysis"
  • l.135 - "to others arbitrary actions" → "to others' arbitrary actions"

@arminsabouri

Copy link
Copy Markdown
Collaborator

for @arminsabouri to decide if this is ready to be merged

@arminsabouri arminsabouri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This document has always read more like personal notes polished and shared than, for example, the overview #51 (which I believe was produced with a specific audience in mind). There are problems being defined and concrete solutions being presented here

A simplified version of the waste metric ... is to consider some

Breaking up the problem def and the how (concrete ideas) may help the flow of ideas.

The other thing that is missed is the why! A sentence or three in the intro about why this matters. Where other protocols fall short. This really contextualizes things for a reader. Currently it tells the reader: this document is about scoring outcomes and then goes right into "Coin selection with objective costs". Even if its citing the problem def that will be useful.

Comment thread cost_function.md
@@ -0,0 +1,137 @@
# Cost function framework for Bitcoin wallets

This document describes how score the outcomes of transacting on Bitcoin, in particular privacy outcomes. This is the basis for automating various decisions delegated by the user to the wallet.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This document describes how score the outcomes of transacting on Bitcoin, in particular privacy outcomes. This is the basis for automating various decisions delegated by the user to the wallet.
This document describes how to score the privacy preserving outcomes of transacting on Bitcoin. This is the basis for automating various decisions delegated by the user to the wallet.

Comment thread cost_function.md

### Future cost of spending (Bitcoin Core's waste metric)

At the time an output is created, the size of the input that will eventually spend it already known (or an expected size in the case of P2TR outputs). This size is independent of whatever else happens in that transaction. Reliably predicting a feerate, on the other hand, is not possible, as that is a market prediction problem.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2tr

Isn't it always an expected size for other script types e.g p2sh. Im not sure what this paranthesis is trying to get across.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so p2{w,}pkh and generally anything involving ECDSA sigs will have an expected size due to variable length encoding. that is not worth dwelling on IMO we should just model its concrete cost as the worst case or perhaps as a fixed discount for wallets which always grind... so not what i'm referring to

ignoring signature variations, you're right: p2{w,}sh you're right that will still have a variable size due to OP_IF potentially requiring different witnesses or scriptSig pushes, so yes

i was mainly referring to the fact that in p2tr the tap leaf is itself not determined until spend time, which is the same as branching pre-taproot, there are options and one of the options must be taken

anyway the point of this is to brush off this complexity and say it's not our problem here, we just want to thinking about that as a fixed expected cost rather than a decision tree whose leaves have different costs, because theoretically that assumption is without loss of generality, and because practically speaking there isn't a compelling reason to spend complex p2{w,}sh or use non keyspend p2tr paths in the collaborative setting unless that's the only spend path available, we can kind of assume users (honest users, see https://gist.github.com/nothingmuch/a6ce459475a527a0e51f0600e354ef0e) will pick the smallest available one

so all that is to say that we're going to ignore these contingencies in the doc and the cost function implementation and treat the spend path as having a fixed block weight

Comment thread cost_function.md

Optimizing these costs is the underlying motivation for the [*waste metric*](https://bitcoin.stackexchange.com/questions/113622/what-does-waste-metric-mean-in-the-context-of-coin-selection) used in Bitcoin Core's coin selection. This term in the cost function which governs coin selection internalizes an expected cost of spending a coin to the evaluation of the cost of its output. When the target feerate is lower than this predicted future feerate, this makes the evaluation of spending an input right now lower than this predicted future cost.

A simplified version of the waste metric (omitting some terms) is to consider some expected future feerate as effectively the zero point, by subtracting it from the target feerate at the time of coin selection. When the target feerate is lower, this will be negative, which makes *larger* transactions seem cheaper than smaller ones. The rationale for rewarding larger transactions is that the total blockspace that will eventually be consumed spending all of coins available in a wallet is essentially fixed, but what matters is simply *when* is that blockspace consumed, and at what feerate. During times of low demand for blockspace (and therefore low target feerates), coin selection will therefore tend to consolidate a fragmented wallet. When demand rises again, if coin selection must be done at the higher target feerate, since there would be fewer, higher value coins available on average, the expected transaction size at these higher target feerates will be lower, which in turn will result in a lower overall spending on fees.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target feerate is lower

lower than what? the zero point?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you consolidated prior to the increased demand/fees, then you are able to transact at a lower cost than if you hadn't done that

Comment thread cost_function.md

The worst case for this kind of leakage, in terms of how much information is revealed in a single transaction, is when the entire wallet's balance is spent in a single transaction. This is problematic not just because it links all coins together but also because it reveals the wallet's total balance, potentially aiding in additional analysis (such as correlating this amount to past transactions that aren't unambiguously linked to it).

Let's momentarily ignore both the severity of the harm of such privacy leaks (assume it's just some fixed penalty), as well as the risk, i.e. the probability that liquidating the entire wallet is necessary (for example when under duress, in response to a security incident, or when needing to make an urgent payment for an amount close to the available balance). What then determines the cost is how much information is leaked. Considering this potential contingency after every wallet state, and then internalizing the cost into the cost of the transaction which results in that state implicitly encodes the implicit goal of privacy enhancing transactions, which is to leave the wallet in a state where the harm of any such liquidation is reduced, because the information leaks are minimized.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This information can be conveyed i believe without the first sentence

Suggested change
Let's momentarily ignore both the severity of the harm of such privacy leaks (assume it's just some fixed penalty), as well as the risk, i.e. the probability that liquidating the entire wallet is necessary (for example when under duress, in response to a security incident, or when needing to make an urgent payment for an amount close to the available balance). What then determines the cost is how much information is leaked. Considering this potential contingency after every wallet state, and then internalizing the cost into the cost of the transaction which results in that state implicitly encodes the implicit goal of privacy enhancing transactions, which is to leave the wallet in a state where the harm of any such liquidation is reduced, because the information leaks are minimized.
What then determines the cost is how much information is leaked. Considering this potential contingency after every wallet state, and then internalizing the cost into the cost of the transaction which results in that state implicitly encodes the implicit goal of privacy enhancing transactions, which is to leave the wallet in a state where the harm of any such liquidation is reduced, because the information leaks are minimized.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yes this reads better but i do think think this kind of thing should be spelled out eventually because thinking of cost/benefit and risk analysis leaves things very abstract, almost to the point of meaninglessness for some people, so at least anecdotally when explaining why a cost function or optimization framework makes sense it helps to tie it back to real world economic harm/injury that people would generally want to protect themselves from

@yuval-block

Copy link
Copy Markdown
Collaborator Author

shit i forgot to push some major changes to this one from last week, i'll integrate your comments

This document has always read more like personal notes polished and shared than, for example, the overview #51 (which I believe was produced with a specific audience in mind). There are problems being defined and concrete solutions being presented here

A simplified version of the waste metric ... is to consider some

Breaking up the problem def and the how (concrete ideas) may help the flow of ideas.

What do you mean by concrete ideas? Or maybe, how concrete?

There's multiple levels of abstract concrete, e.g. this doc was not intended to contain any concrete cost function terms, hence "abstract" but it still has a lot of concrete ideas in it, like how to manage time dependence...

The original goal of this document was to document enough to get the cost function work started

The other thing that is missed is the why! A sentence or three in the intro about why this matters. Where other protocols fall short. This really contextualizes things for a reader. Currently it tells the reader: this document is about scoring outcomes and then goes right into "Coin selection with objective costs". Even if its citing the problem def that will be useful.

yes, what do you think of the way this was briefly summarized in the overview? i can do something similar

@xstoicunicornx

Copy link
Copy Markdown

@nothingmuch friendly reminder to push update :)

@arminsabouri

Copy link
Copy Markdown
Collaborator

@nothingmuch friendly reminder to push update :)

@nothingmuch @yuval-block ping

@bc1cindy bc1cindy added documentation Improvements or additions to documentation high and removed high documentation Improvements or additions to documentation labels Oct 20, 2026
@bc1cindy bc1cindy added documentation Improvements or additions to documentation high and removed documentation Improvements or additions to documentation high labels Oct 23, 2026
yuval-block pushed a commit to fungi-protocol/docs-draft that referenced this pull request Oct 24, 2026
…nditions

The conditions under "Coalition formation proposal structure" cover
consensus fields, the feerate, the vbytes allocation, and input and
output types, but say nothing about the values of inputs. Two kinds of
value constraint came up in the review of `cost_function.md`
(fungi-protocol/docs#15), whose multiparty section lists the
constraints parties must agree on before accepting a proposal:

- Nominal values: a required multiplicity of each of a set of standard
  denominations. These must be specified in terms of prevout values,
  since effective values also depend on the feerate. Checking them
  involves no search, so they need no certificate.

- Effective values: these determine subset sum densities, and
  satisfying constraints on them involves a search, so they take the
  form of a challenge for a subset sum density certificate. The
  challenge is a target interval and a density requirement ρ. The
  aggregator picks a concrete feerate within the agreed feerate
  interval, and responds with ρ pairwise disjoint subsets of all the
  aggregated inputs whose effective values at that feerate each sum to
  within the interval, which every party can check before signing just
  by summing.

---

- [x] Add per-denomination multiplicity constraints to the conditions
- [x] Add subset sum density challenges to the conditions
- [x] Specify the certificate: when the aggregator responds, at which
      feerate effective values are computed (the concrete feerate the
      aggregator picks), and how parties check it before signing
- [x] Decide whether a challenge can leak which outputs belong to the
      party posing it
- [ ] Link from `cost_function.md` once both are merged

---

Notes:

- Multiplicities fit the existing numerical intervals under
  intersection: one interval per denomination, counting the inputs
  whose prevout value equals it.
- Challenges combine by union, since every challenge must still be
  met. Unlike the intervals, an unsatisfiable combination can't be
  detected when proposals are aggregated, only when the aggregator
  fails to produce a certificate.
- Unlike the range proofs under "Effective value adjustments", which
  compute minimal effective values at the highest acceptable feerate,
  certificates use the concrete feerate the aggregator picks.
- If a challenge's interval brackets one of the challenger's own
  outputs, attaching it to the challenger's proposal may tell peers
  which output is theirs. Avoiding such leaks is up to the cost
  function, and only randomly placed narrow intervals, which serve as
  density estimates, are recommended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants