Skip to content

Commit

Permalink
Creates ValidationSignature helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Dec 13, 2024
1 parent 0a53373 commit af08361
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
45 changes: 45 additions & 0 deletions curve/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,48 @@ func (sc StarkCurve) PrivateToPoint(privKey *big.Int) (x, y *big.Int, err error)
x, y = sc.EcMult(privKey, sc.EcGenX, sc.EcGenY)
return x, y, nil
}

// VerifySignature verifies the ECDSA signature of a given message hash using the provided public key.
//
// It takes the message hash, the r and s values of the signature, and the public key as strings and
// verifies the signature using the public key.
//
// Parameters:
// - msgHash: The hash of the message to be verified as a string
// - r: The r value (the first part) of the signature as a string
// - s: The s value (the second part) of the signature as a string
// - pubKey: The public key (only the x coordinate) as a string
// Return values:
// - bool: A boolean indicating whether the signature is valid
// - error: An error if any occurred during the verification process
func VerifySignature(msgHash, r, s, pubKey string) bool {
feltMsgHash, err := new(felt.Felt).SetString(msgHash)
if err != nil {
return false
}
feltR, err := new(felt.Felt).SetString(r)
if err != nil {
return false
}
feltS, err := new(felt.Felt).SetString(s)
if err != nil {
return false
}
pubKeyFelt, err := new(felt.Felt).SetString(pubKey)
if err != nil {
return false
}

signature := junoCrypto.Signature{
R: *feltR,
S: *feltS,
}

pubKeyStruct := junoCrypto.NewPublicKey(pubKeyFelt)
resp, err := pubKeyStruct.Verify(&signature, feltMsgHash)
if err != nil {
return false
}

return resp
}
25 changes: 25 additions & 0 deletions curve/curve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,28 @@ func TestGeneral_SplitFactStr(t *testing.T) {
require.Equal(t, d["h"], h)
}
}

// TestGeneral_VerifySignature is a test function that verifies the correctness of the VerifySignature function.
//
// It checks if the signature of a given message hash is valid using the provided r, s values and the public key.
// The function takes no parameters and returns no values.
//
// Parameters:
// - t: The testing.T object for running the test
// Returns:
//
// none
func TestGeneral_VerifySignature(t *testing.T) {
// values verified with starknet.js

msgHash := "0x2789daed76c8b750d5a609a706481034db9dc8b63ae01f505d21e75a8fc2336"
r := "0x13e4e383af407f7ccc1f13195ff31a58cad97bbc6cf1d532798b8af616999d4"
s := "0x44dd06cf67b2ba7ea4af346d80b0b439e02a0b5893c6e4dfda9ee204211c879"
fullPubKey := "0x6c7c4408e178b2999cef9a5b3fa2a3dffc876892ad6a6bd19d1451a2256906c"

require.True(t, VerifySignature(msgHash, r, s, fullPubKey))

// Change the last digit of the message hash to test invalid signature
wrongMsgHash := "0x2789daed76c8b750d5a609a706481034db9dc8b63ae01f505d21e75a8fc2337"
require.False(t, VerifySignature(wrongMsgHash, r, s, fullPubKey))
}
10 changes: 1 addition & 9 deletions examples/typedData/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ func main() {
fmt.Println("Signature:", signature)

// This is how you can verify the signature
privKeyBI, ok := new(big.Int).SetString(setup.GetPrivateKey(), 0)
if !ok {
panic("Fail to convert privKey to bitInt")
}
x, y, err := curve.Curve.PrivateToPoint(privKeyBI)
if err != nil {
panic(fmt.Errorf("fail to get point: %w", err))
}
isValid := curve.Curve.Verify(messageHash.BigInt(new(big.Int)), signature[0].BigInt(new(big.Int)), signature[1].BigInt(new(big.Int)), x, y)
isValid := curve.VerifySignature(messageHash.String(), signature[0].String(), signature[1].String(), setup.GetPublicKey())
fmt.Println("Verification result:", isValid)
}

Expand Down

0 comments on commit af08361

Please sign in to comment.