std:Arith
lib/Arith.omg
lib/Arith.omg1339 lines
1{- Arithmetic without axioms.
2
3 The kernel computes its `Natural` builtins on literals, so `2 * 3 === 6`
4 holds by `refl`. On variables it only knows a few laws: `+` adds the
5 literal parts of sums, `0 + n`, `n + 0`, `0 * n`, `n * 1` and the like
6 are `n` or `0`, `Natural/subtract 0 n` is `n` and `Natural/isZero
7 (n + 1)` is `False`. What it cannot decide, such as `(n + 1) * m ===
8 n * m + m` or even `Natural/subtract n n === 0`, has to be proved.
9
10 The facts that proofs need and conversion cannot decide form the record
11 `NatFacts`: six defining equations of `*`, `Natural/subtract`,
12 `Natural/div` and `Natural/mod`. It is a hypothesis, never a kernel
13 constant: a theorem that needs arithmetic takes it as its first argument,
14
15 forall (h : std:Arith.NatFacts) -> ...
16
17 so the assumption is visible in the type of everything that rests on it.
18 Each fact is true of the kernel's builtins (the comment of each says
19 why), and is checked on literals below, at 0, at small numbers and
20 around 2^64, 2^256 and 2^512; the language tests check it on random
21 literals as well. The standard model satisfies the record, so it is
22 consistent; were one of its facts false, the theorems that assume it
23 would say nothing, but the logic would stay consistent (docs/evm.md,
24 sections 2.4 and 11.4, assumption A9).
25
26 Everything else is proved here from it, by induction: the laws of the
27 semiring, the order `Le` and its interplay with `+` and `*`,
28 cancellation, truncated subtraction, Euclidean division (existence and
29 uniqueness of quotient and remainder), the soundness of the Boolean
30 comparisons (`decideLe`, `eqRefl`, `equalSound`), and the bridges
31 between native `Word 256` values (and the numeric adapter
32 `std:EVM/Model.natural`) and the naturals: a word operation whose result fits in a word is the
33 operation on naturals. `std:Ring` proves polynomial identities by
34 reflection on top of this module.
35
36 The numbers in the statements are explicit arguments: the type checker
37 cannot find them from equations, because sums and products are not
38 data it can take apart.
39-}
40
41let E = std:Equal
42
43let W256 = std:EVM/Signature.natural
44
45{- The hypothesis: the defining equations of the builtins that conversion
46 does not decide on variables. Each holds of the kernel's builtins,
47 which compute on literals with Haskell's `Natural`
48 (`core/Omegyu/Core/Eval.hs`, `reduce`).
49
50 The two sides of each equation are written once, as functions of the
51 numbers it is about: the record `NatFacts` says that they are equal,
52 and the asserts after it compute them on literals. So the asserts
53 check what the record says, and a false fact does not load.
54-}
55let sides =
56 { -- `Natural/times` is the exact product of naturals, and
57 -- (n + 1) * m = n * m + m is the distributive law of the naturals.
58 timesSucc =
59 { left = \(n m : Natural) -> (n + 1) * m, right = \(n m : Natural) -> n * m + m }
60 , -- `Natural/subtract a b` is b - a, or 0 when a > b: if b >= a both
61 -- sides are b - a, and otherwise both are 0.
62 subtractSucc =
63 { left = \(a b : Natural) -> Natural/subtract (a + 1) (b + 1)
64 , right = \(a b : Natural) -> Natural/subtract a b
65 }
66 , -- A quotient by 0 is 0: the kernel's rule for a divisor of 0.
67 divZero = { left = \(n : Natural) -> Natural/div n 0, right = \(n : Natural) -> 0 }
68 , -- A remainder by 0 is the number itself: the kernel's rule for a
69 -- divisor of 0.
70 modZero = { left = \(n : Natural) -> Natural/mod n 0, right = \(n : Natural) -> n }
71 , -- For a positive divisor, `Natural/div` and `Natural/mod` are the
72 -- quotient and remainder of Euclidean division (Haskell's `div` and
73 -- `mod`, which satisfy (n `div` d) * d + n `mod` d = n).
74 divMod =
75 { left = \(n d : Natural) -> Natural/div n (d + 1) * (d + 1) + Natural/mod n (d + 1)
76 , right = \(n d : Natural) -> n
77 }
78 , -- ... and the remainder is less than the divisor: written with the
79 -- builtins, (n mod (d + 1)) + 1 - (d + 1) is 0, that is
80 -- n mod (d + 1) < d + 1.
81 modLess =
82 { left =
83 \(n d : Natural) ->
84 Natural/isZero (Natural/subtract (d + 1) (Natural/mod n (d + 1) + 1))
85 , right = \(n d : Natural) -> True
86 }
87 }
88
89let NatFacts =
90 { timesSucc : forall (n m : Natural) -> sides.timesSucc.left n m === sides.timesSucc.right n m
91 , subtractSucc :
92 forall (a b : Natural) -> sides.subtractSucc.left a b === sides.subtractSucc.right a b
93 , divZero : forall (n : Natural) -> sides.divZero.left n === sides.divZero.right n
94 , modZero : forall (n : Natural) -> sides.modZero.left n === sides.modZero.right n
95 , divMod : forall (n d : Natural) -> sides.divMod.left n d === sides.divMod.right n d
96 , modLess : forall (n d : Natural) -> sides.modLess.left n d === sides.modLess.right n d
97 }
98
99{- Each fact, checked on literals: the two sides computed by the kernel for
100 every sample (and every pair of samples, for the facts of two numbers)
101 are the same. The samples are 0, small numbers and the numbers around
102 2^64, 2^256 and 2^512, where a computation on machine words would wrap.
103 Loading this module checks them.
104-}
105let samples
106 : List Natural
107 = [ 0
108 , 1
109 , 2
110 , 3
111 , 255
112 , 18446744073709551615
113 , 18446744073709551616
114 , W256.M
115 , W256.N
116 , W256.N + 1
117 , W256.N * W256.N + W256.M
118 ]
119
120let Pair = { n : Natural, m : Natural }
121
122let pairs
123 : List Pair
124 = std:List.concatMap
125 (\(n : Natural) -> std:List.map (\(m : Natural) -> { n, m }) samples)
126 samples
127
128let onPairs
129 : forall {r : Type} -> (Natural -> Natural -> r) -> List r
130 = \{r} f -> std:List.map (\(p : Pair) -> f p.n p.m) pairs
131
132let onSamples = \(f : Natural -> Natural) -> std:List.map f samples
133
134let timesSuccHolds = assert : onPairs sides.timesSucc.left === onPairs sides.timesSucc.right
135
136let subtractSuccHolds =
137 assert : onPairs sides.subtractSucc.left === onPairs sides.subtractSucc.right
138
139let divZeroHolds = assert : onSamples sides.divZero.left === onSamples sides.divZero.right
140
141let modZeroHolds = assert : onSamples sides.modZero.left === onSamples sides.modZero.right
142
143let divModHolds = assert : onPairs sides.divMod.left === onPairs sides.divMod.right
144
145let modLessHolds = assert : onPairs sides.modLess.left === onPairs sides.modLess.right
146
147-- Logic ------------------------------------------------------------------------
148
149-- From a falsity, anything.
150let exFalso : forall {l : Level} {a : Type l} -> <> -> a = \{l} {a} x -> absurd (\(_ : <>) -> a) x
151
152-- `False` is not `True`, nor a successor 0.
153let falseNotTrue
154 : forall {l : Level} {a : Type l} -> False === True -> a
155 = \{l} {a} p -> exFalso (E.subst (\(b : Bool) -> if b then <> else unit) p {=})
156
157let succNotZero
158 : forall {l : Level} {a : Type l} (n : Natural) -> n + 1 === 0 -> a
159 = \{l} {a} n p ->
160 exFalso (E.subst (\(m : Natural) -> if Natural/isZero m then <> else unit) p {=})
161
162-- Case analysis on a Boolean that remembers which case holds: how a proof
163-- splits on a test whose outcome it does not know.
164let boolCases
165 : forall {l : Level} (P : Bool -> Type l) (b : Bool)
166 -> (b === True -> P True)
167 -> (b === False -> P False)
168 -> P b
169 = \{l} P b t f -> Bool/elim (\(c : Bool) -> b === c -> P c) t f b refl
170
171-- Case analysis on a natural: zero or a successor.
172let natCases
173 : forall {l : Level} (P : Natural -> Type l)
174 -> P 0
175 -> (forall (n : Natural) -> P (n + 1))
176 -> forall (n : Natural)
177 -> P n
178 = \{l} P z s n -> Natural/elim P z (\(k : Natural) (_ : P k) -> s k) n
179
180let andLeft
181 : forall (x y : Bool) -> x && y === True -> x === True
182 = \x y ->
183 Bool/elim
184 (\(c : Bool) -> c && y === True -> c === True)
185 (\(_ : y === True) -> refl)
186 (\(p : False === True) -> p)
187 x
188
189let andRight
190 : forall (x y : Bool) -> x && y === True -> y === True
191 = \x y ->
192 Bool/elim
193 (\(c : Bool) -> c && y === True -> y === True)
194 (\(p : y === True) -> p)
195 (\(p : False === True) -> falseNotTrue p)
196 x
197
198let isZeroSound
199 : forall (n : Natural) -> Natural/isZero n === True -> n === 0
200 = natCases
201 (\(n : Natural) -> Natural/isZero n === True -> n === 0)
202 (\(_ : True === True) -> refl)
203 (\(n : Natural) (p : False === True) -> falseNotTrue p)
204
205-- Addition: no hypothesis needed --------------------------------------------------
206
207let succInjective
208 : forall (a b : Natural) -> a + 1 === b + 1 -> a === b
209 = \a b p -> E.cong (\(n : Natural) -> Natural/subtract 1 n) p
210
211let plusAssoc
212 : forall (a b c : Natural) -> a + b + c === a + (b + c)
213 = \a b c ->
214 Natural/elim
215 (\(k : Natural) -> a + b + k === a + (b + k))
216 refl
217 (\(k : Natural) (ih : a + b + k === a + (b + k)) -> E.cong (\(n : Natural) -> n + 1) ih)
218 c
219
220let plusComm
221 : forall (a b : Natural) -> a + b === b + a
222 = \a b ->
223 Natural/elim
224 (\(k : Natural) -> k + b === b + k)
225 refl
226 (\(k : Natural) (ih : k + b === b + k) -> E.cong (\(n : Natural) -> n + 1) ih)
227 a
228
229-- (a + b) + c = (a + c) + b
230let plusSwapRight
231 : forall (a b c : Natural) -> a + b + c === a + c + b
232 = \a b c ->
233 E.trans
234 (plusAssoc a b c)
235 (E.trans (E.cong (\(x : Natural) -> a + x) (plusComm b c)) (E.sym (plusAssoc a c b)))
236
237-- (a + b) + (c + d) = (a + c) + (b + d)
238let plusShuffle
239 : forall (a b c d : Natural) -> a + b + (c + d) === a + c + (b + d)
240 = \a b c d ->
241 E.trans
242 (plusAssoc a b (c + d))
243 (E.trans
244 (E.cong (\(x : Natural) -> a + x) (E.sym (plusAssoc b c d)))
245 (E.trans
246 (E.cong (\(x : Natural) -> a + (x + d)) (plusComm b c))
247 (E.trans
248 (E.cong (\(x : Natural) -> a + x) (plusAssoc c b d))
249 (E.sym (plusAssoc a c (b + d))))))
250
251let plusCancelRight
252 : forall (a b c : Natural) -> a + c === b + c -> a === b
253 = \a b c ->
254 Natural/elim
255 (\(k : Natural) -> a + k === b + k -> a === b)
256 (\(p : a === b) -> p)
257 (\(k : Natural) (ih : a + k === b + k -> a === b) (p : a + k + 1 === b + k + 1) ->
258 ih (succInjective (a + k) (b + k) p))
259 c
260
261let plusCancelLeft
262 : forall (a b c : Natural) -> c + a === c + b -> a === b
263 = \a b c p -> plusCancelRight a b c (E.trans (plusComm a c) (E.trans p (plusComm c b)))
264
265let plusEqZero
266 : forall (a b : Natural) -> a + b === 0 -> a === 0
267 = \a ->
268 natCases
269 (\(k : Natural) -> a + k === 0 -> a === 0)
270 (\(p : a === 0) -> p)
271 (\(k : Natural) (p : a + k + 1 === 0) -> succNotZero (a + k) p)
272
273-- Multiplication ---------------------------------------------------------------------
274
275let timesSuccRight
276 : NatFacts -> forall (n m : Natural) -> n * (m + 1) === n * m + n
277 = \h n m ->
278 Natural/elim
279 (\(k : Natural) -> k * (m + 1) === k * m + k)
280 refl
281 (\(k : Natural) (ih : k * (m + 1) === k * m + k) ->
282 E.trans
283 (h.timesSucc k (m + 1))
284 (E.trans
285 (E.cong (\(x : Natural) -> x + (m + 1)) ih)
286 (E.trans
287 (E.cong (\(x : Natural) -> x + 1) (plusSwapRight (k * m) k m))
288 (E.cong (\(x : Natural) -> x + (k + 1)) (E.sym (h.timesSucc k m))))))
289 n
290
291let timesComm
292 : NatFacts -> forall (n m : Natural) -> n * m === m * n
293 = \h n m ->
294 Natural/elim
295 (\(k : Natural) -> k * m === m * k)
296 refl
297 (\(k : Natural) (ih : k * m === m * k) ->
298 E.trans
299 (h.timesSucc k m)
300 (E.trans (E.cong (\(x : Natural) -> x + m) ih) (E.sym (timesSuccRight h m k))))
301 n
302
303let distribRight
304 : NatFacts -> forall (a b c : Natural) -> (a + b) * c === a * c + b * c
305 = \h a b c ->
306 Natural/elim
307 (\(k : Natural) -> (a + k) * c === a * c + k * c)
308 refl
309 (\(k : Natural) (ih : (a + k) * c === a * c + k * c) ->
310 E.trans
311 (h.timesSucc (a + k) c)
312 (E.trans
313 (E.cong (\(x : Natural) -> x + c) ih)
314 (E.trans
315 (plusAssoc (a * c) (k * c) c)
316 (E.cong (\(x : Natural) -> a * c + x) (E.sym (h.timesSucc k c))))))
317 b
318
319let distribLeft
320 : NatFacts -> forall (a b c : Natural) -> a * (b + c) === a * b + a * c
321 = \h a b c ->
322 E.trans
323 (timesComm h a (b + c))
324 (E.trans
325 (distribRight h b c a)
326 (E.trans
327 (E.cong (\(x : Natural) -> x + c * a) (timesComm h b a))
328 (E.cong (\(x : Natural) -> a * b + x) (timesComm h c a))))
329
330let timesAssoc
331 : NatFacts -> forall (a b c : Natural) -> a * b * c === a * (b * c)
332 = \h a b c ->
333 Natural/elim
334 (\(k : Natural) -> k * b * c === k * (b * c))
335 refl
336 (\(k : Natural) (ih : k * b * c === k * (b * c)) ->
337 E.trans
338 (E.cong (\(x : Natural) -> x * c) (h.timesSucc k b))
339 (E.trans
340 (distribRight h (k * b) b c)
341 (E.trans
342 (E.cong (\(x : Natural) -> x + b * c) ih)
343 (E.sym (h.timesSucc k (b * c))))))
344 a
345
346-- a * (b * c) = b * (a * c)
347let timesSwapLeft
348 : NatFacts -> forall (a b c : Natural) -> a * (b * c) === b * (a * c)
349 = \h a b c ->
350 E.trans
351 (E.sym (timesAssoc h a b c))
352 (E.trans (E.cong (\(x : Natural) -> x * c) (timesComm h a b)) (timesAssoc h b a c))
353
354-- Order -------------------------------------------------------------------------------
355
356-- a <= b: some d makes up the difference.
357let Le : Natural -> Natural -> Type = \a b -> exists (d : Natural) * a + d === b
358
359-- a < b
360let Lt : Natural -> Natural -> Type = \a b -> Le (a + 1) b
361
362{- Taking an inequality apart: its difference and the equation, given to a
363 function as variables. The difference is computed by the proof of the
364 inequality; projected with `.1` into a type that the type checker
365 compares, it would make it run that proof (lemmas proved by induction,
366 applied to large literals, run for as long), where `leElim` keeps it a
367 variable.
368-}
369let leElim
370 : forall {l : Level} {r : Type l} (a b : Natural)
371 -> Le a b
372 -> (forall (d : Natural) -> a + d === b -> r)
373 -> r
374 = \{l} {r} a b p k -> k p.1 p.2
375
376let leRefl : forall (a : Natural) -> Le a a = \a -> (0, refl)
377
378let leOfEq : forall (a b : Natural) -> a === b -> Le a b = \a b p -> (0, p)
379
380let leTrans
381 : forall (a b c : Natural) -> Le a b -> Le b c -> Le a c
382 = \a b c p q ->
383 let sum
384 : a + (p.1 + q.1) === c
385 = E.trans
386 (E.sym (plusAssoc a p.1 q.1))
387 (E.trans (E.cong (\(x : Natural) -> x + q.1) p.2) q.2)
388
389 in (p.1 + q.1, sum)
390
391-- Rewriting either side of an inequality.
392let leTransEq
393 : forall (a b c : Natural) -> Le a b -> b === c -> Le a c
394 = \a b c p q -> (p.1, E.trans p.2 q)
395
396let eqTransLe
397 : forall (a b c : Natural) -> a === b -> Le b c -> Le a c
398 = \a b c p q -> (q.1, E.trans (E.cong (\(x : Natural) -> x + q.1) p) q.2)
399
400let leZero : forall (a : Natural) -> Le 0 a = \a -> (a, refl)
401
402let lePlusRight : forall (a b : Natural) -> Le a (a + b) = \a b -> (b, refl)
403
404let lePlusLeft : forall (a b : Natural) -> Le b (a + b) = \a b -> (a, plusComm b a)
405
406let leSucc : forall (a : Natural) -> Le a (a + 1) = \a -> (1, refl)
407
408let leSuccSucc
409 : forall (a b : Natural) -> Le a b -> Le (a + 1) (b + 1)
410 = \a b p -> (p.1, E.cong (\(x : Natural) -> x + 1) p.2)
411
412let leSuccCancel
413 : forall (a b : Natural) -> Le (a + 1) (b + 1) -> Le a b
414 = \a b p -> (p.1, succInjective (a + p.1) b p.2)
415
416let leOfLt : forall (a b : Natural) -> Lt a b -> Le a b = \a b p -> leTrans a (a + 1) b (leSucc a) p
417
418let leLtTrans
419 : forall (a b c : Natural) -> Le a b -> Lt b c -> Lt a c
420 = \a b c p q -> leTrans (a + 1) (b + 1) c (leSuccSucc a b p) q
421
422let ltLeTrans
423 : forall (a b c : Natural) -> Lt a b -> Le b c -> Lt a c
424 = \a b c p q -> leTrans (a + 1) b c p q
425
426let lePlus
427 : forall (a b c d : Natural) -> Le a b -> Le c d -> Le (a + c) (b + d)
428 = \a b c d p q ->
429 let sum
430 : a + c + (p.1 + q.1) === b + d
431 = E.trans
432 (plusShuffle a c p.1 q.1)
433 (E.trans
434 (E.cong (\(x : Natural) -> x + (c + q.1)) p.2)
435 (E.cong (\(x : Natural) -> b + x) q.2))
436
437 in (p.1 + q.1, sum)
438
439let lePlusCancel
440 : forall (a b c : Natural) -> Le (a + c) (b + c) -> Le a b
441 = \a b c p -> (p.1, plusCancelRight (a + p.1) b c (E.trans (plusSwapRight a p.1 c) p.2))
442
443let leAntisym
444 : forall (a b : Natural) -> Le a b -> Le b a -> a === b
445 = \a b p q ->
446 let back
447 : a + (p.1 + q.1) === a + 0
448 = E.trans
449 (E.sym (plusAssoc a p.1 q.1))
450 (E.trans (E.cong (\(x : Natural) -> x + q.1) p.2) q.2)
451
452 let none : p.1 === 0 = plusEqZero p.1 q.1 (plusCancelLeft (p.1 + q.1) 0 a back)
453
454 in E.trans (E.cong (\(x : Natural) -> a + x) (E.sym none)) p.2
455
456let leZeroEq : forall (a : Natural) -> Le a 0 -> a === 0 = \a p -> plusEqZero a p.1 p.2
457
458let ltIrrefl
459 : forall (a : Natural) -> Lt a a -> <>
460 = \a p -> succNotZero p.1 (plusCancelLeft (p.1 + 1) 0 a p.2)
461
462let leLtAbsurd
463 : forall {l : Level} {r : Type l} (a b : Natural) -> Le a b -> Lt b a -> r
464 = \{l} {r} a b p q -> exFalso (ltIrrefl b (leTrans (b + 1) a b q p))
465
466-- The order is total: either a <= b or b < a ...
467let LeOrGt = \(a : Natural) (b : Natural) -> < Le : Le a b | Gt : Lt b a >
468
469let leTotal
470 : forall (a b : Natural) -> LeOrGt a b
471 = \a ->
472 Natural/elim
473 (\(j : Natural) -> forall (b : Natural) -> LeOrGt j b)
474 (\(b : Natural) -> (LeOrGt 0 b).Le (leZero b))
475 (\(j : Natural) (ih : forall (b : Natural) -> LeOrGt j b) ->
476 natCases
477 (\(k : Natural) -> LeOrGt (j + 1) k)
478 ((LeOrGt (j + 1) 0).Gt (j, refl))
479 (\(k : Natural) ->
480 merge
481 { Le = \(p : Le j k) -> (LeOrGt (j + 1) (k + 1)).Le (leSuccSucc j k p)
482 , Gt = \(p : Lt k j) -> (LeOrGt (j + 1) (k + 1)).Gt (leSuccSucc (k + 1) j p)
483 }
484 (ih k)))
485 a
486
487-- ... and a < b, a = b or b < a.
488let Compare = \(a : Natural) (b : Natural) -> < Lt : Lt a b | Eq : a === b | Gt : Lt b a >
489
490let compare
491 : forall (a b : Natural) -> Compare a b
492 = \a b ->
493 merge
494 { Le =
495 \(p : Le a b) ->
496 merge
497 { Le = \(q : Le b a) -> (Compare a b).Eq (leAntisym a b p q)
498 , Gt = \(q : Lt a b) -> (Compare a b).Lt q
499 }
500 (leTotal b a)
501 , Gt = \(p : Lt b a) -> (Compare a b).Gt p
502 }
503 (leTotal a b)
504
505-- Strong induction: a property that holds of n whenever it holds of the
506-- numbers below n holds of every number.
507let ltInduction
508 : forall {l : Level} (P : Natural -> Type l)
509 -> (forall (n : Natural) -> (forall (m : Natural) -> Lt m n -> P m) -> P n)
510 -> forall (n : Natural)
511 -> P n
512 = \{l} P step n ->
513 let Below = \(k : Natural) -> forall (m : Natural) -> Lt m k -> P m
514
515 let below
516 : forall (k : Natural) -> Below k
517 = Natural/elim
518 Below
519 (\(m : Natural) (p : Lt m 0) -> succNotZero (m + p.1) p.2)
520 (\(k : Natural) (ih : Below k) (m : Natural) (p : Lt m (k + 1)) ->
521 merge
522 { Lt = \(q : Lt m k) -> ih m q
523 , Eq = \(q : m === k) -> E.subst P (E.sym q) (step k ih)
524 , Gt = \(q : Lt k m) -> leLtAbsurd m k (leSuccCancel m k p) q
525 }
526 (compare m k))
527
528 in step n (below n)
529
530let leTimesLeft
531 : NatFacts -> forall (a b c : Natural) -> Le a b -> Le (c * a) (c * b)
532 = \h a b c p ->
533 (c * p.1, E.trans (E.sym (distribLeft h c a p.1)) (E.cong (\(x : Natural) -> c * x) p.2))
534
535let leTimesRight
536 : NatFacts -> forall (a b c : Natural) -> Le a b -> Le (a * c) (b * c)
537 = \h a b c p ->
538 (p.1 * c, E.trans (E.sym (distribRight h a p.1 c)) (E.cong (\(x : Natural) -> x * c) p.2))
539
540let leTimes
541 : NatFacts -> forall (a b c d : Natural) -> Le a b -> Le c d -> Le (a * c) (b * d)
542 = \h a b c d p q ->
543 leTrans (a * c) (b * c) (b * d) (leTimesRight h a b c p) (leTimesLeft h c d b q)
544
545-- Multiplying by a positive number keeps a strict inequality.
546let ltTimesRight
547 : NatFacts -> forall (a b c : Natural) -> Lt a b -> Lt (a * (c + 1)) (b * (c + 1))
548 = \h a b c p ->
549 leTrans
550 (a * (c + 1) + 1)
551 (a * (c + 1) + (c + 1))
552 (b * (c + 1))
553 (c, refl)
554 (eqTransLe
555 (a * (c + 1) + (c + 1))
556 ((a + 1) * (c + 1))
557 (b * (c + 1))
558 (E.sym (h.timesSucc a (c + 1)))
559 (leTimesRight h (a + 1) b (c + 1) p))
560
561-- A positive factor cancels.
562let leTimesCancel
563 : NatFacts -> forall (a b c : Natural) -> Le (a * (c + 1)) (b * (c + 1)) -> Le a b
564 = \h a b c p ->
565 merge
566 { Le = \(q : Le a b) -> q
567 , Gt = \(q : Lt b a) -> leLtAbsurd (a * (c + 1)) (b * (c + 1)) p (ltTimesRight h b a c q)
568 }
569 (leTotal a b)
570
571let timesCancel
572 : NatFacts -> forall (a b c : Natural) -> a * (c + 1) === b * (c + 1) -> a === b
573 = \h a b c p ->
574 leAntisym
575 a
576 b
577 (leTimesCancel h a b c (leOfEq (a * (c + 1)) (b * (c + 1)) p))
578 (leTimesCancel h b a c (leOfEq (b * (c + 1)) (a * (c + 1)) (E.sym p)))
579
580-- Truncated subtraction: `Natural/subtract a b` is b - a, or 0 -------------------------
581
582-- a - (a + d) = 0
583let subtractZero
584 : NatFacts -> forall (a d : Natural) -> Natural/subtract (a + d) a === 0
585 = \h a d ->
586 Natural/elim
587 (\(k : Natural) -> Natural/subtract (k + d) k === 0)
588 refl
589 (\(k : Natural) (ih : Natural/subtract (k + d) k === 0) ->
590 E.trans (h.subtractSucc (k + d) k) ih)
591 a
592
593-- (a + d) - a = d
594let subtractPlus
595 : NatFacts -> forall (a d : Natural) -> Natural/subtract a (a + d) === d
596 = \h a d ->
597 Natural/elim
598 (\(k : Natural) -> Natural/subtract k (k + d) === d)
599 refl
600 (\(k : Natural) (ih : Natural/subtract k (k + d) === d) ->
601 E.trans (h.subtractSucc k (k + d)) ih)
602 a
603
604let subtractSelf
605 : NatFacts -> forall (n : Natural) -> Natural/subtract n n === 0
606 = \h n -> subtractZero h n 0
607
608-- (b - a) + a = b when a <= b.
609let subtractOfLe
610 : NatFacts -> forall (a b : Natural) -> Le a b -> Natural/subtract a b + a === b
611 = \h a b p ->
612 E.subst
613 (\(x : Natural) -> Natural/subtract a x + a === x)
614 p.2
615 (E.trans (E.cong (\(x : Natural) -> x + a) (subtractPlus h a p.1)) (plusComm p.1 a))
616
617-- b - a = 0 when b <= a.
618let subtractOfGe
619 : NatFacts -> forall (a b : Natural) -> Le b a -> Natural/subtract a b === 0
620 = \h a b p -> E.subst (\(x : Natural) -> Natural/subtract x b === 0) p.2 (subtractZero h b p.1)
621
622-- Comparisons as Booleans --------------------------------------------------------------
623
624{- The Boolean `a <= b` of `std:Natural`, which the kernel computes on
625 literals: a fact about literals such as `997 * 2^112 < 2^122` is
626 `decideLeSound h a b refl`, whatever the size of the numbers.
627-}
628let decideLe : Natural -> Natural -> Bool = std:Natural.lessThanEqual
629
630let decideLeSound
631 : NatFacts -> forall (a b : Natural) -> decideLe a b === True -> Le a b
632 = \h a ->
633 Natural/elim
634 (\(j : Natural) -> forall (b : Natural) -> decideLe j b === True -> Le j b)
635 (\(b : Natural) (_ : decideLe 0 b === True) -> leZero b)
636 (\(j : Natural) (ih : forall (b : Natural) -> decideLe j b === True -> Le j b) ->
637 natCases
638 (\(k : Natural) -> decideLe (j + 1) k === True -> Le (j + 1) k)
639 (\(p : False === True) -> falseNotTrue p)
640 (\(k : Natural) (p : decideLe (j + 1) (k + 1) === True) ->
641 leSuccSucc
642 j
643 k
644 (ih k (E.trans (E.cong Natural/isZero (E.sym (h.subtractSucc k j))) p))))
645 a
646
647let decideLeComplete
648 : NatFacts -> forall (a b : Natural) -> Le a b -> decideLe a b === True
649 = \h a b p -> E.cong Natural/isZero (subtractOfGe h b a p)
650
651let decideLeFalse
652 : NatFacts -> forall (a b : Natural) -> decideLe a b === False -> Lt b a
653 = \h a b p ->
654 merge
655 { Le = \(q : Le a b) -> falseNotTrue (E.trans (E.sym p) (decideLeComplete h a b q))
656 , Gt = \(q : Lt b a) -> q
657 }
658 (leTotal a b)
659
660let decideLtSound
661 : NatFacts -> forall (a b : Natural) -> std:Natural.lessThan a b === True -> Lt a b
662 = \h a b p -> decideLeSound h (a + 1) b p
663
664let decideLtComplete
665 : NatFacts -> forall (a b : Natural) -> Lt a b -> std:Natural.lessThan a b === True
666 = \h a b p -> decideLeComplete h (a + 1) b p
667
668-- Every number is equal to itself, for the Boolean equality of
669-- `std:Natural` (and of the words, `std:EVM/Model.natural.eq`, the same
670-- function): conversion alone does not see it of a variable.
671let eqRefl
672 : NatFacts -> forall (n : Natural) -> std:Natural.equal n n === True
673 = \h n -> E.cong (\(x : Natural) -> Natural/isZero x && Natural/isZero x) (subtractSelf h n)
674
675let equalSound
676 : NatFacts -> forall (a b : Natural) -> std:Natural.equal a b === True -> a === b
677 = \h a b p ->
678 leAntisym
679 a
680 b
681 (decideLeSound h a b (andLeft (decideLe a b) (decideLe b a) p))
682 (decideLeSound h b a (andRight (decideLe a b) (decideLe b a) p))
683
684let equalComplete
685 : NatFacts -> forall (a b : Natural) -> a === b -> std:Natural.equal a b === True
686 = \h a b p -> E.subst (\(x : Natural) -> std:Natural.equal a x === True) p (eqRefl h a)
687
688-- Euclidean division ---------------------------------------------------------------------
689
690-- The remainder is less than the divisor.
691let modLt
692 : NatFacts -> forall (n d : Natural) -> Lt (Natural/mod n (d + 1)) (d + 1)
693 = \h n d -> decideLeSound h (Natural/mod n (d + 1) + 1) (d + 1) (h.modLess n d)
694
695-- With a remainder below the divisor, a larger quotient makes a larger
696-- number.
697let euclidLt
698 : NatFacts
699 -> forall (d q1 r1 q2 r2 : Natural)
700 -> Lt r1 (d + 1)
701 -> Lt q1 q2
702 -> Lt (q1 * (d + 1) + r1) (q2 * (d + 1) + r2)
703 = \h d q1 r1 q2 r2 small larger ->
704 let base = q1 * (d + 1)
705
706 in leTrans
707 (base + r1 + 1)
708 (base + (d + 1))
709 (q2 * (d + 1) + r2)
710 (lePlus base base (r1 + 1) (d + 1) (leRefl base) small)
711 (leTrans
712 (base + (d + 1))
713 (q2 * (d + 1))
714 (q2 * (d + 1) + r2)
715 (eqTransLe
716 (base + (d + 1))
717 ((q1 + 1) * (d + 1))
718 (q2 * (d + 1))
719 (E.sym (h.timesSucc q1 (d + 1)))
720 (leTimesRight h (q1 + 1) q2 (d + 1) larger))
721 (lePlusRight (q2 * (d + 1)) r2))
722
723-- Quotient and remainder are unique.
724let euclidUnique
725 : NatFacts
726 -> forall (d q1 r1 q2 r2 : Natural)
727 -> Lt r1 (d + 1)
728 -> Lt r2 (d + 1)
729 -> q1 * (d + 1) + r1 === q2 * (d + 1) + r2
730 -> (q1 === q2) * (r1 === r2)
731 = \h d q1 r1 q2 r2 small1 small2 same ->
732 merge
733 { Lt =
734 \(p : Lt q1 q2) ->
735 exFalso
736 (ltIrrefl
737 (q2 * (d + 1) + r2)
738 (eqTransLe
739 (q2 * (d + 1) + r2 + 1)
740 (q1 * (d + 1) + r1 + 1)
741 (q2 * (d + 1) + r2)
742 (E.cong (\(x : Natural) -> x + 1) (E.sym same))
743 (euclidLt h d q1 r1 q2 r2 small1 p)))
744 , Eq =
745 \(p : q1 === q2) ->
746 let rest
747 : r1 === r2
748 = plusCancelLeft
749 r1
750 r2
751 (q2 * (d + 1))
752 (E.trans (E.cong (\(x : Natural) -> x * (d + 1) + r1) (E.sym p)) same)
753
754 in (p, rest)
755 , Gt =
756 \(p : Lt q2 q1) ->
757 exFalso
758 (ltIrrefl
759 (q1 * (d + 1) + r1)
760 (eqTransLe
761 (q1 * (d + 1) + r1 + 1)
762 (q2 * (d + 1) + r2 + 1)
763 (q1 * (d + 1) + r1)
764 (E.cong (\(x : Natural) -> x + 1) same)
765 (euclidLt h d q2 r2 q1 r1 small2 p)))
766 }
767 (compare q1 q2)
768
769-- `Natural/div` and `Natural/mod` are the quotient and remainder of any
770-- such decomposition.
771let divModUnique
772 : NatFacts
773 -> forall (n d q r : Natural)
774 -> q * (d + 1) + r === n
775 -> Lt r (d + 1)
776 -> (Natural/div n (d + 1) === q) * (Natural/mod n (d + 1) === r)
777 = \h n d q r p small ->
778 euclidUnique
779 h
780 d
781 (Natural/div n (d + 1))
782 (Natural/mod n (d + 1))
783 q
784 r
785 (modLt h n d)
786 small
787 (E.trans (h.divMod n d) (E.sym p))
788
789let modOfLt
790 : NatFacts -> forall (n d : Natural) -> Lt n (d + 1) -> Natural/mod n (d + 1) === n
791 = \h n d p -> (divModUnique h n d 0 n refl p).2
792
793let divOfLt
794 : NatFacts -> forall (n d : Natural) -> Lt n (d + 1) -> Natural/div n (d + 1) === 0
795 = \h n d p -> (divModUnique h n d 0 n refl p).1
796
797-- The quotient rounds down: (n / d) * d <= n, for every d (0 included) ...
798let divTimesLe
799 : NatFacts -> forall (n d : Natural) -> Le (Natural/div n d * d) n
800 = \h n ->
801 natCases
802 (\(d : Natural) -> Le (Natural/div n d * d) n)
803 (leZero n)
804 (\(d : Natural) -> (Natural/mod n (d + 1), h.divMod n d))
805
806-- ... by less than the divisor: n < (n / d + 1) * d.
807let ltDivSucc
808 : NatFacts -> forall (n d : Natural) -> Lt n ((Natural/div n (d + 1) + 1) * (d + 1))
809 = \h n d ->
810 let q = Natural/div n (d + 1)
811
812 let r = Natural/mod n (d + 1)
813
814 in eqTransLe
815 (n + 1)
816 (q * (d + 1) + r + 1)
817 ((q + 1) * (d + 1))
818 (E.cong (\(x : Natural) -> x + 1) (E.sym (h.divMod n d)))
819 (leTransEq
820 (q * (d + 1) + r + 1)
821 (q * (d + 1) + (d + 1))
822 ((q + 1) * (d + 1))
823 (lePlus
824 (q * (d + 1))
825 (q * (d + 1))
826 (r + 1)
827 (d + 1)
828 (leRefl (q * (d + 1)))
829 (modLt h n d))
830 (E.sym (h.timesSucc q (d + 1))))
831
832-- Adding a multiple of the divisor adds to the quotient and keeps the
833-- remainder.
834let divModPlusTimes
835 : NatFacts
836 -> forall (n d q : Natural)
837 -> (Natural/div (n + q * (d + 1)) (d + 1) === Natural/div n (d + 1) + q)
838 * (Natural/mod (n + q * (d + 1)) (d + 1) === Natural/mod n (d + 1))
839 = \h n d q ->
840 let q0 = Natural/div n (d + 1)
841
842 let r0 = Natural/mod n (d + 1)
843
844 in divModUnique
845 h
846 (n + q * (d + 1))
847 d
848 (q0 + q)
849 r0
850 (E.trans
851 (E.cong (\(x : Natural) -> x + r0) (distribRight h q0 q (d + 1)))
852 (E.trans
853 (plusSwapRight (q0 * (d + 1)) (q * (d + 1)) r0)
854 (E.cong (\(x : Natural) -> x + q * (d + 1)) (h.divMod n d))))
855 (modLt h n d)
856
857let divTimes
858 : NatFacts -> forall (n d : Natural) -> Natural/div (n * (d + 1)) (d + 1) === n
859 = \h n d -> (divModUnique h (n * (d + 1)) d n 0 refl (d, refl)).1
860
861let modTimes
862 : NatFacts -> forall (n d : Natural) -> Natural/mod (n * (d + 1)) (d + 1) === 0
863 = \h n d -> (divModUnique h (n * (d + 1)) d n 0 refl (d, refl)).2
864
865{- Bridges from native words to naturals. The numeric adapter encodes
866 its operands as Word 256 and observes the native result. Statements
867 about the original naturals therefore require canonical input bounds.
868 Unsigned observations have kernel computation rules; arithmetic
869 consequences are derived from the existing NatFacts assumption.
870-}
871
872let litOfLt
873 : NatFacts -> forall (n : Natural) -> Lt n W256.N -> W256.lit n === n
874 = \h n p -> modOfLt h n W256.M p
875
876-- Every operation gives a word: its result is reduced modulo N.
877let litLt : NatFacts -> forall (n : Natural) -> Lt (W256.lit n) W256.N = \h n -> modLt h n W256.M
878
879let addBridge
880 : NatFacts -> forall (a b : Natural) -> Lt (a + b) W256.N -> W256.add a b === a + b
881 = \h a b p ->
882 let pa = litOfLt h a (leLtTrans a (a + b) W256.N (lePlusRight a b) p)
883 let pb = litOfLt h b (leLtTrans b (a + b) W256.N (lePlusLeft a b) p)
884 in E.trans
885 (E.cong (\(x : Natural) -> Natural/mod (x + W256.lit b) W256.N) pa)
886 (E.trans
887 (E.cong (\(y : Natural) -> Natural/mod (a + y) W256.N) pb)
888 (litOfLt h (a + b) p))
889
890let mulBounded
891 : NatFacts
892 -> forall (a b : Natural)
893 -> Lt a W256.N
894 -> Lt b W256.N
895 -> Lt (a * b) W256.N
896 -> W256.mul a b === a * b
897 = \h a b pa pb p ->
898 E.trans
899 (E.cong (\(x : Natural) -> Natural/mod (x * W256.lit b) W256.N) (litOfLt h a pa))
900 (E.trans
901 (E.cong (\(y : Natural) -> Natural/mod (a * y) W256.N) (litOfLt h b pb))
902 (litOfLt h (a * b) p))
903
904let mulBridge
905 : NatFacts -> forall (a b : Natural) -> Lt (a * b) W256.N -> W256.mul a b === a * b
906 = \h ->
907 natCases
908 (\(a : Natural) -> forall (b : Natural) -> Lt (a * b) W256.N -> W256.mul a b === a * b)
909 (\(b : Natural) (_ : Lt (0 * b) W256.N) -> refl)
910 (\(a : Natural) ->
911 natCases
912 (\(b : Natural) -> Lt ((a + 1) * b) W256.N -> W256.mul (a + 1) b === (a + 1) * b)
913 (\(_ : Lt ((a + 1) * 0) W256.N) -> refl)
914 (\(b : Natural) (p : Lt ((a + 1) * (b + 1)) W256.N) ->
915 mulBounded
916 h
917 (a + 1)
918 (b + 1)
919 (leLtTrans
920 (a + 1)
921 ((a + 1) * (b + 1))
922 W256.N
923 (leTimesLeft h 1 (b + 1) (a + 1) (b, refl))
924 p)
925 (leLtTrans
926 (b + 1)
927 ((a + 1) * (b + 1))
928 W256.N
929 (leTimesRight h 1 (a + 1) (b + 1) (a, refl))
930 p)
931 p))
932
933-- SUB of words a >= b is a - b: the word computes a + (N - b) modulo N.
934let subBridge
935 : NatFacts
936 -> forall (a b : Natural)
937 -> Lt a W256.N
938 -> Le b a
939 -> W256.sub a b === Natural/subtract b a
940 = \h a b word below ->
941 let c = Natural/subtract b a
942
943 let borrow = Natural/subtract b W256.N
944
945 let whole : c + b === a = subtractOfLe h b a below
946
947 let cWord
948 : Lt c W256.N
949 = leTrans (c + 1) (a + 1) W256.N (leSuccSucc c a (b, whole)) word
950
951 let bWord
952 : Le b W256.N
953 = leTrans b a W256.N below (leTrans a (a + 1) W256.N (leSucc a) word)
954
955 let wrapped
956 : a + borrow === c + W256.N
957 = E.trans
958 (E.cong (\(x : Natural) -> x + borrow) (E.sym whole))
959 (E.trans
960 (plusAssoc c b borrow)
961 (E.cong
962 (\(x : Natural) -> c + x)
963 (E.trans (plusComm b borrow) (subtractOfLe h b W256.N bWord))))
964
965 let canonical
966 : W256.sub a b === Natural/mod (a + borrow) W256.N
967 = E.trans
968 (E.cong
969 (\(x : Natural) -> Natural/mod (x + Natural/subtract (W256.lit b) W256.N) W256.N)
970 (litOfLt h a word))
971 (E.cong
972 (\(y : Natural) -> Natural/mod (a + Natural/subtract y W256.N) W256.N)
973 (litOfLt h b (leLtTrans b a W256.N below word)))
974 in E.trans
975 canonical
976 (E.trans
977 (E.cong (\(x : Natural) -> Natural/mod x W256.N) wrapped)
978 (divModUnique h (c + W256.N) W256.M 1 c (plusComm W256.N c) cWord).2)
979
980let divNatural
981 : NatFacts
982 -> forall (a b : Natural)
983 -> Lt a W256.N
984 -> Lt b W256.N
985 -> W256.div a b === Natural/div a b
986 = \h a b pa pb ->
987 E.trans
988 (E.cong (\(x : Natural) -> Natural/div x (W256.lit b)) (litOfLt h a pa))
989 (E.cong (Natural/div a) (litOfLt h b pb))
990
991let selectNatural
992 : NatFacts
993 -> forall (c : Bool) (a b : Natural)
994 -> Lt a W256.N
995 -> Lt b W256.N
996 -> W256.select c a b === (if c then a else b)
997 = \h c a b pa pb ->
998 boolCases
999 (\(flag : Bool) -> W256.select flag a b === (if flag then a else b))
1000 c
1001 (\(_ : c === True) -> litOfLt h a pa)
1002 (\(_ : c === False) -> litOfLt h b pb)
1003
1004let divBridge
1005 : forall (a b : Word 256)
1006 -> Word/toNatural (Word/div a b) === Natural/div (Word/toNatural a) (Word/toNatural b)
1007 = \a b -> refl
1008
1009let modBridge
1010 : forall (a b : Word 256)
1011 -> Word/toNatural (Word/mod a b)
1012 === (if Natural/isZero (Word/toNatural b)
1013 then 0
1014 else Natural/mod (Word/toNatural a) (Word/toNatural b))
1015 = \a b -> refl
1016
1017let addmodBridge
1018 : forall (a b n : Word 256)
1019 -> Word/toNatural (Word/addmod a b n)
1020 === (if Natural/isZero (Word/toNatural n)
1021 then 0
1022 else Natural/mod (Word/toNatural a + Word/toNatural b) (Word/toNatural n))
1023 = \a b n -> refl
1024
1025let mulmodBridge
1026 : forall (a b n : Word 256)
1027 -> Word/toNatural (Word/mulmod a b n)
1028 === (if Natural/isZero (Word/toNatural n)
1029 then 0
1030 else Natural/mod (Word/toNatural a * Word/toNatural b) (Word/toNatural n))
1031 = \a b n -> refl
1032
1033-- The comparisons of words are those of naturals.
1034let ltCanonical
1035 : NatFacts
1036 -> forall (a b : Natural)
1037 -> Lt a W256.N
1038 -> Lt b W256.N
1039 -> W256.lt a b === std:Natural.lessThan a b
1040 = \h a b pa pb ->
1041 E.trans
1042 (E.cong (\(x : Natural) -> std:Natural.lessThan x (W256.lit b)) (litOfLt h a pa))
1043 (E.cong (std:Natural.lessThan a) (litOfLt h b pb))
1044
1045let ltBridge
1046 : NatFacts
1047 -> forall (a b : Natural)
1048 -> Lt a W256.N
1049 -> Lt b W256.N
1050 -> W256.lt a b === True
1051 -> Lt a b
1052 = \h a b pa pb p -> decideLeSound h (a + 1) b (E.trans (E.sym (ltCanonical h a b pa pb)) p)
1053
1054let ltBridgeTrue
1055 : NatFacts
1056 -> forall (a b : Natural)
1057 -> Lt a W256.N
1058 -> Lt b W256.N
1059 -> Lt a b
1060 -> W256.lt a b === True
1061 = \h a b pa pb p -> E.trans (ltCanonical h a b pa pb) (decideLeComplete h (a + 1) b p)
1062
1063let ltBridgeFalse
1064 : NatFacts
1065 -> forall (a b : Natural)
1066 -> Lt a W256.N
1067 -> Lt b W256.N
1068 -> W256.lt a b === False
1069 -> Le b a
1070 = \h a b pa pb p ->
1071 leSuccCancel b a (decideLeFalse h (a + 1) b (E.trans (E.sym (ltCanonical h a b pa pb)) p))
1072
1073let eqBridge
1074 : NatFacts
1075 -> forall (a b : Natural)
1076 -> Lt a W256.N
1077 -> Lt b W256.N
1078 -> W256.eq a b === True
1079 -> a === b
1080 = \h a b pa pb p ->
1081 let canonical
1082 : W256.eq a b === std:Natural.equal a b
1083 = E.trans
1084 (E.cong (\(x : Natural) -> std:Natural.equal x (W256.lit b)) (litOfLt h a pa))
1085 (E.cong (std:Natural.equal a) (litOfLt h b pb))
1086 in equalSound h a b (E.trans (E.sym canonical) p)
1087
1088let isZeroBridge
1089 : NatFacts -> forall (a : Natural) -> Lt a W256.N -> W256.isZero a === True -> a === 0
1090 = \h a pa p -> isZeroSound a (E.trans (E.sym (E.cong Natural/isZero (litOfLt h a pa))) p)
1091
1092-- SHR and SHL by less than 256 bits divide and multiply by a power of 2.
1093let shrBridge
1094 : forall (s : Natural) (a : Word 256)
1095 -> std:Natural.lessThan s 256 === True
1096 -> Word/toNatural (Word/shr s a) === Natural/div (Word/toNatural a) (W256.pow2 s)
1097 = \s a p ->
1098 E.cong (\(c : Bool) -> if c then Natural/div (Word/toNatural a) (W256.pow2 s) else 0) p
1099
1100let shlBridge
1101 : NatFacts
1102 -> forall (s a : Natural)
1103 -> Lt s W256.N
1104 -> Lt a W256.N
1105 -> W256.lt s 256 === True
1106 -> Lt (a * W256.pow2 s) W256.N
1107 -> W256.shl s a === a * W256.pow2 s
1108 = \h s a ps pa p small ->
1109 E.trans
1110 (E.cong
1111 (\(k : Natural) ->
1112 if std:Natural.lessThan k 256 then W256.lit (W256.lit a * W256.pow2 k) else 0)
1113 (litOfLt h s ps))
1114 (E.trans
1115 (E.cong
1116 (\(x : Natural) ->
1117 if std:Natural.lessThan s 256 then W256.lit (x * W256.pow2 s) else 0)
1118 (litOfLt h a pa))
1119 (E.trans
1120 (E.cong
1121 (\(c : Bool) -> if c then W256.lit (a * W256.pow2 s) else 0)
1122 (E.trans (E.sym (ltCanonical h s 256 ps (Natural/subtract 256 W256.M, refl))) p))
1123 (litOfLt h (a * W256.pow2 s) small)))
1124
1125-- Native finite words: these proofs concern the kernel's Word 256 itself.
1126-- Elimination exposes a canonical natural representative; its constructor
1127-- and unsigned observations have defining equations in the kernel. The
1128-- arithmetic assumptions remain exactly NatFacts, as for the natural lemmas.
1129let Word256 = Word 256
1130
1131let word256RoundTrip
1132 : forall (w : Word256) -> Word/fromNatural 256 (Word/toNatural w) === w
1133 = Word/elim
1134 (\(w : Word256) -> Word/fromNatural 256 (Word/toNatural w) === w)
1135 (\(n : Natural) -> refl)
1136
1137let word256Bound
1138 : NatFacts -> forall (w : Word256) -> Lt (Word/toNatural w) W256.N
1139 = \h ->
1140 Word/elim
1141 (\(w : Word256) -> Lt (Word/toNatural w) W256.N)
1142 (\(n : Natural) -> modLt h n W256.M)
1143
1144let word256Ext
1145 : forall (a b : Word256) -> Word/toNatural a === Word/toNatural b -> a === b
1146 = \a b p ->
1147 E.trans
1148 (E.sym (word256RoundTrip a))
1149 (E.trans (E.cong (Word/fromNatural 256) p) (word256RoundTrip b))
1150
1151let word256AddExact
1152 : NatFacts
1153 -> forall (a b : Word256)
1154 -> Lt (Word/toNatural a + Word/toNatural b) W256.N
1155 -> Word/toNatural (Word/add a b) === Word/toNatural a + Word/toNatural b
1156 = \h a b p -> modOfLt h (Word/toNatural a + Word/toNatural b) W256.M p
1157
1158let word256MulExact
1159 : NatFacts
1160 -> forall (a b : Word256)
1161 -> Lt (Word/toNatural a * Word/toNatural b) W256.N
1162 -> Word/toNatural (Word/mul a b) === Word/toNatural a * Word/toNatural b
1163 = \h a b p -> modOfLt h (Word/toNatural a * Word/toNatural b) W256.M p
1164
1165let word256SubExact
1166 : NatFacts
1167 -> forall (a b : Word256)
1168 -> Le (Word/toNatural b) (Word/toNatural a)
1169 -> Word/toNatural (Word/sub a b) === Natural/subtract (Word/toNatural b) (Word/toNatural a)
1170 = \h a b below ->
1171 E.trans
1172 (E.sym
1173 (E.trans
1174 (E.cong
1175 (\(x : Word256) ->
1176 Word/toNatural (Word/sub x (Word/fromNatural 256 (Word/toNatural b))))
1177 (word256RoundTrip a))
1178 (E.cong (\(y : Word256) -> Word/toNatural (Word/sub a y)) (word256RoundTrip b))))
1179 (subBridge h (Word/toNatural a) (Word/toNatural b) (word256Bound h a) below)
1180
1181let word256Div
1182 : forall (a b : Word256)
1183 -> Word/toNatural (Word/div a b) === Natural/div (Word/toNatural a) (Word/toNatural b)
1184 = \a b -> refl
1185
1186-- A literal below the bound is itself, and SHL by less than 256 bits
1187-- multiplies by a power of two modulo the word's bound, as SHR divides.
1188let word256FromNatural
1189 : NatFacts
1190 -> forall (n : Natural)
1191 -> Lt n W256.N
1192 -> Word/toNatural (Word/fromNatural 256 n) === n
1193 = \h n p -> modOfLt h n W256.M p
1194
1195let word256Shl
1196 : forall (s : Natural) (a : Word256)
1197 -> std:Natural.lessThan s 256 === True
1198 -> Word/toNatural (Word/shl s a) === Natural/mod (Word/toNatural a * W256.pow2 s) W256.N
1199 = \s a p ->
1200 E.cong
1201 (\(c : Bool) -> if c then Natural/mod (Word/toNatural a * W256.pow2 s) W256.N else 0)
1202 p
1203
1204let word256LtSound
1205 : NatFacts
1206 -> forall (a b : Word256)
1207 -> Word/lt a b === True
1208 -> Lt (Word/toNatural a) (Word/toNatural b)
1209 = \h a b p -> decideLeSound h (Word/toNatural a + 1) (Word/toNatural b) p
1210
1211let word256LtFalse
1212 : NatFacts
1213 -> forall (a b : Word256)
1214 -> Word/lt a b === False
1215 -> Le (Word/toNatural b) (Word/toNatural a)
1216 = \h a b p ->
1217 leSuccCancel
1218 (Word/toNatural b)
1219 (Word/toNatural a)
1220 (decideLeFalse h (Word/toNatural a + 1) (Word/toNatural b) p)
1221
1222let word256EqSound
1223 : NatFacts -> forall (a b : Word256) -> Word/equal a b === True -> a === b
1224 = \h a b p -> word256Ext a b (equalSound h (Word/toNatural a) (Word/toNatural b) p)
1225
1226in { NatFacts
1227 , word256RoundTrip
1228 , word256Bound
1229 , word256Ext
1230 , word256AddExact
1231 , word256MulExact
1232 , word256SubExact
1233 , word256Div
1234 , word256FromNatural
1235 , word256Shl
1236 , word256LtSound
1237 , word256LtFalse
1238 , word256EqSound
1239 , exFalso
1240 , falseNotTrue
1241 , succNotZero
1242 , boolCases
1243 , natCases
1244 , andLeft
1245 , andRight
1246 , isZeroSound
1247 , succInjective
1248 , plusAssoc
1249 , plusComm
1250 , plusSwapRight
1251 , plusShuffle
1252 , plusCancelRight
1253 , plusCancelLeft
1254 , plusEqZero
1255 , timesSuccRight
1256 , timesComm
1257 , distribRight
1258 , distribLeft
1259 , timesAssoc
1260 , timesSwapLeft
1261 , Le
1262 , Lt
1263 , leElim
1264 , leRefl
1265 , leOfEq
1266 , leTrans
1267 , leTransEq
1268 , eqTransLe
1269 , leZero
1270 , lePlusRight
1271 , lePlusLeft
1272 , leSucc
1273 , leSuccSucc
1274 , leSuccCancel
1275 , leOfLt
1276 , leLtTrans
1277 , ltLeTrans
1278 , lePlus
1279 , lePlusCancel
1280 , leAntisym
1281 , leZeroEq
1282 , ltIrrefl
1283 , leLtAbsurd
1284 , LeOrGt
1285 , leTotal
1286 , Compare
1287 , compare
1288 , ltInduction
1289 , leTimesLeft
1290 , leTimesRight
1291 , leTimes
1292 , ltTimesRight
1293 , leTimesCancel
1294 , timesCancel
1295 , subtractZero
1296 , subtractPlus
1297 , subtractSelf
1298 , subtractOfLe
1299 , subtractOfGe
1300 , decideLe
1301 , decideLeSound
1302 , decideLeComplete
1303 , decideLeFalse
1304 , decideLtSound
1305 , decideLtComplete
1306 , eqRefl
1307 , equalSound
1308 , equalComplete
1309 , modLt
1310 , euclidLt
1311 , euclidUnique
1312 , divModUnique
1313 , modOfLt
1314 , divOfLt
1315 , divTimesLe
1316 , ltDivSucc
1317 , divModPlusTimes
1318 , divTimes
1319 , modTimes
1320 , litOfLt
1321 , litLt
1322 , addBridge
1323 , mulBridge
1324 , subBridge
1325 , divNatural
1326 , selectNatural
1327 , ltCanonical
1328 , divBridge
1329 , modBridge
1330 , addmodBridge
1331 , mulmodBridge
1332 , ltBridge
1333 , ltBridgeTrue
1334 , ltBridgeFalse
1335 , eqBridge
1336 , isZeroBridge
1337 , shrBridge
1338 , shlBridge
1339 }