Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/blake2b/test/oracle.py # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / blake2b / test / oracle.py` 74 lines ยท 2529 bytes Raw Blame History 1 # SPDX-License-Identifier: Apache-2.0 2 """Test oracle for om/blake2b: python3 hashlib.blake2b generates vectors only; nothing here ships. 3 4 oracle.py boundary print vectors/boundary.txt (lines: OUTN KN N DIGEST; key byte i = i, 5 message byte i = i mod 251) 6 oracle.py diff SEED DIR write DIR/diff.in (driver case lines) and DIR/diff.out (expected digests): 7 every length 0..1100 unkeyed (outn 64) and keyed (kn 1 + L%64, 8 outn 64 - L%64), then 64 seeded-random cases with lengths up to 65536 9 (lengths 65535 and 65536 included), random key length 0..64 and outn 1..64 10 """ 11 import hashlib 12 import os 13 import random 14 import sys 15 16 CAP = 65536 17 18 19 def pattern(n): 20 return bytes(i % 251 for i in range(n)) 21 22 23 def line(outn, key, msg): 24 return "%d %s %s\n" % (outn, key.hex() or "-", msg.hex() or "-") 25 26 27 def digest(outn, key, msg): 28 return hashlib.blake2b(msg, key=key, digest_size=outn).hexdigest() 29 30 31 def boundary(): 32 edges = [0, 1, 111, 112, 127, 128, 129, 255, 256, 257, 383, 384, 385, 33 1023, 1024, 1025, 65407, 65408, 65409, 65535, CAP] 34 rows = [] 35 for kn in (0, 64): 36 for n in edges: 37 rows.append((64, kn, n)) 38 for kn in (1, 32, 63): 39 for n in (0, 128): 40 rows.append((64, kn, n)) 41 for outn in (1, 2, 20, 32, 48, 63): 42 for kn in (0, 64): 43 rows.append((outn, kn, 128)) 44 for outn, kn, n in rows: 45 print(outn, kn, n, digest(outn, bytes(range(kn)), pattern(n))) 46 47 48 def diff(seed, outdir): 49 rng = random.Random(seed) 50 cases = [] 51 for n in range(1101): 52 msg = rng.randbytes(n) 53 cases.append((64, b"", msg)) 54 r = n % 64 55 cases.append((64 - r, rng.randbytes(1 + r), msg)) 56 lengths = [CAP - 1, CAP] + [rng.randint(0, CAP) for _ in range(62)] 57 for n in lengths: 58 kn = rng.choice([0, rng.randint(1, 64)]) 59 cases.append((rng.randint(1, 64), rng.randbytes(kn), rng.randbytes(n))) 60 with open(os.path.join(outdir, "diff.in"), "w") as fi, \ 61 open(os.path.join(outdir, "diff.out"), "w") as fo: 62 for outn, key, msg in cases: 63 fi.write(line(outn, key, msg)) 64 fo.write(digest(outn, key, msg) + "\n") 65 print(len(cases)) 66 67 68 if __name__ == "__main__": 69 if sys.argv[1:] == ["boundary"]: 70 boundary() 71 elif len(sys.argv) == 4 and sys.argv[1] == "diff": 72 diff(int(sys.argv[2]), sys.argv[3]) 73 else: 74 sys.exit(__doc__)