Skip to content

Commit 30455ac

Browse files
committed
Adapt sliding windows test -3/256 and verify the test functionality
1 parent 1cd46bd commit 30455ac

File tree

2 files changed

+12
-92
lines changed

2 files changed

+12
-92
lines changed

src/sha256-class.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bytes, UInt32, UInt8 } from 'o1js';
1+
import { Bytes, Field, UInt32, UInt8 } from 'o1js';
22
import { H as initialHashWords, K } from './constants.js';
33
import {
44
ch,
@@ -267,50 +267,6 @@ class SHA256 {
267267
}
268268
}
269269

270-
import { Provable, Field } from 'o1js';
271-
import { sha256 as nobleSha256 } from '@noble/hashes/sha256';
272-
import { bytesToHex, concatBytes } from '@noble/hashes/utils';
273-
import { Timer } from './test-utils.js';
274-
275-
let input = Bytes.fromString('abc');
276-
let digest = new SHA256().update(input).digest();
277-
Provable.log('digest from class: ', digest.toHex());
278-
279-
let input1 = new Uint8Array([1, 2]);
280-
let input2 = new Uint8Array([3, 4]);
281-
let input12 = concatBytes(input1, input2);
282-
283-
let nobleChain = nobleSha256(input12);
284-
let nobleConcat = nobleSha256.create().update(input1).update(input2).digest();
285-
Provable.log('\nnobleChain: ', bytesToHex(nobleChain));
286-
Provable.log('nobleWhole: ', bytesToHex(nobleConcat));
287-
288-
let shaChain = new SHA256().update(Bytes.from(input12)).digest().toHex();
289-
let shaConcat = new SHA256()
290-
.update(Bytes.from(input1))
291-
.update(Bytes.from(input2))
292-
.digest()
293-
.toHex();
294-
Provable.log('\nshaChain: ', shaChain);
295-
Provable.log('shaWholet: ', shaConcat);
296-
297-
// measure run time
298-
const timer = new Timer();
299-
SHA256.hash(Bytes.fromString('abc')).toHex();
300-
timer.end();
301-
console.log('timer: ', timer.executionTime);
302-
303-
// prove that the class hash function is provable
304-
class Bytes32 extends Bytes(32) {}
305-
console.time('sha256 witness');
306-
Provable.runAndCheck(() => {
307-
let input = Provable.witness(Bytes32.provable, () => Bytes32.random());
308-
SHA256.hash(input);
309-
});
310-
console.timeEnd('sha256 witness');
311-
312-
//TODO Refactor and refine code
313-
//TODO Adapt and verify sliding window test
314270
//TODO Update code documentation
315271
//TODO Omit unnecessary files
316272
//TODO? point to the fact that the o1js used custom sigma functions

src/sha256.test.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { o1jsHash, nodeHash, generateRandomInput } from './test-utils';
22
import { bytesToHex, concatBytes } from '@noble/hashes/utils';
33
import { sha256 as nobleHashUint } from '@noble/hashes/sha256';
4-
import { sha256O1js, SHA256 } from './sha256';
4+
import { sha256O1js } from './sha256';
5+
import { SHA256 } from './sha256-class';
56
import * as crypto from 'crypto';
67
import { Bytes } from 'o1js';
78

@@ -130,7 +131,7 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
130131
}
131132
});
132133

133-
// !This test takes an extremely long time to finish
134+
// !This test takes extremely long time to finish
134135
test.skip('should have passing sliding window tests - 4096', () => {
135136
const testWindow4096 = new Array<string>(4096);
136137
for (let i = 0; i < testWindow4096.length; i++)
@@ -142,12 +143,12 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
142143
}
143144
});
144145

145-
// ===============================================================
146-
147-
// This test aims to destruct an input into seperate 32-bit blocks and then compare the digest of the full input against recursive updates of the 32-bit blocks
148-
// It has the same concept of the sliding window test but on sequential update of destruced message blocks.
149-
//TODO: This test requires update method for the hash function.
150-
test.only('should pass sliding window tests - 3/256', () => {
146+
/*
147+
This test aims to destruct an input into seperate 32-bit blocks and then compare the digest of the full input against recursive updates of the 32-bit blocks
148+
It has the same concept of the sliding window test 4096 but on sequential update of destruced message blocks.
149+
! This test takes around 1.5 hours to finish
150+
*/
151+
test.skip('should pass sliding window tests - 3/256', () => {
151152
let BUF_768 = new Uint8Array(256 * 3);
152153

153154
// Fill with random data
@@ -167,46 +168,9 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
167168
let b3Bytes = Bytes.from(b3);
168169

169170
expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
170-
// expect(nobleHashUint.create().update(b1).update(b2).update(b3).digest()).toStrictEqual(fnH);
171-
expect(new SHA256().update(b1Bytes).update(b2Bytes).digest().toHex())
172-
.toEqual(bytesToHex(nobleHashUint.create().update(b1).update(b2).digest()))
173-
// expect(new SHA256().update(b1Bytes).update(b2Bytes).update(b3Bytes).digest().toHex()).toStrictEqual(digest768.toHex());
171+
expect(new SHA256().update(b1Bytes).update(b2Bytes).update(b3Bytes).digest().toHex())
172+
.toEqual(digest768.toHex());
174173
}
175174
}
176175
});
177-
178-
// ===============================================================
179-
let BUF_768 = new Uint8Array(256 * 3);
180-
// Fill with random data
181-
for (let i = 0; i < (256 * 3) / 32; i++)
182-
BUF_768.set(crypto.createHash('sha256').update(new Uint8Array(i)).digest(), i * 32);
183-
// ===============================================================
184-
185-
test.skip(`Partial: sha256`, () => {
186-
const fnH = nobleHashUint(BUF_768);
187-
for (let i = 0; i < 256; i++) {
188-
let b1 = BUF_768.subarray(0, i);
189-
for (let j = 0; j < 256; j++) {
190-
let b2 = BUF_768.subarray(i, i + j);
191-
let b3 = BUF_768.subarray(i + j);
192-
expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
193-
expect(nobleHashUint.create().update(b1).update(b2).update(b3).digest()).toStrictEqual(fnH);
194-
}
195-
}
196-
});
197-
198-
// Same as before, but creates copy of each slice, which changes dataoffset of typed array
199-
// Catched bug in blake2
200-
test.skip(`Partial (copy): sha256 partial`, () => {
201-
const fnH = nobleHashUint(BUF_768);
202-
for (let i = 0; i < 256; i++) {
203-
let b1 = BUF_768.subarray(0, i).slice();
204-
for (let j = 0; j < 256; j++) {
205-
let b2 = BUF_768.subarray(i, i + j).slice();
206-
let b3 = BUF_768.subarray(i + j).slice();
207-
expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
208-
expect(nobleHashUint.create().update(b1).update(b2).update(b3).digest()).toStrictEqual(fnH);
209-
}
210-
}
211-
});
212176
});

0 commit comments

Comments
 (0)