mirror of
https://github.com/sec-bit/kzg-rln-go.git
synced 2026-01-09 21:37:55 -05:00
add web demo src
This commit is contained in:
164
cmd/VersionA-wasm/client.go
Normal file
164
cmd/VersionA-wasm/client.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"strings"
|
||||
"syscall/js"
|
||||
"time"
|
||||
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fr/kzg"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/sec-bit/kzg-rln-go/keypair"
|
||||
"github.com/sec-bit/kzg-rln-go/types"
|
||||
)
|
||||
|
||||
var (
|
||||
// load abi from string
|
||||
STAKE_ABI_STRING = `[ { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "bytes32", "name": "publicKeyHash", "type": "bytes32" } ], "name": "Deposit", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "bytes32", "name": "publicKeyHash", "type": "bytes32" } ], "name": "Withdraw", "type": "event" }, { "inputs": [], "name": "DEPOSIT_AMOUNT", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "WAIT_TIME", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint256[2]", "name": "X", "type": "uint256[2]" }, { "internalType": "uint256[2]", "name": "Y", "type": "uint256[2]" } ], "internalType": "struct BN254HashToG1.G2Point", "name": "publicKey", "type": "tuple" } ], "name": "deposit", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ { "internalType": "bytes", "name": "data", "type": "bytes" } ], "name": "hashToPoint", "outputs": [ { "internalType": "uint256[2]", "name": "result", "type": "uint256[2]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "bytes32", "name": "", "type": "bytes32" } ], "name": "publicKeys", "outputs": [ { "internalType": "bool", "name": "exists", "type": "bool" }, { "internalType": "uint256", "name": "timestamp", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "point", "type": "tuple" }, { "internalType": "uint256", "name": "scalar", "type": "uint256" } ], "name": "scalarMul", "outputs": [ { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "message", "type": "tuple" }, { "components": [ { "internalType": "uint256[2]", "name": "X", "type": "uint256[2]" }, { "internalType": "uint256[2]", "name": "Y", "type": "uint256[2]" } ], "internalType": "struct BN254HashToG1.G2Point", "name": "pubKey", "type": "tuple" }, { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "signature", "type": "tuple" } ], "name": "verify", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "bytes", "name": "message", "type": "bytes" }, { "components": [ { "internalType": "uint256[2]", "name": "X", "type": "uint256[2]" }, { "internalType": "uint256[2]", "name": "Y", "type": "uint256[2]" } ], "internalType": "struct BN254HashToG1.G2Point", "name": "pubKey", "type": "tuple" }, { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "signature", "type": "tuple" } ], "name": "verifyMessage", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint256[2]", "name": "X", "type": "uint256[2]" }, { "internalType": "uint256[2]", "name": "Y", "type": "uint256[2]" } ], "internalType": "struct BN254HashToG1.G2Point", "name": "publicKey", "type": "tuple" }, { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "signature", "type": "tuple" } ], "name": "withdraw90Percent", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint256[2]", "name": "X", "type": "uint256[2]" }, { "internalType": "uint256[2]", "name": "Y", "type": "uint256[2]" } ], "internalType": "struct BN254HashToG1.G2Point", "name": "publicKey", "type": "tuple" }, { "components": [ { "internalType": "uint256", "name": "X", "type": "uint256" }, { "internalType": "uint256", "name": "Y", "type": "uint256" } ], "internalType": "struct BN254HashToG1.G1Point", "name": "signature", "type": "tuple" } ], "name": "withdrawWaitFor1day", "outputs": [], "stateMutability": "nonpayable", "type": "function" } ]`
|
||||
STAKE_ABI, _ = abi.JSON(strings.NewReader(STAKE_ABI_STRING))
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
limit int
|
||||
srs *kzg.SRS
|
||||
polynomial []fr.Element
|
||||
keyPairProof *keypair.KeyPairProof
|
||||
commitment kzg.Digest
|
||||
nonce int
|
||||
points []*types.Point
|
||||
server *Server
|
||||
}
|
||||
|
||||
func NewClient(limit int, srs *kzg.SRS) *Client {
|
||||
return &Client{limit, srs, nil, nil, kzg.Digest{}, 0, nil, nil}
|
||||
}
|
||||
|
||||
func (c *Client) genNewPoly(this js.Value, args []js.Value) interface{} {
|
||||
start := time.Now()
|
||||
poly, commitment, proof := GenerateRandomPolynomialAndPrivateKey(c.srs, c.limit)
|
||||
elapsed := time.Since(start)
|
||||
c.polynomial = poly
|
||||
c.commitment = commitment
|
||||
c.keyPairProof = proof
|
||||
|
||||
proof.PrivateKey = nil // discard private key
|
||||
commitmentHex := fmt.Sprintf("%02x", commitment.Bytes())
|
||||
|
||||
polyB, err := json.Marshal(poly)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
proofB, err := json.Marshal(proof)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
js.Global().Get("document").Call("getElementById", "commitment").Set("value", commitmentHex)
|
||||
js.Global().Get("document").Call("getElementById", "keyProof").Set("value", string(proofB))
|
||||
js.Global().Get("document").Call("getElementById", "coefficients").Set("value", string(polyB))
|
||||
|
||||
response := fmt.Sprintf("Generate Polynomial And Proof took %s", elapsed)
|
||||
js.Global().Get("document").Call("getElementById", "rightTextArea").Set("value", response)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) registerPubKeyOnChain(this js.Value, args []js.Value) interface{} {
|
||||
response := fmt.Sprintf("Server received: %s", "register")
|
||||
js.Global().Get("document").Call("getElementById", "leftTextArea").Set("value", response)
|
||||
pubKeyProofStr := args[0].String()
|
||||
fmt.Printf("pubKeyProofStr: %s\n", pubKeyProofStr)
|
||||
var keyPairProof keypair.KeyPairProof
|
||||
err := json.Unmarshal([]byte(pubKeyProofStr), &keyPairProof)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
spew.Dump(keyPairProof)
|
||||
|
||||
pubKeyXA0 := new(big.Int)
|
||||
pubKeyXA1 := new(big.Int)
|
||||
pubKeyYA0 := new(big.Int)
|
||||
pubKeyYA1 := new(big.Int)
|
||||
keyPairProof.PublicKeyG2Aff.X.A0.BigInt(pubKeyXA0)
|
||||
keyPairProof.PublicKeyG2Aff.X.A1.BigInt(pubKeyXA1)
|
||||
keyPairProof.PublicKeyG2Aff.Y.A0.BigInt(pubKeyYA0)
|
||||
keyPairProof.PublicKeyG2Aff.Y.A1.BigInt(pubKeyYA1)
|
||||
var pubKeyG2 BN254HashToG1G2Point
|
||||
pubKeyG2.X = [2]*big.Int{pubKeyXA0, pubKeyXA1}
|
||||
pubKeyG2.Y = [2]*big.Int{pubKeyYA0, pubKeyYA1}
|
||||
target := "0x5FbDB2315678afecb367f032d93F642f64180aa3"
|
||||
calldata := GenerateDepositArgs(pubKeyG2)
|
||||
resultMap := struct {
|
||||
Target string `json:"target"`
|
||||
CallData string `json:"calldata"`
|
||||
}{
|
||||
Target: target,
|
||||
CallData: "0x" + hex.EncodeToString(calldata),
|
||||
}
|
||||
|
||||
jsonString, err := json.Marshal(resultMap)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling resultMap:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return string(jsonString)
|
||||
}
|
||||
|
||||
func (c *Client) sendMessage(this js.Value, args []js.Value) interface{} {
|
||||
message := args[0].String()
|
||||
|
||||
var frMsg fr.Element
|
||||
hasher := crypto.SHA256.New()
|
||||
hasher.Write([]byte(fmt.Sprint(c.nonce)))
|
||||
hasher.Write([]byte(message))
|
||||
frMsg.SetBytes(hasher.Sum(nil))
|
||||
start := time.Now()
|
||||
msgProof, err := kzg.Open(c.polynomial, frMsg, c.srs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("Message proof generation took %s", elapsed)
|
||||
|
||||
response := fmt.Sprintf("Msg: [%s] (proof took: %s)", message, elapsed)
|
||||
Print("rightTextArea", response)
|
||||
var msg types.Message
|
||||
msg.Commitment = c.commitment
|
||||
msg.Text = message
|
||||
msg.Proof = msgProof
|
||||
msgJson, err := json.Marshal(&msg)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
// spew.Dump(msgJson)
|
||||
// c.server.ReceiveMessage(msg)
|
||||
c.nonce++
|
||||
return string(msgJson)
|
||||
}
|
||||
|
||||
func GenerateRandomPolynomialAndPrivateKey(srs *kzg.SRS, limit int) (poly []fr.Element, commitment kzg.Digest, keyPairProof *keypair.KeyPairProof) {
|
||||
if limit > len(srs.G1) {
|
||||
log.Fatalf("Limit %d is greater than srs size %d", limit, len(srs.G1))
|
||||
}
|
||||
poly = keypair.RandomPolynomial(limit)
|
||||
commitment, keyPairProof = keypair.GenerateCommitmentAndKeyPairProof(poly, srs)
|
||||
log.Printf("commitment: %s", commitment.String())
|
||||
log.Printf("User created\n Private key: %s\n\nPublicKey: %s", keyPairProof.PrivateKey.String(), keyPairProof.PublicKeyG1Aff.String())
|
||||
return
|
||||
}
|
||||
|
||||
func GenerateDepositArgs(pubkeyG2 BN254HashToG1G2Point) []byte {
|
||||
ret, _ := STAKE_ABI.Pack("deposit", pubkeyG2)
|
||||
return ret
|
||||
}
|
||||
54
cmd/VersionA-wasm/main.go
Normal file
54
cmd/VersionA-wasm/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/consensys/gnark-crypto/ecc"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fr/kzg"
|
||||
)
|
||||
|
||||
var (
|
||||
// srs re-used accross tests of the KZG scheme
|
||||
GLOBAL_SRS *kzg.SRS
|
||||
)
|
||||
|
||||
const (
|
||||
MESSAGE_LIMIT = 10
|
||||
)
|
||||
|
||||
func init() {
|
||||
const srsSize = MESSAGE_LIMIT
|
||||
GLOBAL_SRS, _ = kzg.NewSRS(ecc.NextPowerOfTwo(srsSize), new(big.Int).SetInt64(42))
|
||||
}
|
||||
|
||||
func registerCallbacks() {
|
||||
fmt.Printf("srs length: %d\n", len(GLOBAL_SRS.G1))
|
||||
server := NewServer(len(GLOBAL_SRS.G1), GLOBAL_SRS)
|
||||
client := NewClient(len(GLOBAL_SRS.G1), GLOBAL_SRS)
|
||||
client.server = server
|
||||
js.Global().Set("sendMessage", js.FuncOf(client.sendMessage))
|
||||
js.Global().Set("genNewPoly", js.FuncOf(client.genNewPoly))
|
||||
js.Global().Set("registerPubKeyOnChain", js.FuncOf(client.registerPubKeyOnChain))
|
||||
js.Global().Set("registerOnServer", js.FuncOf(server.registerOnServer))
|
||||
js.Global().Set("receiveMessage", js.FuncOf(server.receiveMessage))
|
||||
|
||||
}
|
||||
|
||||
func Print(elementId, msg string) {
|
||||
currentText := js.Global().Get("document").Call("getElementById", elementId).Get("value").String()
|
||||
if currentText == "" {
|
||||
currentText = msg
|
||||
} else {
|
||||
currentText += "\n" + msg
|
||||
}
|
||||
js.Global().Get("document").Call("getElementById", elementId).Set("value", currentText)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Printf("WASM Go Initialized\n")
|
||||
c := make(chan struct{})
|
||||
registerCallbacks()
|
||||
<-c
|
||||
}
|
||||
184
cmd/VersionA-wasm/server.go
Normal file
184
cmd/VersionA-wasm/server.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"syscall/js"
|
||||
"time"
|
||||
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fr/kzg"
|
||||
"github.com/sec-bit/kzg-rln-go/interpolation"
|
||||
"github.com/sec-bit/kzg-rln-go/keypair"
|
||||
"github.com/sec-bit/kzg-rln-go/types"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
limit int
|
||||
srs *kzg.SRS
|
||||
users map[string]*Client
|
||||
}
|
||||
|
||||
func NewServer(limit int, srs *kzg.SRS) *Server {
|
||||
users := make(map[string]*Client)
|
||||
return &Server{limit, srs, users}
|
||||
}
|
||||
|
||||
func (s *Server) registerOnServer(this js.Value, args []js.Value) interface{} {
|
||||
commitmentStr := args[0].String()
|
||||
keyPairProofStr := args[1].String()
|
||||
fmt.Printf("Server received user registration, commitment: %s, keyPairProof: %s\n", commitmentStr, keyPairProofStr)
|
||||
var commitment kzg.Digest
|
||||
commitmentB, err := hex.DecodeString(commitmentStr)
|
||||
if err != nil {
|
||||
fmt.Println("[E]", commitmentStr, err)
|
||||
return nil
|
||||
}
|
||||
commitment.SetBytes(commitmentB)
|
||||
if s.users[string(commitmentB)] != nil {
|
||||
fmt.Println("[E] user already registered")
|
||||
return nil
|
||||
}
|
||||
// spew.Dump(commitment)
|
||||
var keyPairProof keypair.KeyPairProof
|
||||
err = json.Unmarshal([]byte(keyPairProofStr), &keyPairProof)
|
||||
if err != nil {
|
||||
fmt.Println("[E]", keyPairProofStr, err)
|
||||
return nil
|
||||
}
|
||||
// spew.Dump(keyPairProof)
|
||||
|
||||
hash := sha3.NewLegacyKeccak256()
|
||||
pubKeyXA0 := new(big.Int)
|
||||
pubKeyXA1 := new(big.Int)
|
||||
pubKeyYA0 := new(big.Int)
|
||||
pubKeyYA1 := new(big.Int)
|
||||
keyPairProof.PublicKeyG2Aff.X.A0.BigInt(pubKeyXA0)
|
||||
keyPairProof.PublicKeyG2Aff.X.A1.BigInt(pubKeyXA1)
|
||||
keyPairProof.PublicKeyG2Aff.Y.A0.BigInt(pubKeyYA0)
|
||||
keyPairProof.PublicKeyG2Aff.Y.A1.BigInt(pubKeyYA1)
|
||||
hash.Write(pubKeyXA0.Bytes())
|
||||
hash.Write(pubKeyXA1.Bytes())
|
||||
hash.Write(pubKeyYA0.Bytes())
|
||||
hash.Write(pubKeyYA1.Bytes())
|
||||
var keyHash [32]byte
|
||||
copy(keyHash[:], hash.Sum(nil))
|
||||
log.Printf("PubKeyHash: 0x%02x", keyHash)
|
||||
|
||||
start := time.Now()
|
||||
err = keypair.VerifyPubKey(&commitment, &keyPairProof, s.srs)
|
||||
if err != nil {
|
||||
fmt.Printf("VerifyPubKey failed: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
// pairing check for publicKeyG1 and publicKeyG2
|
||||
eLeft, err := bn254.Pair([]bn254.G1Affine{keyPairProof.PublicKeyG1Aff}, []bn254.G2Affine{s.srs.G2[0]})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
eRight, err := bn254.Pair([]bn254.G1Affine{s.srs.G1[0]}, []bn254.G2Affine{keyPairProof.PublicKeyG2Aff})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !eLeft.Equal(&eRight) {
|
||||
fmt.Printf("pubkey G1 G2 pairing check failed: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("VerifyPubKey time: %s", elapsed)
|
||||
response := fmt.Sprintf("Register success, commitment: %s, verify pubkey took: %s", commitmentStr, elapsed)
|
||||
log.Println(response)
|
||||
Print("leftTextArea", response)
|
||||
client := NewClient(s.limit, s.srs)
|
||||
client.commitment = commitment
|
||||
client.keyPairProof = &keyPairProof
|
||||
s.users[string(commitmentB)] = client
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) receiveMessage(this js.Value, args []js.Value) interface{} {
|
||||
msgStr := args[0].String()
|
||||
var msg types.Message
|
||||
err := json.Unmarshal([]byte(msgStr), &msg)
|
||||
if err != nil {
|
||||
log.Printf("unmarshal failed: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
commitmentB := msg.Commitment.Bytes()
|
||||
user, ok := s.users[string(commitmentB[:])]
|
||||
if !ok {
|
||||
log.Printf("user not registered")
|
||||
return nil
|
||||
}
|
||||
// if user.nonce >= 95 {
|
||||
// userInput <- msg.Text
|
||||
// } else {
|
||||
log.Printf("Server received message: %s", msg.Text)
|
||||
// }
|
||||
// verify message
|
||||
hasher := crypto.SHA256.New()
|
||||
hasher.Write([]byte(fmt.Sprint(user.nonce)))
|
||||
hasher.Write([]byte(msg.Text))
|
||||
var frMsg fr.Element
|
||||
frMsg.SetBytes(hasher.Sum(nil))
|
||||
start := time.Now()
|
||||
err = kzg.Verify(&msg.Commitment, &msg.Proof, frMsg, s.srs)
|
||||
if err != nil {
|
||||
log.Printf("verify failed: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("verify time: %s", elapsed)
|
||||
|
||||
user.nonce++
|
||||
response := fmt.Sprintf("Recv: [%s] (verify took: %s) used(%d/%d)", msg.Text, elapsed, user.nonce, user.limit-1)
|
||||
Print("leftTextArea", response)
|
||||
|
||||
user.points = append(user.points, &types.Point{X: frMsg, Y: msg.Proof.ClaimedValue})
|
||||
// update nonce
|
||||
fmt.Printf("user nonce: %d, limit: %d\n", user.nonce, user.limit)
|
||||
if user.nonce >= user.limit {
|
||||
privateKey := interpolation.RecoverPrivateKeyByPoints(user.points[:])
|
||||
log.Printf("private key recovered: %s", privateKey.String())
|
||||
Print("leftTextArea", fmt.Sprintf("private key recovered: %s", privateKey.String()))
|
||||
// auth := utils.NewTransactor(client, "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a")
|
||||
// message := auth.From.Hash().Bytes()
|
||||
// log.Printf("message: %02X", message)
|
||||
// pubKey, _, signature := sign(message, privateKey)
|
||||
// var pubKeyPoint BN254HashToG1G2Point
|
||||
// var sigPoint BN254HashToG1G1Point
|
||||
|
||||
// pubKeyXA0 := new(big.Int)
|
||||
// pubKeyXA1 := new(big.Int)
|
||||
// pubKeyYA0 := new(big.Int)
|
||||
// pubKeyYA1 := new(big.Int)
|
||||
// pubKey.X.A0.BigInt(pubKeyXA0)
|
||||
// pubKey.X.A1.BigInt(pubKeyXA1)
|
||||
// pubKey.Y.A0.BigInt(pubKeyYA0)
|
||||
// pubKey.Y.A1.BigInt(pubKeyYA1)
|
||||
|
||||
// signatureX := new(big.Int)
|
||||
// signatureY := new(big.Int)
|
||||
// signature.X.BigInt(signatureX)
|
||||
// signature.Y.BigInt(signatureY)
|
||||
|
||||
// pubKeyPoint.X = [2]*big.Int{pubKeyXA0, pubKeyXA1}
|
||||
// pubKeyPoint.Y = [2]*big.Int{pubKeyYA0, pubKeyYA1}
|
||||
// sigPoint.X = signatureX
|
||||
// sigPoint.Y = signatureY
|
||||
// tx, err := stake.Withdraw90Percent(auth, pubKeyPoint, sigPoint)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// log.Printf("tx sent: %s", tx.Hash().String())
|
||||
log.Printf("Slash 90 percent of stake 💸💸💸")
|
||||
// log.Printf("Reward to the user: %s", auth.From.Hex())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
90
cmd/VersionA-wasm/signature.go
Normal file
90
cmd/VersionA-wasm/signature.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
|
||||
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
func hashToPoint(data []byte) bn254.G1Affine {
|
||||
var X, Y fp.Element
|
||||
X.SetString("0x059dac1925a1d0bee704dd2ae3836a3d8e76a4c4249f17860ce1d0a530c5f8f7")
|
||||
Y.SetString("0x03870b29cb77fab35c1394ac29e19344465046309674e8d138da412f834ecaee")
|
||||
nothingUpMySleeve := &bn254.G1Affine{
|
||||
X: X,
|
||||
Y: Y,
|
||||
}
|
||||
|
||||
hash := sha3.NewLegacyKeccak256()
|
||||
hash.Write(data)
|
||||
hashedData := hash.Sum(nil)
|
||||
hashedBigInt := new(big.Int).SetBytes(hashedData)
|
||||
|
||||
var result bn254.G1Affine
|
||||
result.ScalarMultiplication(nothingUpMySleeve, hashedBigInt)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func sign(message []byte, privateKey *fr.Element) (bn254.G2Affine, bn254.G1Affine, bn254.G1Affine) {
|
||||
// Hash the message to a G1 point
|
||||
hashedMsg := hashToPoint(message)
|
||||
// log.Printf("msgHash = %s", hashedMsg.String())
|
||||
|
||||
_, _, _, g2Aff := bn254.Generators()
|
||||
|
||||
// Compute the signature (in G1) by multiplying the private key with the generator of G1
|
||||
var signature bn254.G1Affine
|
||||
signature.ScalarMultiplication(&hashedMsg, privateKey.BigInt(new(big.Int)))
|
||||
|
||||
// Compute the public key (in G2) by multiplying the private key with the generator of G2
|
||||
var publicKey bn254.G2Affine
|
||||
privateKeyBN := new(big.Int)
|
||||
privateKey.BigInt(privateKeyBN)
|
||||
// fmt.Printf("privateKey = %s\n", privateKey.String())
|
||||
// fmt.Printf("privateKeyBN = %s\n", privateKeyBN.String())
|
||||
publicKey.ScalarMultiplication(&g2Aff, privateKeyBN)
|
||||
|
||||
// var negG2Aff bn254.G2Affine
|
||||
// negG2Aff.Neg(&g2Aff)
|
||||
|
||||
// negXA0 := negG2Aff.X.A0
|
||||
// negXA1 := negG2Aff.X.A1
|
||||
// negYA0 := negG2Aff.Y.A0
|
||||
// negYA1 := negG2Aff.Y.A1
|
||||
// fmt.Printf("G2Affine: X A0: %02X, X A1: %02X, Y A0: %02X, Y A1: %02X\n", negXA0.Bytes(), negXA1.Bytes(), negYA0.Bytes(), negYA1.Bytes())
|
||||
|
||||
return publicKey, hashedMsg, signature
|
||||
}
|
||||
|
||||
func verify(message []byte, publicKey bn254.G2Affine, signature bn254.G1Affine) (bool, error) {
|
||||
// Hash the message to a G1 point
|
||||
hashedMsg := hashToPoint(message)
|
||||
isOnCurve := hashedMsg.IsOnCurve()
|
||||
fmt.Printf("isOnCurve = %t\n", isOnCurve)
|
||||
// log.Printf("msgHash = %s", hashedMsg.String())
|
||||
|
||||
// Pairing check: e(hashedMsg, publicKey) == e(signature, g2)
|
||||
eLeft, err := bn254.Pair([]bn254.G1Affine{hashedMsg}, []bn254.G2Affine{publicKey})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, _, _, g2Aff := bn254.Generators()
|
||||
var g2AffNeg bn254.G2Affine
|
||||
g2AffNeg.Neg(&g2Aff)
|
||||
fmt.Printf("g2AffNeg.X.A0 = %02X\n", g2AffNeg.X.A0.Bytes())
|
||||
fmt.Printf("g2AffNeg.X.A1 = %02X\n", g2AffNeg.X.A1.Bytes())
|
||||
fmt.Printf("g2AffNeg.Y.A0 = %02X\n", g2AffNeg.Y.A0.Bytes())
|
||||
fmt.Printf("g2AffNeg.Y.A1 = %02X\n", g2AffNeg.Y.A1.Bytes())
|
||||
eRight, err := bn254.Pair([]bn254.G1Affine{signature}, []bn254.G2Affine{g2Aff})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return eLeft.Equal(&eRight), nil
|
||||
}
|
||||
775
cmd/VersionA-wasm/stake.go
Normal file
775
cmd/VersionA-wasm/stake.go
Normal file
File diff suppressed because one or more lines are too long
20
go.sum
20
go.sum
@@ -15,6 +15,7 @@ github.com/consensys/gnark v0.8.0/go.mod h1:aKmA7dIiLbTm0OV37xTq0z+Bpe4xER8EhRLi
|
||||
github.com/consensys/gnark-crypto v0.9.1 h1:mru55qKdWl3E035hAoh1jj9d7hVnYY5pfb6tmovSmII=
|
||||
github.com/consensys/gnark-crypto v0.9.1/go.mod h1:a2DQL4+5ywF6safEeZFEPGRiiGbjzGFRUN2sg06VuU4=
|
||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -29,13 +30,16 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeC
|
||||
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
|
||||
github.com/ethereum/go-ethereum v1.10.21 h1:5lqsEx92ZaZzRyOqBEXux4/UR06m296RGzN3ol3teJY=
|
||||
github.com/ethereum/go-ethereum v1.10.21/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg=
|
||||
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c=
|
||||
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
|
||||
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
|
||||
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI=
|
||||
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
|
||||
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
|
||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
|
||||
@@ -45,10 +49,13 @@ github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
|
||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpxn4uE=
|
||||
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
|
||||
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
|
||||
github.com/holiman/uint256 v1.2.0 h1:gpSYcPLWGv4sG43I2mVLiDZCNDh/EpGjSk8tmtxitHM=
|
||||
github.com/huin/goupnp v1.0.3 h1:N8No57ls+MnjlB+JPiCVSOyy/ot7MJTqlo7rn+NYSqQ=
|
||||
github.com/iden3/go-iden3-crypto v0.0.13 h1:ixWRiaqDULNyIDdOWz2QQJG5t4PpNHkQk2P6GV94cok=
|
||||
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
|
||||
@@ -60,6 +67,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/minya-konka/pedersen v0.0.0-20221228123429-151d98c6740c h1:zyxTgZ6hAmtK06aqgRfzDi7i1fJo8C8vlVwK0G+wHrc=
|
||||
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
|
||||
github.com/mitchellh/pointerstructure v1.2.0 h1:O+i9nHnXS3l/9Wu7r4NrEdwA2VFTicjUEN1uBnDo34A=
|
||||
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
|
||||
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
|
||||
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
|
||||
@@ -73,11 +82,16 @@ github.com/rjeczalik/notify v0.9.1 h1:CLCKso/QK1snAlnhNR/CNvNiFU2saUtjV0bx3EwNeC
|
||||
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
|
||||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
|
||||
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/sashabaranov/go-openai v1.9.3 h1:uNak3Rn5pPsKRs9bdT7RqRZEyej/zdZOEI2/8wvrFtM=
|
||||
github.com/sashabaranov/go-openai v1.9.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
|
||||
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
@@ -90,15 +104,21 @@ github.com/tklauser/go-sysconf v0.3.5 h1:uu3Xl4nkLzQfXNsWn15rPc/HQCJKObbt1dKJeWp
|
||||
github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
|
||||
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
|
||||
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
|
||||
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef h1:wHSqTBrZW24CsNJDfeh9Ex6Pm0Rcpc7qrgKBiL44vF4=
|
||||
github.com/urfave/cli/v2 v2.10.2 h1:x3p8awjp/2arX+Nl/G2040AZpOCHS/eMJJ1/a+mye4Y=
|
||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
|
||||
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
|
||||
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
|
||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
|
||||
|
||||
@@ -27,7 +27,7 @@ func pRandomPolynomial(size int) []fr.Element {
|
||||
|
||||
type KeyPairProof struct {
|
||||
H bn254.G1Affine
|
||||
PrivateKey fr.Element
|
||||
PrivateKey *fr.Element
|
||||
PublicKeyG1Aff bn254.G1Affine
|
||||
PublicKeyG2Aff bn254.G2Affine
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func GenerateCommitmentAndKeyPairProof(poly []fr.Element, srs *kzg.SRS) (kzg.Dig
|
||||
publicKeyG2.ScalarMultiplication(&srs.G2[0], privateKey)
|
||||
|
||||
pubKeyProof := new(KeyPairProof)
|
||||
pubKeyProof.PrivateKey = proof.ClaimedValue
|
||||
pubKeyProof.PrivateKey = &proof.ClaimedValue
|
||||
pubKeyProof.H = proof.H
|
||||
pubKeyProof.PublicKeyG1Aff = *publicKey
|
||||
pubKeyProof.PublicKeyG2Aff = *publicKeyG2
|
||||
|
||||
Reference in New Issue
Block a user