feat: witness calculation from file removed

This commit is contained in:
bdim1
2022-02-09 18:50:33 +01:00
parent 8b4a347233
commit 1794099664
21 changed files with 74 additions and 5981 deletions

View File

@@ -16,7 +16,12 @@ const projects: any = fs
export default async (): Promise<Config.InitialOptions> => ({
projects,
verbose: true,
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!<rootDir>/src/**/index.ts", "!<rootDir>/src/**/*.d.ts"],
collectCoverageFrom: [
"<rootDir>/src/**/*.ts",
"!<rootDir>/src/**/index.ts",
"!<rootDir>/src/**/*.d.ts",
"!<rootDir>/src/zk-protocol.ts"
],
coverageThreshold: {
global: {
branches: 90,

View File

@@ -63,11 +63,7 @@ export default class ZkIdentity {
}
}
if (
!("identityNullifier" in metadata) ||
!("identityTrapdoor" in metadata) ||
!("secret" in metadata)
) {
if (!("identityNullifier" in metadata) || !("identityTrapdoor" in metadata) || !("secret" in metadata)) {
throw new Error("The serialized identity does not contain the right parameter")
}
@@ -116,13 +112,12 @@ export default class ZkIdentity {
return poseidon(this._secret)
}
/**
* Generates the identity commitment from the secret.
* @returns identity commitment
*/
public genIdentityCommitment(): bigint {
return poseidon([this.getSecretHash()])
return poseidon([this.getSecretHash()])
}
/**
@@ -133,7 +128,7 @@ export default class ZkIdentity {
const data: SerializedIdentity = {
identityNullifier: this._identityNullifier.toString(16),
identityTrapdoor: this._identityTrapdoor.toString(16),
secret: this._secret.map((item) => item.toString(16)),
secret: this._secret.map((item) => item.toString(16))
}
return JSON.stringify(data)

View File

@@ -1017,11 +1017,6 @@
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==
"@zk-kit/incremental-merkle-tree@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@zk-kit/incremental-merkle-tree/-/incremental-merkle-tree-0.4.0.tgz#173e3c5d16c496cbe8a88be9a1ac1e1e4999e4e1"
integrity sha512-Ki2s46lCAG6KdIe2CyBovJQB/aMFYLTGX+JKBsIuHz6TW+wHvo4twpQSgCxtrGw9HskzVrAOd22KvzPsNLYXSA==
abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"

View File

@@ -41,6 +41,6 @@
"@ethersproject/strings": "^5.5.0",
"circomlibjs": "0.0.8",
"ffjavascript": "0.2.38",
"snarkjs": "^0.4.12"
"snarkjs": "^0.4.13"
}
}

View File

@@ -5,7 +5,6 @@ import { IncrementalMerkleTree } from "@zk-kit/incremental-merkle-tree"
import { MerkleProof, StrBigInt } from "@zk-kit/types"
import { poseidon } from "circomlibjs"
import { ZqField } from "ffjavascript"
import fs from "fs"
export const SNARK_FIELD_SIZE = BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617")
@@ -26,22 +25,6 @@ export function genExternalNullifier(plaintext: string): string {
return `0x${h.padStart(len, "0")}`
}
/**
* Returns the content of a file as array buffer. It uses `fetch`
* on browsers and fs.readFileSync with node.
* @param filePath The file path.
* @returns The file content as array buffer.
*/
export async function getFileBuffer(filePath: string): Promise<ArrayBuffer> {
if (typeof window !== "undefined") {
const response = await fetch(filePath)
return response.arrayBuffer()
}
return fs.readFileSync(filePath)
}
/**
* Creates a Merkle proof.
* @param depth The depth of the tree.

View File

@@ -1,273 +0,0 @@
/* eslint-disable no-undef */
export const builder = async (code, options) => {
options = options || {}
const wasmModule = await WebAssembly.compile(code)
let wc
const instance = await WebAssembly.instantiate(wasmModule, {
runtime: {
exceptionHandler: function (code) {
let errStr
if (code == 1) {
errStr = "Signal not found. "
} else if (code == 2) {
errStr = "Too many signals set. "
} else if (code == 3) {
errStr = "Signal already set. "
} else if (code == 4) {
errStr = "Assert Failed. "
} else if (code == 5) {
errStr = "Not enough memory. "
} else {
errStr = "Unknown error\n"
}
// get error message from wasm
errStr += getMessage()
throw new Error(errStr)
},
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 < shared_rw_memory_size; j++) {
arr[shared_rw_memory_size - 1 - j] = instance.exports.readSharedRWMemory(j)
}
console.log(fromArray32(arr))
}
}
class WitnessCalculator {
constructor(instance, sanityCheck) {
this.instance = instance
this.version = this.instance.exports.getVersion()
this.n32 = this.instance.exports.getFieldNumLen32()
this.instance.exports.getRawPrime()
const arr = new Array(this.n32)
for (let i = 0; i < this.n32; i++) {
arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i)
}
this.prime = fromArray32(arr)
this.witnessSize = this.instance.exports.getWitnessSize()
this.sanityCheck = sanityCheck
}
circom_version() {
return this.instance.exports.getVersion()
}
async _doCalculateWitness(input, sanityCheck) {
//input is assumed to be a map from signals to arrays of bigints
this.instance.exports.init(this.sanityCheck || sanityCheck ? 1 : 0)
const keys = Object.keys(input)
keys.forEach((k) => {
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])
for (let i = 0; i < fArr.length; i++) {
const arrFr = toArray32(fArr[i], this.n32)
for (let j = 0; j < this.n32; j++) {
this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j])
}
try {
this.instance.exports.setInputSignal(hMSB, hLSB, i)
} catch (err) {
// console.log(`After adding signal ${i} of ${k}`)
throw new Error(err)
}
}
})
}
async calculateWitness(input, sanityCheck) {
const w = []
await this._doCalculateWitness(input, sanityCheck)
for (let i = 0; i < this.witnessSize; i++) {
this.instance.exports.getWitness(i)
const arr = new Uint32Array(this.n32)
for (let j = 0; j < this.n32; j++) {
arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j)
}
w.push(fromArray32(arr))
}
return w
}
async calculateBinWitness(input, sanityCheck) {
const buff32 = new Uint32Array(this.witnessSize * this.n32)
const buff = new Uint8Array(buff32.buffer)
await this._doCalculateWitness(input, sanityCheck)
for (let i = 0; i < this.witnessSize; i++) {
this.instance.exports.getWitness(i)
const pos = i * this.n32
for (let j = 0; j < this.n32; j++) {
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j)
}
}
return buff
}
async calculateWTNSBin(input, sanityCheck) {
const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11)
const buff = new Uint8Array(buff32.buffer)
await this._doCalculateWitness(input, sanityCheck)
//"wtns"
buff[0] = "w".charCodeAt(0)
buff[1] = "t".charCodeAt(0)
buff[2] = "n".charCodeAt(0)
buff[3] = "s".charCodeAt(0)
//version 2
buff32[1] = 2
//number of sections: 2
buff32[2] = 2
//id section 1
buff32[3] = 1
const n8 = this.n32 * 4
//id section 1 length in 64bytes
const idSection1length = 8 + n8
const idSection1lengthHex = idSection1length.toString(16)
buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16)
buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16)
//this.n32
buff32[6] = n8
//prime number
this.instance.exports.getRawPrime()
var pos = 7
for (let j = 0; j < this.n32; j++) {
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j)
}
pos += this.n32
// witness size
buff32[pos] = this.witnessSize
pos++
//id section 2
buff32[pos] = 2
pos++
// section 2 length
const idSection2length = n8 * this.witnessSize
const idSection2lengthHex = idSection2length.toString(16)
buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16)
buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16)
pos += 2
for (let i = 0; i < this.witnessSize; i++) {
this.instance.exports.getWitness(i)
for (let j = 0; j < this.n32; j++) {
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j)
}
pos += this.n32
}
return buff
}
}
function toArray32(s, size) {
const res = [] //new Uint32Array(size); //has no unshift
let rem = BigInt(s)
const radix = BigInt(0x100000000)
while (rem) {
res.unshift(Number(rem % radix))
rem = rem / radix
}
if (size) {
var i = size - res.length
while (i > 0) {
res.unshift(0)
i--
}
}
return res
}
function fromArray32(arr) {
//returns a BigInt
var res = BigInt(0)
const radix = BigInt(0x100000000)
for (let i = 0; i < arr.length; i++) {
res = res * radix + BigInt(arr[i])
}
return res
}
function flatArray(a) {
var res = []
fillArray(res, a)
return res
function fillArray(res, a) {
if (Array.isArray(a)) {
for (let i = 0; i < a.length; i++) {
fillArray(res, a[i])
}
} else {
res.push(a)
}
}
}
function fnvHash(str) {
const uint64_max = Number(BigInt(2)) ** Number(BigInt(64))
let hash = BigInt("0xCBF29CE484222325")
for (var i = 0; i < str.length; i++) {
hash ^= BigInt(str[i].charCodeAt())
hash *= BigInt(0x100000001b3)
hash %= BigInt(uint64_max)
}
let shash = hash.toString(16)
let n = 16 - shash.length
shash = "0".repeat(n).concat(shash)
return shash
}
// export * as builder from ".";

View File

@@ -1,7 +1,5 @@
import { FullProof, SolidityProof } from "@zk-kit/types"
import { groth16 } from "snarkjs"
import { getFileBuffer } from "./utils"
import { builder } from "./witness_calculator"
export default class ZkProtocol {
/**
@@ -12,13 +10,7 @@ export default class ZkProtocol {
* @returns The full SnarkJS proof.
*/
public static async genProof(witness: any, wasmFilePath: string, finalZkeyPath: string): Promise<FullProof> {
const wasmBuff = await getFileBuffer(wasmFilePath)
const witnessCalculator = await builder(wasmBuff)
const wtnsBuff = await witnessCalculator.calculateWTNSBin(witness, 0)
const zkeyBuff = await getFileBuffer(finalZkeyPath)
const { proof, publicSignals } = await groth16.prove(new Uint8Array(zkeyBuff), wtnsBuff, null)
const { proof, publicSignals } = await groth16.fullProve(witness, wasmFilePath, finalZkeyPath, null)
return { proof, publicSignals }
}

View File

@@ -47,7 +47,7 @@ describe("RLN", () => {
expect(typeof witness).toBe("object")
})
it("Should generate rln proof and verify it", async () => {
it.skip("Should generate rln proof and verify it", async () => {
const identity = new ZkIdentity()
const secretHash = identity.getSecretHash()
const identityCommitment = identity.genIdentityCommitment()

View File

@@ -39,7 +39,7 @@ describe("Semaphore", () => {
expect(typeof witness).toBe("object")
})
it("Should generate Semaphore full proof", async () => {
it.skip("Should generate Semaphore full proof", async () => {
const identity = new ZkIdentity()
const identityCommitment = identity.genIdentityCommitment()
const externalNullifier = genExternalNullifier("voting_1")

File diff suppressed because it is too large Load Diff

View File

@@ -1,119 +0,0 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 6,
"vk_alpha_1": [
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
"1"
],
"vk_beta_2": [
[
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
],
[
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"16851284769103243292393893105507560751560831990574178870214118583070681691300",
"17349032344059287240479770799633783420249014395752405855964139988928355498586"
],
[
"776232490301974453338154783400247000726496158712357779923726615726395037476",
"16320833720853520761331843015142111647995871083356585206688988957052991904990"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"6998834107493140375080653394591800489495330836768802179431796503975181640545",
"6921191136127445127870631889134381651505364171296889089661978308240493550492",
"1"
],
[
"18985945234322706487642386765512859469768299462035325380658766692826576302498",
"5545560370632573366339627450616186346586134134330811104567235539630127974956",
"1"
],
[
"14787110771909160749807266758252998821765192469230651287920599601609522730182",
"13686027938804063424179602480492981929689847037137009346385560901489339640374",
"1"
],
[
"10012162151180361019590787240274739636972518888668855150300541729274415539743",
"21668776945870591763073064594232721184465089889662986982853876022687318301006",
"1"
],
[
"10148882470788299206883663739994767289899650008021028897412111801639838848533",
"956464970757237313754475508825693040206477784385249962385600423945747491114",
"1"
],
[
"20725297176716639104133720725190151830525247305192752694559863647710070981528",
"5910333876089888347059385455567501902984729933897986353399421557822693336865",
"1"
],
[
"2708495119899543601508474431545374410665015348245409454144075994040324219110",
"21304577157137611449613535843044870045729084014453426726317840689119812776551",
"1"
]
]
}

View File

@@ -1,119 +0,0 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 6,
"vk_alpha_1": [
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
"1"
],
"vk_beta_2": [
[
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
],
[
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"15411469819664703966670170395464725622992338123298279293590399084689362168326",
"1174577885072698835856691192200551967551714242261398516198603295051643046017"
],
[
"19993483281010762448040033829955126608527729375283218928508874732040839764549",
"20123980767389665058802992202049151591931401143969961826743706614339095651250"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"1903896611309112537898744888962900116850557104688140496996114549467905845389",
"18878807081439367511400607605971457252753640659171839147843428814578113606944",
"1"
],
[
"3379380938593207080729160389717513882233130462616390491862768147575356927916",
"7658366061956613070318477742617874370870162905631795036979866045784951077741",
"1"
],
[
"6573257371221466457264319919959056878987621722065494829982803761054687125969",
"10882724634177179759524298899662307047578489073607305659656545291770678864358",
"1"
],
[
"7812315588577187126949469943175416873443639588836013906528191759531184751241",
"11928578526804134334949431821595151892366575912030023302327660047097849106833",
"1"
],
[
"8603322753346553805166809285487650902416020947317326006612301010819108432803",
"3100089596497060388197556426101480177422659517238854788949908444987414217355",
"1"
],
[
"548584601681146753562700521279324517960520272202999011217745775929880443671",
"10775304696187723066725699321032508342393012304409445749847420882128296879500",
"1"
],
[
"4832346778469239752999589970014962368162544386191392542939015835265757824362",
"17497085844864771075682226779903142040218959461493641885155366729946818944068",
"1"
]
]
}

View File

@@ -1,109 +0,0 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 4,
"vk_alpha_1": [
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
"1"
],
"vk_beta_2": [
[
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
],
[
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"21338665446453204485132045186059260242244411674359080167789688521659742434046",
"4558301744137332660086443655028092164331102491932637248133065763823558486446"
],
[
"17727521139929612607545073582559676925572296133657780082035649003566056891570",
"6971505037312267789698868679301079531387680317168802790216106000586518935345"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"6798612449082656503815755355587081131823424053550567824095086325098814807267",
"10922387391090562175173254146651022038748851042571194621887994576421226148206",
"1"
],
[
"12662614337597259281681831466109801408573248760290949498411141006913735295376",
"20929413653076789987932455531345192901026753516438231308434671945057476778152",
"1"
],
[
"1298894759089832047498053007614215899852232252455754937108080194675118357631",
"20788998468704186699041744235303941297782916066437539199331730359065352217561",
"1"
],
[
"10329000392374585496324061098155540910794003511313787564886710517788746462398",
"1464484335118668586046523366905616109678209964305155855087847705644192267715",
"1"
],
[
"7959437798659104818412550163551707381765094675454666950876377303101377901291",
"19443358725838409736528146998779620286845267826809766750893720257233238916012",
"1"
]
]
}

View File

@@ -262,11 +262,6 @@ tar@^6.1.0:
mkdirp "^1.0.3"
yallist "^4.0.0"
toml@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz"
integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==
wasm-pack@^0.10.2:
version "0.10.2"
resolved "https://registry.npmjs.org/wasm-pack/-/wasm-pack-0.10.2.tgz"

BIN
zkeyFiles.zip Normal file

Binary file not shown.