Created
July 24, 2022 10:54
-
-
Save liamzebedee/53f2eba5b6a7ae4556da94b38f6b2de5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { SelfProof, Field, ZkProgram, verify, Poseidon, SmartContract, Scalar, arrayProp } from 'snarkyjs'; | |
| import { | |
| CircuitValue, | |
| prop, | |
| Signature, | |
| PrivateKey, | |
| PublicKey, | |
| } from 'snarkyjs'; | |
| // | |
| // Helpers. | |
| // | |
| class Wallet { | |
| constructor( | |
| public pubKey: PublicKey, | |
| public privKey: PrivateKey | |
| ) { | |
| } | |
| static createRandom() { | |
| let privKey = PrivateKey.random(); | |
| let pubKey = PublicKey.fromPrivateKey(privKey); | |
| return new Wallet(pubKey, privKey) | |
| } | |
| } | |
| // This seems unnatural [1], but is actually a very natural consequence of how circuit definition works. | |
| // See: https://github.com/o1-labs/snarkyjs/issues/224 | |
| // [1]: Contrasted with - return parseInt(field.toString()) | |
| function fieldToNumber(field: Field): number { | |
| let i = 0 | |
| while (!field.equals(i)) { | |
| i++ | |
| } | |
| return i | |
| } | |
| // | |
| // Circuit data types. | |
| // | |
| class Coin extends CircuitValue { | |
| @prop amount: Field | |
| @prop owner: PublicKey | |
| constructor(amount: Field, owner: PublicKey) { | |
| super(); | |
| this.amount = amount; | |
| this.owner = owner; | |
| } | |
| static from(obj: any): Coin { | |
| return new Coin(obj.amount, obj.owner) | |
| } | |
| } | |
| class RawTransaction extends CircuitValue { | |
| @prop inputIdx: Field; | |
| @arrayProp(Coin, 5) outputs: Coin[]; | |
| constructor(input: Field, outputs: Coin[]) { | |
| super(); | |
| this.inputIdx = input; | |
| this.outputs = outputs; | |
| } | |
| sighash() { | |
| return Poseidon.hash(this.toFields()) | |
| } | |
| } | |
| class Transaction extends CircuitValue { | |
| @prop inputIdx: Field; | |
| @prop sig: Signature; | |
| @arrayProp(Coin, 5) outputs: Coin[]; | |
| constructor(input: Field, sig: Signature, outputs: Coin[]) { | |
| super(); | |
| this.inputIdx = input; | |
| this.sig = sig; | |
| this.outputs = outputs; | |
| } | |
| sighash(): Field { | |
| return (new RawTransaction(this.inputIdx, this.outputs)).sighash() | |
| } | |
| sign(privKey: PrivateKey) { | |
| let msg = [this.sighash()] | |
| let sig = Signature.create(privKey, msg); | |
| this.sig = sig | |
| } | |
| // sigHash() { | |
| // let data = [ | |
| // this.inputIdx, | |
| // ...this.outputs.flatMap(output => ([ output.amount, ...output.owner.toFields() ])) | |
| // ] | |
| // return Poseidon.hash(data) | |
| // } | |
| } | |
| // | |
| // The ZK program. | |
| // | |
| let Minicoin = ZkProgram({ | |
| publicInput: Transaction, | |
| methods: { | |
| genesis: { | |
| privateInputs: [], | |
| method(tx: Transaction) { | |
| // Genesis. | |
| // (new Field(tx.outputs.length)).assertEquals(1) | |
| tx.outputs[0].amount.assertEquals(new Field(1000)) | |
| } | |
| }, | |
| transfer: { | |
| privateInputs: [SelfProof], | |
| method(t1: Transaction, t0: SelfProof<Transaction>) { | |
| // Verify the sequence is correct. | |
| // Verify the state at t=0 was computed correctly. | |
| t0.verify(); | |
| // Verify the state at t=1. | |
| // This is the core of the state machine. | |
| // Perform transfer of the coin. | |
| // Verify input coin. | |
| // A1: select an output coin of the previous tx. | |
| const prevNumOutputs = t0.publicInput.outputs.length | |
| t1.inputIdx.assertLt(prevNumOutputs) | |
| const uxto = t0.publicInput.outputs[fieldToNumber(t1.inputIdx)] | |
| // A2: the transfer is from the owner of this uxto. | |
| const hash = t1.sighash() | |
| const msg = [hash]; | |
| t1.sig.verify(uxto.owner, msg) | |
| // Verify output coins. | |
| // A3: input == sum(outputs) | |
| let sumOutputs = Field.zero | |
| for (const output of t1.outputs) { | |
| sumOutputs.add(output.amount) | |
| } | |
| uxto.amount.assertEquals(sumOutputs) | |
| }, | |
| }, | |
| }, | |
| }); | |
| // | |
| // Run. | |
| // | |
| let account1 = Wallet.createRandom() | |
| let account2 = Wallet.createRandom() | |
| const SIGNATURE_NULL = Signature.create(account1.privKey, [new Field(1)]) | |
| // const SIGNATURE_NULL = new Signature(Field.zero, new Scalar()) | |
| console.log('compiling Minicoin...'); | |
| const { verificationKey } = await Minicoin.compile(); | |
| let genesisTx = new Transaction( | |
| Field.zero, | |
| SIGNATURE_NULL, | |
| [ | |
| Coin.from({ | |
| amount: new Field(1000), | |
| owner: account1.pubKey | |
| }) | |
| ] | |
| ) | |
| console.log(genesisTx.outputs.length) | |
| console.log('Genesis...'); | |
| let genesis = await Minicoin.genesis(genesisTx) | |
| process.exit(-1) | |
| console.log('transfer #1...'); | |
| let input = genesis | |
| let inputIdx = Field.zero | |
| let outputs = [ | |
| { | |
| amount: new Field(500), | |
| owner: account1.pubKey, | |
| }, | |
| { | |
| amount: new Field(500), | |
| owner: account2.pubKey, | |
| } | |
| ].map(Coin.from) | |
| process.exit(0) | |
| let tx = new Transaction(inputIdx, SIGNATURE_NULL, outputs) | |
| tx.sign(account1.privKey) | |
| let proof = await Minicoin.transfer(tx, genesis); | |
| proof.verify() | |
liamzebedee
commented
Jul 24, 2022
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment