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

GPG Verification Precompile #2

Merged
merged 17 commits into from
Jan 22, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
precompile expects dearmored bytes inputs
nkrishang committed Dec 27, 2024
commit 72aaf0b4a774cb4326454b07133373b3f1ff3106
38 changes: 10 additions & 28 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ package vm
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"maps"
@@ -1366,8 +1365,7 @@ var (
errMessageTooShort = errors.New("message too short")
errPubKeyTooShort = errors.New("public key too short")
errSignatureTooShort = errors.New("signature too short")
errInvalidPublicKey = errors.New("invalid public key format")
errInvalidSignature = errors.New("invalid signature format")
errInvalidPublicKey = errors.New("invalid public key")
)

// RequiredGas returns the gas required to execute the pre-compiled contract
@@ -1390,6 +1388,7 @@ func (c *gpgEd25519Verify) Run(input []byte) ([]byte, error) {
return nil, errMessageTooShort
}
message := input[32 : 32+msgLen]
messageObj := pgpcrypto.NewPlainMessage(message)

// Extract public key length and public key
offset := 32 + msgLen
@@ -1399,20 +1398,8 @@ func (c *gpgEd25519Verify) Run(input []byte) ([]byte, error) {
}
pubKey := input[offset+32 : offset+32+pubKeyLen]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a specific length the pubKey is supposed to be that we should verify?

Copy link
Author

Choose a reason for hiding this comment

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

Similar to the comment on signature length. The armored public key length is non-deterministic (exported via gpg --export --armor <your-key-id>).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed, no way to do this. I'm going to leave this comment open as a reminder that I need to talk to the Tea team to figure out a reasonable upper bound to make sure that (a) there is sufficient gas to handle it, and (b) that there is no DOS risk based on whatever gas params we choose.


// Extract signature length and signature
offset = offset + 32 + pubKeyLen
sigLen := new(big.Int).SetBytes(input[offset : offset+32]).Uint64()
if len(input) < int(offset+32+sigLen) {
return nil, errSignatureTooShort
}
signature := input[offset+32 : offset+32+sigLen]

// Convert raw bytes to armored format
armoredPubKey := string(pubKey)
armoredSig := string(signature)

// Create public key object
pubKeyObj, err := pgpcrypto.NewKeyFromArmored(armoredPubKey)
pubKeyObj, err := pgpcrypto.NewKey(pubKey)
if err != nil {
return nil, errInvalidPublicKey
}
@@ -1423,19 +1410,14 @@ func (c *gpgEd25519Verify) Run(input []byte) ([]byte, error) {
return nil, errInvalidPublicKey
}

// Parse the armored signature
signatureObj, err := pgpcrypto.NewPGPSignatureFromArmored(armoredSig)
if err != nil {
return nil, errInvalidSignature
}

// Create message object
messageHex := hex.EncodeToString(message)
messageBytes, err := hex.DecodeString(messageHex)
if err != nil {
return nil, err
// Extract signature length and signature
offset = offset + 32 + pubKeyLen
sigLen := new(big.Int).SetBytes(input[offset : offset+32]).Uint64()
if len(input) < int(offset+32+sigLen) {
return nil, errSignatureTooShort
}
messageObj := pgpcrypto.NewPlainMessage(messageBytes)
signature := input[offset+32 : offset+32+sigLen]
signatureObj := pgpcrypto.NewPGPSignature(signature)

// Verify signature
err = pubKeyRing.VerifyDetached(messageObj, signatureObj, 0)
27 changes: 16 additions & 11 deletions core/vm/contracts_test.go
Original file line number Diff line number Diff line change
@@ -428,9 +428,9 @@ func TestPrecompiledGpgEd25519Verify(t *testing.T) { testJson("gpgEd25519Ve

func BenchmarkPrecompiledGpgEd25519Verify(b *testing.B) {
t := precompiledTest{
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000002932d2d2d2d2d424547494e20504750205055424c4943204b455920424c4f434b2d2d2d2d2d0a0a6d444d455a3270644468594a4b7759424241486152773842415164415852437247514d50696a3763724f453944685a6a5a394b563865455537346649387743630a32704d61447575304b30747961584e6f5957356e494535685a4764686457526849447872636d6c7a614746755a7935756233526c5147647459576c734c6d4e760a625436496b77515446676f414f785968424d4443334e69684230626b66485778675a5a326376684652454f3442514a6e616c304f416873444251734a434163430a416949434268554b4351674c4167515741674d42416834484168654141416f4a454a5a326376684652454f34496b49412f33584556616c50354d6775624646760a556a72734764516f562f4636644f48485143514256412b6531777764415034714c6b342f57684e67684c7931716c396f364a6c6164622b4e4370504d416b554a0a3542566b51374e51424c6734424764715851345343697347415151426c31554242514542423041564530447175367235436e336168574b3449585174426f30610a51576764666855753737397a4243796a4c674d42434165496541515946676f4149425968424d4443334e69684230626b66485778675a5a326376684652454f340a42514a6e616c304f4168734d41416f4a454a5a326376684652454f343255674241503268773168454c6856574576344b3931667937726c50366d585a2b5133610a705875724e3267346b4d47664150774a7a323448736a6a3445324874756377526e3868327556396f7167416467776a565059382f6d647a3841673d3d0a3d3367346b0a2d2d2d2d2d454e4420504750205055424c4943204b455920424c4f434b2d2d2d2d2d00000000000000000000000000000000000000000000000000000000000000e32d2d2d2d2d424547494e20504750205349474e41545552452d2d2d2d2d0a0a694855454142594b414230574951544177747a596f5164473548783173594757646e4c3452555244754155435a3271616b41414b43524357646e4c34525552440a7545304841503942314d67646c31364a5463324647554f4e67455a6c746d783439694a6c4a773979756145497551747746414541374636745a7a72505a37366f0a796d705439354366484e327964794d734870424855513270446b4a4f4a67383d0a3d2f692f4d0a2d2d2d2d2d454e4420504750205349474e41545552452d2d2d2d2d",
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000001ab983304676a5d0e16092b06010401da470f010107405d10ab19030f8a3edcace13d0e166367d295f1e114ef87c8f3009cda931a0eebb42b4b72697368616e67204e61646761756461203c6b72697368616e672e6e6f746540676d61696c2e636f6d3e88930413160a003b162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a5d0e021b03050b0908070202220206150a09080b020416020301021e07021780000a0910967672f8454443b8224200ff75c455a94fe4c82e6c516f523aec19d42857f17a74e1c7402401540f9ed70c1d00fe2a2e4e3f5a136084bcb5aa5f68e8995a75bf8d0a93cc024509e4156443b35004b83804676a5d0e120a2b060104019755010501010740151340eabbaaf90a7dda8562b821742d068d1a41681d7e152eefbf73042ca32e0301080788780418160a0020162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a5d0e021b0c000a0910967672f8454443b8d9480100fda1c358442e155612fe0af757f2eeb94fea65d9f90ddaa57bab37683890c19f00fc09cf6e07b238f81361edb9cc119fc876b95f68aa001d8308d53d8f3f99dcfc02000000000000000000000000000000000000000000000000000000000000007788750400160a001d162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a9a90000a0910967672f8454443b84d0700ff41d4c81d975e894dcd8519438d804665b66c78f62265270f72b9a108b90b70140100ec5ead673acf67bea8ca6a53f7909f1cddb277232c1e9047510da90e424e260f",
Expected: "0000000000000000000000000000000000000000000000000000000000000001",
Name: "verify_valid_gpg_ed25519",
Name: "verify_gpg_ed25519_success",
}
benchmarkPrecompiled("ed", t, b)
}
@@ -443,19 +443,24 @@ var gpgEd25519MalformedInputTests = []precompiledFailureTest{
Name: "empty input",
},
{
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000002932d2d2d2d2d424547494e20504750205055424c",
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000001ab983304676a5d0e16092b06010401da470f01010",
ExpectedError: "message too short",
Name: "input shorter than 96 bytes",
Name: "input does not include full message",
},
{
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000002942d2d2d2d2d424547494e20504750205055424c4943204b455920424c4f434b2d2d2d2d2d0a700a6d444d455a3270644468594a4b7759424241486152773842415164415852437247514d50696a3763724f453944685a6a5a394b563865455537346649387743630a32704d61447575304b30747961584e6f5957356e494535685a4764686457526849447872636d6c7a614746755a7935756233526c5147647459576c734c6d4e760a625436496b77515446676f414f785968424d4443334e69684230626b66485778675a5a326376684652454f3442514a6e616c304f416873444251734a434163430a416949434268554b4351674c4167515741674d42416834484168654141416f4a454a5a326376684652454f34496b49412f33584556616c50354d6775624646760a556a72734764516f562f4636644f48485143514256412b6531777764415034714c6b342f57684e67684c7931716c396f364a6c6164622b4e4370504d416b554a0a3542566b51374e51424c6734424764715851345343697347415151426c31554242514542423041564530447175367235436e336168574b3449585174426f30610a51576764666855753737397a4243796a4c674d42434165496541515946676f4149425968424d4443334e69684230626b66485778675a5a326376684652454f340a42514a6e616c304f4168734d41416f4a454a5a326376684652454f343255674241503268773168454c6856574576344b3931667937726c50366d585a2b5133610a705875724e3267346b4d47664150774a7a323448736a6a3445324874756377526e3868327556396f7167416467776a565059382f6d647a3841673d3d0a3d3367346b0a2d2d2d2d2d454e4420504750205055424c4943204b455920424c4f434b2d2d2d2d2d00000000000000000000000000000000000000000000000000000000000000e32d2d2d2d2d424547494e20504750205349474e41545552452d2d2d2d2d0a0a694855454142594b414230574951544177747a596f5164473548783173594757646e4c3452555244754155435a3271616b41414b43524357646e4c34525552440a7545304841503942314d67646c31364a5463324647554f4e67455a6c746d783439694a6c4a773979756145497551747746414541374636745a7a72505a37366f0a796d705439354366484e327964794d734870424855513270446b4a4f4a67383d0a3d2f692f4d0a2d2d2d2d2d454e4420504750205349474e41545552452d2d2d2d2d",
ExpectedError: "invalid public key format",
Name: "invalid armored public key",
{
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000001ab983304676a5d0e16092b06010401da470f010107405d10ab19030f8a3edcace13d0e166367d295f1e114ef87c8f3009cda931a0eebb42b4b72697368616e67204e61646761756461203c6b72697368616e672e6e6f746540676d61696c2e636f6d3e88930413160a003b162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a5d0e021b03050b0908070202220206150a09080b020416020301021e07021780000a0910967672f8454443b8224200ff75c455a94fe4c82e6c516f523aec19d42857f17a74e1c7402401540f9ed70c1d00fe2a2e4e3f5a136084bcb5aa5f68e8995a75bf8d0a93cc024509e4156443b35004b83804676a5d0e120a2b060104019755010501010740151340eabbaaf90a7dda8562b821742d068d1a41681d7e152eefbf73042ca32e0301080788780418160a0020162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a5d0e021b0c000a0910967672f8454443b8d9480100fda1c358442e155612fe0af757f2eeb94fea65d9f90ddaa57bab37683890c19f00fc09cf6e07b238f81361edb9cc119fc876b95f68aa001d8308d53d8f3f99dcfc0",
ExpectedError: "public key too short",
Name: "input does not include full public key",
},
{
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000001ab983304676a5d0e16092b06010401da470f010107405d10ab19030f8a3edcace13d0e166367d295f1e114ef87c8f3009cda931a0eebb42b4b72697368616e67204e61646761756461203c6b72697368616e672e6e6f746540676d61696c2e636f6d3e88930413160a003b162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a5d0e021b03050b0908070202220206150a09080b020416020301021e07021780000a0910967672f8454443b8224200ff75c455a94fe4c82e6c516f523aec19d42857f17a74e1c7402401540f9ed70c1d00fe2a2e4e3f5a136084bcb5aa5f68e8995a75bf8d0a93cc024509e4156443b35004b83804676a5d0e120a2b060104019755010501010740151340eabbaaf90a7dda8562b821742d068d1a41681d7e152eefbf73042ca32e0301080788780418160a0020162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a5d0e021b0c000a0910967672f8454443b8d9480100fda1c358442e155612fe0af757f2eeb94fea65d9f90ddaa57bab37683890c19f00fc09cf6e07b238f81361edb9cc119fc876b95f68aa001d8308d53d8f3f99dcfc02000000000000000000000000000000000000000000000000000000000000007788750400160a001d162104c0c2dcd8a10746e47c75b181967672f8454443b80502676a9a90000a0910967672f8454443b84d0700ff41d4c81d975e894dcd8519438d804665b66c78f62265270f72b9a108b90b70140100ec5ead673acf67bea8ca6a53f7909f1cddb277232c1e9047510da90e424e26",
ExpectedError: "signature too short",
Name: "input does not include full signature",
},
{
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000002932d2d2d2d2d424547494e20504750205055424c4943204b455920424c4f434b2d2d2d2d2d0a0a6d444d455a3270644468594a4b7759424241486152773842415164415852437247514d50696a3763724f453944685a6a5a394b563865455537346649387743630a32704d61447575304b30747961584e6f5957356e494535685a4764686457526849447872636d6c7a614746755a7935756233526c5147647459576c734c6d4e760a625436496b77515446676f414f785968424d4443334e69684230626b66485778675a5a326376684652454f3442514a6e616c304f416873444251734a434163430a416949434268554b4351674c4167515741674d42416834484168654141416f4a454a5a326376684652454f34496b49412f33584556616c50354d6775624646760a556a72734764516f562f4636644f48485143514256412b6531777764415034714c6b342f57684e67684c7931716c396f364a6c6164622b4e4370504d416b554a0a3542566b51374e51424c6734424764715851345343697347415151426c31554242514542423041564530447175367235436e336168574b3449585174426f30610a51576764666855753737397a4243796a4c674d42434165496541515946676f4149425968424d4443334e69684230626b66485778675a5a326376684652454f340a42514a6e616c304f4168734d41416f4a454a5a326376684652454f343255674241503268773168454c6856574576344b3931667937726c50366d585a2b5133610a705875724e3267346b4d47664150774a7a323448736a6a3445324874756377526e3868327556396f7167416467776a565059382f6d647a3841673d3d0a3d3367346b0a2d2d2d2d2d454e4420504750205055424c4943204b455920424c4f434b2d2d2d2d2d00000000000000000000000000000000000000000000000000000000000000e42d2d2d2d2d424547494e20504750205349474e41545552452d2d2d2d2d0a700a694855454142594b414230574951544177747a596f5164473548783173594757646e4c3452555244754155435a3271616b41414b43524357646e4c34525552440a7545304841503942314d67646c31364a5463324647554f4e67455a6c746d783439694a6c4a773979756145497551747746414541374636745a7a72505a37366f0a796d705439354366484e327964794d734870424855513270446b4a4f4a67383d0a3d2f692f4d0a2d2d2d2d2d454e4420504750205349474e41545552452d2d2d2d2d",
ExpectedError: "invalid signature format",
Name: "invalid signature format",
Input: "000000000000000000000000000000000000000000000000000000000000000c48656c6c6f2c20576f726c6400000000000000000000000000000000000000000000000000000000000002942d2d2d2d2d424547494e20504750205055424c4943204b455920424c4f434b2d2d2d2d2d0a700a6d444d455a3270644468594a4b7759424241486152773842415164415852437247514d50696a3763724f453944685a6a5a394b563865455537346649387743630a32704d61447575304b30747961584e6f5957356e494535685a4764686457526849447872636d6c7a614746755a7935756233526c5147647459576c734c6d4e760a625436496b77515446676f414f785968424d4443334e69684230626b66485778675a5a326376684652454f3442514a6e616c304f416873444251734a434163430a416949434268554b4351674c4167515741674d42416834484168654141416f4a454a5a326376684652454f34496b49412f33584556616c50354d6775624646760a556a72734764516f562f4636644f48485143514256412b6531777764415034714c6b342f57684e67684c7931716c396f364a6c6164622b4e4370504d416b554a0a3542566b51374e51424c6734424764715851345343697347415151426c31554242514542423041564530447175367235436e336168574b3449585174426f30610a51576764666855753737397a4243796a4c674d42434165496541515946676f4149425968424d4443334e69684230626b66485778675a5a326376684652454f340a42514a6e616c304f4168734d41416f4a454a5a326376684652454f343255674241503268773168454c6856574576344b3931667937726c50366d585a2b5133610a705875724e3267346b4d47664150774a7a323448736a6a3445324874756377526e3868327556396f7167416467776a565059382f6d647a3841673d3d0a3d3367346b0a2d2d2d2d2d454e4420504750205055424c4943204b455920424c4f434b2d2d2d2d2d00000000000000000000000000000000000000000000000000000000000000e32d2d2d2d2d424547494e20504750205349474e41545552452d2d2d2d2d0a0a694855454142594b414230574951544177747a596f5164473548783173594757646e4c3452555244754155435a3271616b41414b43524357646e4c34525552440a7545304841503942314d67646c31364a5463324647554f4e67455a6c746d783439694a6c4a773979756145497551747746414541374636745a7a72505a37366f0a796d705439354366484e327964794d734870424855513270446b4a4f4a67383d0a3d2f692f4d0a2d2d2d2d2d454e4420504750205349474e41545552452d2d2d2d2d",
ExpectedError: "invalid public key",
Name: "invalid public key",
},
}

Loading