Skip to content

Commit

Permalink
Merge pull request #1346 from o1-labs/fix/hash-example
Browse files Browse the repository at this point in the history
Fix hash example
  • Loading branch information
mitschabaude committed Dec 23, 2023
2 parents 235c80e + 8ed2556 commit 015b0bf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bindings
11 changes: 6 additions & 5 deletions src/examples/zkapps/hashing/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Field>();
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/examples/zkapps/hashing/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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....');
Expand Down
5 changes: 3 additions & 2 deletions src/lib/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 015b0bf

Please sign in to comment.