Control Flow
Using select and the multiplexer design pattern
Since Inco enables smart contracts to compute over private data without leaking any information, two common programming usages can’t be used.
-
You can’t use an if/else statement with a condition depending on a private value. The flow that the program would take would leak information about the private value.
-
For the same reason you can’t revert a transaction based on a condition depending on a private value.
To go around this, we use a pattern called the multiplexer design pattern.
Multiplexer Design Pattern
The inco equivalent of an if/else statement is the select
statement.
The select
statement takes an encrypted boolean as first argument and two encrypted values as second and third arguments.
The result of the select
statement is the second argument if the first argument is true and the third argument otherwise.
Example usage from the confidential token contract:
In the confidential token example, instead of reverting if the user has insufficient balance, we transfer an amount of 0, which is equivalent to doing nothing. Expect this kind of logic in most confidential apps.
Was this page helpful?