Commit 6a0752f6 authored by madengji's avatar madengji Committed by vipwzw

add commit value calc

parent 928b3daf
...@@ -35,6 +35,9 @@ func TestDeposit(t *testing.T) { ...@@ -35,6 +35,9 @@ func TestDeposit(t *testing.T) {
//authorize prikey="17822967620457187568904804290291537271142779717280482398091401115827760898835" //authorize prikey="17822967620457187568904804290291537271142779717280482398091401115827760898835"
//authorize pubkey="13519883267141251871527102103999205179714486518503885909948192364772977661583" //authorize pubkey="13519883267141251871527102103999205179714486518503885909948192364772977661583"
//spend prikey="10407830929890509544473717262275616077696950294748419792758056545898949331744"
//spend pubkey="12419942056983622012214804185935674735538011812395392042541464417352183370586"
r1cs := NewDeposit() r1cs := NewDeposit()
r1csBN256 := backend_bn256.Cast(r1cs) r1csBN256 := backend_bn256.Cast(r1cs)
{ {
......
package main package main
import ( import (
"strconv"
"github.com/consensys/gnark/frontend" "github.com/consensys/gnark/frontend"
twistededwards_gadget "github.com/consensys/gnark/gadgets/algebra/twistededwards" twistededwards_gadget "github.com/consensys/gnark/gadgets/algebra/twistededwards"
"github.com/consensys/gnark/gadgets/hash/mimc" "github.com/consensys/gnark/gadgets/hash/mimc"
"github.com/consensys/gurvy" "github.com/consensys/gurvy"
fr_bn256 "github.com/consensys/gurvy/bn256/fr" fr_bn256 "github.com/consensys/gurvy/bn256/fr"
"strconv"
) )
func merkelPathPart(circuit *frontend.CS, mimc mimc.MiMCGadget, noteHash *frontend.Constraint) { func merkelPathPart(circuit *frontend.CS, mimc mimc.MiMCGadget, noteHash *frontend.Constraint) {
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
mimcbn256 "github.com/consensys/gnark/crypto/hash/mimc/bn256" mimcbn256 "github.com/consensys/gnark/crypto/hash/mimc/bn256"
fr_bn256 "github.com/consensys/gurvy/bn256/fr" fr_bn256 "github.com/consensys/gurvy/bn256/fr"
"github.com/consensys/gurvy/bn256/twistededwards"
) )
// verifyCmd represents the verify command // verifyCmd represents the verify command
...@@ -34,17 +35,29 @@ var calcCmd = &cobra.Command{ ...@@ -34,17 +35,29 @@ var calcCmd = &cobra.Command{
Version: Version, Version: Version,
} }
var hashCmd = &cobra.Command{ var (
Use: "hash ...", fCommit string
Short: "read strings to calc hash", fRandom string
Run: hash, )
Version: Version,
}
func init() { func init() {
calcCmd.AddCommand(hashCmd) calcCmd.AddCommand(hashCmd)
calcCmd.AddCommand(commitCmd)
rootCmd.AddCommand(calcCmd) rootCmd.AddCommand(calcCmd)
commitCmd.PersistentFlags().StringVar(&fCommit, "value", "", "specifies commit value")
commitCmd.PersistentFlags().StringVar(&fRandom, "random", "", "specifies random value")
_ = commitCmd.MarkPersistentFlagRequired("value")
_ = commitCmd.MarkPersistentFlagRequired("random")
}
var hashCmd = &cobra.Command{
Use: "hash ...",
Short: "read strings to calc hash",
Run: hash,
Version: Version,
} }
func hash(cmd *cobra.Command, args []string) { func hash(cmd *cobra.Command, args []string) {
...@@ -72,3 +85,50 @@ func getFrString(v []byte) string { ...@@ -72,3 +85,50 @@ func getFrString(v []byte) string {
f.SetBytes(v) f.SetBytes(v)
return f.String() return f.String()
} }
var commitCmd = &cobra.Command{
Use: "commit",
Short: "commit value",
Run: commit,
Version: Version,
}
func commit(cmd *cobra.Command, args []string) {
//if len(args) < 1 {
// fmt.Println("missing input strings")
// os.Exit(-1)
//}
var basex, basey, baseHx, baseHy fr_bn256.Element
basex.SetString("5299619240641551281634865583518297030282874472190772894086521144482721001553")
basey.SetString("16950150798460657717958625567821834550301663161624707787222815936182638968203")
baseHx.SetString("10190477835300927557649934238820360529458681672073866116232821892325659279502")
baseHy.SetString("7969140283216448215269095418467361784159407896899334866715345504515077887397")
basePoint := twistededwards.NewPoint(basex, basey)
baseHPoint := twistededwards.NewPoint(baseHx, baseHy)
var frCommit, frRandom fr_bn256.Element
frCommit.SetString(fCommit).FromMont()
frRandom.SetString(fRandom).FromMont()
fmt.Println("commit", fCommit, "random", fRandom)
var commitPoint, randomPoint, finalPoint twistededwards.Point
commitPoint.ScalarMul(&basePoint, frCommit)
randomPoint.ScalarMul(&baseHPoint, frRandom)
finalPoint.Add(&commitPoint, &randomPoint)
fmt.Println("finalPoint X:", finalPoint.X.String())
fmt.Println("finalPoint Y:", finalPoint.Y.String())
//
//fmt.Println("commitX:",commitPoint.X.String())
//fmt.Println("commitY:",commitPoint.Y.String())
//
//fmt.Println("randomX:",randomPoint.X.String())
//fmt.Println("randomY:",randomPoint.Y.String())
}
...@@ -52,7 +52,6 @@ var ( ...@@ -52,7 +52,6 @@ var (
func init() { func init() {
rootCmd.AddCommand(readCmd) rootCmd.AddCommand(readCmd)
readCmd.PersistentFlags().Int32VarP(&fType, "type", "t", 0, "0: proof or vk file, 1: input file, default 0") readCmd.PersistentFlags().Int32VarP(&fType, "type", "t", 0, "0: proof or vk file, 1: input file, default 0")
_ = readCmd.MarkPersistentFlagRequired("type")
} }
......
...@@ -29,6 +29,7 @@ func ParcCmd() *cobra.Command { ...@@ -29,6 +29,7 @@ func ParcCmd() *cobra.Command {
CreateWithdrawCmd(), CreateWithdrawCmd(),
CreateConfigCmd(), CreateConfigCmd(),
CreateAuthorizeCmd(), CreateAuthorizeCmd(),
QueryCmd(),
) )
return cmd return cmd
} }
...@@ -260,14 +261,14 @@ func mixConfigVerifyKeyParaCmd() *cobra.Command { ...@@ -260,14 +261,14 @@ func mixConfigVerifyKeyParaCmd() *cobra.Command {
func addVkConfigFlags(cmd *cobra.Command) { func addVkConfigFlags(cmd *cobra.Command) {
cmd.Flags().Uint32P("action", "a", 0, "0:add,1:delete") cmd.Flags().Uint32P("action", "a", 0, "0:add,1:delete")
cmd.Flags().Uint32P("curveid", "i", 0, "zk curve id,1:bls377,2:bls381,3:bn256") cmd.Flags().Uint32P("curveid", "i", 3, "zk curve id,1:bls377,2:bls381,3:bn256")
cmd.MarkFlagRequired("curveid") cmd.MarkFlagRequired("curveid")
cmd.Flags().Uint32P("circuit", "c", 0, "mix circuit type,0:deposit,1:withdraw,2:spendinput,3:spendout,4:authorize") cmd.Flags().Uint32P("circuit", "c", 0, "mix circuit type,0:deposit,1:withdraw,2:spendinput,3:spendout,4:authorize")
cmd.MarkFlagRequired("circuit") cmd.MarkFlagRequired("circuit")
cmd.Flags().StringP("key", "k", "", "zk proof verify key") cmd.Flags().StringP("zkey", "z", "", "zk proof verify key")
cmd.MarkFlagRequired("key") cmd.MarkFlagRequired("zkey")
} }
...@@ -276,7 +277,7 @@ func createConfigVerify(cmd *cobra.Command, args []string) { ...@@ -276,7 +277,7 @@ func createConfigVerify(cmd *cobra.Command, args []string) {
action, _ := cmd.Flags().GetUint32("action") action, _ := cmd.Flags().GetUint32("action")
curveid, _ := cmd.Flags().GetUint32("curveid") curveid, _ := cmd.Flags().GetUint32("curveid")
circuit, _ := cmd.Flags().GetUint32("circuit") circuit, _ := cmd.Flags().GetUint32("circuit")
key, _ := cmd.Flags().GetString("key") key, _ := cmd.Flags().GetString("zkey")
var zkVk mixTy.ZkVerifyKey var zkVk mixTy.ZkVerifyKey
zkVk.Value = key zkVk.Value = key
...@@ -313,15 +314,15 @@ func mixConfigAuthPubKeyParaCmd() *cobra.Command { ...@@ -313,15 +314,15 @@ func mixConfigAuthPubKeyParaCmd() *cobra.Command {
func addPubKeyConfigFlags(cmd *cobra.Command) { func addPubKeyConfigFlags(cmd *cobra.Command) {
cmd.Flags().Uint32P("action", "a", 0, "0:add,1:delete") cmd.Flags().Uint32P("action", "a", 0, "0:add,1:delete")
cmd.Flags().StringP("key", "k", "", "zk proof verify key") cmd.Flags().StringP("zkey", "z", "", "zk proof verify key")
cmd.MarkFlagRequired("key") cmd.MarkFlagRequired("zkey")
} }
func createConfigPubKey(cmd *cobra.Command, args []string) { func createConfigPubKey(cmd *cobra.Command, args []string) {
paraName, _ := cmd.Flags().GetString("paraName") paraName, _ := cmd.Flags().GetString("paraName")
action, _ := cmd.Flags().GetUint32("action") action, _ := cmd.Flags().GetUint32("action")
key, _ := cmd.Flags().GetString("key") key, _ := cmd.Flags().GetString("zkey")
var pubkey mixTy.AuthorizePubKey var pubkey mixTy.AuthorizePubKey
pubkey.Value = key pubkey.Value = key
...@@ -341,3 +342,51 @@ func createConfigPubKey(cmd *cobra.Command, args []string) { ...@@ -341,3 +342,51 @@ func createConfigPubKey(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal() ctx.RunWithoutMarshal()
} }
func QueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "query cmd",
}
cmd.AddCommand(GetTreePathCmd())
return cmd
}
// GetParaInfoCmd get para chain status by height
func GetTreePathCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "path",
Short: "Get leaf tree path",
Run: treePath,
}
addGetPathCmdFlags(cmd)
return cmd
}
func addGetPathCmdFlags(cmd *cobra.Command) {
cmd.Flags().StringP("root", "r", "", "tree root hash, null allowed")
cmd.Flags().StringP("leaf", "l", "", "leaf hash")
cmd.MarkFlagRequired("leaf")
}
func treePath(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
root, _ := cmd.Flags().GetString("root")
leaf, _ := cmd.Flags().GetString("leaf")
var params rpctypes.Query4Jrpc
params.Execer = mixTy.MixX
params.FuncName = "GetTreePath"
req := mixTy.TreePathReq{
RootHash: root,
LeafHash: leaf,
}
params.Payload = types.MustPBToJSON(&req)
var res mixTy.CommitTreeProve
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
}
...@@ -108,10 +108,10 @@ func getNewCommitLeaves() (*mixTy.CommitTreeLeaves, *mixTy.CommitTreeRoots) { ...@@ -108,10 +108,10 @@ func getNewCommitLeaves() (*mixTy.CommitTreeLeaves, *mixTy.CommitTreeRoots) {
return leaves, roots return leaves, roots
} }
func initNewLeaves(leaf []byte) *types.Receipt { func initNewLeaves(leaf [][]byte) *types.Receipt {
leaves, roots := getNewCommitLeaves() leaves, roots := getNewCommitLeaves()
if len(leaf) > 0 { if len(leaf) > 0 {
leaves.Data = append(leaves.Data, leaf) leaves.Data = append(leaves.Data, leaf...)
roots.Data = append(roots.Data, calcTreeRoot(leaves)) roots.Data = append(roots.Data, calcTreeRoot(leaves))
} }
...@@ -140,7 +140,7 @@ func archiveRoots(db dbm.KV, root []byte, leaves *mixTy.CommitTreeLeaves) (*type ...@@ -140,7 +140,7 @@ func archiveRoots(db dbm.KV, root []byte, leaves *mixTy.CommitTreeLeaves) (*type
3. 归档同时初始化新的current leaves 和roots 3. 归档同时初始化新的current leaves 和roots
*/ */
func pushTree(db dbm.KV, leaf []byte) (*types.Receipt, error) { func pushTree(db dbm.KV, leaf [][]byte) (*types.Receipt, error) {
leaves, err := getCurrentCommitTreeLeaves(db) leaves, err := getCurrentCommitTreeLeaves(db)
if isNotFound(errors.Cause(err)) { if isNotFound(errors.Cause(err)) {
//系统初始状态 //系统初始状态
...@@ -160,7 +160,7 @@ func pushTree(db dbm.KV, leaf []byte) (*types.Receipt, error) { ...@@ -160,7 +160,7 @@ func pushTree(db dbm.KV, leaf []byte) (*types.Receipt, error) {
return nil, err return nil, err
} }
leaves.Data = append(leaves.Data, leaf) leaves.Data = append(leaves.Data, leaf...)
currentRoot := calcTreeRoot(leaves) currentRoot := calcTreeRoot(leaves)
roots.Data = append(roots.Data, currentRoot) roots.Data = append(roots.Data, currentRoot)
r := makeCurrentTreeReceipt(leaves, roots) r := makeCurrentTreeReceipt(leaves, roots)
......
...@@ -110,13 +110,15 @@ func (a *action) Deposit(deposit *mixTy.MixDepositAction) (*types.Receipt, error ...@@ -110,13 +110,15 @@ func (a *action) Deposit(deposit *mixTy.MixDepositAction) (*types.Receipt, error
return nil, errors.Wrapf(err, "ExecTransfer") return nil, errors.Wrapf(err, "ExecTransfer")
} }
//push new commit to merkle tree //push new commit to merkle tree
var leaves [][]byte
for _, h := range commitHashs { for _, h := range commitHashs {
rpt, err := pushTree(a.db, h) leaves = append(leaves, h)
if err != nil { }
return nil, err rpt, err := pushTree(a.db, leaves)
} if err != nil {
mergeReceipt(receipt, rpt) return nil, err
} }
mergeReceipt(receipt, rpt)
return receipt, nil return receipt, nil
......
...@@ -24,16 +24,16 @@ var ( ...@@ -24,16 +24,16 @@ var (
) )
func setPrefix() { func setPrefix() {
verifyKeys = "mavl-mixcoin-verify-keys-" verifyKeys = "mavl-mix-verify-keys-"
authPubKeys = "mavl-mixcoin-auth-pubkeys-" authPubKeys = "mavl-mix-auth-pubkeys-"
commitTreeArchiveRoots = "mavl-mixcoin-commitTree-roots-archive-" commitTreeArchiveRoots = "mavl-mix-commitTree-roots-archive-"
commitTreeCurrentRoots = "mavl-mixcoin-commitTree-current-roots" commitTreeCurrentRoots = "mavl-mix-commitTree-current-roots"
commitTreeCurrentLeaves = "mavl-mixcoin-commitTree-current-leaves-" commitTreeCurrentLeaves = "mavl-mix-commitTree-current-leaves-"
commitTreeRootLeaves = "mavl-mixcoin-commitTree-rootLeaves-" commitTreeRootLeaves = "mavl-mix-commitTree-rootLeaves-"
authorizeHash = "mavl-mixcoin-authorizeHash" authorizeHash = "mavl-mix-authorizeHash"
authorizeSpendHash = "mavl-mixcoin-authorizeHash-spend-" authorizeSpendHash = "mavl-mix-authorizeHash-spend-"
nullifierHash = "mavl-mixcoin-nullifierHash" nullifierHash = "mavl-mix-nullifierHash"
} }
......
...@@ -137,14 +137,15 @@ func (a *action) Transfer(transfer *mixTy.MixTransferAction) (*types.Receipt, er ...@@ -137,14 +137,15 @@ func (a *action) Transfer(transfer *mixTy.MixTransferAction) (*types.Receipt, er
} }
//push new commit to merkle tree //push new commit to merkle tree
var leaves [][]byte
for _, h := range outputs { for _, h := range outputs {
rpt, err := pushTree(a.db, transferFr2Bytes(h.NoteHash)) leaves = append(leaves, transferFr2Bytes(h.NoteHash))
if err != nil {
return nil, err
}
mergeReceipt(receipt, rpt)
} }
rpt, err := pushTree(a.db, leaves)
if err != nil {
return nil, err
}
mergeReceipt(receipt, rpt)
return receipt, nil return receipt, nil
} }
...@@ -33,8 +33,8 @@ func (a *action) spendVerify(treeRootHash, nulliferHash, authorizeSpendHash stri ...@@ -33,8 +33,8 @@ func (a *action) spendVerify(treeRootHash, nulliferHash, authorizeSpendHash stri
} }
// authorize should exist if needed // authorize should exist if needed
if len(authorizeSpendHash) > 0 { if len(authorizeSpendHash) > 1 {
authKey := calcAuthorizeHashKey(authorizeSpendHash) authKey := calcAuthorizeSpendHashKey(authorizeSpendHash)
_, err = a.db.Get(authKey) _, err = a.db.Get(authKey)
if err != nil { if err != nil {
return errors.Wrapf(err, "authorize=%s", authorizeSpendHash) return errors.Wrapf(err, "authorize=%s", authorizeSpendHash)
......
...@@ -9,9 +9,10 @@ import ( ...@@ -9,9 +9,10 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/33cn/plugin/plugin/dapp/mix/types"
"testing" "testing"
"github.com/33cn/plugin/plugin/dapp/mix/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
......
...@@ -22,12 +22,12 @@ const ( ...@@ -22,12 +22,12 @@ const (
TyLogMixConfigVk = 754 TyLogMixConfigVk = 754
TyLogMixConfigAuth = 755 TyLogMixConfigAuth = 755
TyLogCurrentCommitTreeLeaves = 756 TyLogCurrentCommitTreeLeaves = 756
TyLogCurrentCommitTreeRoots = 756 TyLogCurrentCommitTreeRoots = 757
TyLogCommitTreeRootLeaves = 757 TyLogCommitTreeRootLeaves = 758
TyLogCommitTreeArchiveRoots = 758 TyLogCommitTreeArchiveRoots = 759
TyLogNulliferSet = 759 TyLogNulliferSet = 760
TyLogAuthorizeSet = 760 TyLogAuthorizeSet = 761
TyLogAuthorizeSpendSet = 761 TyLogAuthorizeSpendSet = 762
) )
//action type //action type
......
...@@ -5,8 +5,9 @@ package types ...@@ -5,8 +5,9 @@ package types
import ( import (
fmt "fmt" fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math" math "math"
proto "github.com/golang/protobuf/proto"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
......
...@@ -62,8 +62,10 @@ func (p *MixType) GetName() string { ...@@ -62,8 +62,10 @@ func (p *MixType) GetName() string {
// GetLogMap get receipt log map // GetLogMap get receipt log map
func (p *MixType) GetLogMap() map[int64]*types.LogInfo { func (p *MixType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{ return map[int64]*types.LogInfo{
TyLogMixConfigVk: {Ty: reflect.TypeOf(ZkVerifyKeys{}), Name: "LogMixConfigVk"}, TyLogMixConfigVk: {Ty: reflect.TypeOf(ZkVerifyKeys{}), Name: "LogMixConfigVk"},
TyLogMixConfigAuth: {Ty: reflect.TypeOf(AuthPubKeys{}), Name: "LogMixConfigAuthPubKey"}, TyLogMixConfigAuth: {Ty: reflect.TypeOf(AuthPubKeys{}), Name: "LogMixConfigAuthPubKey"},
TyLogCurrentCommitTreeLeaves: {Ty: reflect.TypeOf(CommitTreeLeaves{}), Name: "TyLogCommitTreeLeaves"},
TyLogCurrentCommitTreeRoots: {Ty: reflect.TypeOf(CommitTreeRoots{}), Name: "TyLogCommitTreeRoots"},
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment