diff --git a/.gitignore b/.gitignore index 66fd13c..d1f065b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,7 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +# Tree persistence +snap.* +blobs/ \ No newline at end of file diff --git a/go.mod b/go.mod index 01f46b0..44b1216 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,9 @@ go 1.18 require ( github.com/stretchr/testify v1.7.2 - github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230803113401-9a7ef94d120e - github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230801152407-8101ff87ee0a - github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230801140722-0a4e68d0b8f5 + github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230807124901-05ef8aca570d + github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230807124929-ea702b1b4305 + github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230807124913-ea636e5b4005 ) require ( diff --git a/go.sum b/go.sum index cbcae50..f2abfe3 100644 --- a/go.sum +++ b/go.sum @@ -15,10 +15,20 @@ github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8 github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230803113401-9a7ef94d120e h1:Ad0rJod5F1FuYCJ8SUB/bQZsQwirNHQRE0IcaVloxZo= github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230803113401-9a7ef94d120e/go.mod h1:KYykqtdApHVYZ3G0spwMnoxc5jH5eI3jyO9SwsSfi48= +github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230807124901-05ef8aca570d h1:7Ot7vJAniJWQkarZBFxmjRo39gGksKcqs4kZ10l+szs= +github.com/waku-org/go-zerokit-rln-apple v0.0.0-20230807124901-05ef8aca570d/go.mod h1:KYykqtdApHVYZ3G0spwMnoxc5jH5eI3jyO9SwsSfi48= github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230801152407-8101ff87ee0a h1:10cre+P76QvnLeyeCVAM8WDbUCri/y5xY3LtwI9Y5DE= github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230801152407-8101ff87ee0a/go.mod h1:7cSGUoGVIla1IpnChrLbkVjkYgdOcr7rcifEfh4ReR4= +github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230804193517-1b9eff57e592 h1:v1+rpINORPzMfBxoLpJMTB+8Z69gb7tbfTu7gAn4heg= +github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230804193517-1b9eff57e592/go.mod h1:7cSGUoGVIla1IpnChrLbkVjkYgdOcr7rcifEfh4ReR4= +github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230807124929-ea702b1b4305 h1:33LEcvkC5eRdCIKt0bTG6G6DYZRNQGcpdoScA1ZFgRI= +github.com/waku-org/go-zerokit-rln-arm v0.0.0-20230807124929-ea702b1b4305/go.mod h1:7cSGUoGVIla1IpnChrLbkVjkYgdOcr7rcifEfh4ReR4= github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230801140722-0a4e68d0b8f5 h1:GseAHwGMixJ2zlY1kFYr3z1Ts0dREIYbgW4yIji9Ksw= github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230801140722-0a4e68d0b8f5/go.mod h1:+LeEYoW5/uBUTVjtBGLEVCUe9mOYAlu5ZPkIxLOSr5Y= +github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230804193023-1bc0109378f1 h1:tE9HmLSOubdXPGy229HJHx9rGZd6Cz6UPsnDTuRsozI= +github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230804193023-1bc0109378f1/go.mod h1:+LeEYoW5/uBUTVjtBGLEVCUe9mOYAlu5ZPkIxLOSr5Y= +github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230807124913-ea636e5b4005 h1:kJfvDpiZZGNTpHB7Mp4BBNj/hsG6UzMg84E+bl+n7Eo= +github.com/waku-org/go-zerokit-rln-x86_64 v0.0.0-20230807124913-ea636e5b4005/go.mod h1:+LeEYoW5/uBUTVjtBGLEVCUe9mOYAlu5ZPkIxLOSr5Y= 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/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/rln/link/arm.go b/rln/link/arm.go index d5e4444..6f2a6ab 100644 --- a/rln/link/arm.go +++ b/rln/link/arm.go @@ -10,16 +10,16 @@ type RLNWrapper struct { ffi *r.RLN } -func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLNWrapper, error) { - rln, err := r.NewWithParams(depth, wasm, zkey, verifKey) +func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte, treeConfig []byte) (*RLNWrapper, error) { + rln, err := r.NewWithParams(depth, wasm, zkey, verifKey, treeConfig) if err != nil { return nil, err } return &RLNWrapper{ffi: rln}, nil } -func NewWithFolder(depth int, resourcesFolderPath string) (*RLNWrapper, error) { - rln, err := r.NewWithFolder(depth, resourcesFolderPath) +func New(depth int, config []byte) (*RLNWrapper, error) { + rln, err := r.New(uint(depth), config) if err != nil { return nil, err } @@ -105,3 +105,7 @@ func (i RLNWrapper) SetMetadata(metadata []byte) bool { func (i RLNWrapper) GetMetadata() ([]byte, error) { return i.ffi.GetMetadata() } + +func (i RLNWrapper) Flush() bool { + return i.ffi.Flush() +} diff --git a/rln/link/x86_64.go b/rln/link/x86_64.go index 79df456..3e87b86 100644 --- a/rln/link/x86_64.go +++ b/rln/link/x86_64.go @@ -11,16 +11,16 @@ type RLNWrapper struct { ffi *r.RLN } -func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLNWrapper, error) { - rln, err := r.NewWithParams(depth, wasm, zkey, verifKey) +func NewWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte, treeConfig []byte) (*RLNWrapper, error) { + rln, err := r.NewWithParams(depth, wasm, zkey, verifKey, treeConfig) if err != nil { return nil, err } return &RLNWrapper{ffi: rln}, nil } -func NewWithFolder(depth int, resourcesFolderPath string) (*RLNWrapper, error) { - rln, err := r.NewWithFolder(depth, resourcesFolderPath) +func New(depth int, config []byte) (*RLNWrapper, error) { + rln, err := r.New(uint(depth), config) if err != nil { return nil, err } @@ -106,3 +106,7 @@ func (i RLNWrapper) SetMetadata(metadata []byte) bool { func (i RLNWrapper) GetMetadata() ([]byte, error) { return i.ffi.GetMetadata() } + +func (i RLNWrapper) Flush() bool { + return i.ffi.Flush() +} diff --git a/rln/rln.go b/rln/rln.go index ccf6f0e..a5621aa 100644 --- a/rln/rln.go +++ b/rln/rln.go @@ -3,6 +3,7 @@ package rln import "C" import ( "encoding/binary" + "encoding/json" "errors" "fmt" @@ -37,7 +38,7 @@ func NewRLN() (*RLN, error) { depth := 20 - r.w, err = link.NewWithParams(depth, wasm, zkey, verifKey) + r.w, err = link.NewWithParams(depth, wasm, zkey, verifKey, nil) if err != nil { return nil, err } @@ -47,11 +48,19 @@ func NewRLN() (*RLN, error) { // NewRLNWithParams generates an instance of RLN. An instance supports both zkSNARKs logics // and Merkle tree data structure and operations. The parameter `depth“ indicates the depth of Merkle tree -func NewRLNWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RLN, error) { +func NewRLNWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte, treeConfig *TreeConfig) (*RLN, error) { r := &RLN{} var err error - r.w, err = link.NewWithParams(depth, wasm, zkey, verifKey) + treeConfigBytes := []byte{} + if treeConfig != nil { + treeConfigBytes, err = json.Marshal(treeConfig) + if err != nil { + return nil, err + } + } + + r.w, err = link.NewWithParams(depth, wasm, zkey, verifKey, treeConfigBytes) if err != nil { return nil, err } @@ -59,15 +68,21 @@ func NewRLNWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte) (*RL return r, nil } -// NewRLNWithFolder generates an instance of RLN. An instance supports both zkSNARKs logics -// and Merkle tree data structure and operations. The parameter `deptk` indicates the depth of Merkle tree -// The parameter “ -func NewRLNWithFolder(depth int, resourcesFolderPath string) (*RLN, error) { +// NewWithConfig generates an instance of RLN. An instance supports both zkSNARKs logics +// and Merkle tree data structure and operations. The parameter `depth` indicates the depth of Merkle tree +func NewWithConfig(depth int, config *Config) (*RLN, error) { r := &RLN{} - var err error - r.w, err = link.NewWithFolder(depth, resourcesFolderPath) + configBytes := []byte{} + if config != nil { + configBytes, err = json.Marshal(config) + if err != nil { + return nil, err + } + } + + r.w, err = link.New(depth, configBytes) if err != nil { return nil, err } @@ -481,3 +496,12 @@ func (r *RLN) AtomicOperation(index MembershipIndex, idCommsToInsert []IDCommitm } return nil } + +// Flush +func (r *RLN) Flush() error { + success := r.w.Flush() + if !success { + return errors.New("cannot flush db") + } + return nil +} diff --git a/rln/rln_test.go b/rln/rln_test.go index 269a275..be9b96b 100644 --- a/rln/rln_test.go +++ b/rln/rln_test.go @@ -17,6 +17,27 @@ type RLNSuite struct { suite.Suite } +func (s *RLNSuite) TestNew() { + rln, err := NewRLN() + s.NoError(err) + + root1, err := rln.GetMerkleRoot() + s.NoError(err) + s.Len(root1, 32) + + config := &Config{ + ResourcesFolder: "tree_height_20", + } + + rln2, err := NewWithConfig(20, config) + s.NoError(err) + + root2, err := rln2.GetMerkleRoot() + s.NoError(err) + s.Len(root2, 32) + s.Equal(root1, root2) +} + func (s *RLNSuite) TestMembershipKeyGen() { rln, err := NewRLN() s.NoError(err) diff --git a/rln/types.go b/rln/types.go index 5ff2a0c..5344b19 100644 --- a/rln/types.go +++ b/rln/types.go @@ -62,6 +62,19 @@ type RateLimitProof struct { RLNIdentifier RLNIdentifier `json:"rlnIdentifier"` } +type TreeConfig struct { + CacheCapacity int `json:"cache_capacity"` + Mode string `json:"mode"` + Compression bool `json:"compression"` + FlushInterval int `json:"flush_interval"` + Path string `json:"path"` +} + +type Config struct { + ResourcesFolder string `json:"resources_folder"` + TreeConfig *TreeConfig `json:"tree_config,omitempty"` +} + type MembershipIndex = uint type ProofMetadata struct {