Skip to content

Commit

Permalink
fix hash example
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Dec 21, 2023
1 parent 235c80e commit 82e0fcc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
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 82e0fcc

Please sign in to comment.