refactor: only expose tree config

This commit is contained in:
Richard Ramos 2023-08-10 18:25:06 -04:00
parent f5dbeaf1a7
commit eed6c087b3
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
3 changed files with 17 additions and 19 deletions

View File

@ -15,17 +15,19 @@ type RLN struct {
w *link.RLNWrapper w *link.RLNWrapper
} }
func getResourcesFolder(depth TreeDepth) string {
return fmt.Sprintf("tree_height_%d", depth)
}
// NewRLN generates an instance of RLN. An instance supports both zkSNARKs logics // NewRLN generates an instance of RLN. An instance supports both zkSNARKs logics
// and Merkle tree data structure and operations. It uses a depth of 20 by default // and Merkle tree data structure and operations. It uses a depth of 20 by default
func NewRLN() (*RLN, error) { func NewRLN() (*RLN, error) {
return NewWithConfig(DefaultTreeDepth, &Config{ return NewWithConfig(DefaultTreeDepth, nil)
ResourcesFolder: "tree_height_20",
})
} }
// NewRLNWithParams generates an instance of RLN. An instance supports both zkSNARKs logics // 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 // and Merkle tree data structure and operations. The parameter `depth“ indicates the depth of Merkle tree
func NewRLNWithParams(depth TreeDepth, wasm []byte, zkey []byte, verifKey []byte, treeConfig *TreeConfig) (*RLN, error) { func NewRLNWithParams(depth int, wasm []byte, zkey []byte, verifKey []byte, treeConfig *TreeConfig) (*RLN, error) {
r := &RLN{} r := &RLN{}
var err error var err error
@ -47,19 +49,19 @@ func NewRLNWithParams(depth TreeDepth, wasm []byte, zkey []byte, verifKey []byte
// NewWithConfig generates an instance of RLN. An instance supports both zkSNARKs logics // 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 // and Merkle tree data structure and operations. The parameter `depth` indicates the depth of Merkle tree
func NewWithConfig(depth TreeDepth, config *Config) (*RLN, error) { func NewWithConfig(depth TreeDepth, treeConfig *TreeConfig) (*RLN, error) {
r := &RLN{} r := &RLN{}
var err error var err error
configBytes := []byte{} configBytes, err := json.Marshal(config{
if config != nil { ResourcesFolder: getResourcesFolder(depth),
configBytes, err = json.Marshal(config) TreeConfig: treeConfig,
if err != nil { })
return nil, err if err != nil {
} return nil, err
} }
r.w, err = link.New(depth, configBytes) r.w, err = link.New(int(depth), configBytes)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -25,11 +25,7 @@ func (s *RLNSuite) TestNew() {
s.NoError(err) s.NoError(err)
s.Len(root1, 32) s.Len(root1, 32)
config := &Config{ rln2, err := NewWithConfig(DefaultTreeDepth, nil)
ResourcesFolder: "tree_height_20",
}
rln2, err := NewWithConfig(20, config)
s.NoError(err) s.NoError(err)
root2, err := rln2.GetMerkleRoot() root2, err := rln2.GetMerkleRoot()

View File

@ -62,7 +62,7 @@ type RateLimitProof struct {
RLNIdentifier RLNIdentifier `json:"rlnIdentifier"` RLNIdentifier RLNIdentifier `json:"rlnIdentifier"`
} }
type TreeDepth = int type TreeDepth int
const ( const (
TreeDepth20 TreeDepth = 20 TreeDepth20 TreeDepth = 20
@ -87,7 +87,7 @@ type TreeConfig struct {
Path string `json:"path"` Path string `json:"path"`
} }
type Config struct { type config struct {
ResourcesFolder string `json:"resources_folder"` ResourcesFolder string `json:"resources_folder"`
TreeConfig *TreeConfig `json:"tree_config,omitempty"` TreeConfig *TreeConfig `json:"tree_config,omitempty"`
} }