-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #999 from o1-labs/feat/add-sha
Add new hashing functions (SHA & Keccak)
- Loading branch information
Showing
26 changed files
with
2,068 additions
and
186 deletions.
There are no files selected for viewing
Submodule bindings
updated
from c09afc to 794659
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
Hash, | ||
Field, | ||
SmartContract, | ||
state, | ||
State, | ||
method, | ||
Permissions, | ||
Bytes, | ||
} from 'o1js'; | ||
|
||
let initialCommitment: Field = Field(0); | ||
|
||
export class HashStorage extends SmartContract { | ||
@state(Field) commitment = State<Field>(); | ||
|
||
init() { | ||
super.init(); | ||
this.account.permissions.set({ | ||
...Permissions.default(), | ||
editState: Permissions.proofOrSignature(), | ||
}); | ||
this.commitment.set(initialCommitment); | ||
} | ||
|
||
@method SHA256(xs: Bytes) { | ||
const shaHash = Hash.SHA3_256.hash(xs); | ||
const commitment = Hash.hash(shaHash.toFields()); | ||
this.commitment.set(commitment); | ||
} | ||
|
||
@method SHA384(xs: Bytes) { | ||
const shaHash = Hash.SHA3_384.hash(xs); | ||
const commitment = Hash.hash(shaHash.toFields()); | ||
this.commitment.set(commitment); | ||
} | ||
|
||
@method SHA512(xs: Bytes) { | ||
const shaHash = Hash.SHA3_512.hash(xs); | ||
const commitment = Hash.hash(shaHash.toFields()); | ||
this.commitment.set(commitment); | ||
} | ||
|
||
@method Keccak256(xs: Bytes) { | ||
const shaHash = Hash.Keccak256.hash(xs); | ||
const commitment = Hash.hash(shaHash.toFields()); | ||
this.commitment.set(commitment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { HashStorage } from './hash.js'; | ||
import { Mina, PrivateKey, AccountUpdate, Bytes } from 'o1js'; | ||
|
||
let txn; | ||
let proofsEnabled = true; | ||
// setup local ledger | ||
let Local = Mina.LocalBlockchain({ proofsEnabled }); | ||
Mina.setActiveInstance(Local); | ||
|
||
if (proofsEnabled) { | ||
console.log('Proofs enabled'); | ||
HashStorage.compile(); | ||
} | ||
|
||
// test accounts that pays all the fees, and puts additional funds into the zkapp | ||
const feePayer = Local.testAccounts[0]; | ||
|
||
// zkapp account | ||
const zkAppPrivateKey = PrivateKey.random(); | ||
const zkAppAddress = zkAppPrivateKey.toPublicKey(); | ||
const zkAppInstance = new HashStorage(zkAppAddress); | ||
|
||
// 0, 1, 2, 3, ..., 32 | ||
const hashData = Bytes.from(Array.from({ length: 32 }, (_, i) => i)); | ||
|
||
console.log('Deploying Hash Example....'); | ||
txn = await Mina.transaction(feePayer.publicKey, () => { | ||
AccountUpdate.fundNewAccount(feePayer.publicKey); | ||
zkAppInstance.deploy(); | ||
}); | ||
await txn.sign([feePayer.privateKey, zkAppPrivateKey]).send(); | ||
|
||
const initialState = | ||
Mina.getAccount(zkAppAddress).zkapp?.appState?.[0].toString(); | ||
|
||
let currentState; | ||
console.log('Initial State', initialState); | ||
|
||
console.log(`Updating commitment from ${initialState} using SHA256 ...`); | ||
txn = await Mina.transaction(feePayer.publicKey, () => { | ||
zkAppInstance.SHA256(hashData); | ||
}); | ||
await txn.prove(); | ||
await txn.sign([feePayer.privateKey]).send(); | ||
currentState = Mina.getAccount(zkAppAddress).zkapp?.appState?.[0].toString(); | ||
console.log(`Current state successfully updated to ${currentState}`); | ||
|
||
console.log(`Updating commitment from ${initialState} using SHA384 ...`); | ||
txn = await Mina.transaction(feePayer.publicKey, () => { | ||
zkAppInstance.SHA384(hashData); | ||
}); | ||
await txn.prove(); | ||
await txn.sign([feePayer.privateKey]).send(); | ||
currentState = Mina.getAccount(zkAppAddress).zkapp?.appState?.[0].toString(); | ||
console.log(`Current state successfully updated to ${currentState}`); | ||
|
||
console.log(`Updating commitment from ${initialState} using SHA512 ...`); | ||
txn = await Mina.transaction(feePayer.publicKey, () => { | ||
zkAppInstance.SHA512(hashData); | ||
}); | ||
await txn.prove(); | ||
await txn.sign([feePayer.privateKey]).send(); | ||
currentState = Mina.getAccount(zkAppAddress).zkapp?.appState?.[0].toString(); | ||
console.log(`Current state successfully updated to ${currentState}`); | ||
|
||
console.log(`Updating commitment from ${initialState} using Keccak256...`); | ||
txn = await Mina.transaction(feePayer.publicKey, () => { | ||
zkAppInstance.Keccak256(hashData); | ||
}); | ||
await txn.prove(); | ||
await txn.sign([feePayer.privateKey]).send(); | ||
currentState = Mina.getAccount(zkAppAddress).zkapp?.appState?.[0].toString(); | ||
console.log(`Current state successfully updated to ${currentState}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.