Musig2 Sr25519 Api For Go

Import

import (
	musig2sr25519 "github.com/chainx-org/musig2-sr25519-go-api/musig2-sr25519"
)

Api

Musig2

GetRound1State()

To get the State of the first round
Returns: State Pointer If the calculation fails just a null pointer will be returned.

GetMyPrivkey(phrase)

Pass in the phrase to get the private key.
Returns: <String>
Return a 32-byte private key key string.
Possible error string returned is `InvalidPhrase`.

GetMyPubkey(private)

Help to get the PublicKey from privkey
Returns: pubkey string Possible errors are Null KeyPair Pointer and Normal Error.

GetRound1Msg(state)

Passed round1 State to generate msg which will broadcast
Returns: msg String. 
Possible errors are Normal Error and Null Round1 State Pointer.

EncodeRound1State(state)

encode State object.
Returns: state String 
Possible error is Null Round1 State Pointer or Encode Fail.

DecodeRound1State(round1_state)

Use string to decode State object.
Returns: State. 
Failure will return a null pointer.

GetRound2Msg(state, msg, privkey, pubkeys, received_round1_msg)

It takes a lot of preparation to switch to round2 state(StatePrime). You need the round1 State, the message to sign for it, your own private key, everyone's public key, and everyone else's msgs from the round1.
Returns: StatePrime Pointer. 
Failure will return a null pointer.

GetAggSignature(round2_msg)

To construct a signature requires the status of the round2 msg about the second round of all other signers, and its own R.
Returns: signature String. 
Possible errors are Normal Error and Null Round2 State Pointer.

GetAggPublicKey([pubkeys])

Pass in the public key to generate the aggregated public key
Returns: pubkey String. 
Possible error is Normal Error.

Mast

GenerateThresholdPubkey(pubkeys, threshold)

Generate threshold signature addresses by passing in 
all signer public keys and signature thresholds.
Returns: <String>
Return the public key of the threshold-signature address.
Possible error string returned is `Invalid Public Bytes`.

GenerateControlBlock(pubkeys, threshold, aggPubkey)

Generate a proof of the aggregated public key by 
passing in the public key and signature threshold of 
all signers and the aggregated public key of everyone 
who performed the signature this time.
Returns: <String>
Return signed proofs for transaction validation.
Possible error string returned is `Invalid Public Bytes`.

Example

The specific usage can be viewed in musig2sr25519demo.This example simulates three people generating aggregated public keys and aggregated signatures.

Details

Musig2

  • Pass in the phrase to generate private key
privateA, error := musig2sr25519.GetMyPrivkey(phrase1)
  • Pass in the private key to declare a State pointer and get my pubkey

round1StateA := musig2sr25519.GetRound1State()
publicA, error := musig2sr25519.GetMyPubkey(privateA)
  • Use State pointer to round1 message.
round1MsgA, error := musig2sr25519.GetRound1Msg(round1StateA)
  • Round1 state object serialization
encodedRound1StateA := musig2sr25519.EncodeRound1State(round1StateA)
  • Round1 state object deserialization
round1StateA = musig2sr25519.DecodeRound1State(encodedRound1StateA)
  • Pass the self-generated my_pubkey and round1 message to the other two parties, and the other two parties do the same. In the end, I got the public key and round1 message of the other two parties. Pass in the getRound2Msg function to generate my own round2 message.
round2MsgA, error := musig2sr25519.GetRound2Msg(round1StateA, msg, privateA, pubkeys, []string{round1MsgB, round1MsgC})
  • Pass the round1 message to the other two parties, and I can finally generate the signature by getAggSignature.
sig, error := musig2sr25519.GetAggSignature([]string{round2MsgA, round2MsgB, round2MsgC})
  • When I obtain the public keys of the other two parties, I can generate the aggregate public key.
aggPubkey, error := musig2sr25519.GetAggPublicKey(pubkeys)

Mast

  • The threshold signature address can be generated by using the threshold and the public key of the all participant.
thresholdPubkey, error := musig2sr25519.GenerateThresholdPubkey(pubkeys, 2)
  • Using the threshold, the public keys of all participants and the aggregation of the public keys can generate a proof.
control, error := musig2sr25519.GenerateControlBlock(pubkeys, 2, pubkeyAB)

GitHub

View Github