Skip to content

Commit 9b00732

Browse files
authored
removing zk tree usage (#1529)
* wip: removing zk tree usage * simplify tree * fix unit tests + update lint version * fix test and some p2p deps * disable nft tests for now * disable nft test * review comment
1 parent 9da4fb3 commit 9b00732

22 files changed

+195
-579
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ install-deps: ## Install Dependencies
2323
@go install github.com/swaggo/swag/cmd/swag
2424
@go install github.com/ethereum/go-ethereum/cmd/abigen
2525
@git submodule update --init --recursive
26-
@command -v golangci-lint >/dev/null 2>&1 || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin v1.45.2)
26+
@command -v golangci-lint >/dev/null 2>&1 || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin v1.46.2)
2727

2828
lint-check: ## runs linters on go code
2929
@golangci-lint run --skip-dirs=build/* --disable-all --enable=revive --enable=goimports --enable=vet --enable=nakedret \

centchain/api_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func TestMain(m *testing.M) {
3838

3939
func TestApi_GetMetadataLatest(t *testing.T) {
4040
mockSAPI := new(MockSubstrateAPI)
41-
mockSAPI.On("GetMetadataLatest").Return(types.NewMetadataV8(), nil).Once()
41+
mockSAPI.On("GetMetadataLatest").Return(types.NewMetadataV14(), nil).Once()
4242
api := NewAPI(mockSAPI, nil, nil)
4343
meta, err := api.GetMetadataLatest()
4444
assert.NoError(t, err)
45-
assert.Equal(t, types.NewMetadataV8(), meta)
45+
assert.Equal(t, types.NewMetadataV14(), meta)
4646
}
4747

4848
func TestApi_Call(t *testing.T) {

documents/coredocument.go

+26-109
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99
"time"
1010

11+
"golang.org/x/crypto/blake2b"
12+
1113
coredocumentpb "github.com/centrifuge/centrifuge-protobufs/gen/go/coredocument"
1214
"github.com/centrifuge/go-centrifuge/contextutil"
1315
"github.com/centrifuge/go-centrifuge/crypto"
@@ -46,9 +48,6 @@ const (
4648
// BasicDataRootField represents the basic document data tree root
4749
BasicDataRootField = "bd_root"
4850

49-
// ZKDataRootField represents the zk document data tree root
50-
ZKDataRootField = "zkd_root"
51-
5251
// SignaturesRootField represents the signatures property of a tree
5352
SignaturesRootField = "signatures_root"
5453

@@ -64,9 +63,6 @@ const (
6463
// BasicDataRootPrefix represents the basic document data tree
6564
BasicDataRootPrefix = "bdr_tree"
6665

67-
// ZKDataRootPrefix represents the zk document data tree
68-
ZKDataRootPrefix = "zkdr_tree"
69-
7066
// SigningTreePrefix is the human readable prefix for signing tree props
7167
SigningTreePrefix = "signing_tree"
7268

@@ -103,8 +99,6 @@ func CompactProperties(key string) []byte {
10399
return []byte{0, 0, 0, 10}
104100
case BasicDataRootField:
105101
return []byte{0, 0, 0, 11}
106-
case ZKDataRootField:
107-
return []byte{0, 0, 0, 12}
108102
case CDTreePrefix:
109103
return []byte{1, 0, 0, 0}
110104
case SigningTreePrefix:
@@ -115,8 +109,6 @@ func CompactProperties(key string) []byte {
115109
return []byte{4, 0, 0, 0}
116110
case BasicDataRootPrefix:
117111
return []byte{5, 0, 0, 0}
118-
case ZKDataRootPrefix:
119-
return []byte{6, 0, 0, 0}
120112
default:
121113
return []byte{}
122114
}
@@ -411,49 +403,37 @@ func newRoleWithCollaborators(collaborators ...identity.DID) *coredocumentpb.Rol
411403

412404
// CreateProofs takes document data leaves and list of fields and generates proofs.
413405
func (cd *CoreDocument) CreateProofs(docType string, dataLeaves []proofs.LeafNode, fields []string) (*DocumentProof, error) {
414-
return cd.createProofs(false, docType, dataLeaves, fields)
415-
}
416-
417-
// CreateProofsFromZKTree takes document data leaves and list of fields and generates proofs from ZK-ready Tree.
418-
func (cd *CoreDocument) CreateProofsFromZKTree(docType string, dataLeaves []proofs.LeafNode, fields []string) (*DocumentProof, error) {
419-
return cd.createProofs(true, docType, dataLeaves, fields)
406+
return cd.createProofs(docType, dataLeaves, fields)
420407
}
421408

422409
// createProofs takes document data tree and list to fields and generates proofs.
423410
// it will generate proofs from the dataTree and cdTree.
424411
// It only generates proofs up to the root of the tree/s that correspond to
425-
func (cd *CoreDocument) createProofs(fromZKTree bool, docType string, dataLeaves []proofs.LeafNode, fields []string) (*DocumentProof, error) {
412+
func (cd *CoreDocument) createProofs(docType string, dataLeaves []proofs.LeafNode, fields []string) (*DocumentProof, error) {
426413
treeProofs := make(map[string]*proofs.DocumentTree, 4)
427414
drTree, err := cd.DocumentRootTree(docType, dataLeaves)
428415
if err != nil {
429416
return nil, err
430417
}
431418

432-
signatureTree, err := cd.GetSignaturesDataTree()
419+
signaturesTree, err := cd.GetSignaturesDataTree()
433420
if err != nil {
434421
return nil, errors.NewTypedError(ErrCDTree, errors.New("failed to generate signatures tree: %v", err))
435422
}
436423

437-
dTrees, sdr, err := cd.SigningDataTrees(docType, dataLeaves)
424+
signingTree, err := cd.SigningDataTree(docType, dataLeaves)
438425
if err != nil {
439426
return nil, errors.NewTypedError(ErrCDTree, errors.New("failed to generate signing data trees: %v", err))
440427
}
441-
basicDataTree := dTrees[0]
442-
zkDataTree := dTrees[1]
443428

444429
dataPrefix, err := getDataTreePrefix(dataLeaves)
445430
if err != nil {
446431
return nil, err
447432
}
448433

449-
targetTree := basicDataTree
450-
if fromZKTree {
451-
targetTree = zkDataTree
452-
}
453-
454-
treeProofs[dataPrefix] = targetTree
434+
treeProofs[dataPrefix] = signingTree
455435
treeProofs[CDTreePrefix] = treeProofs[dataPrefix]
456-
treeProofs[SignaturesTreePrefix] = signatureTree
436+
treeProofs[SignaturesTreePrefix] = signaturesTree
457437
treeProofs[DRTreePrefix] = drTree
458438

459439
rawProofs, err := generateProofs(fields, treeProofs)
@@ -463,10 +443,8 @@ func (cd *CoreDocument) createProofs(fromZKTree bool, docType string, dataLeaves
463443

464444
return &DocumentProof{
465445
FieldProofs: rawProofs,
466-
LeftDataRooot: basicDataTree.RootHash(),
467-
RightDataRoot: zkDataTree.RootHash(),
468-
SigningRoot: sdr,
469-
SignaturesRoot: signatureTree.RootHash(),
446+
SigningRoot: signingTree.RootHash(),
447+
SignaturesRoot: signaturesTree.RootHash(),
470448
}, nil
471449
}
472450

@@ -619,99 +597,34 @@ func (cd *CoreDocument) DocumentRootTree(docType string, dataLeaves []proofs.Lea
619597
return tree, nil
620598
}
621599

622-
func (cd *CoreDocument) basicDataTree(dataLeaves []proofs.LeafNode, cdLeaves []proofs.LeafNode) (tree *proofs.DocumentTree, err error) {
600+
// SigningDataTree returns the merkle tree + signingRoot Hash for the document data tree provided
601+
func (cd *CoreDocument) SigningDataTree(docType string, dataLeaves []proofs.LeafNode) (trees *proofs.DocumentTree, err error) {
623602
if dataLeaves == nil {
624603
return nil, errors.NewTypedError(ErrCDTree, errors.New("data tree is invalid"))
625604
}
626-
if cdLeaves == nil {
627-
return nil, errors.NewTypedError(ErrCDTree, errors.New("cd tree is invalid"))
628-
}
629-
// create the docDataTrees out of docData and coredoc trees
630-
tree, err = cd.DefaultTreeWithPrefix(BasicDataRootPrefix, CompactProperties(BasicDataRootPrefix))
631-
if err != nil {
632-
return nil, err
633-
}
634-
err = tree.AddLeaves(append(dataLeaves, cdLeaves...))
635-
if err != nil {
636-
return nil, err
637-
}
638-
err = tree.Generate()
605+
cdLeaves, err := cd.coredocLeaves(docType)
639606
if err != nil {
640607
return nil, err
641608
}
642-
return tree, nil
643-
}
644609

645-
func (cd *CoreDocument) zkDataTree(dataLeaves []proofs.LeafNode, cdLeaves []proofs.LeafNode) (tree *proofs.DocumentTree, err error) {
646-
if dataLeaves == nil {
647-
return nil, errors.NewTypedError(ErrCDTree, errors.New("data tree is invalid"))
648-
}
649-
if cdLeaves == nil {
650-
return nil, errors.NewTypedError(ErrCDTree, errors.New("cd tree is invalid"))
651-
}
652610
// create the docDataTrees out of docData and coredoc trees
653-
tree, err = cd.DefaultZTreeWithPrefix(ZKDataRootPrefix, CompactProperties(ZKDataRootPrefix))
611+
tree, err := cd.DefaultTreeWithPrefix(SigningTreePrefix, CompactProperties(SigningTreePrefix))
654612
if err != nil {
655613
return nil, err
656614
}
657-
err = tree.AddLeaves(append(dataLeaves, cdLeaves...))
658-
if err != nil {
659-
return nil, err
660-
}
661-
err = tree.Generate()
662-
if err != nil {
663-
return nil, err
664-
}
665-
return tree, nil
666-
}
667615

668-
// SigningDataTrees returns the merkle trees (basicData and zkData) + signingRoot Hash for the document data tree provided
669-
func (cd *CoreDocument) SigningDataTrees(docType string, dataLeaves []proofs.LeafNode) (trees []*proofs.DocumentTree, rootHash []byte, err error) {
670-
if dataLeaves == nil {
671-
return nil, nil, errors.NewTypedError(ErrCDTree, errors.New("data tree is invalid"))
672-
}
673-
cdLeaves, err := cd.coredocLeaves(docType)
674-
if err != nil {
675-
return nil, nil, err
676-
}
677-
// create the docDataTrees out of docData and coredoc trees
678-
tree, err := cd.DefaultOrderedTreeWithPrefix(SigningTreePrefix, CompactProperties(SigningTreePrefix))
679-
if err != nil {
680-
return nil, nil, err
681-
}
682-
basicTree, err := cd.basicDataTree(dataLeaves, cdLeaves)
616+
signingDataLeaves := append(dataLeaves, cdLeaves...)
617+
err = tree.AddLeaves(signingDataLeaves)
683618
if err != nil {
684-
return nil, nil, err
685-
}
686-
zkTree, err := cd.zkDataTree(dataLeaves, cdLeaves)
687-
if err != nil {
688-
return nil, nil, err
689-
}
690-
691-
// The first leave added is the basic data tree root
692-
err = tree.AddLeaf(proofs.LeafNode{
693-
Hash: basicTree.RootHash(),
694-
Hashed: true,
695-
Property: NewLeafProperty(fmt.Sprintf("%s.%s", SigningTreePrefix, BasicDataRootField), append(CompactProperties(SigningTreePrefix), CompactProperties(BasicDataRootField)...))})
696-
if err != nil {
697-
return nil, nil, err
698-
}
699-
700-
// The second leave added is the zkSnarks docData tree root
701-
err = tree.AddLeaf(proofs.LeafNode{
702-
Hash: zkTree.RootHash(),
703-
Hashed: true,
704-
Property: NewLeafProperty(fmt.Sprintf("%s.%s", SigningTreePrefix, ZKDataRootField), append(CompactProperties(SigningTreePrefix), CompactProperties(ZKDataRootField)...))})
705-
if err != nil {
706-
return nil, nil, err
619+
return nil, err
707620
}
708621

709622
err = tree.Generate()
710623
if err != nil {
711-
return nil, nil, err
624+
return nil, err
712625
}
713626

714-
return []*proofs.DocumentTree{basicTree, zkTree}, tree.RootHash(), nil
627+
return tree, nil
715628
}
716629

717630
func (cd *CoreDocument) coredocRawTree(docType string) (*proofs.DocumentTree, error) {
@@ -732,7 +645,11 @@ func (cd *CoreDocument) coredocRawTree(docType string) (*proofs.DocumentTree, er
732645
Value: []byte(docType),
733646
}
734647

735-
err = documentTypeNode.HashNode(sha3.NewLegacyKeccak256(), true)
648+
b2b, err := blake2b.New256(nil)
649+
if err != nil {
650+
return nil, err
651+
}
652+
err = documentTypeNode.HashNode(b2b, true)
736653
if err != nil {
737654
return nil, err
738655
}
@@ -880,12 +797,12 @@ func (cd *CoreDocument) CalculateDocumentRoot(docType string, dataLeaves []proof
880797

881798
// CalculateSigningRoot calculates the signing root of the core document.
882799
func (cd *CoreDocument) CalculateSigningRoot(docType string, dataLeaves []proofs.LeafNode) ([]byte, error) {
883-
_, treeHash, err := cd.SigningDataTrees(docType, dataLeaves)
800+
tree, err := cd.SigningDataTree(docType, dataLeaves)
884801
if err != nil {
885802
return nil, err
886803
}
887804

888-
return treeHash, nil
805+
return tree.RootHash(), nil
889806
}
890807

891808
// PackCoreDocument prepares the document into a core document.

0 commit comments

Comments
 (0)