Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pedersen in typescript. #3111

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"@koa/cors": "^4.0.0",
"@noble/curves": "^1.2.0",
"debug": "^4.3.4",
"detect-node": "^2.1.0",
"hash.js": "^1.1.7",
Expand Down
17 changes: 17 additions & 0 deletions yarn-project/foundation/src/crypto/pedersen/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { toBufferBE } from '../../bigint-buffer/index.js';
import { pedersenCommit, pedersenHashWithHashIndex } from './index.js';

describe('pedersen', () => {
it('pedersen commit', () => {
const r = pedersenCommit([toBufferBE(1n, 32), toBufferBE(1n, 32)]);
expect(r).toEqual([
Buffer.from('2f7a8f9a6c96926682205fb73ee43215bf13523c19d7afe36f12760266cdfe15', 'hex'),
Buffer.from('01916b316adbbf0e10e39b18c1d24b33ec84b46daddf72f43878bcc92b6057e6', 'hex'),
]);
});

it('pedersen hash', () => {
const r = pedersenHashWithHashIndex([toBufferBE(1n, 32), toBufferBE(1n, 32)]);
expect(r).toEqual(Buffer.from('07ebfbf4df29888c6cd6dca13d4bb9d1a923013ddbbcbdc3378ab8845463297b', 'hex'));
});
});
314 changes: 314 additions & 0 deletions yarn-project/foundation/src/crypto/pedersen/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
/* cSpell:disable */
import { Field } from '@noble/curves/abstract/modular';
import { weierstrassPoints } from '@noble/curves/abstract/weierstrass';

import { toBufferBE } from '../../bigint-buffer/index.js';

const grumpkin = weierstrassPoints({
a: 0n,
b: 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffff0n,
Fp: Field(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n),
n: 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n,
h: 1n,
Gx: 1n,
Gy: 0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272cn,
});

const defaultGenerators = [
[
0x083e7911d835097629f0067531fc15cafd79a89beecb39903f69572c636f4a5an,
0x1a7f5efaad7f315c25a918f30cc8d7333fccab7ad7c90f14de81bcc528f9935dn,
],
[
0x054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402n,
0x209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126n,
],
[
0x1c44f2a5207c81c28a8321a5815ce8b1311024bbed131819bbdaf5a2ada84748n,
0x03aaee36e6422a1d0191632ac6599ae9eba5ac2c17a8c920aa3caf8b89c5f8a8n,
],
[
0x26d8b1160c6821a30c65f6cb47124afe01c29f4338f44d4a12c9fccf22fb6fb2n,
0x05c70c3b9c0d25a4c100e3a27bf3cc375f8af8cdd9498ec4089a823d7464caffn,
],
[
0x20ed9c6a1d27271c4498bfce0578d59db1adbeaa8734f7facc097b9b994fcf6en,
0x29cd7d370938b358c62c4a00f73a0d10aba7e5aaa04704a0713f891ebeb92371n,
],
[
0x0224a8abc6c8b8d50373d64cd2a1ab1567bf372b3b1f7b861d7f01257052d383n,
0x2358629b90eafb299d6650a311e79914b0215eb0a790810b26da5a826726d711n,
],
[
0x0f106f6d46bc904a5290542490b2f238775ff3c445b2f8f704c466655f460a2an,
0x29ab84d472f1d33f42fe09c47b8f7710f01920d6155250126731e486877bcf27n,
],
[
0x0298f2e42249f0519c8a8abd91567ebe016e480f219b8c19461d6a595cc33696n,
0x035bec4b8520a4ece27bd5aafabee3dfe1390d7439c419a8c55aceb207aac83bn,
],
[
0x2c9628479de4181ea77e7b0913ccf41d2a74155b1d9c82eaa220c218781f6f3bn,
0x278f86b8fd95520b5da23bee1a5e354dc5dcb0cb43d6b76e628ddbffb101d776n,
],
[
0x0be1916f382e3532aa53a766fe74b1a983784caab90290aea7bf616bc371fb41n,
0x0f65545005e896f14249956344faf9addd762b7573a487b58f805a361d920a20n,
],
[
0x29ff8437ae5bec89981441b23036a22b7fd5bee9eff0e83c0dd5b87bfb5bd60en,
0x1fd247352b77e2676b22db23cf7cd482474f543e3480b5a39c42f839a306be10n,
],
[
0x2f3bd4e98f8c8458cd58888749f0f5e582a43565767398e08e50e94b9b19a4d9n,
0x1f534906d1aa8b4ba74ad9e3f85ae3f8295e51eaafd15b5d116801b96360205bn,
],
[
0x27759098f425b76447c2c52728576803a1ac5de37bba875ac47cdcff539ab931n,
0x0aa47ee64d12d856cfb81b595c1d60ceecb693f0fdae644746ff333e39f61db7n,
],
[
0x015ca8d68616fde86c9108e3db04f588e0f308e60d367e963b7d460fe9a65e6cn,
0x2cf918009dda942ac9d59903cd2d0294d8738f938b1394170d892a027d0f347bn,
],
[
0x0d1783d5b256765515f3c9988df9f1ba7e6f5fb0248c8971fbc503ffd5187714n,
0x2ebb434ff4857fc3621f3bc3c6b8002b17d02d9c204e75f19b8f0b99ea68402cn,
],
[
0x300f20942e37abb19520f931f7bf7c6bbac93e4461a7789677f986e344d1f5e5n,
0x1172a6b674c66bb037109fbb4d93d4ca8054bb9dda08839a6189eb578511116dn,
],
[
0x13a6b381a663aa8a757dd386a3a8947d456874a356f52f8af1a3d84ba1067eaan,
0x224854000fa9c70e873fb14ad7c0d01dcf6df5f30261333d6b63c78a7da5a348n,
],
[
0x2b37dc5eb4c3f8cba62f0085ec131053eae6181a3e73a82e7d79764a023394ccn,
0x27c817c1376228a3a9f5ac6caed57857a3baa580a0c1b70a17f75a6ab7e2377cn,
],
[
0x26ae1cc453dab3f2cf4810cc3fb8f71257383333aa4f3ace8eb69c8ba4fa5da1n,
0x28bfbf80541bde37e9db113ae1eb049cf163ff4ea9654d61f42a95bc2e0b17bdn,
],
[
0x0e29626ab6be74417d08b28c8f8ebf02fd701e28d1d47b1961e716e735e02d12n,
0x119f55d6bffb6bba81779eaf98f310c5661d85397b094ffbddbb41fb264b0876n,
],
[
0x055cda53e781f3eedd760ba2c48c6d36d2a061544c3d33e76100bd4c65e89748n,
0x28391e899d6407e40294faa5e5561ab472e43ea5d54c15343b9a47cb7a80ed09n,
],
[
0x203c402369e8b2bc2c3827bc82a278d32c51dc9ab95e55ce1566e8b625b7bce7n,
0x2bf25525d4a62f1252355559dd496b68ba4d6ea65f4b669bb3da73c42455f0a3n,
],
[
0x2468739cedeabea49dbc46c2356bd5cf341aa4574272ba53a40d8049e5ee949fn,
0x0e8b73bcac6b2ccffaa2e50fce992db11a0b46dcdb8ad4b8325393f903a2543an,
],
[
0x242d8ad05dc6e2a8a6fd636f48eec881e05ee8d2a58e2653173f4954ec867654n,
0x0afe0d7aa588a3dcf791f11fcdf54c1d07cfc805c5ae0b02101faafab9cb55f0n,
],
[
0x05c378d0596af9f9a2471be52b7ad0112d48ef355c372088eb0db86e42887e14n,
0x1ba1f1adb66e67bc88c1b3eafe95b202812803a8b71ca62de9ed06c8b9f3cff3n,
],
[
0x04e5c213eebfffcdb42b6c371e52f0d598646cf0ca2a353b4b99889074c5a2bcn,
0x0ae6442bc73b0f2b2d45829fd8fa321ab6b5eecdd3988a39f0c5850544de5082n,
],
[
0x2c96765650bd7c3bee01114b9097b036e20d5c774afe2ba7d5d6099f498f47fdn,
0x0879c9cc5e7eea97f1f9193e1fd08b8d0390320197dee299af1eb67f92c506a3n,
],
[
0x2113980404df880af7d25a27aec16e61c770619912435b618914da7fa1a88385n,
0x106827183994d8da15037851748b05392620516277bf0000f8b38e97f5db602an,
],
[
0x13b4c5f15cc69af871b91e862db8b0044834c5e8da5a287d0794342bb97ffca6n,
0x17941f13343835b572e18d37ab3897f80e0461f9b72ab61da61927fc980a6bd2n,
],
[
0x085c0f69b19e05986883dcc9d53334bb6799968688935d938cebd4b4455f9187n,
0x242b8a5ab074d09e26427b1b6633a79a82ba41d64c4d658017f3186d6926721bn,
],
[
0x1bb111daaf71e632a4c9244a362bdde16a5efc5e7c7dda42b20f3290c2776d2cn,
0x2b4506bf741b2dc59f71f7036716cac04b6f808b4ca1469eb2fd7bccff5882c0n,
],
[
0x1a76ba8ca6b8e2c65915521b175da98137dfcaf0b5faff1019f37afce77c99ean,
0x1894c8c79e69e52a495bf6fe467aa4f5bdbe606490d6803df325a075c5b0079an,
],
[
0x2fea40a9bfb252ba7e192be641a4051381982719b77d567caee2657f36b701abn,
0x1e80d5ea79b50a722e65ce274b8ba2a133de94b757ef71b452183ee958c620e1n,
],
[
0x217c100e6cd64b68e91fe26e90eda02eaa505859201253bd5e550e0bb8f0f29an,
0x12a5c4fc942353f962bc5b97f36b9a326d5dbb6c505266d2a016415d8da0be65n,
],
[
0x2f50fc293e4529108595329b6e7b0110c0db563927bd57a169e81884535660f9n,
0x03547f619f9c30a761963381fe31f031ecd5b393d2281455a66e01aff0896338n,
],
[
0x06e0f1aab04c530ed9e9584b7c06cb40d6dbd1752313b1bf8498accb8227e743n,
0x2b47e83f03e635a9c20eb0d6412955dec1578a197c7d51f648550623ac5b3baen,
],
[
0x1dee7c6f5d7527d26da506eec28cc3b71acf9a8e694567749dc5e6b073748632n,
0x235955a911e3744859c1db3996aa1f0f64424f67c2ef1c8fcc8c767af5749068n,
],
[
0x1e5aa70095a858d1c4153691e91c19f01a883f422efc19ec766c86bc416ceb2an,
0x0f1e06c820067d8d0af14cf87633c1bd761d65bc5b3889a17bae7d70d2b19f5dn,
],
[
0x2f4d8f60344443350f226f19c4b01e2198f6b475b0291dd2e0c3981e3797a5e0n,
0x1aaab92caf692c5fb89e6eab983f26ebe009463c1ddece0a650508994cb84a82n,
],
[
0x06e0aba4044a552343f7ba02f234dafe4ccca7fd552342a8493ce544758df012n,
0x243269273e7c7ee7a50288f282cb6a7169ec2aac03d2ff7b40463638ad5722d5n,
],
[
0x1aecf2ef53262227e12a9ca29ae7f14be031c03488a7604a03cdc77115f26136n,
0x0753180b7d445c215d55dbfd6ebfb1c6aabd690ac92fd76cea5144ef3c9a8b3fn,
],
[
0x207d50d783c727a979303bc8f50538ad75e5f856180d4f3dc2c7ee186916cef8n,
0x20155253cd087a980aad326564240cca45a72c888e69395e4d8f5ca3d2b96e0an,
],
[
0x21db09b3c2c299f1b0be7a8f3031fd283cd5f1efca0a468b7ea9a0f170e81fc7n,
0x21b50f8107353afc7a1656040c9888ee393608420d88c2a07f34293fb609a26dn,
],
[
0x02190016f4cc44935d100bc7b7dae46cea5922261e9889112cc815fafd5c45ccn,
0x01dfaacfa00ddb9133042d9833d1201172f69a456f0c9483d0b58e5f01b571fdn,
],
[
0x04e67573df0c1591ec51d2f60b74f1964abeef3e201084425a55cd71ffe0b4f8n,
0x1f0285b2ede69aff961919827312afad0753c112cab001cb28ff6c2c27f277c6n,
],
[
0x092450548d189dfe4e852370c17343aeef92ac3a8fbdeb8fdd244f87d3af03cdn,
0x23e3c53d9265d6debfdb40988a908e64673b2f87b1fe2a08b56c6a3fcf84f5d1n,
],
[
0x2935be388ee1365bf6a9cc6fd25d130607835dbf7926c5cafed690b9072cd316n,
0x059edfbc50a9699e29b975a2fc3bfe7855997e708ce82fa30ed19cc91850f6ccn,
],
[
0x21cc891713fb8570152149a9fbb23410d7c42dece266bc6093650e46111a465cn,
0x248f486105ee09f447e7e0ff522b5ff1db5f64522f2bbbb28e7a07595cff3e79n,
],
[
0x147f06fa12cbb8d1c98f9c06d57899e20d9651bb8da104ad5168c9be10d4419fn,
0x2766f677196fb611814f5d7af22aa8555c3c65b572151576b116df5b9f05857dn,
],
[
0x0143beefa7f0b8fb72262a3c3d1502a36d8b4cb35befbb37dba3a70ffe2571c7n,
0x1808ba6988bbb8585b77765760fb2afccdfe4946702043a46b8d461c7d211b11n,
],
[
0x1ac3c1edb1eddd9c5011f88769fac4050aa8820de1f84fcda7e400f87028ba3an,
0x050cbb4da87ecd3f0c84d6252e76e19b3678b955c603f35abdcee3b988a57fa4n,
],
[
0x05a7092c674d8f44add477edf7369dc7bcb9da4d1b903fd58dd8bd4d166de73en,
0x182545ce8ba6fa781823673d50fc62c7d1bb5f3515da6e5c2bdfd7248f311e1bn,
],
[
0x0154a12dbf3fd3ae8957fe154f378630c12306eb55425d9732e0ae2b56ca0306n,
0x140688ffb0de33c9625e323a13e9df6fb37c841b34c4b18b7e19d51792d393ccn,
],
[
0x2fcdf4e4b421286df484fdd08725ffe53728759508f74f0d87e65bd932af28d4n,
0x25f57213413f9f0982573c4a6640efe32b612c6c8f0b027425260c4629d38b21n,
],
[
0x203ed9a7ff1ef21910003e658dbff87a3e10da7782de00af71995c233587961dn,
0x0c7ae4445018e5d451f197d7881b1b95fb66574c26ff6967a37c1eab5f907a08n,
],
[
0x131abdc8d6c6319aeee7e8b186ace3c07d140eb4037b21285b2260cd7bf310f7n,
0x2371e4c2b73ce0af013b30ee9f434584742cdd7e91f1d842c1a6a95bd12d915dn,
],
[
0x124029864085263e79448fdeef91ee2ea5a4952637646b02f132c1ac917edbdan,
0x29d11c62ccfca5d8328e5171466137e6cfd19d56bdc3bbc4633a0aed34a90e69n,
],
[
0x1ded46fcf96cba70ac0cb4ca58348c90041b257232e476acd236b45271413fben,
0x26d04c6660ab9693743bf5c5fcf13047860dfeed3dd903e6ff6fe917fddf73fbn,
],
[
0x2b31b48b7313f6a5c3f1f59fe9ad3d4b8900d873056af1691d143208eb5c8790n,
0x27cec87606fe5f9562a3d0b1fef90da3e8d17721c668c342b681468a501deff3n,
],
[
0x22080b093f3b3c98f9afc52b80594dcf51a714e77c40c3a08513897d73a70806n,
0x1cbcacedf498eaebd03b602c4908c165c50ba184230c7c52244c9a5d0e9759ebn,
],
[
0x0560b3857af53bc7262482310593e316543551ee57417583161bfce9d12a4d40n,
0x2c91b186a9740d4b9f49c8889878381833cc5b5fe29e6e83affa2916e2008988n,
],
[
0x2cf006347b380b6e0eb766ebdd1d24dc1eed9a43c206ee58e9457303d509c308n,
0x03d11688f54726466399ddce8523148453cd1b6cad3cd414ee6e2af66a6b6addn,
],
[
0x2e5c7e4d9a6069892bac121490fee89df1c238c7738c9f3ab599d6b4cc2ff491n,
0x0f3c26f5de9ad898d4a4b1a08d1e87fd5cf4362f9796fb8715c60d431902cd54n,
],
[
0x15298a52895eb8c9399509dc1f0ef68a90afc29084d3a0cec6956c8420819b5cn,
0x2dc0ccd80f1bbbdf364f5b912a961332d9f5db54763745b64e0d85e33faf87c7n,
],
].map(([x, y]) => new grumpkin.ProjectivePoint(x, y, 1n));

const lengthGenerator = new grumpkin.ProjectivePoint(
0x2df8b940e5890e4e1377e05373fae69a1d754f6935e6a780b666947431f2cdcdn,
0x2ecd88d15967bc53b885912e0d16866154acb6aac2d3f85e27ca7eefb2c19083n,
1n,
);

const pointAtInfinity = lengthGenerator.subtract(lengthGenerator);

/**
* Create a pedersen commitment (point) from an array of input fields.
*/
function pedersenCommitInternal(input: Buffer[], generatorOffset = 0) {
if (generatorOffset + input.length > defaultGenerators.length) {
throw new Error('Pedersen commit overflowed default generators.');
}
const generators = defaultGenerators.slice(generatorOffset, generatorOffset + input.length);
let result = pointAtInfinity;
for (let i = 0; i < input.length; ++i) {
const value = BigInt(`0x${input[i].toString('hex')}`);
result = result.add(generators[i].multiply(value));
}
return result;
}

/**
* Create a pedersen commitment (point) from an array of input fields.
*/
export function pedersenCommit(input: Buffer[], generatorOffset = 0) {
const result = pedersenCommitInternal(input, generatorOffset);
return [toBufferBE(result.x, 32), toBufferBE(result.y, 32)];
}

/**
* Create a pedersen hash (field) from an array of input fields.
*/
export function pedersenHashWithHashIndex(input: Buffer[], index = 0) {
const result = lengthGenerator.multiply(BigInt(input.length));
return toBufferBE(result.add(pedersenCommitInternal(input, index)).x, 32);
}
17 changes: 17 additions & 0 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ __metadata:
dependencies:
"@jest/globals": ^29.5.0
"@koa/cors": ^4.0.0
"@noble/curves": ^1.2.0
"@rushstack/eslint-patch": ^1.1.4
"@types/debug": ^4.1.7
"@types/detect-node": ^2.0.0
Expand Down Expand Up @@ -3459,6 +3460,15 @@ __metadata:
languageName: node
linkType: hard

"@noble/curves@npm:^1.2.0":
version: 1.2.0
resolution: "@noble/curves@npm:1.2.0"
dependencies:
"@noble/hashes": 1.3.2
checksum: bb798d7a66d8e43789e93bc3c2ddff91a1e19fdb79a99b86cd98f1e5eff0ee2024a2672902c2576ef3577b6f282f3b5c778bebd55761ddbb30e36bf275e83dd0
languageName: node
linkType: hard

"@noble/hashes@npm:1.3.0":
version: 1.3.0
resolution: "@noble/hashes@npm:1.3.0"
Expand All @@ -3473,6 +3483,13 @@ __metadata:
languageName: node
linkType: hard

"@noble/hashes@npm:1.3.2":
version: 1.3.2
resolution: "@noble/hashes@npm:1.3.2"
checksum: fe23536b436539d13f90e4b9be843cc63b1b17666a07634a2b1259dded6f490be3d050249e6af98076ea8f2ea0d56f578773c2197f2aa0eeaa5fba5bc18ba474
languageName: node
linkType: hard

"@nodelib/fs.scandir@npm:2.1.5":
version: 2.1.5
resolution: "@nodelib/fs.scandir@npm:2.1.5"
Expand Down