Skip to content

Commit

Permalink
Libsovrin cross-compability (hyperledger-indy#64)
Browse files Browse the repository at this point in the history
* Changed structure of claim

* fix types

* Added methods to/from dict

* Fixed error

* Fix error

* fix

* updated create proof according to new claim structure

* fix module

* Fix

* Updated ProofInput structure

* Changed  create proof and verify methods. Corrected and added tests

* Added schema_seq_no cheking

* Updated ClaimInfo

* Try resolve problem with AggregatedProof serialization

* Fix AggregatedProof serialization

* Updated find_claims function

* Corrected serialization

* Replaced claim_def_seq_no on issuer_did

* Renamed claim to claimAttributes

* Fixed mistakes

* Renamed AttributeValues to ClaimAttributeValues
  • Loading branch information
Artemkaaas authored and ashcherbakov committed Jul 10, 2017
1 parent 3aa24bd commit 6dc0144
Show file tree
Hide file tree
Showing 20 changed files with 1,189 additions and 486 deletions.
15 changes: 9 additions & 6 deletions anoncreds/protocol/issuer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing import Dict

from anoncreds.protocol.globals import LARGE_MASTER_SECRET, TYPE_CL
from anoncreds.protocol.globals import LARGE_MASTER_SECRET
from anoncreds.protocol.primary.primary_claim_issuer import PrimaryClaimIssuer
from anoncreds.protocol.repo.attributes_repo import AttributeRepo
from anoncreds.protocol.revocation.accumulators.non_revocation_claim_issuer import \
NonRevocationClaimIssuer
from anoncreds.protocol.types import PrimaryClaim, NonRevocationClaim, \
Schema, ID, Claims, ClaimRequest, Attribs, PublicKey, \
RevocationPublicKey, AccumulatorPublicKey
RevocationPublicKey, AccumulatorPublicKey, ClaimAttributeValues
from anoncreds.protocol.utils import strToInt, get_hash_as_int
from anoncreds.protocol.wallet.issuer_wallet import IssuerWallet
from config.config import cmod
Expand Down Expand Up @@ -97,7 +97,7 @@ async def revoke(self, schemaId: ID, i):

async def issueClaim(self, schemaId: ID, claimRequest: ClaimRequest,
iA=None,
i=None) -> Claims:
i=None) -> (Claims, Dict[str, ClaimAttributeValues]):
"""
Issue a claim for the given user and schema.
Expand All @@ -120,13 +120,16 @@ async def issueClaim(self, schemaId: ID, claimRequest: ClaimRequest,
# TODO this has un-obvious side-effects
await self._genContxt(schemaId, iA, claimRequest.userId)

c1 = await self._issuePrimaryClaim(schemaId, attributes,
(c1, claim) = await self._issuePrimaryClaim(schemaId, attributes,
claimRequest.U)
# TODO re-enable when revocation registry is fully implemented
c2 = await self._issueNonRevocationClaim(schemaId, claimRequest.Ur,
iA,
i) if claimRequest.Ur else None
return Claims(primaryClaim=c1, nonRevocClaim=c2)

signature = Claims(primaryClaim=c1, nonRevocClaim=c2)

return (signature, claim)

async def issueClaims(self, allClaimRequest: Dict[ID, ClaimRequest]) -> \
Dict[ID, Claims]:
Expand Down Expand Up @@ -156,7 +159,7 @@ async def _genContxt(self, schemaId: ID, iA, userId):
return m2

async def _issuePrimaryClaim(self, schemaId: ID, attributes: Attribs,
U) -> PrimaryClaim:
U) -> (PrimaryClaim, Dict[str, ClaimAttributeValues]):
return await self._primaryIssuer.issuePrimaryClaim(schemaId,
attributes, U)

Expand Down
11 changes: 7 additions & 4 deletions anoncreds/protocol/primary/primary_claim_issuer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from anoncreds.protocol.globals import LARGE_VPRIME_PRIME, LARGE_E_START, \
LARGE_E_END_RANGE, LARGE_PRIME
from anoncreds.protocol.types import PublicKey, SecretKey, PrimaryClaim, ID, \
Attribs
Attribs, ClaimAttributeValues
from anoncreds.protocol.utils import get_prime_in_range, strToCryptoInteger, \
randomQR
from anoncreds.protocol.wallet.issuer_wallet import IssuerWallet
from config.config import cmod
from typing import Dict


class PrimaryClaimIssuer:
Expand Down Expand Up @@ -73,7 +74,7 @@ def _genPrime(cls):
return prime

async def issuePrimaryClaim(self, schemaId: ID, attributes: Attribs,
U) -> PrimaryClaim:
U) -> (PrimaryClaim, Dict[str, ClaimAttributeValues]):
u = strToCryptoInteger(U) if isinstance(U, str) else U

if not u:
Expand All @@ -90,8 +91,10 @@ async def issuePrimaryClaim(self, schemaId: ID, attributes: Attribs,
A = await self._sign(schemaId, encodedAttrs, vprimeprime, u, e)

m2 = await self._wallet.getContextAttr(schemaId)
return PrimaryClaim(attributes._vals, encodedAttrs, m2, A, e,
vprimeprime)
claimAttributes = \
{attr: ClaimAttributeValues(attributes._vals[attr], encodedAttrs[attr]) for attr in attributes.keys()}

return (PrimaryClaim(m2, A, e, vprimeprime), claimAttributes)

async def _sign(self, schemaId: ID, attrs, v, u, e):
pk = await self._wallet.getPublicKey(schemaId)
Expand Down
53 changes: 28 additions & 25 deletions anoncreds/protocol/primary/primary_proof_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence
from typing import Sequence, Dict

from anoncreds.protocol.globals import LARGE_VPRIME, LARGE_MVECT, LARGE_E_START, \
LARGE_ETILDE, \
Expand All @@ -8,8 +8,8 @@
from anoncreds.protocol.types import PrimaryClaim, Predicate, PrimaryInitProof, \
PrimaryEqualInitProof, PrimaryPrecicateGEInitProof, PrimaryProof, \
PrimaryEqualProof, PrimaryPredicateGEProof, \
ID, ClaimInitDataType
from anoncreds.protocol.utils import getUnrevealedAttrs, fourSquares
ID, ClaimInitDataType, ClaimAttributeValues
from anoncreds.protocol.utils import splitRevealedAttrs, fourSquares
from anoncreds.protocol.wallet.prover_wallet import ProverWallet
from config.config import cmod

Expand Down Expand Up @@ -40,47 +40,47 @@ class PrimaryProofBuilder:
def __init__(self, wallet: ProverWallet):
self._wallet = wallet

async def initProof(self, schemaKey, c1: PrimaryClaim,
async def initProof(self, schemaId, c1: PrimaryClaim,
revealedAttrs: Sequence[str],
predicates: Sequence[Predicate],
m1Tilde, m2Tilde) -> PrimaryInitProof:
m1Tilde, m2Tilde, claimAttributes: Dict[str, ClaimAttributeValues]) -> PrimaryInitProof:
if not c1:
return None

eqProof = await self._initEqProof(schemaKey, c1, revealedAttrs,
m1Tilde, m2Tilde)
eqProof = await self._initEqProof(schemaId, c1, revealedAttrs,
m1Tilde, m2Tilde, claimAttributes)
geProofs = []
for predicate in predicates:
geProof = await self._initGeProof(schemaKey, eqProof, c1,
predicate)
geProof = await self._initGeProof(schemaId, eqProof, c1,
predicate, claimAttributes)
geProofs.append(geProof)
return PrimaryInitProof(eqProof, geProofs)

async def finalizeProof(self, schemaKey, cH,
async def finalizeProof(self, schemaId, cH,
initProof: PrimaryInitProof) -> PrimaryProof:
if not initProof:
return None

cH = cmod.integer(cH)
eqProof = await self._finalizeEqProof(schemaKey, cH,
eqProof = await self._finalizeEqProof(schemaId, cH,
initProof.eqProof)
geProofs = []
for initGeProof in initProof.geProofs:
geProof = await self._finalizeGeProof(schemaKey, cH, initGeProof,
geProof = await self._finalizeGeProof(schemaId, cH, initGeProof,
eqProof)
geProofs.append(geProof)
return PrimaryProof(eqProof, geProofs)

async def _initEqProof(self, schemaKey, c1: PrimaryClaim,
revealedAttrs: Sequence[str], m1Tilde, m2Tilde) \
async def _initEqProof(self, schemaId, c1: PrimaryClaim,
revealedAttrs: Sequence[str], m1Tilde, m2Tilde, claimAttributes: Dict[str, ClaimAttributeValues]) \
-> PrimaryEqualInitProof:
m2Tilde = m2Tilde if m2Tilde else cmod.integer(
cmod.randomBits(LARGE_MVECT))
unrevealedAttrs = getUnrevealedAttrs(c1.encodedAttrs, revealedAttrs)
revealedAttrs, unrevealedAttrs = splitRevealedAttrs(claimAttributes, [a.name for a in revealedAttrs])
mtilde = self._getMTilde(unrevealedAttrs)

Ra = cmod.integer(cmod.randomBits(LARGE_VPRIME))
pk = await self._wallet.getPublicKey(ID(schemaKey))
pk = await self._wallet.getPublicKey(ID(schemaId=schemaId))

A, e, v = c1.A, c1.e, c1.v
Aprime = A * (pk.S ** Ra) % pk.N
Expand All @@ -92,7 +92,7 @@ async def _initEqProof(self, schemaKey, c1: PrimaryClaim,

Rur = 1 % pk.N
for k, value in unrevealedAttrs.items():
if k in c1.encodedAttrs:
if k in claimAttributes:
Rur = Rur * (pk.R[k] ** mtilde[k])
Rur *= pk.Rms ** m1Tilde
Rur *= pk.Rctxt ** m2Tilde
Expand All @@ -105,13 +105,13 @@ async def _initEqProof(self, schemaKey, c1: PrimaryClaim,
vprime, mtilde, m1Tilde, m2Tilde,
unrevealedAttrs.keys(), revealedAttrs)

async def _initGeProof(self, schemaKey, eqProof: PrimaryEqualInitProof,
c1: PrimaryClaim, predicate: Predicate) \
async def _initGeProof(self, schemaId, eqProof: PrimaryEqualInitProof,
c1: PrimaryClaim, predicate: Predicate, claimAttributes: Dict[str, ClaimAttributeValues]) \
-> PrimaryPrecicateGEInitProof:
# gen U for Delta
pk = await self._wallet.getPublicKey(ID(schemaKey))
pk = await self._wallet.getPublicKey(ID(schemaId=schemaId))
k, value = predicate.attrName, predicate.value
delta = c1.encodedAttrs[k] - value
delta = claimAttributes[k].encoded - value
if delta < 0:
raise ValueError("Predicate is not satisfied")

Expand Down Expand Up @@ -142,23 +142,26 @@ async def _initGeProof(self, schemaKey, eqProof: PrimaryEqualInitProof,
return PrimaryPrecicateGEInitProof(CList, TauList, u, utilde, r, rtilde,
alphatilde, predicate, T)

async def _finalizeEqProof(self, schemaKey, cH,
async def _finalizeEqProof(self, schemaId, cH,
initProof: PrimaryEqualInitProof) -> PrimaryEqualProof:
e = initProof.eTilde + (cH * initProof.ePrime)
v = initProof.vTilde + (cH * initProof.vPrime)

m = {}

claimAttributes = await self._wallet.getClaimAttributes(ID(schemaId=schemaId))

for k in initProof.unrevealedAttrs:
m[str(k)] = initProof.mTilde[str(k)] + (
cH * initProof.c1.encodedAttrs[str(k)])
ms = await self._wallet.getMasterSecret(ID(schemaKey))
cH * claimAttributes[str(k)].encoded)
ms = await self._wallet.getMasterSecret(ID(schemaId=schemaId))
m1 = initProof.m1Tilde + (cH * ms)
m2 = initProof.m2Tilde + (cH * initProof.c1.m2)

return PrimaryEqualProof(e, v, m, m1, m2, initProof.Aprime,
initProof.revealedAttrs)

async def _finalizeGeProof(self, schemaKey, cH,
async def _finalizeGeProof(self, schemaId, cH,
initProof: PrimaryPrecicateGEInitProof,
eqProof: PrimaryEqualProof) \
-> PrimaryPredicateGEProof:
Expand Down
28 changes: 12 additions & 16 deletions anoncreds/protocol/primary/primary_proof_verifier.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from anoncreds.protocol.globals import LARGE_E_START, ITERATIONS, DELTA
from anoncreds.protocol.primary.primary_proof_common import calcTeq, calcTge
from anoncreds.protocol.types import PrimaryEqualProof, \
PrimaryPredicateGEProof, PrimaryProof, ID, ProofInput
from anoncreds.protocol.utils import encodeAttr
PrimaryPredicateGEProof, PrimaryProof, ID
from anoncreds.protocol.wallet.wallet import Wallet
from config.config import cmod

Expand All @@ -11,40 +10,37 @@ class PrimaryProofVerifier:
def __init__(self, wallet: Wallet):
self._wallet = wallet

async def verify(self, schemaKey, cHash, primaryProof: PrimaryProof,
allRevealedAttrs):
async def verify(self, schemaId, cHash, primaryProof: PrimaryProof):
cH = cmod.integer(cHash)
THat = await self._verifyEquality(schemaKey, cH, primaryProof.eqProof,
allRevealedAttrs)
THat = await self._verifyEquality(schemaId, cH, primaryProof.eqProof)
for geProof in primaryProof.geProofs:
THat += await self._verifyGEPredicate(schemaKey, cH, geProof)
THat += await self._verifyGEPredicate(schemaId, cH, geProof)

return THat

async def _verifyEquality(self, schemaKey, cH, proof: PrimaryEqualProof,
allRevealedAttrs):
async def _verifyEquality(self, schemaId, cH, proof: PrimaryEqualProof):
THat = []
pk = await self._wallet.getPublicKey(ID(schemaKey))
attrNames = (await self._wallet.getSchema(ID(schemaKey))).attrNames
unrevealedAttrNames = set(attrNames) - set(proof.revealedAttrNames)
pk = await self._wallet.getPublicKey(ID(schemaId=schemaId))
attrNames = (await self._wallet.getSchema(ID(schemaId=schemaId))).attrNames
unrevealedAttrNames = set(attrNames) - set(proof.revealedAttrs.keys())

T1 = calcTeq(pk, proof.Aprime, proof.e, proof.v,
proof.m, proof.m1, proof.m2,
unrevealedAttrNames)

Rar = 1 % pk.N
for attrName in proof.revealedAttrNames:
Rar *= pk.R[str(attrName)] ** encodeAttr(allRevealedAttrs[attrName])
for attrName in proof.revealedAttrs.keys():
Rar *= pk.R[str(attrName)] ** proof.revealedAttrs[str(attrName)]
Rar *= proof.Aprime ** (2 ** LARGE_E_START)
T2 = (pk.Z / Rar) ** (-1 * cH) % pk.N
T = T1 * T2 % pk.N

THat.append(T)
return THat

async def _verifyGEPredicate(self, schemaKey, cH,
async def _verifyGEPredicate(self, schemaId, cH,
proof: PrimaryPredicateGEProof):
pk = await self._wallet.getPublicKey(ID(schemaKey))
pk = await self._wallet.getPublicKey(ID(schemaId=schemaId))
k, v = proof.predicate.attrName, proof.predicate.value

TauList = calcTge(pk, proof.u, proof.r, proof.mj, proof.alpha, proof.T)
Expand Down
Loading

0 comments on commit 6dc0144

Please sign in to comment.