std:Word/Law

lib/Word/Law.omg

lib/Word/Law.omg226 lines
1-- The policy controls which results a caller may receive. These equations
2-- hold at every width, including zero, without assuming arithmetic laws.
3let Words = std:Word
4let A = std:Arith
5let B = std:Bool
6let E = std:Equal
7let present
8    : forall (T : Type) (fits : Bool) (value : T)
9      -> std:Optional.isSome (Words.checked.2 T fits value) === fits
10    = \T fits value ->
11        Bool/elim (\b -> std:Optional.isSome (Words.checked.2 T b value) === b) refl refl fits
12let wrapAdd
13    : forall (bits : Natural) (a b : Word bits)
14      -> (Words.arithmetic Words.wrapping bits).add a b === Word/add a b
15    = \bits a b -> refl
16let checkedSub
17    : forall (bits : Natural) (a b : Word bits)
18      -> Word/lt a b === False
19      -> (Words.arithmetic Words.checked bits).sub a b === Some (Word/sub a b)
20    = \bits a b enough ->
21        E.subst
22          (\(less : Bool) ->
23             Words.checked.2 (Word bits) (B.not less) (Word/sub a b) === Some (Word/sub a b))
24          (E.sym enough)
25          refl
26
27-- Under NatFacts, the product test on 256-bit operands implies equality
28-- with unbounded multiplication, not merely equality modulo 2^256.
29let U256 = Word 256
30let n = Word/toNatural @256
31let N = std:Natural.pow2 256
32let maxWord = Word/not (Word/fromNatural 256 0)
33let atLeast = \(x y : U256) -> B.not (Word/lt x y)
34let mulFits = Words.productFits @256 maxWord
35let notTrue
36    : forall (b : Bool) -> B.not b === True -> b === False
37    = \b ->
38        A.boolCases
39          (\(c : Bool) -> B.not c === True -> c === False)
40          b
41          (\_ p -> A.falseNotTrue p)
42          (\_ _ -> refl)
43
44let atLeastSound
45    : forall (h : A.NatFacts) (x y : U256) -> atLeast x y === True -> A.Le (n y) (n x)
46    = \h x y ok -> A.word256LtFalse h x y (notTrue (Word/lt x y) ok)
47
48-- The quotient guard bounds the unbounded product by any available room,
49-- not just by the largest word. Both mul and addProduct use this fact.
50let productBound
51    : forall (h : A.NatFacts) (room x y : U256)
52      -> Words.productFits room x y === True
53      -> A.Le (n x * n y) (n room)
54    = \h room x y ok ->
55        A.boolCases
56          (\(z : Bool) -> Word/isZero x === z -> A.Le (n x * n y) (n room))
57          (Word/isZero x)
58          (\(zero : Word/isZero x === True) _ ->
59             let none : n x === 0 = A.isZeroSound (n x) zero
60             in  E.subst
61                   (\(v : Natural) -> A.Le (v * n y) (n room))
62                   (E.sym none)
63                   (A.leZero (n room)))
64          (\(nonzero : Word/isZero x === False) _ ->
65             let bounded
66                 : A.Le (n y) (Natural/div (n room) (n x))
67                 = atLeastSound
68                     h
69                     (Word/div room x)
70                     y
71                     (E.subst (\(z : Bool) -> z || atLeast (Word/div room x) y === True) nonzero ok)
72             let q = Natural/div (n room) (n x)
73             in  A.leTrans
74                   (n x * n y)
75                   (n x * q)
76                   (n room)
77                   (A.leTimesLeft h (n y) q (n x) bounded)
78                   (A.eqTransLe
79                      (n x * q)
80                      (q * n x)
81                      (n room)
82                      (A.timesComm h (n x) q)
83                      (A.divTimesLe h (n room) (n x))))
84          refl
85
86let M = n maxWord
87let mulFitsSound
88    : forall (h : A.NatFacts) (x y : U256) -> mulFits x y === True -> n (Word/mul x y) === n x * n y
89    = \h x y ok ->
90        A.word256MulExact
91          h
92          x
93          y
94          (A.leLtTrans (n x * n y) M N (productBound h maxWord x y ok) (0, refl))
95
96-- If the sum wrapped, it is N + r with r < x (because y < N). Its
97-- observed word would be r, contradicting the actual comparison to x.
98let addFitsSound
99    : forall (h : A.NatFacts) (x y : U256)
100      -> atLeast (Word/add x y) x === True
101      -> n (Word/add x y) === n x + n y
102    = \h x y ok ->
103        let a = n x
104        let b = n y
105        let sum = a + b
106        let w = Word/add x y
107        in  merge
108              { Le =
109                  \(small : A.Le sum M) ->
110                    A.word256AddExact h x y (A.leLtTrans sum M N small (0, refl))
111              , Gt =
112                  \(wrap : A.Le N sum) ->
113                    let r = wrap.1
114                    let below
115                        : A.Lt r a
116                        = A.lePlusCancel
117                            (r + 1)
118                            a
119                            b
120                            (A.leTransEq
121                               (r + 1 + b)
122                               (r + N)
123                               sum
124                               (A.lePlus r r (b + 1) N (A.leRefl r) (A.word256Bound h y))
125                               (E.trans (A.plusComm r N) wrap.2))
126                    let rem
127                        : n w === r
128                        = (A.divModUnique
129                             h
130                             sum
131                             M
132                             1
133                             r
134                             wrap.2
135                             (A.leLtTrans r a N (A.leOfLt r a below) (A.word256Bound h x))).2
136                    in  A.leLtAbsurd a r (A.leTransEq a (n w) r (atLeastSound h w x ok) rem) below
137              }
138              (A.leTotal sum M)
139
140let observed
141    : forall (fits : Bool) (w result : U256) (exact : Natural)
142      -> (fits === True -> n w === exact)
143      -> Words.checked.2 U256 fits w === Some result
144      -> n result === exact
145    = \fits w result exact ->
146        Bool/elim
147          (\b ->
148             (b === True -> n w === exact)
149             -> Words.checked.2 U256 b w === Some result
150             -> n result === exact)
151          (\proof success ->
152             E.trans
153               (E.sym (E.cong (\(o : Optional U256) -> n (std:Optional.default w o)) success))
154               (proof refl))
155          (\_ success ->
156             A.falseNotTrue (E.cong (\(o : Optional U256) -> std:Optional.isSome o) success))
157          fits
158let checked = Words.arithmetic Words.checked 256
159let checkedAddExact
160    : forall (h : A.NatFacts) (x y result : U256)
161      -> checked.add x y === Some result
162      -> n result === n x + n y
163    = \h x y result ->
164        observed (atLeast (Word/add x y) x) (Word/add x y) result (n x + n y) (addFitsSound h x y)
165let checkedAddProductExact
166    : forall (h : A.NatFacts) (base x y result : U256)
167      -> checked.addProduct base x y === Some result
168      -> n result === n base + n x * n y
169    = \h base x y result ->
170        let a = n base
171        let room = Word/sub maxWord base
172        let p = n x * n y
173        let w = Word/mul x y
174        in  observed
175              (Words.productFits room x y)
176              (Word/add base w)
177              result
178              (a + p)
179              (\ok ->
180                 let bound = productBound h room x y ok
181                 let product =
182                       A.word256MulExact
183                         h
184                         x
185                         y
186                         (A.leLtTrans p (n room) N bound (A.word256Bound h room))
187                 let covered = A.leSuccCancel a M (A.word256Bound h base)
188                 let capacity
189                     : a + n room === M
190                     = E.trans
191                         (A.plusComm a (n room))
192                         (E.trans
193                            (E.cong
194                               (\(v : Natural) -> v + a)
195                               (A.word256SubExact h maxWord base covered))
196                            (A.subtractOfLe h a M covered))
197                 let small
198                     : A.Lt (a + p) N
199                     = A.leLtTrans
200                         (a + p)
201                         M
202                         N
203                         (A.leTransEq
204                            (a + p)
205                            (a + n room)
206                            M
207                            (A.lePlus a a p (n room) (A.leRefl a) bound)
208                            capacity)
209                         (0, refl)
210                 in  E.trans
211                       (A.word256AddExact
212                          h
213                          base
214                          w
215                          (E.subst (\(v : Natural) -> A.Lt (a + v) N) (E.sym product) small))
216                       (E.cong (\(v : Natural) -> a + v) product))
217
218in  { present
219    , wrapAdd
220    , checkedSub
221    , notTrue
222    , atLeastSound
223    , mulFitsSound
224    , checkedAddExact
225    , checkedAddProductExact
226    }
Your result
Complete file: lib/Word/Law.omg · with imports