Skip to content

Commit

Permalink
Lint main src directory files
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed Feb 1, 2024
1 parent 32a8af8 commit a8bd45a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 43 deletions.
10 changes: 8 additions & 2 deletions src/bitwise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe('Bitwise Operation Tests', () => {
specific = false,
rotrExpected?: bigint
) => {
const rotrActual = rotateRight32(UInt32.from(input), rotationBits).value.toBigInt();
const rotrActual = rotateRight32(
UInt32.from(input),
rotationBits
).value.toBigInt();
if (specific) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(rotrActual).toBe(rotrExpected!);
Expand Down Expand Up @@ -113,7 +116,10 @@ describe('Bitwise Operation Tests', () => {
specific = false,
shrExpected?: bigint
) => {
const shrActual = shiftRight32(UInt32.from(input), shiftBits).value.toBigInt();
const shrActual = shiftRight32(
UInt32.from(input),
shiftBits
).value.toBigInt();
if (specific) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(shrActual).toBe(shrExpected!);
Expand Down
4 changes: 3 additions & 1 deletion src/sha256-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ class SHA256 {
}

//TODO Update code documentation
//TODO? add the released sha256 in benchmarks -> released, direct, class.

//TODO Omit unnecessary files
//TODO? point to the fact that the o1js used custom sigma functions
//TODO? add the released sha256 in benchmarks -> released, direct, class.

//TODO? Update the readme and repo description
27 changes: 18 additions & 9 deletions src/sha256.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
}
});

// !This test takes 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 @@ -150,11 +150,14 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
*/
test.skip('should pass sliding window tests - 3/256', () => {
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);

BUF_768.set(
crypto.createHash('sha256').update(new Uint8Array(i)).digest(),
i * 32
);

let BYTES_768 = Bytes.from(BUF_768);
const digest768 = sha256O1js(BYTES_768);
for (let i = 0; i < 256; i++) {
Expand All @@ -163,14 +166,20 @@ describe('Testing o1js SHA256 hash function against to node-js implementation',
for (let j = 0; j < 256; j++) {
let b2 = BUF_768.subarray(i, i + j);
let b2Bytes = Bytes.from(b2);

let b3 = BUF_768.subarray(i + j);
let b3Bytes = Bytes.from(b3);

expect(concatBytes(b1, b2, b3)).toStrictEqual(BUF_768);
expect(new SHA256().update(b1Bytes).update(b2Bytes).update(b3Bytes).digest().toHex())
.toEqual(digest768.toHex());
expect(
new SHA256()
.update(b1Bytes)
.update(b2Bytes)
.update(b3Bytes)
.digest()
.toHex()
).toEqual(digest768.toHex());
}
}
});
});
});
6 changes: 2 additions & 4 deletions src/sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import {
} from './preprocessing.js';
import { wordToBytes } from './binary-utils.js';

export {
sha256O1js,
}
export { sha256O1js };

function sha256O1js(input: Bytes): Bytes {
const H = [...initialHashWords];
Expand Down Expand Up @@ -69,4 +67,4 @@ function sha256O1js(input: Bytes): Bytes {
// the message schedule is converted to big endian bytes
// wordToBytes expects little endian, so we reverse the bytes
return Bytes.from(H.map((x) => wordToBytes(x.value, 4).reverse()).flat());
}
}
8 changes: 0 additions & 8 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,6 @@ function nodeHash(input: string | Uint8Array): string {
return crypto.createHash('sha256').update(input).digest('hex');
}

// function o1jsHashField(input: Field): string {
// const digest = sha256O1js(Bytes.from(Field.toBytes(input)));
// const digestBinary = digest.map(uint32ToBinary).join('');
// const digestHex = binaryToHex(digestBinary);

// return digestHex;
// }

function nobleHash(input: string | Uint8Array): string {
return bytesToHex(nobleSha256(input));
}
Expand Down
38 changes: 19 additions & 19 deletions src/zkprogram.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { ZkProgram, Bytes } from "o1js";
import { sha256O1js } from "./sha256.js";
import { Timer } from "./test-utils.js";

import { ZkProgram, Bytes } from 'o1js';
import { sha256O1js } from './sha256.js';
import { Timer } from './test-utils.js';

class Bytes32 extends Bytes(32) {}
class Bytes3 extends Bytes(3) {}

// Define SHA256 ZkProgram
let sha256ZkProgram = ZkProgram({
name: 'sha256',
publicOutput: Bytes32.provable,
methods: {
hash: {
privateInputs: [Bytes3.provable],
method(input: Bytes3) {
return sha256O1js(input);
}
}
}
name: 'sha256',
publicOutput: Bytes32.provable,
methods: {
hash: {
privateInputs: [Bytes3.provable],
method(input: Bytes3) {
return sha256O1js(input);
},
},
},
});

// Print SHA256 ZkProgram summary
// Print SHA256 ZkProgram summary
console.log('sha256 summary:', sha256ZkProgram.analyzeMethods().hash.summary());

// Compile SHA256 ZkProgram
Expand All @@ -40,12 +39,13 @@ const verifyTimer = new Timer('Verification Time');
const res = await sha256ZkProgram.verify(proof);
verifyTimer.end();
if (res === true) {
console.log("\nProof Verification OK!");
console.log('\nProof Verification OK!');
} else {
console.log("Invalid proof");
console.log('Invalid proof');
}

// Verify Compliance to Expected Digest
const digest = await proof.publicOutput.toHex();
const integrityCheck = digest === 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
console.log("Digest Integrity Check: ", integrityCheck);
const integrityCheck =
digest === 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
console.log('Digest Integrity Check: ', integrityCheck);

0 comments on commit a8bd45a

Please sign in to comment.