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/ed25519/test/gen/diff.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 / ed25519 / test / gen / diff.py` 58 lines ยท 2225 bytes Raw Blame History 1 # SPDX-License-Identifier: Apache-2.0 2 """Differential corpus for om/`ed25519` (test tooling only; needs pynacl and cryptography). 3 4 Usage: python3 diff.py OUTDIR COUNT SEED 5 Writes OUTDIR/diff-NN.txt (driver line format, at most 400 keypairs per file): for each of COUNT 6 keypairs, a random message of 0..4096 bytes signed by pynacl (even index) or cryptography (odd 7 index), expected valid in both modes, then the same case with one random bit of pk || sig || msg 8 flipped, expected invalid in both modes. Each signature is also checked by the signing library. 9 """ 10 import os 11 import random 12 import sys 13 14 import nacl.signing 15 from cryptography.hazmat.primitives import serialization 16 from cryptography.hazmat.primitives.asymmetric.`ed25519` import Ed25519PrivateKey 17 18 RAW = serialization.Encoding.Raw, serialization.PublicFormat.Raw 19 20 21 def keypair_sign(i, seed, msg): 22 if i % 2 == 0: 23 sk = nacl.signing.SigningKey(seed) 24 sig = sk.sign(msg).signature 25 pk = sk.verify_key.encode() 26 sk.verify_key.verify(msg, sig) 27 else: 28 sk = Ed25519PrivateKey.from_private_bytes(seed) 29 sig = sk.sign(msg) 30 pub = sk.public_key() 31 pk = pub.public_bytes(*RAW) 32 pub.verify(sig, msg) 33 return pk, sig 34 35 36 def main(): 37 out, count, seed = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) 38 os.makedirs(out, exist_ok=True) 39 rnd = random.Random(seed) 40 per = 400 41 files = 0 42 for start in range(0, count, per): 43 with open(os.path.join(out, f"diff-{files:02d}.txt"), "w") as f: 44 for i in range(start, min(count, start + per)): 45 msg = rnd.randbytes(rnd.randrange(0, 4097)) 46 pk, sig = keypair_sign(i, rnd.randbytes(32), msg) 47 f.write(f"1 1 {pk.hex()} {sig.hex()} {msg.hex()}\n") 48 blob = bytearray(pk + sig + msg) 49 bit = rnd.randrange(0, 8 * len(blob)) 50 blob[bit // 8] ^= 1 << (bit % 8) 51 fp, fs, fm = bytes(blob[:32]), bytes(blob[32:96]), bytes(blob[96:]) 52 f.write(f"0 0 {fp.hex()} {fs.hex()} {fm.hex()}\n") 53 files += 1 54 print(f"diff: {count} keypairs, {2 * count} cases, {files} files, seed {seed}") 55 56 57 if __name__ == "__main__": 58 main()