@@ -9,41 +9,33 @@ package idemix_test
99import (
1010 "bytes"
1111 "encoding/json"
12- "fmt"
1312 "io/ioutil"
1413 "net/http"
1514 "os"
1615 "path/filepath"
1716 "strings"
1817 "testing"
1918
20- scheme "github.com/IBM/idemix/bccsp/schemes/dlog/crypto"
19+ i "github.com/IBM/idemix/bccsp"
20+ "github.com/IBM/idemix/bccsp/schemes/dlog/crypto/translator/amcl"
21+ bccsp "github.com/IBM/idemix/bccsp/types"
2122 math "github.com/IBM/mathlib"
22- "github.com/golang/protobuf/proto"
2323 lib "github.com/hyperledger/fabric-ca/lib"
2424 . "github.com/hyperledger/fabric-ca/lib/client/credential/idemix"
25- cidemix "github.com/hyperledger/fabric-ca/lib/common/idemix"
2625 "github.com/hyperledger/fabric-ca/lib/server/idemix"
26+ "github.com/pkg/errors"
2727 "github.com/stretchr/testify/assert"
2828)
2929
3030func TestIdemixCredential (t * testing.T ) {
31- for _ , curveID := range []cidemix.CurveID {cidemix .Gurvy } {
32- t .Run (fmt .Sprintf ("%s-%d" , t .Name (), curveID ), func (t * testing.T ) {
33- testIdemixCredential (t , curveID )
34- })
35- }
36- }
37-
38- func testIdemixCredential (t * testing.T , curveID cidemix.CurveID ) {
3931 testDataDir , err := os .MkdirTemp ("" , strings .Replace (t .Name (), "/" , "-" , - 1 ))
4032 assert .NoError (t , err )
4133 defer os .RemoveAll (testDataDir )
4234
4335 testSignerConfigFile := testDataDir + "/IdemixSignerConfig"
4436 testIssuerPublicFile := testDataDir + "/IdemixPublicKey"
4537
46- signerConf , ipk := makeSignerConfigAndIPK (curveID , t )
38+ signerConf , ipk , CSP := makeSignerConfigAndIPK (math . BLS12_381_BBS , t )
4739 rawSignerConf , err := json .Marshal (signerConf )
4840 if err != nil {
4941 t .Fatalf ("Failed to marshal signer config: %s" , err .Error ())
@@ -54,7 +46,7 @@ func testIdemixCredential(t *testing.T, curveID cidemix.CurveID) {
5446 t .Fatalf ("Failed to write signer config to file: %s" , err .Error ())
5547 }
5648
57- rawIPK , err := proto . Marshal ( ipk )
49+ rawIPK , err := ipk . Bytes ( )
5850 if err != nil {
5951 t .Fatalf ("Failed to marshal IPK: %s" , err .Error ())
6052 }
@@ -78,7 +70,7 @@ func testIdemixCredential(t *testing.T, curveID cidemix.CurveID) {
7870 t .Fatalf ("Failed to initialize client: %s" , err .Error ())
7971 }
8072
81- idemixCred := NewCredential (signerConfig , client , curveID )
73+ idemixCred := NewCredential (signerConfig , client , CSP )
8274
8375 assert .Equal (t , idemixCred .Type (), CredType , "Type for a IdemixCredential instance must be Idemix" )
8476 _ , err = idemixCred .Val ()
@@ -205,50 +197,106 @@ func testIdemixCredential(t *testing.T, curveID cidemix.CurveID) {
205197 assert .Error (t , err , "RevokeSelf should fail as it is not implemented for Idemix credential" )
206198}
207199
208- func makeSignerConfigAndIPK (curveID cidemix.CurveID , t * testing.T ) (SignerConfig , * scheme.IssuerPublicKey ) {
209- curve := cidemix .CurveByID (curveID )
210- rand , err := curve .Rand ()
211- assert .NoError (t , err )
200+ // NewDummyKeyStore instantiate a dummy key store
201+ // that neither loads nor stores keys
202+ func NewDummyKeyStore () bccsp.KeyStore {
203+ return & dummyKeyStore {}
204+ }
205+
206+ // dummyKeyStore is a read-only KeyStore that neither loads nor stores keys.
207+ type dummyKeyStore struct {
208+ }
209+
210+ // ReadOnly returns true if this KeyStore is read only, false otherwise.
211+ // If ReadOnly is true then StoreKey will fail.
212+ func (ks * dummyKeyStore ) ReadOnly () bool {
213+ return true
214+ }
212215
216+ // GetKey returns a key object whose SKI is the one passed.
217+ func (ks * dummyKeyStore ) GetKey (ski []byte ) (bccsp.Key , error ) {
218+ return nil , errors .New ("Key not found. This is a dummy KeyStore" )
219+ }
220+
221+ // StoreKey stores the key k in this KeyStore.
222+ // If this KeyStore is read only then the method will fail.
223+ func (ks * dummyKeyStore ) StoreKey (k bccsp.Key ) error {
224+ return nil
225+ }
226+
227+ func makeSignerConfigAndIPK (curveID math.CurveID , t * testing.T ) (SignerConfig , bccsp.Key , bccsp.BCCSP ) {
213228 attrs := []string {idemix .AttrOU , idemix .AttrRole , idemix .AttrEnrollmentID , idemix .AttrRevocationHandle }
214- var numericalAttrs []* math.Zr
215- for _ , attr := range attrs {
216- numericalAttrs = append (numericalAttrs , curve .HashToZr ([]byte (attr )))
217- }
218229
219- idemix := cidemix . InstanceForCurve ( curveID )
220- ik , err := idemix . NewIssuerKey ( attrs , rand , idemix . Translator )
230+ curve := math . Curves [ curveID ]
231+ CSP , err := i . New ( NewDummyKeyStore (), curve , & amcl. Gurvy { C : curve }, true )
221232 assert .NoError (t , err )
222233
223- sk := curve .NewZrFromBytes (ik .Isk )
234+ isk , err := CSP .KeyGen (& bccsp.IdemixIssuerKeyGenOpts {Temporary : true , AttributeNames : attrs })
235+ assert .NoError (t , err )
224236
225- revKey , err := idemix . GenerateLongTermRevocationKey ()
237+ ipk , err := isk . PublicKey ()
226238 assert .NoError (t , err )
227239
228- cri , err := idemix . CreateCRI ( revKey , nil , 1 , scheme . ALG_NO_REVOCATION , rand , idemix . Translator )
240+ revKey , err := CSP . KeyGen ( & bccsp. IdemixRevocationKeyGenOpts { Temporary : true } )
229241 assert .NoError (t , err )
230242
231- criBytes , err := proto .Marshal (cri )
243+ cri , err := CSP .Sign (
244+ revKey ,
245+ nil ,
246+ & bccsp.IdemixCRISignerOpts {
247+ UnrevokedHandles : nil ,
248+ Epoch : 1 ,
249+ RevocationAlgorithm : bccsp .AlgNoRevocation ,
250+ },
251+ )
232252 assert .NoError (t , err )
233253
234- nonce := curve . NewRandomZr ( rand )
254+ nonce := [] byte ( "do not reuse me do not reuse me " )
235255
236- credReq , err := idemix . NewCredRequest ( sk , nonce . Bytes (), ik . Ipk , rand , idemix . Translator )
256+ UserKey , err := CSP . KeyGen ( & bccsp. IdemixUserSecretKeyGenOpts { Temporary : true } )
237257 assert .NoError (t , err )
238258
239- cred , err := idemix . NewCredential ( ik , credReq , numericalAttrs , rand , idemix . Translator )
259+ uskBytes , err := UserKey . Bytes ( )
240260 assert .NoError (t , err )
241261
242- credBytes , err := proto .Marshal (cred )
262+ credReq , err := CSP .Sign (
263+ UserKey ,
264+ nil ,
265+ & bccsp.IdemixCredentialRequestSignerOpts {IssuerPK : ipk , IssuerNonce : nonce },
266+ )
243267 assert .NoError (t , err )
244268
245- signerSK := curve .NewRandomZr (rand )
269+ cred , err := CSP .Sign (
270+ isk ,
271+ credReq ,
272+ & bccsp.IdemixCredentialSignerOpts {
273+ Attributes : []bccsp.IdemixAttribute {
274+ {
275+ Type : bccsp .IdemixBytesAttribute ,
276+ Value : []byte (attrs [0 ]),
277+ },
278+ {
279+ Type : bccsp .IdemixIntAttribute ,
280+ Value : 1 ,
281+ },
282+ {
283+ Type : bccsp .IdemixBytesAttribute ,
284+ Value : []byte (attrs [2 ]),
285+ },
286+ {
287+ Type : bccsp .IdemixBytesAttribute ,
288+ Value : []byte (attrs [3 ]),
289+ },
290+ },
291+ },
292+ )
293+ assert .NoError (t , err )
246294
247295 return SignerConfig {
248- CredentialRevocationInformation : criBytes ,
249- Cred : credBytes ,
296+ CredentialRevocationInformation : cri ,
297+ Cred : cred ,
250298 EnrollmentID : "admin" ,
251299 OrganizationalUnitIdentifier : "MSPID" ,
252- Sk : signerSK . Bytes () ,
253- }, ik . Ipk
300+ USk : uskBytes ,
301+ }, ipk , CSP
254302}
0 commit comments