circomlibjs-hinkal-fork 0.0.3 → 0.0.4
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/package.json +1 -1
- package/src/eddsa.js +92 -0
package/package.json
CHANGED
package/src/eddsa.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Scalar } from "ffjavascript";
|
|
2
|
+
import buildBabyJub from "./babyjub.js";
|
|
3
|
+
import { buildPoseidon } from "./poseidon_wasm.js";
|
|
4
|
+
import createBlakeHash from "blake-hash";
|
|
5
|
+
|
|
6
|
+
export default async function buildEddsa() {
|
|
7
|
+
const babyJub = await buildBabyJub("bn128");
|
|
8
|
+
const poseidon = await buildPoseidon();
|
|
9
|
+
return new Eddsa(babyJub, poseidon);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class Eddsa {
|
|
13
|
+
|
|
14
|
+
constructor(babyJub, poseidon) {
|
|
15
|
+
this.babyJub = babyJub;
|
|
16
|
+
this.poseidon = poseidon;
|
|
17
|
+
this.F = babyJub.F;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
pruneBuffer(buff) {
|
|
21
|
+
buff[0] = buff[0] & 0xF8;
|
|
22
|
+
buff[31] = buff[31] & 0x7F;
|
|
23
|
+
buff[31] = buff[31] | 0x40;
|
|
24
|
+
return buff;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
signPoseidon(prv, msg) {
|
|
28
|
+
const F = this.babyJub.F;
|
|
29
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
30
|
+
const s = Scalar.fromRprLE(sBuff, 0, 32);
|
|
31
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));
|
|
32
|
+
|
|
33
|
+
const composeBuff = new Uint8Array(32 + msg.length);
|
|
34
|
+
composeBuff.set(sBuff.slice(32), 0);
|
|
35
|
+
F.toRprLE(composeBuff, 32, msg);
|
|
36
|
+
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
|
|
37
|
+
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
|
|
38
|
+
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
|
|
39
|
+
|
|
40
|
+
const hm = this.poseidon([R8[0], R8[1], A[0], A[1], msg]);
|
|
41
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
42
|
+
const S = Scalar.mod(
|
|
43
|
+
Scalar.add(
|
|
44
|
+
r,
|
|
45
|
+
Scalar.mul(hms, s)
|
|
46
|
+
),
|
|
47
|
+
this.babyJub.subOrder
|
|
48
|
+
)
|
|
49
|
+
return {
|
|
50
|
+
R8: R8,
|
|
51
|
+
S: S
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
verifyPoseidon(msg, sig, A) {
|
|
56
|
+
// Check parameters
|
|
57
|
+
if (typeof sig != "object") return false;
|
|
58
|
+
if (!Array.isArray(sig.R8)) return false;
|
|
59
|
+
if (sig.R8.length!= 2) return false;
|
|
60
|
+
if (!this.babyJub.inCurve(sig.R8)) return false;
|
|
61
|
+
if (!Array.isArray(A)) return false;
|
|
62
|
+
if (A.length!= 2) return false;
|
|
63
|
+
if (!this.babyJub.inCurve(A)) return false;
|
|
64
|
+
if (sig.S>= this.babyJub.subOrder) return false;
|
|
65
|
+
|
|
66
|
+
const hm = this.poseidon([sig.R8[0], sig.R8[1], A[0], A[1], msg]);
|
|
67
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
68
|
+
|
|
69
|
+
const Pleft = this.babyJub.mulPointEscalar(this.babyJub.Base8, sig.S);
|
|
70
|
+
let Pright = this.babyJub.mulPointEscalar(A, Scalar.mul(hms, 8));
|
|
71
|
+
Pright = this.babyJub.addPoint(sig.R8, Pright);
|
|
72
|
+
|
|
73
|
+
if (!this.babyJub.F.eq(Pleft[0],Pright[0])) return false;
|
|
74
|
+
if (!this.babyJub.F.eq(Pleft[1],Pright[1])) return false;
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
packSignature(sig) {
|
|
79
|
+
const buff = new Uint8Array(64);
|
|
80
|
+
const R8p = this.babyJub.packPoint(sig.R8);
|
|
81
|
+
buff.set(R8p, 0)
|
|
82
|
+
const Sp = Scalar.toRprLE(buff, 32, sig.S, 32);
|
|
83
|
+
return buff;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
unpackSignature(sigBuff) {
|
|
87
|
+
return {
|
|
88
|
+
R8: this.babyJub.unpackPoint(sigBuff.slice(0,32)),
|
|
89
|
+
S: Scalar.fromRprLE(sigBuff, 32, 32)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|