Skip to content

all: replace Div/Mul with Rsh/Lsh if possible #29911#1966

Merged
AnilChinchawale merged 1 commit intoXinFinOrg:dev-upgradefrom
gzliudan:big-int-use-shift
Jan 29, 2026
Merged

all: replace Div/Mul with Rsh/Lsh if possible #29911#1966
AnilChinchawale merged 1 commit intoXinFinOrg:dev-upgradefrom
gzliudan:big-int-use-shift

Conversation

@gzliudan
Copy link
Copy Markdown
Collaborator

Proposed changes

Ref: ethereum#29911

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)
  • Regular KTLO or any of the maintaince work. e.g code style
  • CICD Improvement

Impacted Components

Which part of the codebase this PR will touch base on,

Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 19, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gzliudan gzliudan changed the title all: replace Div and Mul with shift if possible #29911 all: replace Div and Mul with Rsh and Lsh if possible #29911 Jan 19, 2026
@gzliudan gzliudan changed the title all: replace Div and Mul with Rsh and Lsh if possible #29911 all: replace Div/Mul with Rsh/Lsh if possible #29911 Jan 19, 2026
@AnilChinchawale
Copy link
Copy Markdown
Member

@gzliudan conflict

Copilot AI review requested due to automatic review settings January 29, 2026 05:56
@gzliudan
Copy link
Copy Markdown
Collaborator Author

@gzliudan conflict

done

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes big integer arithmetic by replacing division and multiplication operations with bit shift operations where the divisor/multiplier is a power of 2. The changes replace Div(x, 2^n) with Rsh(x, n) and Mul(x, 2^n) with Lsh(x, n), which are mathematically equivalent and more efficient operations.

Changes:

  • Replaced multiplication by powers of 2 (2, 4, 8, 16, 32) with left shift operations
  • Replaced division by powers of 2 (2, 4, 8, 16, 32) with right shift operations
  • Removed unused constants big4, big8, and big16 from core/vm/contracts.go

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/fuzzers/bn256/bn256_fuzz.go Optimized scalar multiplication by 2 in BN256 fuzzer
internal/ethapi/transaction_args_test.go Optimized max fee calculation in test
internal/ethapi/transaction_args.go Optimized London fee defaults calculation
internal/ethapi/api.go Optimized staker ROI calculations using right shift
eth/util/util.go Optimized reward inflation halving calculations
eth/gasprice/gasprice.go Optimized gas price suggestion calculations
crypto/secp256k1/curve.go Optimized Jacobian doubling arithmetic
crypto/crypto.go Optimized secp256k1 half N calculation
core/vm/privacy/ringct.go Optimized compressed key deserialization
core/vm/contracts.go Optimized modexp complexity and gas calculations, removed unused constants
core/types/transaction_signing.go Critical bug: Changed deriveChainId to mutate input parameter
core/txpool/legacypool/legacypool_test.go Optimized gas fee calculations in tests
contracts/trc21issuer/simulation/test/main.go Optimized TRC21 gas price calculations
consensus/ethash/consensus.go Optimized uncle reward calculations
XDCxlending/order_processor_test.go Optimized test quantity calculations
XDCxlending/order_processor.go Optimized relayer fee checks
XDCxlending/lendingstate/settle_balance.go Optimized interest rate calculations
XDCx/tradingstate/settle_balance_test.go Optimized test trade quantity setup
XDCx/order_processor.go Optimized relayer fee checks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +592 to +593
v.Sub(v, big.NewInt(35))
return v.Rsh(v, 1)
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

The variable name is inconsistent with the function signature. At line 592, v is being modified (using Sub which modifies the receiver), but this shadows the parameter v from line 584. The original code created a new big.Int with v = new(big.Int).Sub(v, big.NewInt(35)), but the updated code modifies the parameter directly with v.Sub(v, big.NewInt(35)). This changes the behavior by mutating the input parameter, which could cause unexpected side effects if the caller retains a reference to the parameter.

Suggested change
v.Sub(v, big.NewInt(35))
return v.Rsh(v, 1)
vCopy := new(big.Int).Sub(v, big.NewInt(35))
return vCopy.Rsh(vCopy, 1)

Copilot uses AI. Check for mistakes.
Comment thread crypto/secp256k1/curve.go

z3 := new(big.Int).Mul(y, z) //Y1*Z1
z3.Mul(big.NewInt(2), z3) //3*Y1*Z1
z3.Lsh(z3, 1) //3*Y1*Z1
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

The comment is incorrect. The code performs a left shift by 1, which multiplies by 2, not 3. The comment should say "2Y1Z1" instead of "3Y1Z1". Note that this appears to be a pre-existing issue (the original code also multiplied by 2), so it's not introduced by this PR, but it would be good to fix it.

Suggested change
z3.Lsh(z3, 1) //3*Y1*Z1
z3.Lsh(z3, 1) //2*Y1*Z1

Copilot uses AI. Check for mistakes.
@AnilChinchawale AnilChinchawale merged commit e77ac51 into XinFinOrg:dev-upgrade Jan 29, 2026
15 checks passed
@gzliudan gzliudan deleted the big-int-use-shift branch January 29, 2026 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants