Commit 72c56b8e authored by John Doe's avatar John Doe
Browse files

Upload Client.

parent 13d5f931
Pipeline #6 canceled with stages
-r ts-node/register/transpile-only
--timeout 50000
--no-warnings
test/**/*.test.{js,ts}
\ No newline at end of file
const { ethers } = require('hardhat');
const { expect } = require("chai");
const elliptic = require('elliptic');
const ec = new elliptic.ec('secp256k1');
describe("Commitment circuit", () => {
let Token;
let token;
let CommitmentCircuit;
let commCircuit;
let owner, user, amountToDeposit, amountToWithdraw;
before(async () => {
[owner, user] = await ethers.getSigners();
Token = await ethers.getContractFactory("ERC20Mock");
token = await Token.deploy("1000000000000000000000000000");
await token.deployed();
CommitmentCircuit = await ethers.getContractFactory("CommitmentCircuit");
commCircuit = await CommitmentCircuit.deploy(token.address);
await commCircuit.deployed();
amountToDeposit = "5600";
amountToWithdraw = "600";
startComm = [
"80896827534506630690547520492020251026855161557744096096567524235125637166228",
"35340021902447857874733116252574618597035402700515652153758858212804173617786"
];
outpComm = [
"29581854941473273780983167316696779832306506083614655401787514381088218353587",
"95742839520915075053972174008082682421116400817893831937435579328761440955019"
];
invalidComm = [
"2958185494143273780983167316696779832306506083614655401787514381088218353587",
"95742839520915075053972174008082682421116400817893831937435579328761440955019"
];
m = "message";
aggrPubKey = [
"105012878946854625478151668840096389084932252989398520013599206687016371737084",
"109332001306514958330197135056171475144957829515243433050908948584468608042267"
];
aggrR = [
"50076431034714051257113545201217887633355089690556915100431086728944568565495",
"100534334696491181083477625115660637245645677532597488342701629369138874590038"
];
s = "45407579404865038949469822443889530360183727020693714560400763628627243928653";
V1 = ["98643800998832776046857076726380143966566263361880217391586318011264053421760","30843346858215281404360531879074902925812742234979808917433896442383404295811"];
tx = BigNumber.from("9371693311736557046070420808013642477283069066664552120604694923213514659485");
ttx = BigNumber.from("108990598742026189466062582882075466735590285646786143464437267625281894984458");
eex = BigNumber.from("74792148165564554200880594177522147399263789016199833638055550708651561575050");
proofA = ["101489231467176905426412005712620388827237186079457258716926808479293904602928","5030063491826264586711708197433648962429539055960251990629019837101184114477"];
proofS = ["89158358594918955516268084680968734550631439535649874169456239070617316920625","10134396677076491541245486946267895039359362102088588616906305081094770583418"];
proofT1 = ["90468154111395104676141462342169501734060298909352541356458724546732428707334","42969349149287050453272407357708113887265300867021537169319955066482568112046"];
proofT2 = ["75455778666603495386742030251186550595474951459854286226866232749726491145314","72204170643586634455940894385236843654723049642382994565099139609146163941338"];
Lk = [
["110556554007824383663135326600822000322928700283152952952140249969179872327532","12168221817709144516286933821836663774050482777692806860120606232649374266816"],
["22113417191699744162502478916577006805237583698833746310981765030750513907022","102251838342976310148740803378402979003035229598536204761529925815246432443843"],
["43041650227960287461940122965062253610440827792384078200600603423993850570606","58147020486160069739256233744084859708744854439899517248803298735574203028813"],
["43719908148600252502450510119375063491452343360995185459843923775852392467849","1303894536346773904663970656050807146445352787085645054943595758657620233374"],
["13075658464435400031022923973917651180469987941117974098173072376137356651143","14294314626521528617262204760400100145885357325735168177172362299004993387452"],
["4907756214056687914480044309171742599057481701415680229254633758212923979606","84296401102165628884557566680636012562783227863072175128118306399086153581880"]
];
Rk = [
["112333866806597390107818227364761768890195984876036982769626751730774709085843","70570887930193861449552090691067551259606146481386470092004480092389970182323"],
["65742165399439694165964687909191046737257696983192170841624607686035191342264","28355701602967303700368070791177739071459911461266961794896240952013245997782"],
["76025389074674600743757677261033090393019281310654711774386343996497481607231","104275909702442448125908676031471414022039428730669856018947606489500829858160"],
["16631044809275143009590600546130327649008853458768308684735837847042434617295","32795676902897886470950196717225604992013313180431439666111590237261747310078"],
["3735780275342750415451342023318175965128664713139897427366306191071556202048","47486959196656137198301032164907150031779561093596659647243814615328325183816"],
["29849603898593932295789132886525707534660261577289725449344298237959502374020","97900797446180594473809893343135493398791964258573025797890044782568305654249"]
];
a = BigNumber.from("15499164509705287011965261363835363969379973692318739607503101931701628630670");
b = BigNumber.from("109354859999102280466998373663539745924157686547405163294605408503966665053124");
let Vj = [V1];
let rp = [Vj, proofA, proofS, proofT1, proofT2, tx, ttx, eex, Lk, Rk, a, b];
});
// describe('Deposit', () => {
// it("Cannot deposit if signature is invalid", async () => {
// await expect(
// commCircuit.deposit(amountToDeposit, startComm, aggrPubKey, aggrR, s, "not valid message", { from: owner.address })
// ).to.be.revertedWith("invalid signature");
// });
// it("Should deposit commitment and transfer tokens", async () => {
// await token.approve(commCircuit.address, amountToDeposit, { from: owner.address });
// const startUserBalance = await token.balanceOf(owner.address);
// const startContractBalance = await token.balanceOf(commCircuit.address);
// const tx = await commCircuit.deposit(amountToDeposit, startComm, aggrPubKey, aggrR, s, m, { from: owner.address });
// const userBalanceAfterDeposit = await token.balanceOf(owner.address);
// const contractBalanceAfterDeposit = await token.balanceOf(commCircuit.address);
// expect(userBalanceAfterDeposit).to.equal(startUserBalance.sub(amountToDeposit));
// expect(contractBalanceAfterDeposit).to.equal(startContractBalance.add(amountToDeposit));
// const rc = await tx.wait();
// const event = rc.events.find(event => event.event === 'Deposited');
// const [to, amount, commitmentId, commitment] = event.args;
// expect(to).to.equal(owner.address);
// expect(amount).to.equal(amountToDeposit);
// expect(commitment.toString()).to.equal(startComm.toString());
// // expect(commitmentId).to.be.within(timestampBeforeDeposit, timestampAfterDeposit);
// });
// });
describe('Transfer commitment', () => {
// beforeEach(async () => {
// await token.approve(commCircuit.address, amountToDeposit, { from: owner.address });
// const tx = await commCircuit.deposit(amountToDeposit, startComm, aggrPubKey, aggrR, s, m, { from: owner.address });
// const rc = await tx.wait();
// const event = rc.events.find(event => event.event === 'Deposited');
// [to, amount, startCommId, commitment] = event.args;
// });
// it("Cannot transfer if signature is invalid", async () => {
// await expect(
// commCircuit.transferCommitment(user.address, startCommId, outpComm, "not valid message", aggrPubKey, aggrR, s, { from: owner.address })
// ).to.be.revertedWith("invalid signature");
// });
// it("Cannot transfer if commitments is invalid", async () => {
// await expect(
// commCircuit.transferCommitment(user.address, startCommId, startComm, m, aggrPubKey, aggrR, s, { from: owner.address })
// ).to.be.revertedWith("invalid commitments");
// });
// it("Cannot transfer if commitment is already used", async () => {
// await commCircuit.transferCommitment(user.address, startCommId, outpComm, m, aggrPubKey, aggrR, s, { from: owner.address });
// await expect(
// commCircuit.transferCommitment(user.address, startCommId, outpComm, m, aggrPubKey, aggrR, s, { from: owner.address })
// ).to.be.revertedWith("commitment already used");
// });
it("Should transfer correctly", async () => {
tx = await commCircuit.transferCommitment([m, aggrPubKey, aggrR, s], rp, { from: owner.address }); //transfer commitment 'output' from owner to user
// const rc = await tx.wait();
// const event = rc.events.find(event => event.event === 'CommitmentTransferred');
// const [sender, recipient, senderCommitmentId, recipientCommitmentId, senderCommitment, recipientCommitment] = event.args;
// expect(sender).to.equal(owner.address);
// expect(recipient).to.equal(user.address);
// expect(senderCommitmentId).to.equal(startCommId);
// expect(senderCommitment.toString()).to.equal(startComm.toString());
// expect(recipientCommitment.toString()).to.equal(outpComm.toString());
});
});
// describe('Withdraw', () => {
// beforeEach(async () => {
// await token.approve(commCircuit.address, amountToDeposit, { from: owner.address });
// const tx = await commCircuit.deposit(amountToDeposit, startComm, aggrPubKey, aggrR, s, m, { from: owner.address });
// const rc = await tx.wait();
// const event = rc.events.find(event => event.event === 'Deposited');
// [to, amount, startCommId, commitment] = event.args;
// });
// it("Cannot withdraw if signature is invalid", async () => {
// await expect(
// commCircuit.withdraw(amountToWithdraw, startCommId, outpComm, aggrPubKey, aggrR, "not valid message", s, { from: owner.address })
// ).to.be.revertedWith("invalid signature");
// });
// it("Cannot withdraw if commitment is already used", async () => {
// await commCircuit.transferCommitment(user.address, startCommId, outpComm, m, aggrPubKey, aggrR, s, { from: owner.address });
// await expect(
// commCircuit.withdraw(amountToWithdraw, startCommId, outpComm, aggrPubKey, aggrR, m, s, { from: owner.address })
// ).to.be.revertedWith("commitment already used");
// });
// it("Cannot withdraw if commitments is invalid", async () => {
// await expect(
// commCircuit.withdraw(amountToWithdraw, startCommId, startComm, aggrPubKey, aggrR, m, s, { from: owner.address })
// ).to.be.revertedWith("invalid commitments");
// });
// it("Should withdraw correctly", async () => {
// const startUserBalance = await token.balanceOf(owner.address);
// const startContractBalance = await token.balanceOf(commCircuit.address);
// tx = await commCircuit.withdraw(amountToWithdraw, startCommId, outpComm, aggrPubKey, aggrR, m, s, { from: owner.address });
// const userBalanceAfterWithdraw = await token.balanceOf(owner.address);
// const contractBalanceAfterWithdraw = await token.balanceOf(commCircuit.address);
// expect(userBalanceAfterWithdraw).to.equal(startUserBalance.add(amountToWithdraw));
// expect(contractBalanceAfterWithdraw).to.equal(startContractBalance.sub(amountToWithdraw));
// const rc = await tx.wait();
// const event = rc.events.find(event => event.event === 'Withdrawn');
// const [to, amount, commitmentIdOld, commitmentIdNew, commitmentOld, commitmentNew] = event.args;
// expect(to).to.equal(owner.address);
// expect(amount).to.equal(amountToWithdraw);
// expect(commitmentIdOld).to.equal(startCommId);
// expect(commitmentOld.toString()).to.equal(startComm.toString());
// expect(commitmentNew.toString()).to.equal(outpComm.toString());
// });
// });
});
\ No newline at end of file
This diff is collapsed.
const { ethers } = require('hardhat');
const { expect } = require("chai");
const {BigNumber, utils} = require('ethers');
const elliptic = require('elliptic');
const ec = new elliptic.ec('secp256k1');
const abiCoder = new utils.AbiCoder();
describe ("Schnorr signature", () => {
let SchnorrSignature;
let schnorrSignature;
let p = BigNumber.from(ec.n.toString());
let r = BigNumber.from(utils.randomBytes(10));
let ecG =
[
"55066263022277343669578718895168534326250603453777594175500187360389116729240",
"32670510020758816978083085130507043184471273380659243275938904335757337482424"
];
let key = ec.genKeyPair();
let ecX = [key.getPublic().x.toString(), key.getPublic().y.toString()]; //publ_key
let x = BigNumber.from(key.getPrivate().toString()); //priv_key
let m = "message";
before (async () => {
SchnorrSignature = await ethers.getContractFactory("SchnorrSignature");
schnorrSignature = await SchnorrSignature.deploy();
await schnorrSignature.deployed();
});
it("Should be valid schnorr signature with random generated parametrs.", async () =>
{
//R = r*G
ecR = await schnorrSignature.eMul(r.toString(), ecG[0], ecG[1]);
ecR = [ecR[0].toString(), ecR[1].toString()];
// c = H(X, R, m)
c = BigNumber.from(utils.soliditySha256 // = encodePacked
(
["uint", "uint", "uint", "uint", "string"],
[ecX[0], ecX[1], ecR[0], ecR[1], m]
)
);
// s = r + c*x mod p
s = BigNumber.from(r).add(c.mul(x).mod(p)).mod(p);
expect(await schnorrSignature.SchnorrSignatureVerify(m, ecX, ecR, s)).to.equal(true);
});
it ("Should be invalid with a point not eliptic curve", async () => {
let ecFalse = [
"94550510891201125443038496712355512193683746567194802595430747803790456451800",
"100658497413695518730530153947607016384927173293555182183697767262819897530139"
];
let strToHash = abiCoder.encode(["uint", "uint", "uint", "uint", "string"],[ecX[0], ecX[1], ecFalse[0], ecFalse[1], m]);
c = BigNumber.from(utils.sha256(strToHash));
s = r.add(c.mul(x).mod(p)).mod(p);
await expect(
schnorrSignature.SchnorrSignatureVerify(m, ecX, ecFalse, s.toString())
).to.be.revertedWith("Invalid input parametrs to verify the Schnorr signature");
});
it("Should be invalid with false parametrs", async () => {
ecR = await schnorrSignature.eMul(r.toString(), ecG[0], ecG[1]);
ecR = [ecR[0].toString(), ecR[1].toString()];
let strToHash = abiCoder.encode(["uint", "uint", "uint", "uint", "string"],[ecX[0], ecX[1], ecR[0], ecR[1], m]);
c = BigNumber.from(utils.sha256(strToHash));
let sFalse = r.add(c.sub(x).mod(p)).mod(p);
expect(await schnorrSignature.SchnorrSignatureVerify(m, ecX, ecR, sFalse.toString())).to.equal(false);
});
it("Should be valid schnorr signature with client parametrs.", async () =>
{
[m, ecX, ecR, s] = [
"message",
[
"27986936006637619234751661742996901780685381967893915018229061434120181515483",
"70636065538896624272916187918422091966493606259112919971149037509751154464577"
],
[
"74580538217015729279249478019311517768049978358848055562761510406264032602856",
"109508452562322270947608736702231209675885566789465297663534653691453100979389"
],
"100046075731079052227894073046844253882820832097826080819312866318004576931689"
];
expect(await schnorrSignature.SchnorrSignatureVerify(m, ecX, ecR, s)).to.equal(true);
});
});
\ No newline at end of file
{
"compilerType": "solcjs",
"compilerVersion": "0.8.3",
"sourceDirectory": "./contracts",
"outputDirectory": "./build"
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment