Skip to content

Commit

Permalink
Polishing zkLogin SDK (MystenLabs#13435)
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
mskd12 authored Aug 16, 2023
1 parent 7871686 commit f5d5a4e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-seas-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/zklogin': patch
---

Polish utils
2 changes: 2 additions & 0 deletions sdk/zklogin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export { zkBcs } from './bcs.js';
export { poseidonHash } from './poseidon.js';

export { generateNonce } from './nonce.js';

export { convertBase, hashToField, hashASCIIStrToField, genAddressSeed } from './utils.js';
2 changes: 1 addition & 1 deletion sdk/zklogin/src/poseidon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const poseidonNumToHashFN = [
poseidon16,
];

export function poseidonHash(inputs: (string | number | bigint)[]): bigint {
export function poseidonHash(inputs: (number | bigint)[]): bigint {
const hashFN = poseidonNumToHashFN[inputs.length - 1];

if (hashFN) {
Expand Down
40 changes: 22 additions & 18 deletions sdk/zklogin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { poseidonHash } from './poseidon.js';
type bit = 0 | 1;

const MAX_KEY_CLAIM_NAME_LENGTH = 40;
const MAX_KEY_CLAIM_VALUE_LENGTH = 256;
// NOTE: The below param is in flux. It might change in the next few weeks.
const MAX_KEY_CLAIM_VALUE_LENGTH = 200;
const PACK_WIDTH = 248;

// TODO: We need to rewrite this to not depend on Buffer.
Expand All @@ -29,29 +30,32 @@ function chunkArray<T>(arr: T[], chunkSize: number) {
);
}

// Pack into an array of chunks each outWidth bits
function pack(inArr: bigint[], inWidth: number, outWidth: number, outCount: number): bigint[] {
/**
* ConvertBase
* 1. Converts each input element into exactly inWidth bits
* - Prefixing zeroes if needed
* 2. Splits the resulting array into chunks of outWidth bits where
* the last chunk's size is <= outWidth bits.
* 3. Converts each chunk into a bigint
* 4. Returns a vector of size Math.ceil((inArr.length * inWidth) / outWidth)
*/
export function convertBase(inArr: bigint[], inWidth: number, outWidth: number): bigint[] {
const bits = bigintArrayToBitArray(inArr, inWidth);
const extraBits = bits.length % outWidth === 0 ? 0 : outWidth - (bits.length % outWidth);
const bitsPadded = bits.concat(Array(extraBits).fill(0));
if (bitsPadded.length % outWidth !== 0) {
throw new Error('Invalid logic');
}
const packed = chunkArray(bitsPadded, outWidth).map((chunk) => BigInt('0b' + chunk.join('')));
return packed.concat(Array(outCount - packed.length).fill(0));
const packed = chunkArray(bits, outWidth).map((chunk) => BigInt('0b' + chunk.join('')));
return packed;
}

function mapToField(input: bigint[], inWidth: number) {
// hashes a stream of bigints to a field element
export function hashToField(input: bigint[], inWidth: number) {
if (PACK_WIDTH % 8 !== 0) {
throw new Error('PACK_WIDTH must be a multiple of 8');
}
const numElements = Math.ceil((input.length * inWidth) / PACK_WIDTH);
const packed = pack(input, inWidth, PACK_WIDTH, numElements);
const packed = convertBase(input, inWidth, PACK_WIDTH);
return poseidonHash(packed);
}

// Pads a stream of bytes and maps it to a field element
function mapBytesToField(str: string, maxSize: number) {
// hashes an ASCII string to a field element
export function hashASCIIStrToField(str: string, maxSize: number) {
if (str.length > maxSize) {
throw new Error(`String ${str} is longer than ${maxSize} chars`);
}
Expand All @@ -63,13 +67,13 @@ function mapBytesToField(str: string, maxSize: number) {
.split('')
.map((c) => BigInt(c.charCodeAt(0)));

return mapToField(strPadded, 8);
return hashToField(strPadded, 8);
}

export function genAddressSeed(pin: bigint, name: string, value: string) {
return poseidonHash([
mapBytesToField(name, MAX_KEY_CLAIM_NAME_LENGTH),
mapBytesToField(value, MAX_KEY_CLAIM_VALUE_LENGTH),
hashASCIIStrToField(name, MAX_KEY_CLAIM_NAME_LENGTH),
hashASCIIStrToField(value, MAX_KEY_CLAIM_VALUE_LENGTH),
poseidonHash([pin]),
]);
}

0 comments on commit f5d5a4e

Please sign in to comment.