forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Pqc falcon512 #28
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
Merged
Federico2014
merged 7 commits into
Federico2014:pqc-falcon512
from
Little-Peony:pqc-falcon512
May 20, 2026
Merged
Pqc falcon512 #28
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e32012b
style(chainbase): use Set/HashSet imports instead of FQCN
Little-Peony 2e8a236
refactor(witness): bundle PQ priv/pub key lists into PqKeypair
Little-Peony 38af8ea
refactor(actuator): gate PQ verify on isAnyPqSchemeAllowed in getTran…
Little-Peony bd79efc
refactor(block): gate PQ signature recognition behind isAnyPqSchemeAl…
Little-Peony 5b946e9
fix move keypair
Little-Peony 6f5054e
fix hasLegacy hasPq
Little-Peony edad7b3
fix build
Little-Peony File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,10 +47,8 @@ | |
| import org.tron.core.store.DynamicPropertiesStore; | ||
| import org.tron.protos.Protocol.Block; | ||
| import org.tron.protos.Protocol.BlockHeader; | ||
| import org.tron.protos.Protocol.Key; | ||
| import org.tron.protos.Protocol.PQScheme; | ||
| import org.tron.protos.Protocol.PQAuthSig; | ||
| import org.tron.protos.Protocol.Permission; | ||
| import org.tron.protos.Protocol.Transaction; | ||
|
|
||
| @Slf4j(topic = "capsule") | ||
|
|
@@ -203,41 +201,36 @@ private Sha256Hash getRawHash() { | |
| public boolean validateSignature(DynamicPropertiesStore dynamicPropertiesStore, | ||
| AccountStore accountStore) throws ValidateSignatureException { | ||
| BlockHeader header = block.getBlockHeader(); | ||
| boolean hasLegacy = !header.getWitnessSignature().isEmpty(); | ||
| boolean hasPq = header.hasPqAuthSig(); | ||
| byte[] witnessAccountAddress = header.getRawData().getWitnessAddress() | ||
| .toByteArray(); | ||
|
|
||
| if (hasLegacy && hasPq) { | ||
| throw new ValidateSignatureException( | ||
| "witness_signature and pq_auth_sig are mutually exclusive"); | ||
| } | ||
| if (!hasLegacy && !hasPq) { | ||
| throw new ValidateSignatureException("missing witness signature"); | ||
| byte[] witnessPermissionAddress; | ||
| if (dynamicPropertiesStore.getAllowMultiSign() != 1) { | ||
| witnessPermissionAddress = witnessAccountAddress; | ||
| } else { | ||
| witnessPermissionAddress = accountStore.get(witnessAccountAddress) | ||
| .getWitnessPermissionAddress(); | ||
| } | ||
|
|
||
| byte[] witnessAccountAddress = header.getRawData().getWitnessAddress().toByteArray(); | ||
| if (hasPq) { | ||
| return validatePQSignature(dynamicPropertiesStore, accountStore, | ||
| witnessAccountAddress, header.getPqAuthSig()); | ||
| if (dynamicPropertiesStore.isAnyPqSchemeAllowed()) { | ||
| boolean hasLegacy = !header.getWitnessSignature().isEmpty(); | ||
| boolean hasPq = header.hasPqAuthSig(); | ||
| if (hasLegacy && hasLegacy) { | ||
| throw new ValidateSignatureException( | ||
| "witness_signature and pq_auth_sig are mutually exclusive"); | ||
| } | ||
| if (!hasLegacy && !hasPq) { | ||
| throw new ValidateSignatureException("missing witness signature"); | ||
| } | ||
| return validatePQSignature(dynamicPropertiesStore, accountStore, witnessPermissionAddress, | ||
| header.getPqAuthSig()); | ||
| } | ||
| return validateLegacySignature(dynamicPropertiesStore, accountStore, witnessAccountAddress); | ||
| } | ||
|
|
||
| private boolean validateLegacySignature(DynamicPropertiesStore dynamicPropertiesStore, | ||
| AccountStore accountStore, byte[] witnessAccountAddress) | ||
| throws ValidateSignatureException { | ||
| try { | ||
| byte[] sigAddress = SignUtils.signatureToAddress(getRawHash().getBytes(), | ||
| TransactionCapsule.getBase64FromByteString( | ||
| block.getBlockHeader().getWitnessSignature()), | ||
| TransactionCapsule.getBase64FromByteString(header.getWitnessSignature()), | ||
| CommonParameter.getInstance().isECKeyCryptoEngine()); | ||
| if (dynamicPropertiesStore.getAllowMultiSign() != 1) { | ||
| return Arrays.equals(sigAddress, witnessAccountAddress); | ||
| } | ||
| AccountCapsule witnessAccount = accountStore.get(witnessAccountAddress); | ||
| if (witnessAccount == null) { | ||
| throw new ValidateSignatureException("witness account does not exist"); | ||
| } | ||
| byte[] witnessPermissionAddress = witnessAccount.getWitnessPermissionAddress(); | ||
|
|
||
| return Arrays.equals(sigAddress, witnessPermissionAddress); | ||
| } catch (SignatureException e) { | ||
| throw new ValidateSignatureException(e.getMessage()); | ||
|
|
@@ -250,8 +243,11 @@ private boolean validateLegacySignature(DynamicPropertiesStore dynamicProperties | |
| * the witness account's Witness Permission keys[]. | ||
| */ | ||
| private boolean validatePQSignature(DynamicPropertiesStore dynamicPropertiesStore, | ||
| AccountStore accountStore, byte[] witnessAccountAddress, PQAuthSig pqAuthSig) | ||
| AccountStore accountStore, byte[] witnessPermissionAddress, PQAuthSig pqAuthSig) | ||
| throws ValidateSignatureException { | ||
| /* | ||
| Verify the PQ scheme is supported and proposal opened | ||
| */ | ||
| PQScheme scheme = pqAuthSig.getScheme(); | ||
| if (!PQSchemeRegistry.contains(scheme)) { | ||
| throw new ValidateSignatureException( | ||
|
|
@@ -262,38 +258,22 @@ private boolean validatePQSignature(DynamicPropertiesStore dynamicPropertiesStor | |
| "pq_auth_sig scheme " + scheme + " is not activated"); | ||
| } | ||
|
|
||
| AccountCapsule accountCapsule = accountStore.get(witnessAccountAddress); | ||
| Permission witnessPermission = null; | ||
| if (accountCapsule != null && accountCapsule.getInstance().hasWitnessPermission()) { | ||
| witnessPermission = accountCapsule.getInstance().getWitnessPermission(); | ||
| } | ||
| if (witnessPermission == null || witnessPermission.getKeysCount() == 0) { | ||
| throw new ValidateSignatureException( | ||
| "pq_auth_sig present but witness permission is not configured"); | ||
| } | ||
|
|
||
| byte[] publicKey = pqAuthSig.getPublicKey().toByteArray(); | ||
| if (publicKey.length != PQSchemeRegistry.getPublicKeyLength(scheme)) { | ||
| throw new ValidateSignatureException( | ||
| "pq_auth_sig public key length mismatch for scheme " + scheme); | ||
| } | ||
| byte[] signature = pqAuthSig.getSignature().toByteArray(); | ||
| if (!PQSchemeRegistry.isValidSignatureLength(scheme, signature.length)) { | ||
| throw new ValidateSignatureException( | ||
| "pq_auth_sig signature length mismatch for scheme " + scheme); | ||
| } | ||
|
|
||
| byte[] derivedAddr = PQSchemeRegistry.computeAddress(scheme, publicKey); | ||
| Key matched = null; | ||
| for (Key k : witnessPermission.getKeysList()) { | ||
| if (Arrays.equals(k.getAddress().toByteArray(), derivedAddr)) { | ||
| matched = k; | ||
| break; | ||
| } | ||
| if (!Arrays.equals(derivedAddr, witnessPermissionAddress)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: PQ signature address matching is restricted to a single witness key, which can reject valid signatures from other configured witness permission keys. Prompt for AI agents |
||
| throw new ValidateSignatureException( | ||
| "pq_auth_sig public key does not match witness permission address"); | ||
| } | ||
| if (matched == null) { | ||
|
|
||
| byte[] signature = pqAuthSig.getSignature().toByteArray(); | ||
| if (!PQSchemeRegistry.isValidSignatureLength(scheme, signature.length)) { | ||
| throw new ValidateSignatureException( | ||
| "pq_auth_sig public key does not match any witness permission key"); | ||
| "pq_auth_sig signature length mismatch for scheme " + scheme); | ||
| } | ||
|
|
||
| byte[] digest = getRawHash().getBytes(); | ||
|
|
@@ -419,10 +399,13 @@ public long getTimeStamp() { | |
| return this.block.getBlockHeader().getRawData().getTimestamp(); | ||
| } | ||
|
|
||
| public boolean hasWitnessSignature() { | ||
| public boolean hasWitnessSignature(DynamicPropertiesStore dynamicPropertiesStore) { | ||
| BlockHeader header = getInstance().getBlockHeader(); | ||
| return !header.getWitnessSignature().isEmpty() | ||
| || !header.getPqAuthSig().getSignature().isEmpty(); | ||
| boolean hasLegacySignature = !header.getWitnessSignature().isEmpty(); | ||
| if (!dynamicPropertiesStore.isAnyPqSchemeAllowed()) { | ||
| return hasLegacySignature; | ||
| } | ||
| return hasLegacySignature || !header.getPqAuthSig().getSignature().isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Missing null-check on witness account can throw NullPointerException during signature validation.
Prompt for AI agents