Skip to content

Commit ce9c7b7

Browse files
committed
WIP
0 parents  commit ce9c7b7

9 files changed

+870
-0
lines changed

Uniswap.thy

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
theory Uniswap
2+
imports PhiSem_Machine_Integer PhiSem_CF_Basic PhiSem_Variable HOL.Transcendental
3+
begin
4+
5+
consts Tick :: \<open>(VAL, int) \<phi>\<close>
6+
Price :: \<open>(VAL, real) \<phi>\<close>
7+
8+
definition \<open>FACTOR = (1.0001::real)\<close>
9+
10+
lemma FACTOR_LG_1: \<open>1 < FACTOR\<close> unfolding FACTOR_def by simp
11+
12+
definition price_of :: \<open>int \<Rightarrow> real\<close>
13+
where price_of_def': \<open>price_of tick = sqrt (FACTOR powr (of_int tick))\<close>
14+
15+
lemma price_of_def: \<open>price_of t = FACTOR powr (of_int t / 2)\<close>
16+
unfolding price_of_def'
17+
by (simp add: powr_half_sqrt[symmetric] powr_powr)
18+
19+
hide_fact price_of_def'
20+
21+
lemma price_of_mono': \<open>mono price_of\<close>
22+
unfolding price_of_def mono_on_def
23+
by (simp add: FACTOR_LG_1)
24+
25+
lemma price_of_smono': \<open>strict_mono price_of\<close>
26+
unfolding price_of_def strict_mono_on_def
27+
by (simp add: FACTOR_LG_1)
28+
29+
lemma price_of_mono:
30+
\<open>price_of x \<le> price_of y \<longleftrightarrow> x \<le> y\<close>
31+
using price_of_mono'
32+
by (simp add: price_of_smono' strict_mono_less_eq)
33+
34+
lemma price_of_smono:
35+
\<open>price_of x < price_of y \<longleftrightarrow> x < y\<close>
36+
using price_of_smono' using strict_mono_less by blast
37+
38+
definition \<open>tick_of_price p = (@t. price_of t \<le> p \<and> p < price_of (t+1)) \<close>
39+
40+
lemma tick_of_price: \<open>tick_of_price (price_of t) = t\<close>
41+
unfolding tick_of_price_def
42+
by (smt (z3) price_of_smono some_eq_imp)
43+
44+
lemma price_of_tick:
45+
assumes \<open>0 < p\<close>
46+
shows \<open>price_of (tick_of_price p) \<le> p \<and> p < price_of (tick_of_price p + 1)\<close>
47+
proof -
48+
have \<open>\<exists>t. p < FACTOR powr t\<close>
49+
by (metis FACTOR_LG_1 dual_order.strict_trans floor_log_eq_powr_iff linorder_neqE_linordered_idom zero_less_one)
50+
then have \<open>\<exists>t. p < price_of t\<close>
51+
unfolding price_of_def
52+
by (meson FACTOR_LG_1 dual_order.strict_trans ex_less_of_int less_divide_eq_numeral1(1) powr_less_cancel_iff)
53+
moreover have \<open>\<exists>t. FACTOR powr t \<le> p\<close>
54+
using \<open>0 < p\<close> FACTOR_LG_1 floor_log_eq_powr_iff by blast
55+
then have \<open>\<exists>t. price_of t \<le> p\<close>
56+
proof -
57+
have \<open>\<And>x. \<exists>t. real_of_int t / 2 < x\<close> by (simp add: ex_of_int_less)
58+
then show ?thesis
59+
unfolding price_of_def
60+
by (meson FACTOR_LG_1 \<open>\<exists>t. FACTOR powr t \<le> p\<close> dual_order.trans less_le_not_le powr_le_cancel_iff)
61+
qed
62+
ultimately have X: \<open>(\<exists>t. price_of t \<le> p \<and> p < price_of (t+1))\<close>
63+
proof -
64+
obtain t1 where t1: \<open>price_of t1 \<le> p\<close> using \<open>\<exists>t. price_of t \<le> p\<close> by blast
65+
obtain t2 where t2: \<open>p < price_of t2\<close> using \<open>\<exists>t. p < price_of t\<close> by blast
66+
have le: \<open>t1 < t2\<close> using t1 t2 price_of_smono by fastforce
67+
thm int_gr_induct[OF le]
68+
have \<open>p < price_of t2 \<longrightarrow> ?thesis\<close>
69+
proof (induct rule: int_gr_induct[OF le])
70+
case 1
71+
then show ?case
72+
using t1 by blast
73+
next
74+
case (2 i)
75+
then show ?case
76+
by force
77+
qed
78+
then show ?thesis
79+
using t2 by blast
80+
qed
81+
then show ?thesis
82+
unfolding tick_of_price_def
83+
by (metis (no_types, lifting) someI_ex)
84+
qed
85+
86+
87+
debt_axiomatization getSqrtRatioAtTick :: \<open>(VAL,VAL) proc'\<close>
88+
where getSqrtRatioAtTick_\<phi>app:
89+
\<open>\<^bold>p\<^bold>r\<^bold>o\<^bold>c getSqrtRatioAtTick v \<lbrace> t \<Ztypecolon> \<^bold>v\<^bold>a\<^bold>l[v] Tick \<longmapsto> price_of t \<Ztypecolon> \<^bold>v\<^bold>a\<^bold>l Price \<rbrace>\<close>
90+
and getTickAtSqrtRatio_\<phi>app:
91+
\<open>\<^bold>p\<^bold>r\<^bold>e\<^bold>m\<^bold>i\<^bold>s\<^bold>e 0 < p
92+
\<Longrightarrow> \<^bold>p\<^bold>r\<^bold>o\<^bold>c getTickAtSqrtRatio v \<lbrace> p \<Ztypecolon> \<^bold>v\<^bold>a\<^bold>l[v] Price \<longmapsto> tick_of_price p \<Ztypecolon> \<^bold>v\<^bold>a\<^bold>l Tick \<rbrace>\<close>
93+
94+
record tick_info =
95+
liquidityGross :: nat
96+
liquidityNet :: int
97+
feeGrowthOutside0X128 :: nat
98+
feeGrowthOutside1X128 :: nat
99+
tickCumulativeOutside :: int
100+
secondsPerLiquidityOutsideX128 :: nat
101+
secondsOutside :: nat
102+
initialized :: bool
103+
104+
definition \<open>growth_Inv f f' delta current =
105+
(\<forall>i. f i = (if i \<le> current then (sum f' {j. j \<le> i}) + delta i else (sum f' {j. i < j}) - delta i))\<close>
106+
107+
108+
term sum
109+
110+
111+
consts TickInfo' :: \<open>(VAL, tick_info) \<phi>\<close>
112+
TickInfos :: \<open>(assn, int \<Rightarrow> tick_info) \<phi>\<close>
113+
114+
definition \<open>TickInfo = (\<lambda>info. into \<Ztypecolon> TickInfo' \<^bold>s\<^bold>u\<^bold>b\<^bold>j )\<close>
115+
116+
117+
end
238 KB
Binary file not shown.
Binary file not shown.
269 KB
Binary file not shown.

notes/heaps.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
- envir: singleton {sender: address, code: address}
4+
- contracts: address -> field-name -> Value
5+
6+
address := nat (it is logically abstract and inifinite therefore)
7+
field-name := string
8+
Value := usual-Value | mapping-id | array-id
9+
10+
mapping-id, array-id: MLP
11+
(id, type_k, type_v) -> Value -> Value
12+
(id, type_v) -> nat -> Value
13+
14+
15+

notes/note0716.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
3+
4+
- payable address,
5+
6+
7+
8+
9+
## Note!
10+
11+
- address is comparable
12+
- storage of map and array needs Meta Logic Parameter [X no we don't]
13+
- push() is very tricky! more cautions to deal with it! (low priority, Uniswap never uses storage X[]!)
14+
15+
## Heaps:
16+
17+
- balance: address -> uint256???
18+
19+
20+
## Question:
21+
22+
- What is the type of money/ether?
23+
24+
### Need Talk
25+
26+
- **GAS!!!!!**
27+
- Dangling References, acceptable feature or reject?
28+
29+
## TODO Plan:
30+
31+
- Exception & Revert
32+
- Mutability: oure, view, payable, non-payable
33+
- bytes, T[] is a pointer
34+
X nondeterminism if we want true `send`
35+
X true exception if we want `break` & `return`
36+
Success state | PartCorrect | Error excp state
37+
- WF while / recursion
38+
39+
### Special Cautions [necessary]
40+
41+
- abi.decode / abi.encodeWithSelector, .selector
42+
- staticcall
43+
- checkNotDelegateCall (How?)
44+
- It is said before version xx byte is an alias of bytes1. Then what is it now?
45+
- calldata
46+
- assignment between different locations!! see [Data location and assignment behavior]
47+
- the automatic public accessor of mappings
48+
49+
### Special Cautions [less important]
50+
51+
- type expression `type(int32).max` (The uniswap only uses `type(T).max/min`
52+
- Contract is a type, e.g. `type(MyContract).name`
53+
- Constant evaluation: `(2**800 + 1) - 2**800 = 1`!
54+
55+
### Special Cautions [info]
56+
57+
- address.code / .balance / .codehash / .send / .transfer
58+
59+
### Delayed / Not important in Uniswap
60+
61+
- call{gas: 10000 / value: 1ether}
62+
- .code
63+
- all string
64+
- enum
65+
- User defined value type
66+
- Function pointer
67+
- bytes**N**
68+
- Array Slices, basically pointer shift.
69+
70+
71+
## Blueprint
72+
73+
- bytesN : fixed-size list (maybe LENGTH('a))
74+
- keyword external, view: ignore them, since the specification states these info.
75+
76+
77+
78+
79+
## Question:
80+
81+
1. How to deal with `break`
82+
Maybe we need true Exception!

notes/notes.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
1. Do we model GAS?
2+
2. Push() is very tricky!! read `Dangling References to Storage Array Elements`
3+
3. Constant evaluation has arbitrary precision. Is is rational number. `1.5 + 1.5 = 3`, `(1.5**800 + 1) - 1.5**800 = 1`
4+
https://docs.soliditylang.org/en/v0.8.15/types.html?highlight=Operators#operators
5+
Suggestion: It must be done during pre-processing, cuz it never generates the code for, like, `1.5 + 1.5`.
6+
Can we use the constant evaluator of Solidity itself?
7+
8+
Small stuffs, not hard but need attention
9+
1. abi.decode / abi.encodeWithSelector, .selector
10+
These are used in Uniswap! https://docs.soliditylang.org/en/v0.8.15/units-and-global-variables.html?highlight=encodewithselector#abi-encoding-and-decoding-functions
11+
2. type expression `type(int32).max` (The uniswap only uses `type(T).max/min`
12+
https://docs.soliditylang.org/en/v0.8.15/units-and-global-variables.html?highlight=type%20expression#type-information
13+
14+
15+
16+
indeed, our semantic model can support this, by recording the layout on the reference, but the reasoning can be difficult. so i think, we just make it in semantics at most, and

notes/ttt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{} C {..} {\empty}
2+
3+
4+
{} C1 {..}, {if P then E1 else \empty }
5+
{} Throw {...}, {if Q then E2 else \empty }
6+
7+
8+
{} C1 >> Throw, {if P then E1 else if Q then E2 else \empty }
9+
10+
if P then E1 else \empty \subset if P then E1 else if Q then E2 else \empty
11+
if Q then E2 else \empty \not \subset if P then E1 else if Q then E2 else \empty
12+
13+
14+
15+
16+
17+
18+
19+
send : Transition Specifiction (TS)
20+
21+
Wrong implementation:
22+
transfer (address a, int m) = send a 0
23+
24+
{} transfer {X}
25+
26+
a.fallback is certain procedure \subset TS
27+
28+
{(a.fallback : state transition A -> B) * A /\ a.fallback \subset TS } send a { B }
29+
{balance = M} send a 10 {X }
30+
{(a.fallback : state transition (A, balance = M-10) -> (B, balance = N')) * (A, balance = M) /\ a.fallback \subset TS } send a 10 { B, balance = N' }
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
{P} C1 {Q} {A1}
53+
{Q} C2 {R} {A2}
54+
-------------------
55+
{P} C1 {R} {A1 * A2}
56+
57+
A ::= NoErr | Err (type-excep, state)
58+
NoErr * x = x
59+
Err y * _ = Err y
60+
61+
type-excep ::= Break | Return | ...
62+
63+
{P} C {Q} {A} := if
64+
65+
{P} C1 {Q} {if cond then excep else NoErr}
66+
{P} C {Q} {NoErr}
67+
68+
(if cond1 then excep else NoErr) * (if cond2 then excep else NoErr)
69+
= if cond1 then excep else NoErr
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+

0 commit comments

Comments
 (0)