-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export poseidon2_permutation and add to foundation/crypto
- Loading branch information
Showing
11 changed files
with
125 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Fr } from '../../fields/fields.js'; | ||
import { poseidon2Permutation } from './index.js'; | ||
|
||
describe('poseidon2Permutation', () => { | ||
it('test vectors from cpp should match', () => { | ||
const init = [0, 1, 2, 3].map(i => new Fr(i)); | ||
expect(poseidon2Permutation(init)).toEqual([ | ||
new Fr(0x01bd538c2ee014ed5141b29e9ae240bf8db3fe5b9a38629a9647cf8d76c01737n), | ||
new Fr(0x239b62e7db98aa3a2a8f6a0d2fa1709e7a35959aa6c7034814d9daa90cbac662n), | ||
new Fr(0x04cbb44c61d928ed06808456bf758cbf0c18d1e15a7b6dbc8245fa7515d5e3cbn), | ||
new Fr(0x2e11c5cff2a22c64d01304b778d78f6998eff1ab73163a35603f54794c30847an), | ||
]); | ||
}); | ||
|
||
it('test vectors from Noir should match', () => { | ||
const init = [1n, 2n, 3n, 0x0a0000000000000000n].map(i => new Fr(i)); | ||
expect(poseidon2Permutation(init)).toEqual([ | ||
new Fr(0x0369007aa630f5dfa386641b15416ecb16fb1a6f45b1acb903cb986b221a891cn), | ||
new Fr(0x1919fd474b4e2e0f8e0cf8ca98ef285675781cbd31aa4807435385d28e4c02a5n), | ||
new Fr(0x0810e7e9a1c236aae4ebff7d3751d9f7346dc443d1de863977d2b81fe8c557f4n), | ||
new Fr(0x1f4a188575e29985b6f8ad03afc1f0759488f8835aafb6e19e06160fb64d3d4an), | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js'; | ||
|
||
import { strict as assert } from 'assert'; | ||
|
||
import { Fr } from '../../fields/fields.js'; | ||
|
||
/** | ||
* Create a poseidon hash (field) from an array of input fields. | ||
* Left pads any inputs less than 32 bytes. | ||
*/ | ||
export function poseidonHash(input: Buffer[]): Fr { | ||
export function poseidon2Hash(input: Buffer[]): Fr { | ||
return Fr.fromBuffer( | ||
Buffer.from( | ||
BarretenbergSync.getSingleton() | ||
.poseidonHash(input.map(i => new FrBarretenberg(i))) | ||
.poseidon2Hash(input.map(i => new FrBarretenberg(i))) | ||
.toBuffer(), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Runs a Poseidon2 permutation. | ||
* @param input the input state. Expected to be of size 4. | ||
* @returns the output state, size 4. | ||
*/ | ||
export function poseidon2Permutation(input: Fr[]): Fr[] { | ||
assert(input.length === 4, 'Input state must be of size 4'); | ||
const res = BarretenbergSync.getSingleton().poseidon2Permutation(input.map(i => new FrBarretenberg(i.toBuffer()))); | ||
assert(res.length === 4, 'Output state must be of size 4'); | ||
return res.map(o => Fr.fromBuffer(Buffer.from(o.toBuffer()))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters