Skip to content

Commit

Permalink
refactored Saider and Serder to use new Diger and Diger._digest class…
Browse files Browse the repository at this point in the history
… method so code is

much more DRY
  • Loading branch information
SmithSamuelM committed Aug 9, 2024
1 parent 1f3efa1 commit 5969fd5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 62 deletions.
33 changes: 6 additions & 27 deletions src/keri/core/coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3140,18 +3140,18 @@ def __init__(self, raw=None, ser=None, code=DigDex.Blake3_256, **kwa):
if self.code not in DigDex:
raise InvalidCodeError(f"Unsupported Digest {code=}.")


def _digest(self, ser, code=DigDex.Blake3_256):
@classmethod
def _digest(cls, ser, code=DigDex.Blake3_256):
"""Returns raw digest of ser using digest algorithm given by code
Parameters:
ser (bytes): serialization from which raw digest is computed
code (str): derivation code used to lookup digest algorithm
"""
if code not in self.Digests:
if code not in cls.Digests:
raise InvalidCodeError(f"Unsupported Digest {code=}.")

klas, size, length = self.Digests[code] # digest algo size & length
klas, size, length = cls.Digests[code] # digest algo size & length
ikwa = dict(digest_size=size) if size else dict() # opt digest size
dkwa = dict(length=length) if length else dict() # opt digest length
raw = klas(ser, **ikwa).digest(**dkwa)
Expand Down Expand Up @@ -3277,19 +3277,6 @@ class Saider(Matter):
"""
Dummy = "#" # dummy spaceholder char for said. Must not be a valid Base64 char
# should be same set of codes as in coring.DigestCodex coring.DigDex so
# .digestive property works. Unit test ensures code sets match
Digests = {
MtrDex.Blake3_256: Digestage(klas=blake3.blake3, size=None, length=None),
MtrDex.Blake2b_256: Digestage(klas=hashlib.blake2b, size=32, length=None),
MtrDex.Blake2s_256: Digestage(klas=hashlib.blake2s, size=None, length=None),
MtrDex.SHA3_256: Digestage(klas=hashlib.sha3_256, size=None, length=None),
MtrDex.SHA2_256: Digestage(klas=hashlib.sha256, size=None, length=None),
MtrDex.Blake3_512: Digestage(klas=blake3.blake3, size=None, length=64),
MtrDex.Blake2b_512: Digestage(klas=hashlib.blake2b, size=None, length=None),
MtrDex.SHA3_512: Digestage(klas=hashlib.sha3_512, size=None, length=None),
MtrDex.SHA2_512: Digestage(klas=hashlib.sha512, size=None, length=None),
}

def __init__(self, raw=None, *, code=None, sad=None,
kind=None, label=Saids.d, ignore=None, **kwa):
Expand Down Expand Up @@ -3434,18 +3421,10 @@ def _derive(clas, sad: dict, *,
for f in ignore:
del ser[f]

# string now has
# correct size
klas, size, length = clas.Digests[code]
# string now has correct size
# sad as 'v' verision string then use its kind otherwise passed in kind
cpa = [clas._serialize(ser, kind=kind)] # raw pos arg class
ckwa = dict() # class keyword args
if size:
ckwa.update(digest_size=size) # optional digest_size
dkwa = dict() # digest keyword args
if length:
dkwa.update(length=length)
return klas(*cpa, **ckwa).digest(**dkwa), sad # raw digest and sad
return (Diger._digest(ser=cpa[0], code=code), sad) # raw digest and sad


def derive(self, sad, code=None, **kwa):
Expand Down
35 changes: 2 additions & 33 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,6 @@ class Serder:
# Spans dict keyed by version (Versionage instance) of version string span (size)
Spans = {Vrsn_1_0: VER1FULLSPAN, Vrsn_2_0: VER2FULLSPAN}

# Maps digest codes to Digestages of algorithms for computing digest.
# Should be based on the same set of codes as in coring.DigestCodex
# coring.DigDex so .digestive property works.
# Use unit tests to ensure codex elements sets match
Digests = {
DigDex.Blake3_256: Digestage(klas=blake3.blake3, size=None, length=None),
DigDex.Blake2b_256: Digestage(klas=hashlib.blake2b, size=32, length=None),
DigDex.Blake2s_256: Digestage(klas=hashlib.blake2s, size=None, length=None),
DigDex.SHA3_256: Digestage(klas=hashlib.sha3_256, size=None, length=None),
DigDex.SHA2_256: Digestage(klas=hashlib.sha256, size=None, length=None),
DigDex.Blake3_512: Digestage(klas=blake3.blake3, size=None, length=64),
DigDex.Blake2b_512: Digestage(klas=hashlib.blake2b, size=None, length=None),
DigDex.SHA3_512: Digestage(klas=hashlib.sha3_512, size=None, length=None),
DigDex.SHA2_512: Digestage(klas=hashlib.sha512, size=None, length=None),
}

# map seal clan names to seal counter code for grouping seals in anchor list
ClanCodes = dict()
ClanCodes[SClanDom.SealDigest.__name__] = SealDex_2_0.DigestSealSingles
Expand Down Expand Up @@ -777,14 +761,7 @@ def _verify(self):
raw = self.dumps(sad, kind=self.kind) # serialize dummied sad copy
for label, code in saids.items():
if code in DigDex: # subclass override if non digestive allowed
klas, size, length = self.Digests[code] # digest algo size & length
ikwa = dict() # digest algo class initi keyword args
if size:
ikwa.update(digest_size=size) # optional digest_size
dkwa = dict() # digest method keyword args
if length:
dkwa.update(length=length)
dig = Matter(raw=klas(raw, **ikwa).digest(**dkwa), code=code).qb64
dig = Diger(ser=raw, code=code).qb64
if dig != self._sad[label]: # compare to original
raise ValidationError(f"Invalid said field '{label}' in sad"
f" = {self._sad}, should be {dig}.")
Expand Down Expand Up @@ -1005,15 +982,7 @@ def makify(self, sad, *, proto=None, vrsn=None, kind=None,
raw = self.dumps(sad, kind=kind, proto=proto, vrsn=vrsn) # serialize sized dummied sad
for label, code in _saids.items():
if code in DigDex: # subclass override if non digestive allowed
klas, dsize, dlen = self.Digests[code] # digest algo size & length
ikwa = dict() # digest algo class initi keyword args
if dsize:
ikwa.update(digest_size=dsize) # optional digest_size
dkwa = dict() # digest method keyword args
if dlen:
dkwa.update(length=dlen)
dig = Matter(raw=klas(raw, **ikwa).digest(**dkwa), code=code).qb64
sad[label] = dig
sad[label] = Diger(ser=raw, code=code).qb64

raw = self.dumps(sad, kind=kind, proto=proto, vrsn=vrsn) # compute final raw
if kind == Kinds.cesr:# cesr kind version string does not set size
Expand Down
6 changes: 4 additions & 2 deletions tests/core/test_coring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4432,6 +4432,10 @@ def test_diger():
"""
Test the support functionality for Diger subclass of CryMat
"""
# Ensure keyspace of Diger.Digests is same as codes in DigDex
assert set(coring.DigDex) == set(Diger.Digests.keys())


with pytest.raises(EmptyMaterialError):
diger = Diger()

Expand Down Expand Up @@ -4612,8 +4616,6 @@ def test_saider():
"""
Test Saider object
"""
# Test class attribute Digest matches DigDex (i.e.DigestCodex)
assert set(Saider.Digests.keys()) == set(code for code in DigDex)

code = MtrDex.Blake3_256
kind = Kinds.json
Expand Down

0 comments on commit 5969fd5

Please sign in to comment.