def prepareProof(issuerPks, masterSecret, creds: Dict[str, Credential], encodedAttrs: Dict[str, Dict[str, T]], revealedAttrs: Sequence[str], nonce) -> types.Proof: """ Prepare the proof from credentials :param creds: This is a dictionary with key as issuer name and value as the credential :param encodedAttrs: The encoded attributes dictionary :param revealedAttrs: The revealed attributes list :param nonce: The nonce used to have a commit :return: The proof """ def initProofComponent(issuerPks, creds, encodedAttrs, revealedAttrs, nonce): proofComponent = ProofComponent() proofComponent.flatAttrs, proofComponent.unrevealedAttrs = \ getUnrevealedAttrs( encodedAttrs, revealedAttrs) proofComponent.tildeValues, proofComponent.primeValues, \ proofComponent.T = findSecretValues( encodedAttrs, proofComponent.unrevealedAttrs, creds, issuerPks) # Calculate the `c` value as the hash result of Aprime, T and nonce. # This value will be used to verify the proof against the credential proofComponent.c = cmod.integer(get_hash( *get_values_of_dicts(proofComponent.primeValues.Aprime, proofComponent.T, {NONCE: nonce}))) return proofComponent proofComponent = initProofComponent(issuerPks, creds, encodedAttrs, revealedAttrs, nonce) for credIssuer, _ in creds.items(): proofComponent.evect[credIssuer] = \ proofComponent.tildeValues.etilde[credIssuer] + ( proofComponent.c * proofComponent.primeValues.eprime[ credIssuer]) proofComponent.vvect[credIssuer] = \ proofComponent.tildeValues.vtilde[credIssuer] + ( proofComponent.c * proofComponent.primeValues.vprime[ credIssuer]) for k, _ in proofComponent.unrevealedAttrs.items(): proofComponent.mvect[str(k)] = proofComponent.tildeValues.mtilde[ str(k)] + (proofComponent.c * proofComponent.flatAttrs[ str(k)]) proofComponent.mvect[ZERO_INDEX] = proofComponent.tildeValues.mtilde[ ZERO_INDEX] + ( proofComponent.c * masterSecret) return Proof(proofComponent.c, proofComponent.evect, proofComponent.mvect, proofComponent.vvect, proofComponent.primeValues.Aprime)
def getSubProof(creds, predProofComponent): for key, val in creds.items(): predProofComponent.evect[key] = \ predProofComponent.tildeValues.etilde[key] + ( predProofComponent.c * predProofComponent.primeValues.eprime[key]) predProofComponent.vvect[key] = \ predProofComponent.tildeValues.vtilde[key] + ( predProofComponent.c * predProofComponent.primeValues.vprime[key]) predProofComponent.mvect = {} for k, value in predProofComponent.unrevealedAttrs.items(): predProofComponent.mvect[str(k)] = \ predProofComponent.tildeValues.mtilde[str(k)] + ( predProofComponent.c * predProofComponent.flatAttrs[ str(k)]) predProofComponent.mvect[ZERO_INDEX] = \ predProofComponent.tildeValues.mtilde[ZERO_INDEX] + ( predProofComponent.c * self._ms) return Proof(predProofComponent.c, predProofComponent.evect, predProofComponent.mvect, predProofComponent.vvect, predProofComponent.primeValues.Aprime)
async def testClaimProofFromToDictPrimaryOnly(prover1, nonce, claimsProver1Gvt): proofInput = ProofInput(['name'], [PredicateGE('age', 18)]) proof, _ = await prover1.presentProof(proofInput, nonce) proofs = [Proof(primaryProof=proof.proofs[0].primaryProof)] proof = proof._replace(proofs=proofs) assert proof == FullProof.fromStrDict(proof.toStrDict())
async def _prepareProof(self, claims: Dict[SchemaKey, ProofClaims], nonce, requestedProof) -> FullProof: m1Tilde = cmod.integer(cmod.randomBits(LARGE_M2_TILDE)) initProofs = {} CList = [] TauList = [] # 1. init proofs for schemaId, val in claims.items(): c1, c2, revealedAttrs, predicates = val.claims.primaryClaim, val.claims.nonRevocClaim, val.revealedAttrs, val.predicates claim = await self.wallet.getClaimAttributes(ID(schemaId=schemaId)) nonRevocInitProof = None if c2: nonRevocInitProof = await self._nonRevocProofBuilder.initProof( schemaId, c2) CList += nonRevocInitProof.asCList() TauList += nonRevocInitProof.asTauList() primaryInitProof = None if c1: m2Tilde = cmod.integer(int(nonRevocInitProof.TauListParams.m2) ) if nonRevocInitProof else None primaryInitProof = await self._primaryProofBuilder.initProof( schemaId, c1, revealedAttrs, predicates, m1Tilde, m2Tilde, claim) CList += primaryInitProof.asCList() TauList += primaryInitProof.asTauList() initProof = InitProof(nonRevocInitProof, primaryInitProof) initProofs[schemaId] = initProof # 2. hash cH = self._get_hash(self._prepare_collection(CList), self._prepare_collection(TauList), nonce) # 3. finalize proofs proofs = {} for schemaId, initProof in initProofs.items(): nonRevocProof = None if initProof.nonRevocInitProof: nonRevocProof = await self._nonRevocProofBuilder.finalizeProof( schemaId, cH, initProof.nonRevocInitProof) primaryProof = await self._primaryProofBuilder.finalizeProof( schemaId, cH, initProof.primaryInitProof) schema = await self.wallet.getSchema(ID(schemaId=schemaId)) proof = Proof(primaryProof, nonRevocProof) proofInfo = ProofInfo(proof=proof, schema_seq_no=schemaId, issuer_did=schema.issuerId) proofs[str(schemaId)] = proofInfo aggregatedProof = AggregatedProof(cH, self._prepare_collection(CList)) return FullProof(proofs, aggregatedProof, requestedProof)
def prepareProofFromDict(proofElements) -> Proof: prfArgs = { C_VALUE: strToCryptoInteger(proofElements[C_VALUE]), APRIME: {issuer: strToCryptoInteger(aprime) for issuer, aprime in proofElements[APRIME].items()}, EVECT: {issuer: strToCryptoInteger(evect) for issuer, evect in proofElements[EVECT].items()}, MVECT: {v: strToCryptoInteger(mvect) for v, mvect in proofElements[MVECT].items()}, VVECT: {issuer: strToCryptoInteger(vvect) for issuer, vvect in proofElements[VVECT].items()} } return Proof(**prfArgs)
def prepareProofFromDict(proofElements) -> Proof: issuer = proofElements[ISSUER] prf = proofElements[PROOF] prfArgs = {} prfArgs[APRIME] = {issuer: strToCharmInteger(prf[APRIME][issuer])} prfArgs[C_VALUE] = strToCharmInteger(prf[C_VALUE]) prfArgs[EVECT] = {issuer: strToCharmInteger(prf[EVECT][issuer])} prfArgs[MVECT] = { k: strToCharmInteger(v) for k, v in prf[MVECT].items() } prfArgs[VVECT] = {issuer: strToCharmInteger(prf[VVECT][issuer])} return Proof(**prfArgs)
async def test_proof_info_from_to_dict(): n = cmod.integer(12345) eqProof = PrimaryEqualProof(e=cmod.integer(1), v=cmod.integer(11), m={'name': cmod.integer(12)}, m1=cmod.integer(12), m2=cmod.integer(32), Aprime=cmod.integer(32) % n, revealedAttrs={'name': cmod.integer(35)}) predicate = PredicateGE(attrName='age', value=18) geProof = PrimaryPredicateGEProof(alpha=cmod.integer(1), mj=cmod.integer(12), r={'1': cmod.integer(13)}, u={'1': cmod.integer(42)}, T={'1': cmod.integer(21) % n}, predicate=predicate) primaryProof = PrimaryProof(eqProof=eqProof, geProofs=[geProof]) proofInfo = Proof(primaryProof=primaryProof) proof = ProofInfo(schema_seq_no=1, proof=proofInfo, issuer_did='did') proof_serialized = { 'issuer_did': 'did', 'schema_seq_no': 1, 'proof': { 'primary_proof': { 'eq_proof': { 'a_prime': '32', 'e': '1', 'm': {'name': '12'}, 'm1': '12', 'm2': '32', 'v': '11', 'revealed_attrs': {'name': '35'} }, 'ge_proofs': [ { 'alpha': '1', 'mj': '12', 't': {'1': '21'}, 'r': {'1': '13'}, 'u': {'1': '42'}, 'predicate': { 'p_type': 'GE', 'attr_name': 'age', 'value': 18, 'schema_seq_no': None, 'issuer_did': None } } ] } } } assert proof.to_str_dict() == proof_serialized assert proof == ProofInfo.from_str_dict(proof_serialized, n) assert proof == ProofInfo.from_str_dict(proof.to_str_dict(), n)
async def testClaimProofFromToDictPrimaryOnly(prover1, nonce, claimsProver1Gvt, schemaGvt): proofRequest = ProofRequest("proof1", "1.0", 1, verifiableAttributes={ 'attr_uuid': AttributeInfo(name='name')}, predicates={'predicate_uuid': PredicateGE('age', 18)}) proof = await prover1.presentProof(proofRequest) proofInfo = proof.proofs[str(schemaGvt.seqId)] proofs = {schemaGvt.seqId: ProofInfo(Proof(primaryProof=proofInfo.proof.primaryProof), issuer_did=schemaGvt.issuerId, schema_seq_no=proofInfo.schema_seq_no)} proof = proof._replace(proofs=proofs) assert proof == FullProof.fromStrDict(proof.toStrDict())
async def test_proof_from_to_dict(prover1, nonce, claimsProver1Gvt, schemaGvt): n = (await prover1.wallet.getPublicKey(ID(schemaId=schemaGvt.seqId))).N proofRequest = ProofRequest("proof1", "1.0", nonce, verifiableAttributes={ 'attr_uuid': AttributeInfo(name='name')}, predicates={'predicate_uuid': PredicateGE('age', 18)}) proof = await prover1.presentProof(proofRequest) proofInfo = proof.proofs[str(schemaGvt.seqId)] proof = ProofInfo(Proof(primaryProof=proofInfo.proof.primaryProof), issuer_did=schemaGvt.issuerId, schema_seq_no=proofInfo.schema_seq_no) assert proof == ProofInfo.from_str_dict(proof.to_str_dict(), n)
async def _prepareProof(self, claims: Dict[SchemaKey, ProofClaims], nonce) -> FullProof: m1Tilde = cmod.integer(cmod.randomBits(LARGE_M2_TILDE)) initProofs = {} CList = [] TauList = [] # 1. init proofs for schemaKey, val in claims.items(): c1, c2, revealedAttrs, predicates = val.claims.primaryClaim, val.claims.nonRevocClaim, val.revealedAttrs, val.predicates nonRevocInitProof = None if c2: nonRevocInitProof = await self._nonRevocProofBuilder.initProof( schemaKey, c2) CList += nonRevocInitProof.asCList() TauList += nonRevocInitProof.asTauList() primaryInitProof = None if c1: m2Tilde = cmod.integer(int( nonRevocInitProof.TauListParams.m2)) if nonRevocInitProof else None primaryInitProof = await self._primaryProofBuilder.initProof( schemaKey, c1, revealedAttrs, predicates, m1Tilde, m2Tilde) CList += primaryInitProof.asCList() TauList += primaryInitProof.asTauList() initProof = InitProof(nonRevocInitProof, primaryInitProof) initProofs[schemaKey] = initProof # 2. hash cH = self._get_hash(CList, TauList, nonce) # 3. finalize proofs proofs = [] schemaKeys = [] for schemaKey, initProof in initProofs.items(): schemaKeys.append(schemaKey) nonRevocProof = None if initProof.nonRevocInitProof: nonRevocProof = await self._nonRevocProofBuilder.finalizeProof( schemaKey, cH, initProof.nonRevocInitProof) primaryProof = await self._primaryProofBuilder.finalizeProof( schemaKey, cH, initProof.primaryInitProof) proofs.append(Proof(primaryProof, nonRevocProof)) return FullProof(cH, schemaKeys, proofs, CList)