Skip to content

Commit

Permalink
Format src directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed Jan 8, 2024
1 parent aefd87e commit fa2510c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 61 deletions.
24 changes: 20 additions & 4 deletions src/bitwise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ describe('Bitwise Operation Tests', () => {

describe('σ0: small sigma0 SHA256 bitwise function tests', () => {
const σ0 = (x: number): bigint => {
return rotateRightNative(x, 7) ^ rotateRightNative(x, 18) ^ shiftRightNative(x, 3);
return (
rotateRightNative(x, 7) ^
rotateRightNative(x, 18) ^
shiftRightNative(x, 3)
);
};

const testσ0 = (input: bigint) => {
Expand All @@ -253,7 +257,11 @@ describe('Bitwise Operation Tests', () => {

describe('σ1: small sigma1 SHA256 bitwise function tests', () => {
const σ1 = (x: number): bigint => {
return rotateRightNative(x, 17) ^ rotateRightNative(x, 19) ^ shiftRightNative(x, 10);
return (
rotateRightNative(x, 17) ^
rotateRightNative(x, 19) ^
shiftRightNative(x, 10)
);
};

const testσ1 = (input: bigint) => {
Expand All @@ -278,7 +286,11 @@ describe('Bitwise Operation Tests', () => {

describe('Σ0: big SIGMA0 SHA256 bitwise function tests', () => {
const Σ0 = (x: number): bigint => {
return rotateRightNative(x, 2) ^ rotateRightNative(x, 13) ^ rotateRightNative(x, 22);
return (
rotateRightNative(x, 2) ^
rotateRightNative(x, 13) ^
rotateRightNative(x, 22)
);
};

const testΣ0 = (input: bigint) => {
Expand All @@ -303,7 +315,11 @@ describe('Bitwise Operation Tests', () => {

describe('Σ1: big SIGMA1 SHA256 bitwise function tests', () => {
const Σ1 = (x: number): bigint => {
return rotateRightNative(x, 6) ^ rotateRightNative(x, 11) ^ rotateRightNative(x, 25);
return (
rotateRightNative(x, 6) ^
rotateRightNative(x, 11) ^
rotateRightNative(x, 25)
);
};

const testΣ1 = (input: bigint) => {
Expand Down
28 changes: 11 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { Field, SmartContract, state, State, method } from 'o1js';
import { sha256 } from './sha256.js';

const o1jsDigest = [
124246274,
3652808864,
113200783,
2811496515,
169711656,
1835052556,
198289410,
3169118214
const o1jsDigest = [
124246274, 3652808864, 113200783, 2811496515, 169711656, 1835052556,
198289410, 3169118214,
].map(Field);

export class Sha256ZkApp extends SmartContract {
Expand All @@ -26,14 +20,14 @@ export class Sha256ZkApp extends SmartContract {
//TODO: change digest into two 128-bit field elements
super.init();
// initial state
this.h1.set(o1jsDigest[0]);
this.h2.set(o1jsDigest[1]);
this.h3.set(o1jsDigest[2]);
this.h4.set(o1jsDigest[3]);
this.h5.set(o1jsDigest[4]);
this.h6.set(o1jsDigest[5]);
this.h7.set(o1jsDigest[6]);
this.h8.set(o1jsDigest[7]);
this.h1.set(o1jsDigest[0]);
this.h2.set(o1jsDigest[1]);
this.h3.set(o1jsDigest[2]);
this.h4.set(o1jsDigest[3]);
this.h5.set(o1jsDigest[4]);
this.h6.set(o1jsDigest[5]);
this.h7.set(o1jsDigest[6]);
this.h8.set(o1jsDigest[7]);
}

@method hash(x: Field) {
Expand Down
22 changes: 9 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Sha256ZkApp } from './index.js';
import {
Field,
Mina,
PrivateKey,
AccountUpdate,
} from 'o1js';
import { Field, Mina, PrivateKey, AccountUpdate } from 'o1js';

const useProof = true;
const useProof = false;

const Local = Mina.LocalBlockchain({ proofsEnabled: useProof });
Mina.setActiveInstance(Local);
const { privateKey: deployerKey, publicKey: deployerAccount } = Local.testAccounts[0];
const { privateKey: senderKey, publicKey: senderAccount } = Local.testAccounts[1];
const { privateKey: deployerKey, publicKey: deployerAccount } =
Local.testAccounts[0];
const { privateKey: senderKey, publicKey: senderAccount } =
Local.testAccounts[1];

// ----------------------------------------------------

Expand All @@ -22,6 +19,7 @@ const zkAppAddress = zkAppPrivateKey.toPublicKey();
// create an instance of Square - and deploy it to zkAppAddress
if (useProof) await Sha256ZkApp.compile();
const zkAppInstance = new Sha256ZkApp(zkAppAddress);

const deployTxn = await Mina.transaction(deployerAccount, () => {
AccountUpdate.fundNewAccount(deployerAccount);
zkAppInstance.deploy();
Expand All @@ -42,14 +40,12 @@ console.log('Part 7/8 of expected digest:', zkAppInstance.h7.get().toString());
console.log('Part 8/8 of expected digest:', zkAppInstance.h8.get().toString());
// ----------------------------------------------------


const txn1 = await Mina.transaction(senderAccount, () => {
const x = Field(123456789);
const x = Field(123456789);
zkAppInstance.hash(x);
});
await txn1.prove();
await txn1.sign([senderKey]).send();
console.log('txn1: ', txn1.transaction);


console.log('Finished compiling')
console.log('Finished compiling');
8 changes: 4 additions & 4 deletions src/preprocessing.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
padInput,
parseBinaryTo512BitBlocks,
parseSha2Input,
import {
padInput,
parseBinaryTo512BitBlocks,
parseSha2Input,
parse512BitBlock,
} from './preprocessing';
import { H } from './constants';
Expand Down
32 changes: 14 additions & 18 deletions src/preprocessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import { addMod32, sigma0, sigma1 } from './functions.js';
*
* @param {Field[]} input - The input parsed into an array of 8-bit field elements.
* @returns {Bool[]} The padded input bits according to SHA-256.
*
*
*/
function padInput(input: Field[]): Bool[] {
// Reverse parsing the input from Field[] into binary=Bool[]
let inputBinary = input.map(f => f.toBits(8)).flat();
let inputBinary = input.map((f) => f.toBits(8)).flat();

const blockSize = 512;
const initialLength = inputBinary.length;
const bitLength = initialLength % blockSize;
const paddingLength = (bitLength < 448) ? (448 - bitLength) : (blockSize + 448 - bitLength);
const paddingLength =
bitLength < 448 ? 448 - bitLength : blockSize + 448 - bitLength;

// Append a single '1' bit
inputBinary.push(Bool(true));
Expand All @@ -30,7 +31,7 @@ function padInput(input: Field[]): Bool[] {
// Append the 64-bit representation of the initial length
const inputBitLengthBinary = toBoolArray(initialLength);
inputBinary.push(...inputBitLengthBinary);

return inputBinary;
}

Expand Down Expand Up @@ -76,7 +77,7 @@ function splitArrayIntoBlocks(inputArray: Bool[]): Field[] {
*/
function parseSha2Input(input: string | Field): Field[] {
let inputBinary: Bool[];

if (typeof input === 'string') inputBinary = toBoolArray(input);
else inputBinary = input.toBits();

Expand Down Expand Up @@ -141,23 +142,18 @@ function parse512BitBlock(bits512Block: Bool[]): Field[] {
*/
function prepareMessageSchedule(bits32Words: Field[]): Field[] {
const W = [...bits32Words];

for (let t = 16; t <= 63; t++) {
W[t] = addMod32(
sigma1(W[t - 2]),
W[t - 7],
sigma0(W[t - 15]),
W[t - 16]
);
W[t] = addMod32(sigma1(W[t - 2]), W[t - 7], sigma0(W[t - 15]), W[t - 16]);
}

return W;
}

export {
padInput,
parseBinaryTo512BitBlocks,
parse512BitBlock,
parseSha2Input,
export {
padInput,
parseBinaryTo512BitBlocks,
parse512BitBlock,
parseSha2Input,
prepareMessageSchedule,
};
10 changes: 5 additions & 5 deletions src/zkcontract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Sha256ZkApp', () => {

it('generates and deploys the `Sha256ZkApp` smart contract', async () => {
await localDeploy();

let digest: Field;
try {
digest = zkApp.h1.get();
Expand All @@ -59,19 +59,19 @@ describe('Sha256ZkApp', () => {
// digest[i].assertEquals(expectedDigest[i])
// }
} catch (error) {
console.log(error)
console.log(error);
}
});

it('asserts on the initial digest compared to the output from smart contract hash interaction', async () => {
await localDeploy();

// update transaction
const x = Field(123456789);
const txn = await Mina.transaction(senderAccount, () => {
const x = Field(123456789);
const txn = await Mina.transaction(senderAccount, () => {
zkApp.hash(x);
});
await txn.prove();
await txn.sign([senderKey]).send();
});
});
});

0 comments on commit fa2510c

Please sign in to comment.