Skip to content

Commit d45fbb4

Browse files
committed
regression tests
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent d589b82 commit d45fbb4

File tree

270 files changed

+1889
-470
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+1889
-470
lines changed

token/core/zkatdlog/nogh/v1/benchmark/setup.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
crypto2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/identity/x509/crypto"
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

148161
func 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
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package benchmark
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
math "github.com/IBM/mathlib"
11+
"github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestSaveTo(t *testing.T) {
16+
require := require.New(t)
17+
dir := t.TempDir()
18+
bits := uint64(16)
19+
curve := math.BN254
20+
pp, err := setup.Setup(bits, []byte("dummy-issuer-pk"), curve)
21+
require.NoError(err, "failed to create public params")
22+
23+
cfg := &SetupConfigurations{
24+
Configurations: map[string]*SetupConfiguration{
25+
key(bits, curve): {
26+
Bits: bits,
27+
CurveID: curve,
28+
PP: pp,
29+
},
30+
},
31+
}
32+
33+
require.NoError(cfg.SaveTo(dir), "SaveTo failed")
34+
35+
targetDir := filepath.Join(dir, key(bits, curve))
36+
st, err := os.Stat(targetDir)
37+
require.NoError(err, "expected target dir to exist")
38+
require.True(st.IsDir(), "expected target to be a directory")
39+
40+
filePath := filepath.Join(targetDir, "pp.json")
41+
data, err := os.ReadFile(filePath)
42+
require.NoError(err, "failed reading pp.json")
43+
44+
var payload SetupConfigurationSer
45+
require.NoError(json.Unmarshal(data, &payload), "failed to unmarshal pp.json")
46+
47+
// bits and curve_id are numbers decoded as float64
48+
require.Equal(bits, payload.Bits, "bits mismatch")
49+
require.Equal(int(curve), payload.CurveID, "curve_id mismatch")
50+
51+
ppB64 := payload.PP
52+
decoded, err := base64.StdEncoding.DecodeString(ppB64)
53+
require.NoError(err, "failed to base64 decode pp")
54+
require.NotEmpty(decoded, "decoded pp is empty")
55+
56+
// check params.txt exists and contains the same base64 string
57+
paramsPath := filepath.Join(targetDir, "params.txt")
58+
paramsData, err := os.ReadFile(paramsPath)
59+
require.NoError(err, "failed reading params.txt")
60+
require.Equal(ppB64, string(paramsData), "params.txt content mismatch")
61+
62+
// try deserializing the stored public params to ensure it's valid
63+
_, err = setup.NewPublicParamsFromBytes(decoded, pp.DriverName, pp.DriverVersion)
64+
require.NoError(err, "failed to deserialize stored public params")
65+
}

token/core/zkatdlog/nogh/v1/validator/testdata/32-BLS12_381_BBS_GURVY/params.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

token/core/zkatdlog/nogh/v1/validator/testdata/32-BLS12_381_BBS_GURVY/pp.json

Lines changed: 5 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)