From f164d7a7e81a4dc3de2e3eccc43a2d0d06696dfd Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 1 Feb 2024 18:36:48 +0100 Subject: [PATCH] Delete zkcontract.test.ts file & Polish main.ts file --- src/binary-utils.ts | 8 +++++--- src/index.ts | 1 + src/main.ts | 21 +++++++++++---------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/binary-utils.ts b/src/binary-utils.ts index 2b03713..ea49f4d 100644 --- a/src/binary-utils.ts +++ b/src/binary-utils.ts @@ -12,7 +12,8 @@ export { }; /** - * This type refers to a string that contains a sequence of '0's and '1' which is the binary representation of a number or an UTF-8 encoded native string ty + * This type refers to a string that contains a sequence of '0's and '1's + * which is the binary representation of a number or an UTF-8 encoded native string ty */ type BinaryString = string; @@ -32,7 +33,7 @@ function uint32ToBinary(input: UInt32): BinaryString { } /** - * Convert a BinaryString into Hexadecimal + * Convert a BinaryString into Hexadecimal string */ function binaryToHex(x: BinaryString): string { let result = ''; @@ -44,7 +45,8 @@ function binaryToHex(x: BinaryString): string { } /** - * Convert a BinaryString into an array of o1js Bool type: [Bool(false), Bool(true), Bool(false), Bool(true), Bool(true), Bool(false)] + * Convert a BinaryString into an array of o1js Bool type + * Example: '010110' --> [Bool(false), Bool(true), Bool(false), Bool(true), Bool(true), Bool(false)] */ function binaryStringToBoolArray(binaryString: BinaryString): Bool[] { const boolArray: Bool[] = []; diff --git a/src/index.ts b/src/index.ts index 71b7c96..4a0cb33 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { Poseidon, } from 'o1js'; import { sha256O1js } from './sha256.js'; + class Bytes3 extends Bytes(3) {} // the sha256 digest is poseidon hashed to output a single field that will serve as a publicInput diff --git a/src/main.ts b/src/main.ts index a41f2f3..66b550b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,13 +12,11 @@ const { privateKey: deployerKey, publicKey: deployerAccount } = const { privateKey: senderKey, publicKey: senderAccount } = Local.testAccounts[1]; -// ---------------------------------------------------- - // Create a public/private key pair. The public key is your address and where you deploy the zkApp to const zkAppPrivateKey = PrivateKey.random(); const zkAppAddress = zkAppPrivateKey.toPublicKey(); -// create an instance of Square - and deploy it to zkAppAddress +// create an instance of Sha256ZkApp - and deploy it to zkAppAddress if (useProof) await Sha256ZkApp.compile(); const zkAppInstance = new Sha256ZkApp(zkAppAddress); @@ -29,19 +27,22 @@ const deployTxn = await Mina.transaction(deployerAccount, () => { }); await deployTxn.sign([deployerKey, zkAppPrivateKey]).send(); -// get the initial state of Square after deployment +// fetch the public input of Sha256ZkApp after deployment const digestInit = zkAppInstance.publicInput.get(); +console.log('expected digest:', digestInit.toBigInt()); -console.log('expected digest:', digestInit.toString()); - -// ---------------------------------------------------- - +/* +- hash the preimage on-chain +- if it is not compliant with the public input the TX will revert! + */ const txn1 = await Mina.transaction(senderAccount, () => { const x = Bytes3.fromString('abc'); zkAppInstance.hash(x); }); + await txn1.prove(); await txn1.sign([senderKey]).send(); -console.log('txn1: ', txn1.transaction); -console.log('Finished compiling'); +// console.log('txn1: ', txn1.toPretty()); + +console.log('\nFinished compiling');