-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use bls signature in telemetry api for the operator (#1489)
Co-authored-by: Uriel Mihura <[email protected]>
- Loading branch information
1 parent
86692fc
commit bb152df
Showing
10 changed files
with
214 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -27,3 +27,6 @@ telemetry_api-*.tar | |
|
||
# Elixir lsp server | ||
.elixir_ls | ||
|
||
# Binaries | ||
priv/bls_verify |
This file contains 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"flag" | ||
"log" | ||
"os" | ||
|
||
bls "github.com/Layr-Labs/eigensdk-go/crypto/bls" | ||
) | ||
|
||
func main() { | ||
signatureArg := flag.String("signature", "", "BLS signature bytes") | ||
publicKeyG1X := flag.String("public-key-g1-x", "", "BLS public key on g1 affine x coord") | ||
publicKeyG1Y := flag.String("public-key-g1-y", "", "BLS public key on g1 affine y coord") | ||
publicKeyG2Arg := flag.String("public-key-g2", "", "BLS public key on g2") | ||
messageArg := flag.String("message", "", "Hex-encoded message") | ||
|
||
flag.Parse() | ||
|
||
if *signatureArg == "" || *publicKeyG1X == "" || *publicKeyG1Y == "" || *publicKeyG2Arg == "" || *messageArg == "" { | ||
log.Fatalf("All arguments (signature, publickey g1 hash, publickey g2, and messagehash) are required") | ||
} | ||
|
||
signature, err := hex.DecodeString(*signatureArg) | ||
if err != nil { | ||
log.Fatalf("Failed to decode signature: %v", err) | ||
} | ||
|
||
var pubkeyG1PointsBytes [2][]byte | ||
xBytes, err := hex.DecodeString(*publicKeyG1X) | ||
if err != nil { | ||
log.Fatalf("Failed to decode G1 X: %v", err) | ||
} | ||
yBytes, err := hex.DecodeString(*publicKeyG1Y) | ||
if err != nil { | ||
log.Fatalf("Failed to decode G1 Y: %v", err) | ||
} | ||
pubkeyG1PointsBytes[0] = xBytes | ||
pubkeyG1PointsBytes[1] = yBytes | ||
|
||
pubkeyG2Bytes, err := hex.DecodeString(*publicKeyG2Arg) | ||
if err != nil { | ||
log.Fatalf("Failed to decode pubkey: %v", err) | ||
} | ||
|
||
messageHash, err := hex.DecodeString(*messageArg) | ||
if err != nil { | ||
log.Fatalf("Failed to decode message hash: %v", err) | ||
} | ||
|
||
isValid, err := verifySignature(signature, pubkeyG1PointsBytes, pubkeyG2Bytes, messageHash) | ||
if err != nil { | ||
log.Fatalf("Error during verification: %v", err) | ||
} | ||
|
||
if isValid { | ||
os.Exit(0) | ||
} else { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func verifySignature(signature []byte, pubkeyG1PointsBytes [2][]byte, pubkeyG2Bytes []byte, message []byte) (bool, error) { | ||
pubkeyG1 := bls.NewZeroG1Point() | ||
pubkeyG1.X.SetBytes(pubkeyG1PointsBytes[0]) | ||
pubkeyG1.Y.SetBytes(pubkeyG1PointsBytes[1]) | ||
|
||
pubkeyG2 := bls.NewZeroG2Point() | ||
_, err := pubkeyG2.SetBytes(pubkeyG2Bytes) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
var messageBytes [32]byte | ||
copy(messageBytes[:], message[:]) | ||
|
||
sign := bls.NewZeroSignature() | ||
_, err = sign.SetBytes(signature) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// verify the equivalence between the points in the generators | ||
valid, err := pubkeyG1.VerifyEquivalence(pubkeyG2) | ||
if err != nil || !valid { | ||
return false, err | ||
} | ||
|
||
return sign.Verify(pubkeyG2, messageBytes) | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule BLSSignatureVerifier do | ||
def verify(signature, {pubkey_g1_x, pubkey_g1_y}, bls_pubkey_g2, message) do | ||
pubkey_g1_x = <<pubkey_g1_x::unsigned-big-integer-size(256)>> | ||
pubkey_g1_y = <<pubkey_g1_y::unsigned-big-integer-size(256)>> | ||
|
||
args = [ | ||
"--signature", | ||
Base.encode16(:binary.list_to_bin(signature)), | ||
"--public-key-g1-x", | ||
Base.encode16(pubkey_g1_x), | ||
"--public-key-g1-y", | ||
Base.encode16(pubkey_g1_y), | ||
"--public-key-g2", | ||
Base.encode16(:binary.list_to_bin(bls_pubkey_g2)), | ||
"--message", | ||
Base.encode16(message) | ||
] | ||
|
||
binary_path = Path.join(:code.priv_dir(:telemetry_api), "bls_verify") | ||
{output, exit_code} = System.cmd(binary_path, args) | ||
|
||
case exit_code do | ||
0 -> {:ok, "Valid"} | ||
1 -> {:error, "Invalid signature"} | ||
_ -> {:error, "Verification failed: #{output}"} | ||
end | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
telemetry_api/lib/telemetry_api/contract_managers/bls_apk_registry.ex
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule BLSApkRegistry do | ||
require Logger | ||
|
||
@aligned_config_file System.get_env("ALIGNED_CONFIG_FILE") | ||
|
||
config_file_path = | ||
case @aligned_config_file do | ||
nil -> raise("ALIGNED_CONFIG_FILE not set in .env") | ||
file -> file | ||
end | ||
|
||
{status, config_json_string} = File.read(config_file_path) | ||
|
||
case status do | ||
:ok -> | ||
Logger.debug("Aligned deployment file read successfully") | ||
|
||
:error -> | ||
raise( | ||
"Config file not read successfully, make sure your .env is correctly created, and make sure Eigenlayer config file is correctly stored" | ||
) | ||
end | ||
|
||
@contract_address Jason.decode!(config_json_string) | ||
|> Map.get("addresses") | ||
|> Map.get("blsApkRegistry") | ||
|
||
use Ethers.Contract, | ||
abi_file: "priv/abi/IBLSApkRegistry.json", | ||
default_address: @contract_address | ||
|
||
def get_bls_apk_registry_address() do | ||
@contract_address | ||
end | ||
|
||
def get_operator_bls_pubkey(operator_address) do | ||
case BLSApkRegistry.get_registered_pubkey(operator_address) | ||
|> Ethers.call() do | ||
{:ok, data} -> | ||
{:ok, data} | ||
|
||
error -> | ||
{:error, error} | ||
end | ||
end | ||
end |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.