Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/issue 270/relispify gas #292

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions gas/columns.lisp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
(module gas)

(defcolumns
(STAMP :i32)
(INPUTS_AND_OUTPUTS_ARE_MEANINGFUL :binary@prove)
(FIRST :binary@prove)
(CT :i3)
(GAS_ACTL :i32)
(CT_MAX :i3)
(GAS_ACTUAL :i32)
(GAS_COST :i64)
(OOGX :binary@prove)
(BYTE :byte@prove :array [2])
(ACC :i64 :array [2]))
(EXCEPTIONS_AHOY :binary@prove)
(OUT_OF_GAS_EXCEPTION :binary@prove)
(WCP_ARG1_LO :i128)
(WCP_ARG2_LO :i128)
(WCP_INST :byte@prove :display :opcode)
(WCP_RES :binary@prove))
lorenzogentile404 marked this conversation as resolved.
Show resolved Hide resolved

(defalias
IOMF INPUTS_AND_OUTPUTS_ARE_MEANINGFUL
XAHOY EXCEPTIONS_AHOY
OOGX OUT_OF_GAS_EXCEPTION)


6 changes: 0 additions & 6 deletions gas/constants.lisp

This file was deleted.

134 changes: 92 additions & 42 deletions gas/constraints.lisp
Original file line number Diff line number Diff line change
@@ -1,72 +1,122 @@
(module gas)

;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 3.1 Binarities ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;
(defconstraint binary-constraints ()
(begin (is-binary IOMF)
(is-binary XAHOY)
(is-binary OOGX)
(debug (is-binary FIRST))
(debug (is-binary WCP_RES))
lorenzogentile404 marked this conversation as resolved.
Show resolved Hide resolved
(if-not-zero OOGX
(eq! XAHOY 1))))

;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 3.1 heartbeat ;;
;; 3.2 Heartbeat ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
;; 1
(defconstraint first-row (:domain {0})
(vanishes! STAMP))
(vanishes! IOMF))

;; 2
(defconstraint stamp-vanishing-values ()
(if-zero STAMP
(begin (vanishes! CT)
(vanishes! [BYTE 1])
(vanishes! [BYTE 2])
(vanishes! GAS_ACTL)
(vanishes! GAS_COST)
(vanishes! OOGX))))
(defconstraint iomf-increments ()
(any! (will-remain-constant! IOMF) (will-inc! IOMF 1)))

;; 3
(defconstraint stamp-increments ()
(any! (will-remain-constant! STAMP) (will-inc! STAMP 1)))
(defconstraint iomf-vanishing-values ()
(if-zero IOMF
(begin (vanishes! FIRST)
(debug (vanishes! CT))
(vanishes! (next CT))
(debug (vanishes! CT_MAX))
(debug (vanishes! GAS_ACTUAL))
(debug (vanishes! GAS_COST))
(debug (vanishes! OOGX))
(debug (vanishes! XAHOY)))))

;; 4
(defconstraint counter-reset ()
(if-not-zero (will-remain-constant! STAMP)
(vanishes! (next CT))))

;; 5
(defconstraint instruction-counter-cycle ()
(if-not-zero STAMP
(if-eq-else CT CT_MAX (will-inc! STAMP 1) (will-inc! CT 1))))
(if-not-zero IOMF
(begin (eq! CT_MAX
(- 2
(* XAHOY (- 1 OOGX))))
(if-zero CT
(eq! FIRST 1)
(eq! FIRST 0))
(if-eq-else CT CT_MAX
(vanishes! (next CT))
(will-inc! CT 1)))))

;; 6
;; 5
(defconstraint final-row (:domain {-1})
(if-not-zero STAMP
(if-not-zero IOMF
(eq! CT CT_MAX)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 3.2 counter constancy ;;
;; 3.3 Constancy constraints ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconstraint counter-constancy ()
(begin (counter-constancy CT GAS_ACTL)
(begin (counter-constancy CT GAS_ACTUAL)
(counter-constancy CT GAS_COST)
(counter-constancy CT XAHOY)
(counter-constancy CT OOGX)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 3.3 Byte decompositions ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconstraint byte-decompositions ()
(for k [1:2] (byte-decomposition CT [ACC k] [BYTE k])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 3.4 Populating the lookup columns ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; defining "WCP macros"
(defun (call-to-LT k ;; row shift parameter
a ;; arg 1 low
b ;; arg 2 low
c ;; res
) (begin (eq! (shift WCP_ARG1_LO k) a)
(eq! (shift WCP_ARG2_LO k) b)
(eq! (shift WCP_INST k) EVM_INST_LT)
(eq! (shift WCP_RES k) c)))

(defun (call-to-LEQ k ;; row shift parameter
a ;; arg 1 low
b ;; arg 2 low
c ;; res
) (begin (eq! (shift WCP_ARG1_LO k) a)
(eq! (shift WCP_ARG2_LO k) b)
(eq! (shift WCP_INST k) WCP_INST_LEQ)
(eq! (shift WCP_RES k) c)))

(defconstraint asserting-the-leftover-gas-is-nonnegative (:guard FIRST)
(call-to-LEQ 0 ;; row shift parameter
0 ;; arg 1 low
GAS_ACTUAL ;; arg 2 low
1 ;; res is TRUE!
))

;; as per the spec, this constraint the following
;; constraint is slightly useless ... not entirely,
;; though: it still asserts "smallness" so that it
;; should filter out MXPX induced out of gas exceptions.
(defconstraint asserting-the-gas-cost-is-nonnegative (:guard FIRST)
(call-to-LT 1 ;; row shift parameter
0 ;; arg 1 low
GAS_COST ;; arg 2 low
1 ;; res is TRUE!
))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 3.3 Target constraints ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconstraint target-1 ()
(if-eq CT CT_MAX
(begin (eq! [ACC 1] GAS_ACTL)
(eq! [ACC 2]
(- (* (- (* 2 OOGX) 1)
(- GAS_COST GAS_ACTL))
OOGX)))))
(defconstraint asserting-either-sufficient-gas-or-insufficient-gas (:guard FIRST)
(if-zero (force-bin (* XAHOY (- 1 OOGX)))
(call-to-LT 2 ;; row shift parameter
GAS_ACTUAL ;; arg 1 low
GAS_COST ;; arg 2 low
OOGX ;; res predicted by HUB
)))


23 changes: 23 additions & 0 deletions gas/gas-into-wcp.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(defun (wcp-activation-flag)
gas.IOMF)

(deflookup
gas-into-wcp
(
wcp.ARG_1_HI
wcp.ARG_1_LO
wcp.ARG_2_HI
wcp.ARG_2_LO
wcp.RES_LO
wcp.INST
)
(
0
(* gas.WCP_ARG1_LO (wcp-activation-flag))
0
(* gas.WCP_ARG2_LO (wcp-activation-flag))
(* gas.WCP_RES (wcp-activation-flag))
(* gas.WCP_INST (wcp-activation-flag))
))


Loading