-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge54.ts
271 lines (257 loc) · 10.5 KB
/
challenge54.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import * as crypto from 'crypto';
import {CompressionFn} from './challenge52';
import {AES_128_BLOCK_LENGTH_BYTES} from '../set1/challenge7';
import {splitIntoBlocks} from '../set1/challenge6';
export type MsgDigestPair = { msg: Buffer; digest: Buffer };
// key: hex state, value: (message block, computed digest)
export type DiamondStructureLayer = Map<string, MsgDigestPair>;
export type DiamondStructure = {
layers: Array<DiamondStructureLayer>;
};
/**
* Generate map: K: digest, V: block
* @param state state used when computing the digest
* @param numOfCandidateBlocks number of blocks which are going to be generated
* @param compressionFn compression function
* @param prevCandidates previous blocks used
*/
function generateCandidateBlocksMap(
state: Buffer,
numOfCandidateBlocks: number,
compressionFn: CompressionFn,
prevCandidates?: ReadonlyArray<Buffer>
): Map<string, Buffer> {
// digest -> block
const dictionary: Map<string, Buffer> = new Map();
const usedBlocks: Set<string> = prevCandidates ?
new Set(prevCandidates.map(c => c.toString('hex'))) : new Set();
let candidateBlock: Buffer, candidateDigest: Buffer;
for (let k = 0; k < numOfCandidateBlocks; k++) {
candidateBlock = crypto.randomBytes(AES_128_BLOCK_LENGTH_BYTES);
while (usedBlocks.has(candidateBlock.toString('hex'))) {
candidateBlock = crypto.randomBytes(AES_128_BLOCK_LENGTH_BYTES);
}
candidateDigest = compressionFn(state, candidateBlock);
usedBlocks.add(candidateBlock.toString('hex'));
dictionary.set(candidateDigest.toString('hex'), candidateBlock);
}
return dictionary;
}
/**
* Randomly generate states
* @param numOfStates number of states to be generated
* @param digestSizeInBytes size of each generated state
*/
function generateStates(
numOfStates: number,
digestSizeInBytes: number
): Array<Buffer> {
const statesSet: Set<string> = new Set();
let randomState: Buffer;
for (let j = 0; j < numOfStates; j++) {
// randomly generate states
randomState = crypto.randomBytes(digestSizeInBytes);
while (statesSet.has(randomState.toString('hex'))) {
randomState = crypto.randomBytes(digestSizeInBytes);
}
statesSet.add(randomState.toString('hex'));
}
return Array.from(statesSet).map(hexState => Buffer.from(hexState, 'hex'));
}
function generateCandidateBlocks(
i: number,
states: ReadonlyArray<Buffer>,
compressionFn: CompressionFn,
digestSizeInBytes: number,
): Map<string, Map<string, Buffer>> {
const numOfCandidateBlocks = Math.pow(2, Math.ceil(((digestSizeInBytes * 8) - i + 1) / 2));
const stateToDigestMap = new Map<string, Map<string, Buffer>>();
for (let j = 0; j < states.length; j++) {
stateToDigestMap.set(states[j].toString('hex'),
generateCandidateBlocksMap(states[j], numOfCandidateBlocks, compressionFn)
);
}
return stateToDigestMap;
}
type DiamondStructureCollision = {
state1: string;
msgPair1: MsgDigestPair;
state2: string;
msgPair2: MsgDigestPair;
};
function findDiamondCollisionForState(
curStateHex: string,
stateToDigestMap: Map<string, Map<string, Buffer>>,
states: ReadonlyArray<Buffer>,
layer: DiamondStructureLayer,
digests: Set<string>
): DiamondStructureCollision | undefined {
const curStateDigestToMsgMap = stateToDigestMap.get(curStateHex) as Map<string, Buffer>;
const curStateDigests = Array.from(curStateDigestToMsgMap.keys());
let otherStateHex: string, otherStateDigests: string[], otherStateDigestToMsgMap: Map<string, Buffer>;
let collision: DiamondStructureCollision | undefined;
for (let otherStateIdx = 0; otherStateIdx < states.length; otherStateIdx++) {
otherStateHex = states[otherStateIdx].toString('hex');
if (curStateHex != otherStateHex && !layer.has(otherStateHex)) {
otherStateDigestToMsgMap = stateToDigestMap.get(otherStateHex) as Map<string, Buffer>;
otherStateDigests = Array.from(otherStateDigestToMsgMap.keys());
const match = curStateDigests.find(digest =>
!digests.has(digest) && otherStateDigests.includes(digest)
);
if (match) {
const matchBuffer = Buffer.from(match, 'hex');
collision = {
state1: curStateHex,
msgPair1: { msg: curStateDigestToMsgMap.get(match) as Buffer, digest: matchBuffer },
state2: otherStateHex,
msgPair2: { msg: otherStateDigestToMsgMap.get(match) as Buffer, digest: matchBuffer },
}
break;
}
}
}
return collision;
}
/**
* Initialize i-th layer of diamond structure
* @param i layer number
* @param compressionFn compression function
* @param digestSizeInBytes digest size in bytes
* @param prevLayer previous layer of diamond structure
*/
export function initIthLayerOfDiamondStructure(
i: number,
compressionFn: CompressionFn,
digestSizeInBytes: number,
prevLayer?: DiamondStructureLayer,
): DiamondStructureLayer {
const numOfStates = Math.pow(2, i);
const layer: DiamondStructureLayer = new Map<string, MsgDigestPair>();
const digests: Set<string> = new Set();
let states: Buffer[];
if (prevLayer) {
// use previous layer
const previousDigests = Array.from(prevLayer.values()).map((msgDigestPair) => msgDigestPair.digest);
states = Array.from(new Set(previousDigests));
} else {
// "first" layer
states = generateStates(numOfStates, digestSizeInBytes);
}
// generate candidate blocks for each state
const stateToDigestMap = generateCandidateBlocks(i, states, compressionFn, digestSizeInBytes);
let curStateHex: string, curStateDigestToMsgMap: Map<string, Buffer>, curCollision: DiamondStructureCollision | undefined;
// find collision for each state
for (let curStateIdx = 0; curStateIdx < states.length; curStateIdx++) {
curStateHex = states[curStateIdx].toString('hex');
while (!layer.has(curStateHex)) {
curStateDigestToMsgMap = stateToDigestMap.get(curStateHex) as Map<string, Buffer>;
curCollision = findDiamondCollisionForState(curStateHex, stateToDigestMap, states, layer, digests);
if (curCollision) {
layer.set(curCollision.state1, curCollision.msgPair1);
layer.set(curCollision.state2, curCollision.msgPair2);
digests.add(curCollision.msgPair1.digest.toString('hex'));
} else {
// if no collision could be found for current state, generate new candidate blocks
const candidates = generateCandidateBlocksMap(
states[curStateIdx], curStateDigestToMsgMap.size,
compressionFn, Array.from(curStateDigestToMsgMap.values())
);
stateToDigestMap.set(curStateHex, candidates);
}
}
}
return layer;
}
/**
* Initialize diamond structure
* @param k k-parameter
* @param compressionFn compression function
* @param compressionFnDigestSize digest size of compression function in bytes
*/
export function initDiamondStructure(
k: number,
compressionFn: CompressionFn,
compressionFnDigestSize: number,
): DiamondStructure {
const layers = Array(k);
let prevLayer: DiamondStructureLayer | null = null;
for (let i = k; i > 0; i--) {
layers[i - 1] = i == k ?
initIthLayerOfDiamondStructure(i, compressionFn, compressionFnDigestSize) :
initIthLayerOfDiamondStructure(i, compressionFn, compressionFnDigestSize, prevLayer as DiamondStructureLayer);
prevLayer = layers[i - 1];
}
return {
layers
}
}
export function findLinkingBlock(
msgDigest: Buffer,
layer: DiamondStructureLayer,
compressionFn: CompressionFn
): Buffer {
let randomBlock: Buffer = crypto.randomBytes(AES_128_BLOCK_LENGTH_BYTES);
let randomDigestHex: string = compressionFn(msgDigest, randomBlock).toString('hex');
while (!layer.has(randomDigestHex)) {
randomBlock = crypto.randomBytes(AES_128_BLOCK_LENGTH_BYTES);
randomDigestHex = compressionFn(msgDigest, randomBlock).toString('hex');
}
return randomBlock;
}
export function constructChainOfBlocks(
k: number,
initialDigest: string,
diamondStructure: DiamondStructure,
): Buffer[] {
const blocks = Array(k);
let curDigest = initialDigest, curLayer, curMsgPair;
for (let curLayerIdx = k - 1; curLayerIdx >= 0; curLayerIdx--) {
curLayer = diamondStructure.layers[curLayerIdx];
curMsgPair = curLayer.get(curDigest) as MsgDigestPair;
blocks[k - 1 - curLayerIdx] = curMsgPair.msg;
curDigest = curMsgPair.digest.toString('hex');
}
return blocks;
}
export function getPredictionHashFromDiamondStructure(
predictionLengthInBytes: number,
finalLayer: DiamondStructureLayer,
compressionFn: CompressionFn
): Buffer {
const key = Array.from(finalLayer.keys())[0]
const msgPair = finalLayer.get(key) as MsgDigestPair;
const prefinalDigest = msgPair.digest;
const paddingBlock = Buffer.alloc(AES_128_BLOCK_LENGTH_BYTES);
paddingBlock[0] = 0x80; // 1 bit
// length of the prediction document
paddingBlock.writeUInt32BE(predictionLengthInBytes * 8, paddingBlock.length - 4);
return compressionFn(prefinalDigest, paddingBlock);
}
export function generatePredictionFromDiamondStructure(
initialState: Buffer,
msg: Buffer,
k: number,
diamondStructure: DiamondStructure,
compressionFn: CompressionFn
): Buffer {
// compute hash of the prediction
const msgBlocks = splitIntoBlocks(msg, AES_128_BLOCK_LENGTH_BYTES);
let msgDigest = initialState;
for (let i = 0; i < msgBlocks.length; i++) {
msgDigest = compressionFn(msgDigest, msgBlocks[i]);
}
// find a linking message
const layer = diamondStructure.layers[k - 1];
const linkingBlock = findLinkingBlock(msgDigest, layer, compressionFn);
// copy original msg
const prediction = Buffer.alloc((msgBlocks.length + k + 1) * AES_128_BLOCK_LENGTH_BYTES);
msg.copy(prediction, 0);
// copy linking block
linkingBlock.copy(prediction, msg.length);
// copy matching blocks from the diamond structure
const initialDigest = compressionFn(msgDigest, linkingBlock).toString('hex');
const diamondBlocks = constructChainOfBlocks(k, initialDigest, diamondStructure);
const suffix = Buffer.concat(diamondBlocks);
suffix.copy(prediction, msg.length + linkingBlock.length);
return prediction;
}