Skip to content

refactor(crypto): remove deprecated methods and harden SM2/SignUtils#9

Open
Federico2014 wants to merge 1 commit intodevelopfrom
refactor/crypto/cleanup-dead-code
Open

refactor(crypto): remove deprecated methods and harden SM2/SignUtils#9
Federico2014 wants to merge 1 commit intodevelopfrom
refactor/crypto/cleanup-dead-code

Conversation

@Federico2014
Copy link
Copy Markdown
Owner

@Federico2014 Federico2014 commented Apr 8, 2026

Summary

  • Remove compressPoint/decompressPoint from both ECKey and SM2
    (all 4 were @deprecated, zero callers confirmed by grep)
  • Add private static final to SM2 curve domain parameters
    (SM2_N, SM2_P, SM2_A, SM2_B, SM2_GX, SM2_GY)
  • Add instanceof type guards in SignUtils.signatureToAddress
    to replace silent ClassCastException with descriptive
    IllegalArgumentException on engine/signature-type mismatch

Compatibility

Removing compressPoint/decompressPoint is a binary-incompatible
change. External code calling these methods via direct reference will
encounter NoSuchMethodError at runtime after upgrading. Migration:
use ECPoint.getEncoded(boolean) directly.

Test plan

  • ./gradlew :framework:test --tests "org.tron.common.crypto.*" — all pass
  • ./gradlew :framework:test --tests "org.tron.core.net.messagehandler.PbftMsgHandlerTest" — pass
  • ./gradlew :framework:test --tests "org.tron.core.pbft.PbftApiTest" — pass
  • ./gradlew :chainbase:test — all pass

Summary by cubic

Removes deprecated point-compression helpers and adds strict signature-type checks to prevent silent casting errors. Also makes SM2 curve parameters immutable for safer crypto operations.

  • Refactors

    • Removed compressPoint/decompressPoint from ECKey and SM2 (no callers).
    • Added instanceof checks in SignUtils.signatureToAddress and throw IllegalArgumentException on engine/signature mismatch.
    • Marked SM2 domain parameters (SM2_N, SM2_P, SM2_A, SM2_B, SM2_GX, SM2_GY) as private static final.
  • Migration

    • Removal is binary-incompatible; direct callers will hit NoSuchMethodError.
    • Use ECPoint.getEncoded(boolean) for point compression/decompression.

Written for commit c8adb6c. Summary will update on new commits.

Summary by CodeRabbit

  • Refactor
    • Removed point compression/decompression utility methods from cryptographic classes.
    • Enhanced signature type validation to ensure correct cryptographic type usage and prevent invalid operations.
    • Improved internal stability by making cryptographic domain constants immutable.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

The PR removes point compression/decompression utility methods from ECKey and SM2 classes, adds runtime type validation in SignUtils to enforce correct signature types for their respective crypto engines, and makes SM2 elliptic-curve domain constants immutable.

Changes

Cohort / File(s) Summary
Point Compression/Decompression Removal
crypto/src/main/java/org/tron/common/crypto/ECKey.java, crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java
Removed public static methods compressPoint(ECPoint) and decompressPoint(ECPoint) from both ECKey and SM2, eliminating the point encoding/decoding utility API surface.
SM2 Domain Constants Immutability
crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java
Changed six elliptic-curve domain constant fields (SM2_N, SM2_P, SM2_A, SM2_B, SM2_GX, SM2_GY) from mutable private static to immutable private static final.
Signature Type Validation
crypto/src/main/java/org/tron/common/crypto/SignUtils.java
Added explicit runtime type checks in signatureToAddress(): validates signatureInterface is ECDSASignature when isECKeyCryptoEngine=true, or SM2Signature when isECKeyCryptoEngine=false, throwing IllegalArgumentException on mismatch.

Sequence Diagram

sequenceDiagram
    participant Client
    participant SignUtils
    participant ECKey
    participant SM2
    
    Client->>SignUtils: signatureToAddress(messageHash, signature, isECKeyCryptoEngine)
    
    alt isECKeyCryptoEngine = true
        SignUtils->>SignUtils: validate signature is ECDSASignature
        SignUtils->>ECKey: signatureToAddress(messageHash, ecdsaSig)
        ECKey-->>SignUtils: address
    else isECKeyCryptoEngine = false
        SignUtils->>SignUtils: validate signature is SM2Signature
        SignUtils->>SM2: signatureToAddress(messageHash, sm2Sig)
        SM2-->>SignUtils: address
    end
    
    SignUtils-->>Client: address
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

  • Federico2014/bn128-ipc#1: The explicit type validation between ECDSASignature and SM2Signature addresses the goal of clarifying crypto engine branching logic and related type safety improvements.

Poem

🐰 Point compression hops away with grace,
Type checks now guard each crypto place,
Constants frozen, firm and bright,
Cleaner pathways, pure delight! ✨🔐

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: removing deprecated methods and hardening type validation in SM2/SignUtils crypto components.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/crypto/cleanup-dead-code

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.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="crypto/src/main/java/org/tron/common/crypto/SignUtils.java">

<violation number="1" location="crypto/src/main/java/org/tron/common/crypto/SignUtils.java:54">
P2: Null `signatureInterface` causes a `NullPointerException` while building the new type-mismatch message, instead of the intended `IllegalArgumentException`.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

if (!(signatureInterface instanceof ECDSASignature)) {
throw new IllegalArgumentException(
"Expected ECDSASignature for ECKey engine, got: "
+ signatureInterface.getClass().getName());
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

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

P2: Null signatureInterface causes a NullPointerException while building the new type-mismatch message, instead of the intended IllegalArgumentException.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At crypto/src/main/java/org/tron/common/crypto/SignUtils.java, line 54:

<comment>Null `signatureInterface` causes a `NullPointerException` while building the new type-mismatch message, instead of the intended `IllegalArgumentException`.</comment>

<file context>
@@ -48,8 +48,18 @@ public static byte[] signatureToAddress(
+      if (!(signatureInterface instanceof ECDSASignature)) {
+        throw new IllegalArgumentException(
+            "Expected ECDSASignature for ECKey engine, got: "
+                + signatureInterface.getClass().getName());
+      }
       return ECKey.signatureToAddress(messageHash, (ECDSASignature) signatureInterface);
</file context>
Suggested change
+ signatureInterface.getClass().getName());
+ (signatureInterface == null ? "null" : signatureInterface.getClass().getName()));
Fix with Cubic

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crypto/src/main/java/org/tron/common/crypto/SignUtils.java`:
- Around line 51-55: Check for null before calling signatureInterface.getClass()
in SignUtils: when validating that signatureInterface is an instance of
ECDSASignature, replace the direct getClass() call in the error message with a
null-safe expression (e.g., use (signatureInterface == null ? "null" :
signatureInterface.getClass().getName())) or explicitly branch to throw an
IllegalArgumentException stating the parameter is null; apply the same
null-guard change to the second occurrence mentioned so neither the instanceof
check nor the error formatting can cause a NullPointerException.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4e22e54c-4b89-4830-ad7b-e88639c15d97

📥 Commits

Reviewing files that changed from the base of the PR and between 3ab5adf and c8adb6c.

📒 Files selected for processing (3)
  • crypto/src/main/java/org/tron/common/crypto/ECKey.java
  • crypto/src/main/java/org/tron/common/crypto/SignUtils.java
  • crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java
💤 Files with no reviewable changes (1)
  • crypto/src/main/java/org/tron/common/crypto/ECKey.java

Comment on lines +51 to +55
if (!(signatureInterface instanceof ECDSASignature)) {
throw new IllegalArgumentException(
"Expected ECDSASignature for ECKey engine, got: "
+ signatureInterface.getClass().getName());
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add explicit null guard for signatureInterface before type-check error formatting.

At Line 54 and Line 61, signatureInterface.getClass() can throw NullPointerException when input is null, so the new descriptive IllegalArgumentException is bypassed.

Suggested fix
   public static byte[] signatureToAddress(
       byte[] messageHash, SignatureInterface signatureInterface, boolean isECKeyCryptoEngine)
       throws SignatureException {
+    if (signatureInterface == null) {
+      throw new IllegalArgumentException("signatureInterface must not be null");
+    }
     if (isECKeyCryptoEngine) {
       if (!(signatureInterface instanceof ECDSASignature)) {
         throw new IllegalArgumentException(
             "Expected ECDSASignature for ECKey engine, got: "
                 + signatureInterface.getClass().getName());

Also applies to: 58-62

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crypto/src/main/java/org/tron/common/crypto/SignUtils.java` around lines 51 -
55, Check for null before calling signatureInterface.getClass() in SignUtils:
when validating that signatureInterface is an instance of ECDSASignature,
replace the direct getClass() call in the error message with a null-safe
expression (e.g., use (signatureInterface == null ? "null" :
signatureInterface.getClass().getName())) or explicitly branch to throw an
IllegalArgumentException stating the parameter is null; apply the same
null-guard change to the second occurrence mentioned so neither the instanceof
check nor the error formatting can cause a NullPointerException.

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.

1 participant