UNPKG

l1-lottery-contracts

Version:

This repo contains smart contracts and related scripts for ZkNoid L1 Lottery proposed [here](https://forums.minaprotocol.com/t/zknoid-l1-lottery/6269)

2 lines (1 loc) 5.12 kB
import{Field as c,MerkleMap as D,Poseidon as M,PublicKey as L,SmartContract as W,State as h,Struct as G,UInt32 as j,ZkProgram as H,method as y,state as g}from"o1js";import{PublicKey as v,UInt64 as z}from"o1js";var Q=z.from(10*10**9),q=480;var Y=v.fromBase58("B62qnBkcyABfjz2cqJPzNZKjVt9M9kx1vgoiWLbkJUnk16Cz8KX8qC4"),I=v.fromBase58("B62qnmsn4Bm4MzPujKeN1faxedz4p1cCAwA9mKAWzDjfb4c1ysVvWeK");import{ZkonZkProgram as J,ZkonRequestCoordinator as S,ExternalRequestEvent as F}from"zkon-zkapp";import{Field as k,Provable as P,provable as T,Struct as _,UInt32 as m,UInt64 as rt}from"o1js";import{StringCircuitValue as x}from"zkon-zkapp";var E="bafkreihvrf5ftl7vslg6frpdtqqjiju3q3sllojlcasldpklqt5b6wfrlm";var N=254n,U=7,B=32n;function Z(o,t,n){if(n*BigInt(t)>N)throw new Error("The Packing Plant is only accepting orders that can fit into one Field, try using MultiPackingPlant");class s extends _({packed:k}){constructor(e){super({packed:e})}static extractField(e){throw new Error("Must implement extractField")}static sizeInBits(){throw new Error("Must implement sizeInBits")}static unpack(e){throw new Error("Must implement unpack")}static checkPack(e){if(e.length>t)throw new Error(`Input of size ${e.length} is larger than expected size of ${t}`)}static pack(e){this.checkPack(e);let i=this.extractField(e[0]),l=Math.min(e.length,t);for(let d=1;d<l;d++){let V=k((2n**this.sizeInBits())**BigInt(d));i=i.add(this.extractField(e[d]).mul(V))}return i}static unpackToBigints(e){let i=new Array(t);i.fill(0n);let l;if(e)l=e.toBigInt();else throw new Error("No Packed Value Provided");for(let d=0;d<t;d++)i[d]=l&(1n<<this.sizeInBits())-1n,l>>=this.sizeInBits();return i}toFields(){return[this.packed]}assertEquals(e){this.packed.assertEquals(e.packed)}}return s.type=T({packed:k},{}),s.l=t,s.bitSize=n,s}function K(o=U){class t extends Z(m,o,B){static extractField(s){return s.value}static sizeInBits(){return B}static unpack(s){let r=P.witness(P.Array(m,o),()=>this.unpackToBigints(s).map(i=>m.from(i)));return s.assertEquals(t.pack(r)),r}static fromUInt32s(s){let r=t.pack(s);return new t(r)}static fromBigInts(s){let r=s.map(e=>m.from(e));return t.fromUInt32s(r)}toBigInts(){return t.unpack(this.packed).map(s=>s.toBigint())}}return t}var w=class extends K(){};var C=()=>{function o(t){let n=t.slice(0,30),s=t.slice(30),r=new x(n).toField(),e=new x(s).toField();return{hashPart1:r,hashPart2:e}}return o(E.toString())};var p=function(o,t,n,s){var r=arguments.length,e=r<3?t:s===null?s=Object.getOwnPropertyDescriptor(t,n):s,i;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")e=Reflect.decorate(o,t,n,s);else for(var l=o.length-1;l>=0;l--)(i=o[l])&&(e=(r<3?i(e):r>3?i(t,n,e):i(t,n))||e);return r>3&&e&&Object.defineProperty(t,n,e),e},a=function(o,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(o,t)},dt=new D().getRoot(),X=H.Proof(J),R=class extends X{},f=class extends G({value:c,salt:c}){hash(){return M.hash([this.value,this.salt])}},{hashPart1:A,hashPart2:b}=C(),O=I,pt=L.fromBase58("B62qjGsPY47SMkTykivPBAU3riS9gvMMrGr7ve6ynoHJNBzAhQmtoBn"),u=class extends W{constructor(){super(...arguments),this.startSlot=h(),this.commit=h(),this.result=h(),this.curRandomValue=h(),this.events={requested:F}}async commitValue(t){this.permissionCheck(),this.checkRoundPass(),this.commit.getAndRequireEquals().assertEquals(c(0),"Already committed"),this.commit.set(t.hash())}async reveal(t){this.permissionCheck(),this.result.getAndRequireEquals().assertEquals(c(0),"reveal: Result already computed");let s=this.curRandomValue.getAndRequireEquals();this.commit.getAndRequireEquals().assertEquals(t.hash(),"reveal: wrong commit value"),this.checkRoundPass();let e=M.hash([t.value,s]);this.result.set(e)}async callZkon(){this.curRandomValue.getAndRequireEquals().assertEquals(c(0),"random value have already been computed");let s=await new S(O).sendRequest(this.address,A,b),r=new F({id:s,hash1:A,hash2:b});this.emitEvent("requested",r)}async receiveZkonResponse(t,n){this.curRandomValue.getAndRequireEquals().assertEquals(c(0),"receiveZkonResponse: prev random value was not consumed. Call reveal first"),await new S(O).recordRequestFullfillment(t,n),this.curRandomValue.set(n.publicInput.dataField)}permissionCheck(){}checkRoundPass(){let t=this.startSlot.getAndRequireEquals();this.network.globalSlotSinceGenesis.requireBetween(t.add(q),j.MAXINT())}};p([g(j),a("design:type",Object)],u.prototype,"startSlot",void 0);p([g(c),a("design:type",Object)],u.prototype,"commit",void 0);p([g(c),a("design:type",Object)],u.prototype,"result",void 0);p([g(c),a("design:type",Object)],u.prototype,"curRandomValue",void 0);p([y,a("design:type",Function),a("design:paramtypes",[f]),a("design:returntype",Promise)],u.prototype,"commitValue",null);p([y,a("design:type",Function),a("design:paramtypes",[f]),a("design:returntype",Promise)],u.prototype,"reveal",null);p([y,a("design:type",Function),a("design:paramtypes",[c,R]),a("design:returntype",Promise)],u.prototype,"receiveZkonResponse",null);var mt=u;export{f as CommitValue,u as RandomManager,R as ZkonProof,X as ZkonProof_,mt as default};