|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "flag" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + |
| 9 | + bls "github.com/Layr-Labs/eigensdk-go/crypto/bls" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + signatureArg := flag.String("signature", "", "BLS signature bytes") |
| 14 | + publicKeyG1X := flag.String("public-key-g1-x", "", "BLS public key on g1 affine x coord") |
| 15 | + publicKeyG1Y := flag.String("public-key-g1-y", "", "BLS public key on g1 affine y coord") |
| 16 | + publicKeyG2Arg := flag.String("public-key-g2", "", "BLS public key on g2") |
| 17 | + messageArg := flag.String("message", "", "Hex-encoded message") |
| 18 | + |
| 19 | + flag.Parse() |
| 20 | + |
| 21 | + if *signatureArg == "" || *publicKeyG1X == "" || *publicKeyG1Y == "" || *publicKeyG2Arg == "" || *messageArg == "" { |
| 22 | + log.Fatalf("All arguments (signature, publickey g1 hash, publickey g2, and messagehash) are required") |
| 23 | + } |
| 24 | + |
| 25 | + signature, err := hex.DecodeString(*signatureArg) |
| 26 | + if err != nil { |
| 27 | + log.Fatalf("Failed to decode signature: %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + var pubkeyG1PointsBytes [2][]byte |
| 31 | + xBytes, err := hex.DecodeString(*publicKeyG1X) |
| 32 | + if err != nil { |
| 33 | + log.Fatalf("Failed to decode G1 X: %v", err) |
| 34 | + } |
| 35 | + yBytes, err := hex.DecodeString(*publicKeyG1Y) |
| 36 | + if err != nil { |
| 37 | + log.Fatalf("Failed to decode G1 Y: %v", err) |
| 38 | + } |
| 39 | + pubkeyG1PointsBytes[0] = xBytes |
| 40 | + pubkeyG1PointsBytes[1] = yBytes |
| 41 | + |
| 42 | + pubkeyG2Bytes, err := hex.DecodeString(*publicKeyG2Arg) |
| 43 | + if err != nil { |
| 44 | + log.Fatalf("Failed to decode pubkey: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + messageHash, err := hex.DecodeString(*messageArg) |
| 48 | + if err != nil { |
| 49 | + log.Fatalf("Failed to decode message hash: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + isValid, err := verifySignature(signature, pubkeyG1PointsBytes, pubkeyG2Bytes, messageHash) |
| 53 | + if err != nil { |
| 54 | + log.Fatalf("Error during verification: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + if isValid { |
| 58 | + os.Exit(0) |
| 59 | + } else { |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func verifySignature(signature []byte, pubkeyG1PointsBytes [2][]byte, pubkeyG2Bytes []byte, message []byte) (bool, error) { |
| 65 | + pubkeyG1 := bls.NewZeroG1Point() |
| 66 | + pubkeyG1.X.SetBytes(pubkeyG1PointsBytes[0]) |
| 67 | + pubkeyG1.Y.SetBytes(pubkeyG1PointsBytes[1]) |
| 68 | + |
| 69 | + pubkeyG2 := bls.NewZeroG2Point() |
| 70 | + _, err := pubkeyG2.SetBytes(pubkeyG2Bytes) |
| 71 | + if err != nil { |
| 72 | + return false, err |
| 73 | + } |
| 74 | + |
| 75 | + var messageBytes [32]byte |
| 76 | + copy(messageBytes[:], message[:]) |
| 77 | + |
| 78 | + sign := bls.NewZeroSignature() |
| 79 | + _, err = sign.SetBytes(signature) |
| 80 | + if err != nil { |
| 81 | + return false, err |
| 82 | + } |
| 83 | + |
| 84 | + // verify the equivalence between the points in the generators |
| 85 | + valid, err := pubkeyG1.VerifyEquivalence(pubkeyG2) |
| 86 | + if err != nil || !valid { |
| 87 | + return false, err |
| 88 | + } |
| 89 | + |
| 90 | + return sign.Verify(pubkeyG2, messageBytes) |
| 91 | +} |
0 commit comments