From 1a245b5de0ad16a3904dac0e2f2a44e40f9edc1f Mon Sep 17 00:00:00 2001 From: koalateectrl Date: Thu, 24 Oct 2024 17:52:50 -0700 Subject: [PATCH] working circuit --- .gitignore | 3 + README.md | 2 +- circuits/README.md | 9 + circuits/input.json | 1 + circuits/multiplier2.circom | 16 + circuits/multiplier2.r1cs | Bin 0 -> 264 bytes circuits/multiplier2_0000.zkey | Bin 0 -> 2580 bytes circuits/multiplier2_0001.zkey | Bin 0 -> 2994 bytes circuits/multiplier2_js/generate_witness.js | 20 ++ circuits/multiplier2_js/multiplier2.wasm | Bin 0 -> 34291 bytes circuits/multiplier2_js/witness_calculator.js | 337 ++++++++++++++++++ circuits/proof.json | 28 ++ circuits/public.json | 3 + circuits/verification_key.json | 94 +++++ circuits/witness.wtns | Bin 0 -> 204 bytes 15 files changed, 512 insertions(+), 1 deletion(-) create mode 100644 circuits/README.md create mode 100644 circuits/input.json create mode 100644 circuits/multiplier2.circom create mode 100644 circuits/multiplier2.r1cs create mode 100644 circuits/multiplier2_0000.zkey create mode 100644 circuits/multiplier2_0001.zkey create mode 100644 circuits/multiplier2_js/generate_witness.js create mode 100644 circuits/multiplier2_js/multiplier2.wasm create mode 100644 circuits/multiplier2_js/witness_calculator.js create mode 100644 circuits/proof.json create mode 100644 circuits/public.json create mode 100644 circuits/verification_key.json create mode 100644 circuits/witness.wtns diff --git a/.gitignore b/.gitignore index c6bba59..f8a6361 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,6 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +#Computer files +.DS_Store diff --git a/README.md b/README.md index 9265b45..a4fee37 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## Foundry +## ZK Circuit Education Module **Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.** diff --git a/circuits/README.md b/circuits/README.md new file mode 100644 index 0000000..42a9609 --- /dev/null +++ b/circuits/README.md @@ -0,0 +1,9 @@ +# Circom Usage + +Assuming you are currently in this folder, + +1. `circom multiplier2.circom --r1cs --wasm` + +2. `node multiplier2_js/generate_witness.js multiplier2_js/multiplier2.wasm input.json witness.wtns` + +3. \ No newline at end of file diff --git a/circuits/input.json b/circuits/input.json new file mode 100644 index 0000000..6674e22 --- /dev/null +++ b/circuits/input.json @@ -0,0 +1 @@ +{"a": "3", "b": "11"} \ No newline at end of file diff --git a/circuits/multiplier2.circom b/circuits/multiplier2.circom new file mode 100644 index 0000000..0588880 --- /dev/null +++ b/circuits/multiplier2.circom @@ -0,0 +1,16 @@ +pragma circom 2.1.8; + +/*This circuit template checks that c is the multiplication of a and b.*/ + +template Multiplier2 () { + + // Declaration of signals. + signal input a; + signal input b; + signal output c; + + // Constraints. + c <== a * b; +} + +component main = Multiplier2(); \ No newline at end of file diff --git a/circuits/multiplier2.r1cs b/circuits/multiplier2.r1cs new file mode 100644 index 0000000000000000000000000000000000000000..e61b9b091d3e08fab09d4639f9763e776451f221 GIT binary patch literal 264 zcmXRiOfF_*U|?VdVkRK20AdgTiGlb)@L}@Tht3lVc2;`4FxH5TXl&f(8n8oif#Jg< zzZ3(QUJ#8BfaIZS2%6^rHAn%X2d{b-h#IgCkb6OV5P-Q$0n9@XFn2=z2KF~d7696s BEn)xw literal 0 HcmV?d00001 diff --git a/circuits/multiplier2_0000.zkey b/circuits/multiplier2_0000.zkey new file mode 100644 index 0000000000000000000000000000000000000000..33afc01b904d06675a2ed433cbd46873d85df9a4 GIT binary patch literal 2580 zcmb=*POW5QU|`?^Vi3&^4-GXe1w5CH@VK6i^mt*cfvyH*KFpq^OV-g1=J^WoCUQT*T2?%+ zDt|5EE2R4PQ{1)be)H?u+FOj+!j9%o4Ojegd&2Ma@3_8Ioeb$^sMbmsdNN_ckL!+JaF*LI7O+5#Y3=LV{c%+{nW`=cSKkXY(wAAq zJb!`g;mB1F{#U!MQtNp0!pFViPMShX@Li{a%&Y3YYMMQ0Retj4J(E%3mJa>uTMf@I z#0AGHTW%YJNKm;r@O1|_-!39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!C3hM0VocKZOmobf*_ zwGX!ImlTr~q0hA)b!A?A3D(@j80gm}$hP$qDg!|&@y>_Ar#JhWC0OZk{OnP^i@pk7 zkTb}Yca zEPYsId%v6{gAf&%LFsB51)s#T6EBH!_StKz=) zOJZN|oR*?U{UyiY6#g{M-5qhv^?JYq>ZenZH}y*PhS#lH48QL@LK;vd>9gCHU+0x- z=>d4RS$@oV-DW~my=yCp{N^f50KYrs;U))gGcze&Pv-&oa}7E%&5(wPF2THoHJ5!J zzRyy`#J+e-LD1d&?p(l+Y;0v0{&*_3qMqpD=yK8!%gptlYDFI^r`cBBDJUXfdqUDK z5X%m}XVUlu_$-RMG{o#hH1=wr;aQAT75ZY-b!4-lPA$*6)NZymkp!o1f%s9oxAm67KUdP zGm3*A9vXo3j$gEARd_OoD$>iv@KNcb1Ou2xMqXwATM!-Zo45Sj9RqH8`$3Eyyqy0A D+ld!0 literal 0 HcmV?d00001 diff --git a/circuits/multiplier2_js/generate_witness.js b/circuits/multiplier2_js/generate_witness.js new file mode 100644 index 0000000..eabb86e --- /dev/null +++ b/circuits/multiplier2_js/generate_witness.js @@ -0,0 +1,20 @@ +const wc = require("./witness_calculator.js"); +const { readFileSync, writeFile } = require("fs"); + +if (process.argv.length != 5) { + console.log("Usage: node generate_witness.js "); +} else { + const input = JSON.parse(readFileSync(process.argv[3], "utf8")); + + const buffer = readFileSync(process.argv[2]); + wc(buffer).then(async witnessCalculator => { + // const w= await witnessCalculator.calculateWitness(input,0); + // for (let i=0; i< w.length; i++){ + // console.log(w[i]); + // } + const buff= await witnessCalculator.calculateWTNSBin(input,0); + writeFile(process.argv[4], buff, function(err) { + if (err) throw err; + }); + }); +} diff --git a/circuits/multiplier2_js/multiplier2.wasm b/circuits/multiplier2_js/multiplier2.wasm new file mode 100644 index 0000000000000000000000000000000000000000..b275bfc63eeb320e3b7ee06164255a1c9ecbd3a1 GIT binary patch literal 34291 zcmeHwd5j#_d1t+<>a+T&={eMMa9G_UEm9K2xk$>24%SelB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r8%G|}( z(@av@)#WoONj+b0&olLWtvxR*%gweTH`=OPUyczyuHsTpsFyJ7NZQ=yAY!-7SxdZy zb0q6|x>RvlSc*%m&R9*BD|JPluY==4Oj(|sxPUI8&X6i2RY7V^kuubc&()fI9K_<* z-4VV%DA_42zF=vsS@{uZb(A;;s0;OFHErE#93G2To7KkIW?ZdZnrkKE7xp{Y%W_h z7d$DJ3jKZEJT`*?8$|UDxnI&wLy^-}3}L3HSkj5e87I_2x{`Ndh){xB@Fdu2v7A3G z-i_~{L7qgnru{R?K7VG=Q+6fDynm*Je`$ZM`)9Po>CWj(&FW%?0whN^jsWhtv~{Pn mJP#9#=_x&Br6bsvb%R7HWZWoXu{4ts7(un`bIVCj`2PcSdk$y- literal 0 HcmV?d00001 diff --git a/circuits/multiplier2_js/witness_calculator.js b/circuits/multiplier2_js/witness_calculator.js new file mode 100644 index 0000000..20e6e20 --- /dev/null +++ b/circuits/multiplier2_js/witness_calculator.js @@ -0,0 +1,337 @@ +module.exports = async function builder(code, options) { + + options = options || {}; + + let wasmModule; + try { + wasmModule = await WebAssembly.compile(code); + } catch (err) { + console.log(err); + console.log("\nTry to run circom --c in order to generate c++ code instead\n"); + throw new Error(err); + } + + let wc; + + let errStr = ""; + let msgStr = ""; + + const instance = await WebAssembly.instantiate(wasmModule, { + runtime: { + exceptionHandler : function(code) { + let err; + if (code == 1) { + err = "Signal not found.\n"; + } else if (code == 2) { + err = "Too many signals set.\n"; + } else if (code == 3) { + err = "Signal already set.\n"; + } else if (code == 4) { + err = "Assert Failed.\n"; + } else if (code == 5) { + err = "Not enough memory.\n"; + } else if (code == 6) { + err = "Input signal array access exceeds the size.\n"; + } else { + err = "Unknown error.\n"; + } + throw new Error(err + errStr); + }, + printErrorMessage : function() { + errStr += getMessage() + "\n"; + // console.error(getMessage()); + }, + writeBufferMessage : function() { + const msg = getMessage(); + // Any calls to `log()` will always end with a `\n`, so that's when we print and reset + if (msg === "\n") { + console.log(msgStr); + msgStr = ""; + } else { + // If we've buffered other content, put a space in between the items + if (msgStr !== "") { + msgStr += " " + } + // Then append the message to the message we are creating + msgStr += msg; + } + }, + showSharedRWMemory : function() { + printSharedRWMemory (); + } + + } + }); + + const sanityCheck = + options +// options && +// ( +// options.sanityCheck || +// options.logGetSignal || +// options.logSetSignal || +// options.logStartComponent || +// options.logFinishComponent +// ); + + + wc = new WitnessCalculator(instance, sanityCheck); + return wc; + + function getMessage() { + var message = ""; + var c = instance.exports.getMessageChar(); + while ( c != 0 ) { + message += String.fromCharCode(c); + c = instance.exports.getMessageChar(); + } + return message; + } + + function printSharedRWMemory () { + const shared_rw_memory_size = instance.exports.getFieldNumLen32(); + const arr = new Uint32Array(shared_rw_memory_size); + for (let j=0; j { + const h = fnvHash(k); + const hMSB = parseInt(h.slice(0,8), 16); + const hLSB = parseInt(h.slice(8,16), 16); + const fArr = flatArray(input[k]); + let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB); + if (signalSize < 0){ + throw new Error(`Signal ${k} not found\n`); + } + if (fArr.length < signalSize) { + throw new Error(`Not enough values for input signal ${k}\n`); + } + if (fArr.length > signalSize) { + throw new Error(`Too many values for input signal ${k}\n`); + } + for (let i=0; i0) { + res.unshift(0); + i--; + } + } + return res; +} + +function fromArray32(arr) { //returns a BigInt + var res = BigInt(0); + const radix = BigInt(0x100000000); + for (let i = 0; i6i^mt*c5gXCixXm?ShvovqheduV d1}s1|Abkx$3<4ni_<$llNuV4v0eNl$@&NBq5flIb literal 0 HcmV?d00001