Skip to content

Instantly share code, notes, and snippets.

@neuodev
Last active March 16, 2023 14:15
Show Gist options
  • Save neuodev/df04d1ff944d890d6d6a549a43d436e1 to your computer and use it in GitHub Desktop.
Save neuodev/df04d1ff944d890d6d6a549a43d436e1 to your computer and use it in GitHub Desktop.
EdDSA on Baby JubJub Elliptic Curve using Poseidon hash
const { buildEddsa } = require("circomlibjs"); // ^0.1.7
const { Scalar } = require("ffjavascript"); // ^0.2.57
async function main() {
const eddsa = await buildEddsa();
// EdDSA for KYC
const poseidonHash = eddsa.poseidon([
BigInt("0x830a030549a7E18db3FffE449ECd44A2D51436D2"), // EoA
BigInt(631040090), // KYC rules
BigInt(1678258616460), // Timestamp
BigInt(56), // Chain ID
]);
// EdDSA for KYT
// const poseidonHash = eddsa.poseidon([
// BigInt("0x44448c9bb6805d7c44602854c352f718de389c16"), // Sender
// BigInt("0xad585afee404a055b41e0927d475a744da3ec791"), // Receiver
// BigInt("0x056bc75e2d63100000"), // Amount
// BigInt("0xe3a59d5e33c6540e18aaa46bf98917ac3158db0d"), // Token ID
// BigInt("631050090"), // Rule ID
// BigInt(56), // Chain ID
// ]);
const msg = eddsa.babyJub.F.e(Scalar.fromRprLE(poseidonHash, 0));
const prvKey = Buffer.from(
"0001020304050607080900010203040506070809000102030405060708090001",
"hex"
);
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signPoseidon(prvKey, msg);
const pSignature = eddsa.packSignature(signature);
const uSignature = eddsa.unpackSignature(pSignature);
const isValidSig = eddsa.verifyPoseidon(msg, uSignature, pubKey);
console.log({ isValidSig }); // { isValidSig: true }
}
main()
.then(() => {
process.exit(0);
})
.catch((err) => {
console.log(err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment