Skip to content

Commit

Permalink
Adapt sliding windows test -3/256 and verify the test functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed Feb 1, 2024
1 parent 1cd46bd commit 30455ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 92 deletions.
46 changes: 1 addition & 45 deletions src/sha256-class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bytes, UInt32, UInt8 } from 'o1js';
import { Bytes, Field, UInt32, UInt8 } from 'o1js';
import { H as initialHashWords, K } from './constants.js';
import {
ch,
Expand Down Expand Up @@ -267,50 +267,6 @@ class SHA256 {
}
}

import { Provable, Field } from 'o1js';
import { sha256 as nobleSha256 } from '@noble/hashes/sha256';
import { bytesToHex, concatBytes } from '@noble/hashes/utils';
import { Timer } from './test-utils.js';

let input = Bytes.fromString('abc');
let digest = new SHA256().update(input).digest();
Provable.log('digest from class: ', digest.toHex());

let input1 = new Uint8Array([1, 2]);
let input2 = new Uint8Array([3, 4]);
let input12 = concatBytes(input1, input2);

let nobleChain = nobleSha256(input12);
let nobleConcat = nobleSha256.create().update(input1).update(input2).digest();
Provable.log('\nnobleChain: ', bytesToHex(nobleChain));
Provable.log('nobleWhole: ', bytesToHex(nobleConcat));

let shaChain = new SHA256().update(Bytes.from(input12)).digest().toHex();
let shaConcat = new SHA256()
.update(Bytes.from(input1))
.update(Bytes.from(input2))
.digest()
.toHex();
Provable.log('\nshaChain: ', shaChain);
Provable.log('shaWholet: ', shaConcat);

// measure run time
const timer = new Timer();
SHA256.hash(Bytes.fromString('abc')).toHex();
timer.end();
console.log('timer: ', timer.executionTime);

// prove that the class hash function is provable
class Bytes32 extends Bytes(32) {}
console.time('sha256 witness');
Provable.runAndCheck(() => {
let input = Provable.witness(Bytes32.provable, () => Bytes32.random());
SHA256.hash(input);
});
console.timeEnd('sha256 witness');

//TODO Refactor and refine code
//TODO Adapt and verify sliding window test
//TODO Update code documentation
//TODO Omit unnecessary files
//TODO? point to the fact that the o1js used custom sigma functions
Expand Down
58 changes: 11 additions & 47 deletions src/sha256.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { o1jsHash, nodeHash, generateRandomInput } from './test-utils';
import { bytesToHex, concatBytes } from '@noble/hashes/utils';
import { sha256 as nobleHashUint } from '@noble/hashes/sha256';
import { sha256O1js, SHA256 } from './sha256';
import { sha256O1js } from './sha256';
import { SHA256 } from './sha256-class';
import * as crypto from 'crypto';
import { Bytes } from 'o1js';

Expand Down Expand Up @@ -130,7 +131,7 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
}
});

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

// ===============================================================

// 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
// It has the same concept of the sliding window test but on sequential update of destruced message blocks.
//TODO: This test requires update method for the hash function.
test.only('should pass sliding window tests - 3/256', () => {
/*
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
It has the same concept of the sliding window test 4096 but on sequential update of destruced message blocks.
! This test takes around 1.5 hours to finish
*/
test.skip('should pass sliding window tests - 3/256', () => {
let BUF_768 = new Uint8Array(256 * 3);

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

expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
// expect(nobleHashUint.create().update(b1).update(b2).update(b3).digest()).toStrictEqual(fnH);
expect(new SHA256().update(b1Bytes).update(b2Bytes).digest().toHex())
.toEqual(bytesToHex(nobleHashUint.create().update(b1).update(b2).digest()))
// expect(new SHA256().update(b1Bytes).update(b2Bytes).update(b3Bytes).digest().toHex()).toStrictEqual(digest768.toHex());
expect(new SHA256().update(b1Bytes).update(b2Bytes).update(b3Bytes).digest().toHex())
.toEqual(digest768.toHex());
}
}
});

// ===============================================================
let BUF_768 = new Uint8Array(256 * 3);
// Fill with random data
for (let i = 0; i < (256 * 3) / 32; i++)
BUF_768.set(crypto.createHash('sha256').update(new Uint8Array(i)).digest(), i * 32);
// ===============================================================

test.skip(`Partial: sha256`, () => {
const fnH = nobleHashUint(BUF_768);
for (let i = 0; i < 256; i++) {
let b1 = BUF_768.subarray(0, i);
for (let j = 0; j < 256; j++) {
let b2 = BUF_768.subarray(i, i + j);
let b3 = BUF_768.subarray(i + j);
expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
expect(nobleHashUint.create().update(b1).update(b2).update(b3).digest()).toStrictEqual(fnH);
}
}
});

// Same as before, but creates copy of each slice, which changes dataoffset of typed array
// Catched bug in blake2
test.skip(`Partial (copy): sha256 partial`, () => {
const fnH = nobleHashUint(BUF_768);
for (let i = 0; i < 256; i++) {
let b1 = BUF_768.subarray(0, i).slice();
for (let j = 0; j < 256; j++) {
let b2 = BUF_768.subarray(i, i + j).slice();
let b3 = BUF_768.subarray(i + j).slice();
expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
expect(nobleHashUint.create().update(b1).update(b2).update(b3).digest()).toStrictEqual(fnH);
}
}
});
});

0 comments on commit 30455ac

Please sign in to comment.