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

Effective gas implementation #290

Merged
merged 6 commits into from
May 31, 2023
Merged

Conversation

ignasirv
Copy link
Contributor

@ignasirv ignasirv commented May 24, 2023

Effective gas price implementation.
In order to be more precise with gasPrice and low tx fees, the sequencer is able to propose a reduction of the signed gasPrice. This param, effectiveGasPrice, is passed at the last bytes of the encoded tx and present in the batchL2Data. The final effectiveGasPrice is compute as:

effectiveGasPrice = floor((gasPrice * (effectivePercentage + 1)) / 256)

txGasPrice is replaced by effectiveGasPrice among tx processing

Copy link
Contributor

@krlosMata krlosMata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😸

main/process-tx.zkasm Outdated Show resolved Hide resolved
@invocamanman
Copy link
Contributor

nice ^^ :D

; get value above 256 bits
${_effGasPriceShifted >> 256} => D
; compute ARITH
${_effGasPriceShifted} => E :ARITH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have out of arith counters check for this code?
(or any other counters (steps, binary?) given that this adds quite a bit of computation)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree we should add an arith checker at beginning of process-tx.zkasm

${_effGasPriceShifted / 256} => A :MSTORE(txGasPrice)
${_effGasPriceShifted % 256} => C
; compute ARITH
E :ARITH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I still don't quite understand how this works, so correct where if I'm wrong.

I believe the purpose of this ARITH is to contstrain the free calculations above (_effGasPriceShifted / 256 and _effGasPriceShifted % 256)

This ARITH line calculates AB + C and updates E and D.

What would stop malicious prover to return arbitrary value into A instead of _effGasPriceShifted / 256? This ARITH doesn't seem to constrain neither of registers values (unlike the first ARITH above?)
(for C it is more clear, it is at least checked for <256 afterwards)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think I understand now: it takes into account previous values of E and D and checks result of ARITH against them, because this expression doesn't have an assignment to register.

@@ -68,9 +70,34 @@ endCheckChainId:
:CALL(isColdAddress) ; add tx.origin to touched addresses
0 :MSTORE(depth) ; Initial depth is 0

;; Set gasPrice global var
;; Set gasPrice global var depending on effectivePercentage [0-255] -> txGasPrice = Floor((gasPrice * (effectivePercentage + 1)) / 256)
; _effGasPriceShifted = gasPrice * (effectivePercentage + 1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmetic: this comment is duplicated on line 83, maybe it's not needed here

;; Set gasPrice global var
;; Set gasPrice global var depending on effectivePercentage [0-255] -> txGasPrice = Floor((gasPrice * (effectivePercentage + 1)) / 256)
; _effGasPriceShifted = gasPrice * (effectivePercentage + 1)
; A => gasPrice
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmetic: notation with => is confusing here, because meaning is opposite of zkasm syntax

Suggested change
; A => gasPrice
; A = gasPrice

$ => A :MLOAD(txGasPriceRLP)
A :MSTORE(txGasPrice)
; B => effectivePercentage
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Suggested change
; B => effectivePercentage
; B = effectivePercentage

A :MSTORE(txGasPrice)
; B => effectivePercentage
$ => B :MLOAD(effectivePercentageRLP)
; B => [0, 255]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmetic: if the comment supposed to explain the effect of the following line

Suggested change
; B => [0, 255]
; B = [1, 256]

@ignasirv ignasirv force-pushed the feature/effective-gas-price branch from d09f1c5 to 33034b9 Compare May 29, 2023 12:47
@ignasirv ignasirv force-pushed the feature/effective-gas-price branch from 33034b9 to ac51215 Compare May 29, 2023 12:49
@@ -285,6 +285,11 @@ vREADTx:
A :MSTORE(txV)
C + D => C :CALL(addBatchHashData)

;; read effective percentage
effectivePercentageTx:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This label is never called. Is this just a documentation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is there for clarity

@@ -285,6 +285,11 @@ vREADTx:
A :MSTORE(txV)
C + D => C :CALL(addBatchHashData)

;; read effective percentage
effectivePercentageTx:
1 => D :CALL(getTxBytes)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of getTxBytes does not say what C is used for.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C description is at the very beginning of the rlp-processing: https://github.com/0xPolygonHermez/zkevm-rom/blob/main/main/load-tx-rlp.zkasm#L28

; compute ARITH
E :ARITH
; check divisor > remainder
C => A
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To confirm: LT treats arguments as unsigned. I.e. this will work in case C is "negative".

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the usage of LT covered by CNT_BINARY - 100?

Copy link
Contributor Author

@ignasirv ignasirv May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LT uses 1 binary, so yes.
LT treats arguments as unsigned, it compares two 256 bit scalars. To treat them as signed, we also have the registry SLT.
In case C is "negative" it will be treated as a Scalar

@krlosMata krlosMata merged commit eb376cc into develop May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants