-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSS_test_API.ts
149 lines (135 loc) · 3.18 KB
/
VSS_test_API.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import axios, { AxiosError } from "axios";
import bigInt, { BigInteger } from "big-integer";
import { VSS, verify_share } from "./VSS_web_fns";
interface Share {
index: number;
value: {
value: string;
prime: string;
};
}
interface Commitment {
tag: string;
data: {
value: string;
prime: string;
};
}
interface Generator {
tag: string;
data: {
value: string;
prime: string;
};
}
interface Group {
generator: Generator;
p: string;
}
interface VSSResponse {
share: Share;
commitments: Commitment[];
group: Group;
}
interface VSSBody {
share: Share;
commitments: Commitment[];
group: Group;
team_message: string;
}
function verifyShare(input: VSSResponse) {
const p = bigInt(input.group.generator.data.prime, 16);
const q = bigInt(input.group.p, 16);
const g = bigInt(input.group.generator.data.value);
const share = [
bigInt(input.share.index),
bigInt(input.share.value.value, 16),
];
const commitments = input.commitments
.map((commitment) => bigInt(commitment.data.value, 16))
.reverse();
return verify_share(share, commitments, p, q, g);
}
function generateCommitments(
secret: string,
n: number,
t: number,
P: BigInteger,
Q: BigInteger,
g: BigInteger
) {
const { commitments, shared_secrets } = VSS(secret, n, t, P, Q, g);
const postData: VSSBody = {
share: {
index: 1,
value: {
value: shared_secrets[0][1].toString(16),
prime: Q.toString(16),
},
},
commitments: commitments
.map((commitment) => {
return {
tag: "prime",
data: {
value: commitment.toString(16),
prime: P.toString(16),
},
};
})
.reverse(),
group: {
generator: {
tag: "prime",
data: {
value: "2",
prime: P.toString(16),
},
},
p: Q.toString(16),
},
team_message: "You have been Pwned",
};
return postData;
}
function verifyWithAPI(type: string) {
axios
.get<VSSResponse>(`http://hash-effect.onrender.com/vss/share/${type}`)
.then((resp) => resp.data)
.then((input) => {
const result = verifyShare(input);
console.log(result);
const postData: VSSBody = {
commitments: input.commitments,
group: input.group,
share: input.share,
team_message: "You have been Pwned",
};
axios
.post("https://hash-effect.onrender.com/vss/verify", postData)
.then((resp) => resp.data)
.then((output) => {
console.log(output);
})
.catch((err: AxiosError) => console.error(err.response?.data));
})
.catch((err: AxiosError) => console.error(err.toJSON()));
}
function verifyGeneratedCommitments() {
const toSend = generateCommitments(
"hey",
10,
7,
bigInt("15943542169520389343"),
bigInt("7971771084760194671"),
bigInt(2)
);
axios
.post("https://hash-effect.onrender.com/vss/verify", toSend)
.then((resp) => resp.data)
.then((output) => console.log(output))
.catch((err: AxiosError) => console.log(err.response?.data));
}
verifyWithAPI("valid");
// verifyWithAPI("invalid");
// verifyGeneratedCommitments();