circomlibjs-hinkal-fork 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main.js CHANGED
@@ -1,23 +1,7 @@
1
1
  export {default as buildBabyjub} from "./src/babyjub.js";
2
- export {default as evmasm} from "./src/evmasm.js";
3
2
 
4
- export {default as buildMimc7} from "./src/mimc7.js";
5
- import * as _mimc7Contract from "./src/mimc7_gencontract.js";
6
- export const mimc7Contract=_mimc7Contract;
7
-
8
- export {default as buildMimcSponge} from "./src/mimcsponge.js";
9
- import * as _mimcSpongeContract from "./src/mimcsponge_gencontract.js";
10
- export const mimcSpongecontract=_mimcSpongeContract;
11
3
 
12
4
 
13
5
  export { buildPoseidon, buildPoseidonWasm } from "./src/poseidon_wasm.js";
14
- import * as _poseidonContract from "./src/poseidon_gencontract.js";
15
- export const poseidonContract=_poseidonContract;
16
-
17
- export {default as buildPoseidonReference} from "./src/poseidon_reference.js";
18
- export {default as buildPoseidonOpt} from "./src/poseidon_opt.js";
19
-
20
- export {SMT, buildSMT, newMemEmptyTrie} from "./src/smt.js";
21
6
 
22
- export { default as SMTMemDb } from "./src/smt_memdb.js";
23
7
 
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "import": "./main.js",
8
8
  "require": "./build/main.cjs"
9
9
  },
10
- "version": "0.0.2",
10
+ "version": "0.0.3",
11
11
  "description": "Javascript library to work with circomlib",
12
12
  "scripts": {
13
13
  "test": "mocha",
@@ -30,11 +30,6 @@
30
30
  "mocha": "^9.1.3"
31
31
  },
32
32
  "dependencies": {
33
- "blake-hash": "^2.0.0",
34
- "blake2b": "^2.1.3",
35
- "ethers": "^5.5.1",
36
- "ffjavascript": "^0.3.0",
37
- "buffer": "6.0.3",
38
- "process": "0.11.10"
33
+ "ffjavascript": "^0.3.0"
39
34
  }
40
35
  }
package/src/evmasm.js DELETED
@@ -1,209 +0,0 @@
1
- // Copyright (c) 2018 Jordi Baylina
2
- // License: LGPL-3.0+
3
- //
4
-
5
- import { ethers } from "ethers";
6
- import {Scalar} from "ffjavascript";
7
-
8
- export default class Contract {
9
- constructor() {
10
- this.code = [];
11
- this.labels = {};
12
- this.pendingLabels = {};
13
- }
14
-
15
- createTxData() {
16
- let C;
17
-
18
- // Check all labels are defined
19
- const pendingLabels = Object.keys(this.pendingLabels);
20
- if (pendingLabels.length>0) {
21
- throw new Error("Lables not defined: "+ pendingLabels.join(", "));
22
- }
23
-
24
- let setLoaderLength = 0;
25
- let genLoadedLength = -1;
26
-
27
- while (genLoadedLength!=setLoaderLength) {
28
- setLoaderLength = genLoadedLength;
29
- C = new Contract();
30
- C.codesize();
31
- C.push(setLoaderLength);
32
- C.push(0);
33
- C.codecopy();
34
-
35
- C.push(this.code.length);
36
- C.push(0);
37
- C.return();
38
- genLoadedLength = C.code.length;
39
- }
40
-
41
- return ethers.utils.hexlify(C.code.concat(this.code));
42
- }
43
-
44
- stop() { this.code.push(0x00); }
45
- add() { this.code.push(0x01); }
46
- mul() { this.code.push(0x02); }
47
- sub() { this.code.push(0x03); }
48
- div() { this.code.push(0x04); }
49
- sdiv() { this.code.push(0x05); }
50
- mod() { this.code.push(0x06); }
51
- smod() { this.code.push(0x07); }
52
- addmod() { this.code.push(0x08); }
53
- mulmod() { this.code.push(0x09); }
54
- exp() { this.code.push(0x0a); }
55
- signextend() { this.code.push(0x0b); }
56
-
57
- lt() { this.code.push(0x10); }
58
- gt() { this.code.push(0x11); }
59
- slt() { this.code.push(0x12); }
60
- sgt() { this.code.push(0x13); }
61
- eq() { this.code.push(0x14); }
62
- iszero() { this.code.push(0x15); }
63
- and() { this.code.push(0x16); }
64
- or() { this.code.push(0x17); }
65
- shor() { this.code.push(0x18); }
66
- not() { this.code.push(0x19); }
67
- byte() { this.code.push(0x1a); }
68
-
69
- keccak() { this.code.push(0x20); }
70
- sha3() { this.code.push(0x20); } // alias
71
-
72
- address() { this.code.push(0x30); }
73
- balance() { this.code.push(0x31); }
74
- origin() { this.code.push(0x32); }
75
- caller() { this.code.push(0x33); }
76
- callvalue() { this.code.push(0x34); }
77
- calldataload() { this.code.push(0x35); }
78
- calldatasize() { this.code.push(0x36); }
79
- calldatacopy() { this.code.push(0x37); }
80
- codesize() { this.code.push(0x38); }
81
- codecopy() { this.code.push(0x39); }
82
- gasprice() { this.code.push(0x3a); }
83
- extcodesize() { this.code.push(0x3b); }
84
- extcodecopy() { this.code.push(0x3c); }
85
- returndatasize() { this.code.push(0x3d); }
86
- returndatacopy() { this.code.push(0x3e); }
87
-
88
- blockhash() { this.code.push(0x40); }
89
- coinbase() { this.code.push(0x41); }
90
- timestamp() { this.code.push(0x42); }
91
- number() { this.code.push(0x43); }
92
- difficulty() { this.code.push(0x44); }
93
- gaslimit() { this.code.push(0x45); }
94
-
95
- pop() { this.code.push(0x50); }
96
- mload() { this.code.push(0x51); }
97
- mstore() { this.code.push(0x52); }
98
- mstore8() { this.code.push(0x53); }
99
- sload() { this.code.push(0x54); }
100
- sstore() { this.code.push(0x55); }
101
-
102
- _pushLabel(label) {
103
- if (typeof this.labels[label] != "undefined") {
104
- this.push(this.labels[label]);
105
- } else {
106
- this.pendingLabels[label] = this.pendingLabels[label] || [];
107
- this.pendingLabels[label].push(this.code.length);
108
- this.push("0x000000");
109
- }
110
- }
111
-
112
- _fillLabel(label) {
113
- if (!this.pendingLabels[label]) return;
114
-
115
- let dst = this.labels[label];
116
-
117
- const dst3 = [dst >> 16, (dst >> 8) & 0xFF, dst & 0xFF];
118
-
119
- this.pendingLabels[label].forEach((p) => {
120
- for (let i=0; i<3; i++) {
121
- this.code[p+i+1] = dst3[i];
122
- }
123
- });
124
-
125
- delete this.pendingLabels[label];
126
- }
127
-
128
-
129
- jmp(label) {
130
- if (typeof label !== "undefined") {
131
- this._pushLabel(label);
132
- }
133
- this.code.push(0x56);
134
- }
135
-
136
- jmpi(label) {
137
- if (typeof label !== "undefined") {
138
- this._pushLabel(label);
139
- }
140
- this.code.push(0x57);
141
- }
142
-
143
- pc() { this.code.push(0x58); }
144
- msize() { this.code.push(0x59); }
145
- gas() { this.code.push(0x5a); }
146
- label(name) {
147
- if (typeof this.labels[name] != "undefined") {
148
- throw new Error("Label already defined");
149
- }
150
- this.labels[name] = this.code.length;
151
- this.code.push(0x5b);
152
-
153
- this._fillLabel(name);
154
- }
155
-
156
- push(data) {
157
- if ((typeof data !== "string") || (data.slice(0,2) != "0x")) {
158
- let v = Scalar.e(data);
159
- if (Scalar.isNegative(v)) {
160
- v = Scalar.add(Scalar.shl(Scalar.e(1), 256), v);
161
- }
162
- let S = Scalar.toString(v, 16);
163
- if (S.length % 2) S = "0"+S;
164
- S = "0x" +S;
165
- data = S;
166
- }
167
- const d = ethers.utils.arrayify(data);
168
- if (d.length == 0 || d.length > 32) {
169
- throw new Error("Assertion failed");
170
- }
171
- const a = [];
172
- this.code.push(0x5F + d.length);
173
- for (let i=0; i<d.length; i++) {
174
- this.code.push(d[i]);
175
- }
176
- }
177
-
178
- dup(n) {
179
- if (n < 0 || n >= 16) {
180
- throw new Error("Assertion failed");
181
- }
182
- this.code.push(0x80 + n);
183
- }
184
-
185
- swap(n) {
186
- if (n < 1 || n > 16) {
187
- throw new Error("Assertion failed");
188
- }
189
- this.code.push(0x8f + n);
190
- }
191
-
192
- log0() { this.code.push(0xa0); }
193
- log1() { this.code.push(0xa1); }
194
- log2() { this.code.push(0xa2); }
195
- log3() { this.code.push(0xa3); }
196
- log4() { this.code.push(0xa4); }
197
-
198
- create() { this.code.push(0xf0); }
199
- call() { this.code.push(0xf1); }
200
- callcode() { this.code.push(0xf2); }
201
- return() { this.code.push(0xf3); }
202
- delegatecall() { this.code.push(0xf4); }
203
-
204
- staticcall() { this.code.push(0xfa); }
205
- revert() { this.code.push(0xfd); }
206
- invalid() { this.code.push(0xfe); }
207
- selfdestruct() { this.code.push(0xff); }
208
- }
209
-
package/src/mimc7.js DELETED
@@ -1,78 +0,0 @@
1
- import {getCurveFromName, Scalar} from "ffjavascript";
2
-
3
- import { ethers } from "ethers";
4
-
5
- const SEED = "mimc";
6
- const NROUNDS = 91;
7
-
8
- export default async function buildMimc7() {
9
- const bn128 = await getCurveFromName("bn128", true);
10
- return new Mimc7(bn128.Fr);
11
- }
12
-
13
-
14
- class Mimc7 {
15
- constructor (F) {
16
- this.F = F;
17
- this.cts = this.getConstants(SEED, 91);
18
- }
19
-
20
- getIV(seed) {
21
- const F = this.F;
22
- if (typeof seed === "undefined") seed = SEED;
23
- const c = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(seed+"_iv"));
24
- const cn = Scalar.e(c);
25
- const iv = Scalar.mod(cn, F.p);
26
- return iv;
27
- };
28
-
29
- getConstants(seed, nRounds) {
30
- const F = this.F;
31
- if (typeof seed === "undefined") seed = SEED;
32
- if (typeof nRounds === "undefined") nRounds = NROUNDS;
33
- const cts = new Array(nRounds);
34
- let c = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(SEED));
35
- for (let i=1; i<nRounds; i++) {
36
- c = ethers.utils.keccak256(c);
37
-
38
- cts[i] = F.e(c);
39
- }
40
- cts[0] = F.e(0);
41
- return cts;
42
- }
43
-
44
- hash (_x_in, _k) {
45
- const F = this.F;
46
- const x_in = F.e(_x_in);
47
- const k = F.e(_k);
48
- let r;
49
- for (let i=0; i<NROUNDS; i++) {
50
- const c = this.cts[i];
51
- const t = (i==0) ? F.add(x_in, k) : F.add(F.add(r, k), c);
52
- const t2 = F.square(t);
53
- const t4 = F.square(t2);
54
- r = F.mul(F.mul(t4, t2), t);
55
- }
56
- return F.add(r, k);
57
- }
58
-
59
- multiHash(arr, key) {
60
- const F = this.F;
61
- let r;
62
- if (typeof(key) === "undefined") {
63
- r = F.zero;
64
- } else {
65
- r = F.e(key);
66
- }
67
- for (let i=0; i<arr.length; i++) {
68
- r = F.add(
69
- F.add(
70
- r,
71
- F.e(arr[i])
72
- ),
73
- this.hash(F.e(arr[i]), r)
74
- );
75
- }
76
- return r;
77
- }
78
- }
@@ -1,113 +0,0 @@
1
- // Copyright (c) 2018 Jordi Baylina
2
- // License: LGPL-3.0+
3
- //
4
-
5
- import { ethers } from "ethers";
6
-
7
- import Contract from "./evmasm.js";
8
-
9
- export function createCode(seed, n) {
10
-
11
- let ci = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(seed));;
12
-
13
- const C = new Contract();
14
-
15
- C.push(0x44);
16
- C.push("0x00");
17
- C.push("0x00");
18
- C.calldatacopy();
19
- C.push("0x0100000000000000000000000000000000000000000000000000000000");
20
- C.push("0x00");
21
- C.mload();
22
- C.div();
23
- C.push("0xd15ca109"); // MiMCpe7(uint256,uint256)
24
- // C.push("0x8c42199e"); // MiMCpe7(uint256,uint256,uint256)
25
- C.eq();
26
- C.jmpi("start");
27
- C.invalid();
28
-
29
- C.label("start");
30
- C.push("0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"); // q
31
- C.push("0x24");
32
- C.mload(); // k q
33
-
34
-
35
- C.dup(1); // q k q
36
- C.dup(0); // q q k q
37
- C.push("0x04");
38
- C.mload(); // x q q k q
39
- C.dup(3); // k x q q k q
40
- C.addmod(); // t=x+k q k q
41
- C.dup(1); // q t q k q
42
- C.dup(0); // q q t q k q
43
- C.dup(2); // t q q t q k q
44
- C.dup(0); // t t q q t q k q
45
- C.mulmod(); // a=t^2 q t q k q
46
- C.dup(1); // q a q t q k q
47
- C.dup(1); // a q a q t q k q
48
- C.dup(0); // a a q a q t q k q
49
- C.mulmod(); // b=t^4 a q t q k q
50
- C.mulmod(); // c=t^6 t q k q
51
- C.mulmod(); // r=t^7 k q
52
-
53
- for (let i=0; i<n-1; i++) {
54
- ci = ethers.utils.keccak256(ci);
55
- C.dup(2); // q r k q
56
- C.dup(0); // q q r k q
57
- C.dup(0); // q q q r k q
58
- C.swap(3); // r q q q k q
59
- C.push(ci); // c r q q k q
60
- C.addmod(); // s=c+r q q k q
61
- C.dup(3); // k s q q k q
62
- C.addmod(); // t=s+k q k q
63
- C.dup(1); // q t q k q
64
- C.dup(0); // q q t q k q
65
- C.dup(2); // t q q t q k q
66
- C.dup(0); // t t q q t q k q
67
- C.mulmod(); // a=t^2 q t q k q
68
- C.dup(1); // q a q t q k q
69
- C.dup(1); // a q a q t q k q
70
- C.dup(0); // a a q a q t q k q
71
- C.mulmod(); // b=t^4 a q t q k q
72
- C.mulmod(); // c=t^6 t q k q
73
- C.mulmod(); // r=t^7 k q
74
- }
75
-
76
- C.addmod(); // res=t^7+k
77
- C.push("0x00");
78
- C.mstore(); // Save it to pos 0;
79
- C.push("0x20");
80
- C.push("0x00");
81
- C.return();
82
-
83
- return C.createTxData();
84
- }
85
-
86
- export const abi = [
87
- {
88
- "constant": true,
89
- "inputs": [
90
- {
91
- "name": "in_x",
92
- "type": "uint256"
93
- },
94
- {
95
- "name": "in_k",
96
- "type": "uint256"
97
- }
98
- ],
99
- "name": "MiMCpe7",
100
- "outputs": [
101
- {
102
- "name": "out_x",
103
- "type": "uint256"
104
- }
105
- ],
106
- "payable": false,
107
- "stateMutability": "pure",
108
- "type": "function"
109
- }
110
- ];
111
-
112
-
113
-
@@ -1,18 +0,0 @@
1
- import buildMimc7 from "./mimc7.js";
2
- import process from "process";
3
-
4
- async function run() {
5
- const mimc7 = await buildMimc7();
6
-
7
- console.log("IV: "+mimc7.getIV().toString());
8
- }
9
-
10
- run().then(()=> {
11
- process.exit(0);
12
- }, (err) => {
13
- console.log(err.stack);
14
- console.log(err.message);
15
- process.exit(1);
16
- });
17
-
18
-
@@ -1,26 +0,0 @@
1
- import buildMimc7 from "./mimc7.js";
2
- import process from "process";
3
-
4
- async function run() {
5
- const mimc7 = await buildMimc7();
6
- const nRounds = 91;
7
- let S = "[\n";
8
- const cts = mimc7.getConstants();
9
- for (let i=0; i<nRounds; i++) {
10
- S = S + mimc7.F.toString(cts[i]);
11
- if (i<nRounds-1) S = S + ",";
12
- S=S+"\n";
13
- }
14
- S = S + "]\n";
15
-
16
- console.log(S);
17
- }
18
-
19
- run().then(()=> {
20
- process.exit(0);
21
- }, (err) => {
22
- console.log(err.stack);
23
- console.log(err.message);
24
- process.exit(1);
25
- });
26
-
@@ -1,14 +0,0 @@
1
- import {createCode} from "./mimc7_gencontract.js";
2
- import process from "process";
3
-
4
- const SEED = "mimc";
5
-
6
- let nRounds;
7
- if (typeof process.argv[2] != "undefined") {
8
- nRounds = parseInt(process.argv[2]);
9
- } else {
10
- nRounds = 91;
11
- }
12
-
13
- console.log(createCode(SEED, nRounds));
14
-
package/src/mimcsponge.js DELETED
@@ -1,100 +0,0 @@
1
- import { Scalar, getCurveFromName } from "ffjavascript";
2
- import { ethers } from "ethers";
3
-
4
- const SEED = "mimcsponge";
5
- const NROUNDS = 220;
6
-
7
- export default async function buildMimcSponge() {
8
- const bn128 = await getCurveFromName("bn128", true);
9
- return new MimcSponge(bn128.Fr);
10
- }
11
-
12
- class MimcSponge {
13
- constructor (F) {
14
- this.F = F;
15
- this.cts = this.getConstants(SEED, NROUNDS);
16
- }
17
-
18
- getIV (seed) {
19
- const F = this.F;
20
- if (typeof seed === "undefined") seed = SEED;
21
- const c = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(seed+"_iv"));
22
- const cn = Scalar.e(c);
23
- const iv = cn.mod(F.p);
24
- return iv;
25
- };
26
-
27
- getConstants (seed, nRounds) {
28
- const F = this.F;
29
- if (typeof seed === "undefined") seed = SEED;
30
- if (typeof nRounds === "undefined") nRounds = NROUNDS;
31
- const cts = new Array(nRounds);
32
- let c = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(SEED));;
33
- for (let i=1; i<nRounds; i++) {
34
- c = ethers.utils.keccak256(c);
35
-
36
- cts[i] = F.e(c);
37
- }
38
- cts[0] = F.e(0);
39
- cts[cts.length - 1] = F.e(0);
40
- return cts;
41
- };
42
-
43
-
44
- hash(_xL_in, _xR_in, _k) {
45
- const F = this.F;
46
- let xL = F.e(_xL_in);
47
- let xR = F.e(_xR_in);
48
- const k = F.e(_k);
49
- for (let i=0; i<NROUNDS; i++) {
50
- const c = this.cts[i];
51
- const t = (i==0) ? F.add(xL, k) : F.add(F.add(xL, k), c);
52
- const t2 = F.square(t);
53
- const t4 = F.square(t2);
54
- const t5 = F.mul(t4, t);
55
- const xR_tmp = F.e(xR);
56
- if (i < (NROUNDS - 1)) {
57
- xR = xL;
58
- xL = F.add(xR_tmp, t5);
59
- } else {
60
- xR = F.add(xR_tmp, t5);
61
- }
62
- }
63
- return {
64
- xL: xL,
65
- xR: xR
66
- };
67
- }
68
-
69
- multiHash(arr, key, numOutputs) {
70
- const F = this.F;
71
- if (typeof(numOutputs) === "undefined") {
72
- numOutputs = 1;
73
- }
74
- if (typeof(key) === "undefined") {
75
- key = F.zero;
76
- }
77
-
78
- let R = F.zero;
79
- let C = F.zero;
80
-
81
- for (let i=0; i<arr.length; i++) {
82
- R = F.add(R, F.e(arr[i]));
83
- const S = this.hash(R, C, key);
84
- R = S.xL;
85
- C = S.xR;
86
- }
87
- let outputs = [R];
88
- for (let i=1; i < numOutputs; i++) {
89
- const S = this.hash(R, C, key);
90
- R = S.xL;
91
- C = S.xR;
92
- outputs.push(R);
93
- }
94
- if (numOutputs == 1) {
95
- return outputs[0];
96
- } else {
97
- return outputs;
98
- }
99
- }
100
- }