From 82e0fcc38711fab4232d7057a2e60b47d631bd6e Mon Sep 17 00:00:00 2001 From: Gregor Date: Thu, 21 Dec 2023 15:33:30 +0100 Subject: [PATCH 1/2] fix hash example --- src/bindings | 2 +- src/examples/zkapps/hashing/hash.ts | 11 ++++++----- src/examples/zkapps/hashing/run.ts | 4 ++-- src/lib/hash.ts | 5 +++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/bindings b/src/bindings index 7fcd4cd6f5..a884dc593d 160000 --- a/src/bindings +++ b/src/bindings @@ -1 +1 @@ -Subproject commit 7fcd4cd6f5b90c7c5c50b15377777528d8b35e41 +Subproject commit a884dc593dbab69e55ab9602b998ec12dfc3a288 diff --git a/src/examples/zkapps/hashing/hash.ts b/src/examples/zkapps/hashing/hash.ts index 9ad9947dfa..e762863c85 100644 --- a/src/examples/zkapps/hashing/hash.ts +++ b/src/examples/zkapps/hashing/hash.ts @@ -9,7 +9,8 @@ import { Bytes, } from 'o1js'; -let initialCommitment: Field = Field(0); +let initialCommitment = Field(0); +class Bytes32 extends Bytes(32) {} export class HashStorage extends SmartContract { @state(Field) commitment = State(); @@ -23,25 +24,25 @@ export class HashStorage extends SmartContract { this.commitment.set(initialCommitment); } - @method SHA256(xs: Bytes) { + @method SHA256(xs: Bytes32) { const shaHash = Hash.SHA3_256.hash(xs); const commitment = Hash.hash(shaHash.toFields()); this.commitment.set(commitment); } - @method SHA384(xs: Bytes) { + @method SHA384(xs: Bytes32) { const shaHash = Hash.SHA3_384.hash(xs); const commitment = Hash.hash(shaHash.toFields()); this.commitment.set(commitment); } - @method SHA512(xs: Bytes) { + @method SHA512(xs: Bytes32) { const shaHash = Hash.SHA3_512.hash(xs); const commitment = Hash.hash(shaHash.toFields()); this.commitment.set(commitment); } - @method Keccak256(xs: Bytes) { + @method Keccak256(xs: Bytes32) { const shaHash = Hash.Keccak256.hash(xs); const commitment = Hash.hash(shaHash.toFields()); this.commitment.set(commitment); diff --git a/src/examples/zkapps/hashing/run.ts b/src/examples/zkapps/hashing/run.ts index 9350211d68..e1568fa95d 100644 --- a/src/examples/zkapps/hashing/run.ts +++ b/src/examples/zkapps/hashing/run.ts @@ -9,7 +9,7 @@ Mina.setActiveInstance(Local); if (proofsEnabled) { console.log('Proofs enabled'); - HashStorage.compile(); + await HashStorage.compile(); } // test accounts that pays all the fees, and puts additional funds into the zkapp @@ -20,7 +20,7 @@ const zkAppPrivateKey = PrivateKey.random(); const zkAppAddress = zkAppPrivateKey.toPublicKey(); const zkAppInstance = new HashStorage(zkAppAddress); -// 0, 1, 2, 3, ..., 32 +// 0, 1, 2, 3, ..., 31 const hashData = Bytes.from(Array.from({ length: 32 }, (_, i) => i)); console.log('Deploying Hash Example....'); diff --git a/src/lib/hash.ts b/src/lib/hash.ts index 8f51d97d0f..1a55da5181 100644 --- a/src/lib/hash.ts +++ b/src/lib/hash.ts @@ -7,6 +7,7 @@ import { MlFieldArray } from './ml/fields.js'; import { Poseidon as PoseidonBigint } from '../bindings/crypto/poseidon.js'; import { assert } from './errors.js'; import { rangeCheckN } from './gadgets/range-check.js'; +import { TupleN } from './util/types.js'; // external API export { Poseidon, TokenSymbol }; @@ -45,13 +46,13 @@ const Poseidon = { if (isConstant(input)) { return Field(PoseidonBigint.hash(toBigints(input))); } - return Poseidon.update(this.initialState(), input)[0]; + return Poseidon.update(Poseidon.initialState(), input)[0]; }, update(state: [Field, Field, Field], input: Field[]) { if (isConstant(state) && isConstant(input)) { let newState = PoseidonBigint.update(toBigints(state), toBigints(input)); - return newState.map(Field); + return TupleN.fromArray(3, newState.map(Field)); } let newState = Snarky.poseidon.update( From 8ed2556b7c7278dc210f1daa609c0f4427e3cd86 Mon Sep 17 00:00:00 2001 From: Gregor Date: Thu, 21 Dec 2023 15:39:16 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b30d0e852..781c521b38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased](https://github.com/o1-labs/o1js/compare/19115a159...HEAD) +### Fixed + +- Fix bug in `Hash.hash()` which always resulted in an error https://github.com/o1-labs/o1js/pull/1346 + ## [0.15.1](https://github.com/o1-labs/o1js/compare/1ad7333e9e...19115a159) ### Breaking changes