Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hash example #1346

Merged
merged 2 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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