Make code independent of NodeJS core modules
This commit is contained in:
parent
138945bfdc
commit
2635e8d3c9
@ -1,14 +1,15 @@
|
|||||||
const bn128 = require("snarkjs").bn128;
|
const bn128 = require("snarkjs").bn128;
|
||||||
const bigInt = require("snarkjs").bigInt;
|
const bigInt = require("snarkjs").bigInt;
|
||||||
const createBlakeHash = require("blake-hash");
|
const createBlakeHash = require("blake-hash");
|
||||||
const assert = require("assert");
|
|
||||||
const babyJub = require("../src/babyjub");
|
const babyJub = require("../src/babyjub");
|
||||||
|
|
||||||
function getPoint(S) {
|
function getPoint(S) {
|
||||||
const F = bn128.Fr;
|
const F = bn128.Fr;
|
||||||
const h = createBlakeHash("blake256").update(S).digest();
|
const h = createBlakeHash("blake256").update(S).digest();
|
||||||
|
|
||||||
assert(h.length == 32);
|
if (h.length != 32) {
|
||||||
|
throw new Error("Invalid length")
|
||||||
|
}
|
||||||
|
|
||||||
let sign = false;
|
let sign = false;
|
||||||
if (h[31] & 0x80) {
|
if (h[31] & 0x80) {
|
||||||
@ -52,7 +53,9 @@ function generatePoint(S) {
|
|||||||
p = getPoint(S+"_"+sidx);
|
p = getPoint(S+"_"+sidx);
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
assert(babyJub.inCurve(p), "Point not in curve");
|
if (!babyJub.inCurve(p)){
|
||||||
|
throw new Error("Point not in curve");
|
||||||
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,9 +3,7 @@ const bigInt = require("snarkjs").bigInt;
|
|||||||
const babyJub = require("./babyjub");
|
const babyJub = require("./babyjub");
|
||||||
const pedersenHash = require("./pedersenHash").hash;
|
const pedersenHash = require("./pedersenHash").hash;
|
||||||
const mimc7 = require("./mimc7");
|
const mimc7 = require("./mimc7");
|
||||||
const crypto = require("crypto");
|
|
||||||
|
|
||||||
exports.cratePrvKey = cratePrvKey;
|
|
||||||
exports.prv2pub= prv2pub;
|
exports.prv2pub= prv2pub;
|
||||||
exports.sign = sign;
|
exports.sign = sign;
|
||||||
exports.signMiMC = signMiMC;
|
exports.signMiMC = signMiMC;
|
||||||
@ -16,10 +14,6 @@ exports.unpackSignature = unpackSignature;
|
|||||||
exports.pruneBuffer = pruneBuffer;
|
exports.pruneBuffer = pruneBuffer;
|
||||||
|
|
||||||
|
|
||||||
function cratePrvKey() {
|
|
||||||
return crypto.randomBytes(32);
|
|
||||||
}
|
|
||||||
|
|
||||||
function pruneBuffer(_buff) {
|
function pruneBuffer(_buff) {
|
||||||
const buff = Buffer.from(_buff);
|
const buff = Buffer.from(_buff);
|
||||||
buff[0] = buff[0] & 0xF8;
|
buff[0] = buff[0] & 0xF8;
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
|
|
||||||
const Web3Utils = require("web3-utils");
|
const Web3Utils = require("web3-utils");
|
||||||
const assert = require("assert");
|
|
||||||
|
|
||||||
class Contract {
|
class Contract {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -141,7 +140,9 @@ class Contract {
|
|||||||
msize() { this.code.push(0x59); }
|
msize() { this.code.push(0x59); }
|
||||||
gas() { this.code.push(0x5a); }
|
gas() { this.code.push(0x5a); }
|
||||||
label(name) {
|
label(name) {
|
||||||
assert(typeof this.labels[name] == "undefined", "Label already defined");
|
if (typeof this.labels[name] != "undefined") {
|
||||||
|
throw new Error("Label already defined");
|
||||||
|
}
|
||||||
this.labels[name] = this.code.length;
|
this.labels[name] = this.code.length;
|
||||||
this.code.push(0x5b);
|
this.code.push(0x5b);
|
||||||
|
|
||||||
@ -150,20 +151,23 @@ class Contract {
|
|||||||
|
|
||||||
push(data) {
|
push(data) {
|
||||||
const d = Web3Utils.hexToBytes(Web3Utils.toHex(data));
|
const d = Web3Utils.hexToBytes(Web3Utils.toHex(data));
|
||||||
assert(d.length>0);
|
if (d.length == 0 || d.length > 32) {
|
||||||
assert(d.length<=32);
|
throw new Error("Assertion failed");
|
||||||
|
}
|
||||||
this.code = this.code.concat([0x5F + d.length], d);
|
this.code = this.code.concat([0x5F + d.length], d);
|
||||||
}
|
}
|
||||||
|
|
||||||
dup(n) {
|
dup(n) {
|
||||||
assert(n>=0);
|
if (n < 0 || n >= 16) {
|
||||||
assert(n<16);
|
throw new Error("Assertion failed");
|
||||||
|
}
|
||||||
this.code.push(0x80 + n);
|
this.code.push(0x80 + n);
|
||||||
}
|
}
|
||||||
|
|
||||||
swap(n) {
|
swap(n) {
|
||||||
assert(n>=1);
|
if (n < 1 || n > 16) {
|
||||||
assert(n<=16);
|
throw new Error("Assertion failed");
|
||||||
|
}
|
||||||
this.code.push(0x8f + n);
|
this.code.push(0x8f + n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
const bn128 = require("snarkjs").bn128;
|
const bn128 = require("snarkjs").bn128;
|
||||||
const bigInt = require("snarkjs").bigInt;
|
const bigInt = require("snarkjs").bigInt;
|
||||||
const babyJub = require("./babyjub");
|
const babyJub = require("./babyjub");
|
||||||
const assert = require("assert");
|
|
||||||
const createBlakeHash = require("blake-hash");
|
const createBlakeHash = require("blake-hash");
|
||||||
|
|
||||||
const GENPOINT_PREFIX = "PedersenGenerator";
|
const GENPOINT_PREFIX = "PedersenGenerator";
|
||||||
@ -73,7 +72,9 @@ function getBasePoint(pointIdx) {
|
|||||||
|
|
||||||
const p8 = babyJub.mulPointEscalar(p, 8);
|
const p8 = babyJub.mulPointEscalar(p, 8);
|
||||||
|
|
||||||
assert(babyJub.inSubgroup(p8), "Point not in curve");
|
if (!babyJub.inSubgroup(p8)) {
|
||||||
|
throw new Error("Point not in curve");
|
||||||
|
}
|
||||||
|
|
||||||
bases[pointIdx] = p8;
|
bases[pointIdx] = p8;
|
||||||
return p8;
|
return p8;
|
||||||
|
@ -2,6 +2,7 @@ const chai = require("chai");
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const snarkjs = require("snarkjs");
|
const snarkjs = require("snarkjs");
|
||||||
const compiler = require("circom");
|
const compiler = require("circom");
|
||||||
|
// const crypto = require("crypto");
|
||||||
|
|
||||||
const eddsa = require("../src/eddsa.js");
|
const eddsa = require("../src/eddsa.js");
|
||||||
const babyJub = require("../src/babyjub.js");
|
const babyJub = require("../src/babyjub.js");
|
||||||
@ -45,7 +46,7 @@ describe("EdDSA test", function () {
|
|||||||
it("Sign a single 10 bytes from 0 to 9", async () => {
|
it("Sign a single 10 bytes from 0 to 9", async () => {
|
||||||
const msg = Buffer.from("00010203040506070809", "hex");
|
const msg = Buffer.from("00010203040506070809", "hex");
|
||||||
|
|
||||||
// const prvKey = eddsa.cratePrvKey();
|
// const prvKey = crypto.randomBytes(32);
|
||||||
|
|
||||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user