@@ -11,9 +11,12 @@ import (
1111 "crypto/ecdsa"
1212 "crypto/elliptic"
1313 "crypto/rand"
14+ "encoding/base64"
15+ "encoding/json"
1416 "fmt"
1517 "os"
1618 "path/filepath"
19+ "strings"
1720
1821 "github.com/IBM/idemix/bccsp/types"
1922 math "github.com/IBM/mathlib"
@@ -30,6 +33,12 @@ import (
3033 "github.com/hyperledger-labs/fabric-token-sdk/token/services/storage/db/kvs"
3134)
3235
36+ type SetupConfigurationSer struct {
37+ PP string `json:"pp"` // Encoded in base64
38+ Bits uint64 `json:"bits"`
39+ CurveID int `json:"curveID"`
40+ }
41+
3342// SetupConfiguration holds the prepared public parameters and related
3443// identities/signers used by a single benchmark configuration.
3544//
@@ -42,6 +51,8 @@ type SetupConfiguration struct {
4251 OwnerIdentity * OwnerIdentity
4352 AuditorSigner * Signer
4453 IssuerSigner * Signer
54+ Bits uint64
55+ CurveID math.CurveID
4556}
4657
4758// SetupConfigurations contains a set of named benchmark configurations.
@@ -112,6 +123,8 @@ func NewSetupConfigurations(idemixTestdataPath string, bits []uint64, curveIDs [
112123 }
113124 pp .AddAuditor (auditorID )
114125 configurations [key (bit , curveID )] = & SetupConfiguration {
126+ Bits : bit ,
127+ CurveID : curveID ,
115128 PP : pp ,
116129 OwnerIdentity : oID ,
117130 AuditorSigner : auditorSigner ,
@@ -146,7 +159,71 @@ func (c *SetupConfigurations) GetSetupConfiguration(bits uint64, curveID math.Cu
146159}
147160
148161func key (bits uint64 , curveID math.CurveID ) string {
149- return fmt .Sprintf ("%d-%d" , bits , curveID )
162+ return fmt .Sprintf ("%d-%s" , bits , math2 .CurveIDToString (curveID ))
163+ }
164+
165+ // SaveTo writes each configuration to disk under the provided directory.
166+ // For each entry in the Configurations map a folder with the map key is
167+ // created. Inside that folder a file named `pp.json` is written. The file
168+ // contains a JSON document with the base64-encoded serialized public
169+ // parameters and the metadata bits and curve_id.
170+ func (c * SetupConfigurations ) SaveTo (dir string ) error {
171+ if c == nil {
172+ return errors .Errorf ("nil SetupConfigurations" )
173+ }
174+ // Ensure target base directory exists
175+ if err := os .MkdirAll (dir , 0o755 ); err != nil {
176+ return errors .Wrapf (err , "failed creating base dir [%s]" )
177+ }
178+
179+ for k , cfg := range c .Configurations {
180+ if strings .ContainsAny (k , "/\\ " ) {
181+ return errors .Errorf ("invalid configuration key: %s" , k )
182+ }
183+ if cfg == nil {
184+ return errors .Errorf ("nil configuration for key: %s" , k )
185+ }
186+ if cfg .PP == nil {
187+ return errors .Errorf ("nil public parameters for key: %s" , k )
188+ }
189+
190+ // serialize public params
191+ ppBytes , err := cfg .PP .Serialize ()
192+ if err != nil {
193+ return errors .WithMessagef (err , "failed serializing public params for key: %s" , k )
194+ }
195+
196+ // prepare JSON payload
197+ payload := & SetupConfigurationSer {
198+ PP : base64 .StdEncoding .EncodeToString (ppBytes ),
199+ Bits : cfg .Bits ,
200+ CurveID : int (cfg .CurveID ),
201+ }
202+ data , err := json .MarshalIndent (payload , "" , " " )
203+ if err != nil {
204+ return errors .Wrap (err , "failed marshalling json payload" )
205+ }
206+
207+ // create target directory and write
208+ targetDir := filepath .Join (dir , filepath .Base (k ))
209+ if err := os .MkdirAll (targetDir , 0o755 ); err != nil {
210+ return errors .WithMessagef (err , "failed creating dir for key: %s" , k )
211+ }
212+
213+ // Write params.txt containing base64(ppBytes)
214+ paramsEncoded := payload .PP
215+ finalParamsPath := filepath .Join (targetDir , "params.txt" )
216+ if err := os .WriteFile (finalParamsPath , []byte (paramsEncoded ), 0o644 ); err != nil {
217+ return errors .WithMessagef (err , "failed writing params file for key: %s" , k )
218+ }
219+
220+ // Write pp.json file
221+ finalPath := filepath .Join (targetDir , "pp.json" )
222+ if err := os .WriteFile (finalPath , data , 0o644 ); err != nil {
223+ return errors .WithMessagef (err , "failed writing pp.json for key: %s" , k )
224+ }
225+ }
226+ return nil
150227}
151228
152229// OwnerIdentity represents the owner identity used by benchmarks. It
0 commit comments