-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
52 lines (47 loc) · 1.62 KB
/
parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
# Python script for parsing Wycheproof test vectors for ECDSA and EDDSA
# for example python3 parse.py ed448_test.json
# Output should be C digestible, outputting on separate lines the public key, the comment, the message, the signature and finally the outcome, valid or invalid
#
import sys
# extract substring at position ptr, return substring and updated ptr
def extract(s,ptr,id) :
pk=s.find(id,ptr)
fpk=s.find('"',pk+len(id)) # skip "
lpk=s.find('"',fpk+1)+1
return s[fpk+1:lpk-1],lpk
inputfile=sys.argv[1]
# read whole file into string
with open(inputfile) as f: # Wycheproof JSON test vectors
data = f.read()
ptr=0
finished=False
pubkey=''
if inputfile[0:5]=="ecdsa" :
pubkey='"uncompressed"' # NOTE: Could be "pk" or "uncompressed"
elif inputfile[0:2]=="ed" :
pubkey='"pk"' # NOTE: Could be "pk" or "uncompressed"
else :
sys.exit(0)
while not finished :
pk,ptr=extract(data,ptr,pubkey) # ptr points to public key.
npk=data.find(pubkey,ptr) # is there a next public key?
if npk<0 :
npk=len(data) # end of file
# print public key, comments, message, signature and outcome
while (True) :
print(pk)
ss,ptr=extract(data,ptr,'"comment"')
print(ss)
ss,ptr=extract(data,ptr,'"msg"')
print(ss)
ss,ptr=extract(data,ptr,'"sig"')
print(ss)
ss,ptr=extract(data,ptr,'"result"')
print(ss)
nmg=data.find('"comment"',ptr)
if nmg<0: # there is no next comment, we are done
finished=True
break
if nmg>=npk: # oops - there is a new public key
break