# GENERATED by tools/generators/rw.py from ../../../tools/generators/secp256k1_hand/ineq.src; edit the .src import Base import ../../../spec/lib/common.bend as C import ../../lib/nat.bend as N import ../../lib/logic.bend as Lg import ../../lib/arith.bend as AR import ../../lib/lemmas/proofs/nat_algebra.bend as NA import ../../math/typed/width.bend as WW # Order facts on Nat used by the secp256k1 bound proofs (monotonicity of # +, * and C.shift), all as {... == True{} : Bool}. def le_subst_r(+a: Nat, +b: Nat, +c: Nat, +e: {b == c : Nat}, +h: {Nat.is_le(a, b) == True{} : Bool}) -> {Nat.is_le(a, c) == True{} : Bool}: Lg.subst(Nat, z => {Nat.is_le(a, z) == True{} : Bool}, b, c, e, h) def le_subst_l(+a: Nat, +b: Nat, +c: Nat, +e: {a == b : Nat}, +h: {Nat.is_le(a, c) == True{} : Bool}) -> {Nat.is_le(b, c) == True{} : Bool}: Lg.subst(Nat, z => {Nat.is_le(z, c) == True{} : Bool}, a, b, e, h) def lt_subst_r(+a: Nat, +b: Nat, +c: Nat, +e: {b == c : Nat}, +h: {Nat.is_lt(a, b) == True{} : Bool}) -> {Nat.is_lt(a, c) == True{} : Bool}: Lg.subst(Nat, z => {Nat.is_lt(a, z) == True{} : Bool}, b, c, e, h) def lt_subst_l(+a: Nat, +b: Nat, +c: Nat, +e: {a == b : Nat}, +h: {Nat.is_lt(a, c) == True{} : Bool}) -> {Nat.is_lt(b, c) == True{} : Bool}: Lg.subst(Nat, z => {Nat.is_lt(z, c) == True{} : Bool}, a, b, e, h) # b <= a + b def le_add_l(+a: Nat, +b: Nat) -> {Nat.is_le(b, Nat.add(a, b)) == True{} : Bool}: le_subst_r(b, Nat.add(b, a), Nat.add(a, b), NA.add_comm(b, a), N.le_add_right(b, a)) # a <= c, b <= d: a + b <= c + d def le_add2(+a: Nat, +b: Nat, +c: Nat, +d: Nat, +h1: {Nat.is_le(a, c) == True{} : Bool}, +h2: {Nat.is_le(b, d) == True{} : Bool}) -> {Nat.is_le(Nat.add(a, b), Nat.add(c, d)) == True{} : Bool}: N.le_trans(Nat.add(a, b), Nat.add(c, b), Nat.add(c, d), WW.le_add_r(a, c, b, h1), N.le_add_left(b, d, c, h2)) # a < c, b <= d: a + b < c + d def lt_le_add(+a: Nat, +b: Nat, +c: Nat, +d: Nat, +h1: {Nat.is_lt(a, c) == True{} : Bool}, +h2: {Nat.is_le(b, d) == True{} : Bool}) -> {Nat.is_lt(Nat.add(a, b), Nat.add(c, d)) == True{} : Bool}: N.lt_le_trans(Nat.add(a, b), Nat.add(c, b), Nat.add(c, d), N.lt_add_r2(a, c, b, h1), N.le_add_left(b, d, c, h2)) # a <= c, b < d: a + b < c + d def le_lt_add(+a: Nat, +b: Nat, +c: Nat, +d: Nat, +h1: {Nat.is_le(a, c) == True{} : Bool}, +h2: {Nat.is_lt(b, d) == True{} : Bool}) -> {Nat.is_lt(Nat.add(a, b), Nat.add(c, d)) == True{} : Bool}: N.le_lt_trans(Nat.add(a, b), Nat.add(c, b), Nat.add(c, d), WW.le_add_r(a, c, b, h1), N.lt_add_left(b, d, c, h2)) # b <= c: a b <= a c def le_mul_r(+a: Nat, +b: Nat, +c: Nat, +h: {Nat.is_le(b, c) == True{} : Bool}) -> {Nat.is_le(Nat.mul(a, b), Nat.mul(a, c)) == True{} : Bool}: %Equal.sym(Nat, Nat.mul(a, b), Nat.mul(b, a), NA.mul_comm(a, b)) : {Nat.is_le(_, Nat.mul(a, c)) == True{} : Bool} %Equal.sym(Nat, Nat.mul(a, c), Nat.mul(c, a), NA.mul_comm(a, c)) : {Nat.is_le(Nat.mul(b, a), _) == True{} : Bool} AR.mul_le(b, c, a, h) # a <= c, b <= d: a b <= c d def le_mul2(+a: Nat, +b: Nat, +c: Nat, +d: Nat, +h1: {Nat.is_le(a, c) == True{} : Bool}, +h2: {Nat.is_le(b, d) == True{} : Bool}) -> {Nat.is_le(Nat.mul(a, b), Nat.mul(c, d)) == True{} : Bool}: N.le_trans(Nat.mul(a, b), Nat.mul(c, b), Nat.mul(c, d), AR.mul_le(a, c, b, h1), le_mul_r(c, b, d, h2)) def shift_le(k: Nat, +a: Nat, +b: Nat, +h: {Nat.is_le(a, b) == True{} : Bool}) -> {Nat.is_le(C.shift(k, a), C.shift(k, b)) == True{} : Bool}: match k: case 0n: h case 1n+ +j: N.double_le(C.shift(j, a), C.shift(j, b), shift_le(j, a, b, h)) def shift_lt(+k: Nat, +a: Nat, +b: Nat, +h: {Nat.is_lt(a, b) == True{} : Bool}) -> {Nat.is_lt(C.shift(k, a), C.shift(k, b)) == True{} : Bool}: WW.shift_lt(k, a, b, h) # a < b gives a + 1 <= b def lt_succ_le(+a: Nat, +b: Nat, +h: {Nat.is_lt(a, b) == True{} : Bool}) -> {Nat.is_le(Nat.add(a, 1n), b) == True{} : Bool}: %Equal.sym(Nat, Nat.add(a, 1n), Nat.add(1n, a), NA.add_comm(a, 1n)) : {Nat.is_le(_, b) == True{} : Bool} N.lt_succ_le_succ(a, b, h) # a < 2^k and e < P: a + 2^k e < 2^k P (2^k written C.shift(k, one)) def digit_lt(+k: Nat, +one: Nat, +h1: {one == 1n : Nat}, +a: Nat, +e: Nat, +pp: Nat, +ha: {Nat.is_lt(a, C.shift(k, one)) == True{} : Bool}, +he: {Nat.is_lt(e, pp) == True{} : Bool}) -> {Nat.is_lt(Nat.add(a, C.shift(k, e)), C.shift(k, pp)) == True{} : Bool}: +s1 = lt_le_add(a, C.shift(k, e), C.shift(k, one), C.shift(k, e), ha, N.le_refl(C.shift(k, e))) +e1 = Equal.trans(Nat, Nat.add(C.shift(k, one), C.shift(k, e)), C.shift(k, Nat.add(one, e)), C.shift(k, Nat.add(e, 1n)), Equal.sym(Nat, C.shift(k, Nat.add(one, e)), Nat.add(C.shift(k, one), C.shift(k, e)), WW.shift_add(k, one, e)), Equal.cong(Nat, Nat, z => C.shift(k, z), Nat.add(one, e), Nat.add(e, 1n), Equal.trans(Nat, Nat.add(one, e), Nat.add(e, one), Nat.add(e, 1n), NA.add_comm(one, e), Equal.cong(Nat, Nat, z => Nat.add(e, z), one, 1n, h1)))) N.lt_le_trans(Nat.add(a, C.shift(k, e)), C.shift(k, Nat.add(e, 1n)), C.shift(k, pp), lt_subst_r(Nat.add(a, C.shift(k, e)), Nat.add(C.shift(k, one), C.shift(k, e)), C.shift(k, Nat.add(e, 1n)), e1, s1), shift_le(k, Nat.add(e, 1n), pp, lt_succ_le(e, pp, he))) # x < b and b <= c: x < c, and friends with rewriting def lt_of_le_lt(+a: Nat, +b: Nat, +c: Nat, +h1: {Nat.is_le(a, b) == True{} : Bool}, +h2: {Nat.is_lt(b, c) == True{} : Bool}) -> {Nat.is_lt(a, c) == True{} : Bool}: N.le_lt_trans(a, b, c, h1, h2) # 0 < b: a < a + b def lt_add_pos(+a: Nat, +b: Nat, +h: {Nat.is_lt(0n, b) == True{} : Bool}) -> {Nat.is_lt(a, Nat.add(a, b)) == True{} : Bool}: +s = N.lt_add_left(0n, b, a, h) lt_subst_l(Nat.add(a, 0n), a, Nat.add(a, b), NA.add_zero(a), s) # a <= a + b, and a <= a * b for b >= 1 def le_mul_pos(+a: Nat, +b: Nat, +h: {Nat.is_le(1n, b) == True{} : Bool}) -> {Nat.is_le(a, Nat.mul(a, b)) == True{} : Bool}: le_subst_l(Nat.mul(a, 1n), a, Nat.mul(a, b), NA.mul_one(a), le_mul_r(a, 1n, b, h))