Hey Zac,

I think this could be a useful opcode. It kinda seems like UC1 and UC2 are basically the same use case: using rate-limiting to reduce risk of theft or mistake. I think this could be a helpful addition to a good wallet setup. 

I don't quite understand why you'd want to define a specific span of blocks for the rate limit. Why not just specify the size of the window (in blocks) to rate limit within, and the limit? 

You mentioned change addresses, however, with the parameters you defined, there would be no way to connect together the change address with the original address, meaning they would have completely separate rate limits, which wouldn't work since the change output would ignore the previous rate limit. I can think of the following options: 

A. You could always send change back to the *same* address. This is the simplest option, and the only downside I can think of is exposing the public key of an address. I'm not quite sure what the consensus is on the dangers of exposing the public key. It theoretically reduces quantum resistance a bit, but I think I read that some of taproot's mechanisms expose the bare public key, so maybe consensus has changed about that in recent years?

B. Have some way to specify connected addresses in the output. This has the edge case that one of the addresses wouldn't be able to specify all the addresses that it should be connected with, because it would create a hash loop (ie if you had address A and B that should be connected, you can create address A and then specify that address B be connected to address A, but address A cannot specify its connection to B because A was created before B was created). You wouldn't want one address to be able to simply define a connection to another address, because this would open up attack vectors where people could encumber other people's addresses with rate limits connected to theirs. You could define connections based on signatures, which could be done without creating a hash loop, however it would require exposing the public keys of other addresses when you do that, at which point you might as well go with option A. 

C. You could specify that rate limits follow a certain output. Eg, if you create a transaction with destination output 1 and change output 2, your rate limiting opcode could specify that output 2 should inherit the rate limit. These inherited rate limits could all be connected together automatically. 

Another consideration is what to use for a receive-address. I would say the simplest option here is to receive at an address that contains an existing output already. If you allowed receiving at an address that contains no coins, you'd have to specify at least one other address to connect it with. This could work, but I don't see any advantage to it, since you don't gain any privacy by creating a new address if you're going to immediately programmatically tie it to the other addresses.

One thing to consider is the cost of carrying around and checking these rate limits. Ideally it should be a very small amount of data carried around in the UTXO set, and be very cheap to verify when the opcode comes up. I think it would make sense for such an opcode to only be able to track rate-limits over short spans, like a month or less. Allowing the user to specify an arbitrary window over which to track a rate-limit seems like something that would probably open up a dos vector or other node resource usage abuse attacks. It might be useful enough to simply rate limit over each epoch (two weeks), but having a small set of options could also be useful (eg 1 day, 1 week, or 1 month). 

In any case, I'd be interested in seeing you write a BIP for this. Of course, don't take my word as community interest. I'm reasonably new to the bitcoin dev community, so definitely don't jump the gun based on my interest. 

On Sat, Jul 31, 2021 at 2:51 PM Zac Greenwood via bitcoin-dev <bitcoin-dev@lists.linuxfoundation.org> wrote:
Hi list,

I'd like to explore whether it is feasible to implement new scripting capabilities in Bitcoin that enable limiting the output amount of a transaction based on the total value of its inputs. In other words, to implement the ability to limit the maximum amount that can be sent from an address.

Two use cases come to mind:

UC1: enable a user to add additional protection their funds by rate-limiting the amount they are able to send during a certain period (measured in blocks). A typical use case might be a user that intends to hodl their bitcoin, but still wishes to occasionally send small amounts. This avoids an attacker from sweeping all their funds in a single transaction, allowing the user to become aware of the theft and intervene to prevent further theft.

UC2: exchanges may wish to rate-limit addresses containing large amounts of bitcoin, adding warm- or hot-wallet functionality to a cold-storage address. This would enable an exchange to drastically reduce the number of times a cold wallet must be accessed with private keys that enable access to the full amount.

In a typical setup, I'd envision using multisig such that the user has two sets of private keys to their encumbered address (with a "set" of keys meaning "one or more" keys). One set of private keys allows only for sending with rate-limiting restrictions in place, and a s second set of private keys allowing for sending any amount without rate-limiting, effectively overriding such restriction.

The parameters that define in what way an output is rate-limited might be defined as follows:

Param 1: a block height "h0" indicating the first block height of an epoch;
Param 2: a block height "h1" indicating the last block height of an epoch;
Param 3: an amount "a" in satoshi indicating the maximum amount that is allowed to be sent in any epoch;
Param 4: an amount "a_remaining" (in satoshi) indicating the maximum amount that is allowed to be sent within the current epoch.

For example, consider an input containing 100m sats (1 BTC) which has been rate-limited with parameters (h0, h1, a, a_remaning) of (800000, 800143, 500k, 500k). These parameters define that the address is rate-limited to sending a maximum of 500k sats in the current epoch that starts at block height 800000 and ends at height 800143 (or about one day ignoring block time variance) and that the full amount of 500k is still sendable. These rate-limiting parameters ensure that it takes at minimum 100m / 500k = 200 transactions and 200 x 144 blocks or about 200 days to spend the full 100m sats. As noted earlier, in a typical setup a user should retain the option to transact the entire amount using a second (set of) private key(s).

For rate-limiting to work, any change output created by a transaction from a rate-limited address must itself be rate-limited as well. For instance, expanding on the above example, assume that the user spends 200k sats from a rate-limited address a1 containing 100m sats:

Start situation:
At block height 800000: rate-limited address a1 is created;
Value of a1: 100.0m sats;
Rate limiting params of a1: h0=800000, h1=800143, a=500k, a_remaining=500k;

Transaction t1:
Included at block height 800100;
Spend: 200k + fee;
Rate limiting params: h0=800000, h1=800143, a=500k, a_remaining=300k.

Result:
Value at destination address: 200k sats;
Rate limiting params at destination address: none;
Value at change address a2: 99.8m sats;
Rate limiting params at change address a2: h0=800000, h1=800143, a=500k, a_remaining=300k.

In order to properly enforce rate limiting, the change address must be rate-limited such that the original rate limit of 500k sats per 144 blocks cannot be exceeded. In this example, the change address a2 were given the same rate limiting parameters as the transaction that served as its input. As a result, from block 800100 up until and including block 800143, a maximum amount of 300k sats is allowed to be spent from the change address.

Example continued:
a2: 99.8 sats at height 800100;
Rate-limit params: h0=800000, h1=800143, a=500k, a_remaining=300k;

Transaction t2:
Included at block height 800200
Spend: 400k + fees.
Rate-limiting params: h0=800144, h1=800287, a=500k, a_remaining=100k.

Result:
Value at destination address: 400k sats;
Rate limiting params at destination address: none;
Value at change address a3: 99.4m sats;
Rate limiting params at change address a3: h0=800144, h1=800287, a=500k, a_remaining=100k.

Transaction t2 is allowed because it falls within the next epoch (running from 800144 to 800287) so a spend of 400k does not violate the constraint of 500k per epoch.

As could be seen, the rate limiting parameters are part of the transaction and chosen by the user (or their wallet). This means that the parameters must be validated to ensure that they do not violate the intended constraints.

For instance, this transaction should not be allowed:
a2: 99.8 sats at height 800100;
Rate-limit params of a2: h0=800000, h1=800143, a=500k, a_remaining=300k;

Transaction t2a:
Included at block height 800200;
Spend: 400k + fees;
Rate-limit params: h0=800124, h1=800267, a=500k, a_remaining=100k.

This transaction t2a attempts to shift the epoch forward by 20 blocks such that it starts at 800124 instead of 800144. Shifting the epoch forward like this must not be allowed because it enables spending more that the rate limit allows, which is 500k in any epoch of 144 blocks. It would enable overspending:

t1: spend 200k at 800100 (epoch 1: total: 200k);
t2a: spend 400k at 800200 (epoch 2: total: 400k);
t3a: spend 100k at 800201 (epoch 2: total: 500k);
t4a: spend 500k at 800268 (epoch 2: total: 1000k, overspending for epoch 2).

Specifying the rate-limiting parameters explicitly at every transaction allows the user to tighten the spending limit by setting tighter limits or for instance by setting a_remainder to 0 if they wish to enforce not spending more during an epoch.

I will stop here because I would like to gauge interest in this idea first before continuing work on other aspects. Two main pieces of work jump to mind:

Define all validations;
Describe aggregate behaviour of multiple (rate-limited) inputs, proof that two rate-limited addresses cannot spend more than the sum of their individual limits.

Zac






_______________________________________________
bitcoin-dev mailing list
bitcoin-dev@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev