Skip to content

Commit

Permalink
Fix nonce length generation (MystenLabs#18081)
Browse files Browse the repository at this point in the history
## Description 

Fixes nonce generation sometimes being off by one byte.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
Jordan-Mysten authored Jun 24, 2024
1 parent 61a3b97 commit 06a900c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-ligers-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@mysten/sui': patch
'@mysten/zklogin': patch
---

Fix nonce generation inconsistency
2 changes: 1 addition & 1 deletion sdk/typescript/src/zklogin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

export { getZkLoginSignature, parseZkLoginSignature } from './signature.js';
export { toBigEndianBytes } from './utils.js';
export { toBigEndianBytes, toPaddedBigEndianBytes } from './utils.js';
export { computeZkLoginAddressFromSeed } from './address.js';
export { toZkLoginPublicIdentifier, ZkLoginPublicIdentifier } from './publickey.js';
export type { ZkLoginSignatureInputs } from './bcs.js';
4 changes: 2 additions & 2 deletions sdk/zklogin/src/nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { toHEX } from '@mysten/bcs';
import type { PublicKey } from '@mysten/sui/cryptography';
import { toBigEndianBytes } from '@mysten/sui/zklogin';
import { toPaddedBigEndianBytes } from '@mysten/sui/zklogin';
import { randomBytes } from '@noble/hashes/utils';
import { base64url } from 'jose';

Expand All @@ -29,7 +29,7 @@ export function generateNonce(publicKey: PublicKey, maxEpoch: number, randomness
const eph_public_key_0 = publicKeyBytes / 2n ** 128n;
const eph_public_key_1 = publicKeyBytes % 2n ** 128n;
const bigNum = poseidonHash([eph_public_key_0, eph_public_key_1, maxEpoch, BigInt(randomness)]);
const Z = toBigEndianBytes(bigNum, 20);
const Z = toPaddedBigEndianBytes(bigNum, 20);
const nonce = base64url.encode(Z);
if (nonce.length !== NONCE_LENGTH) {
throw new Error(`Length of nonce ${nonce} (${nonce.length}) is not equal to ${NONCE_LENGTH}`);
Expand Down

0 comments on commit 06a900c

Please sign in to comment.