-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c861663
commit 59a8623
Showing
1 changed file
with
35 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
function uint64ToBigEndian(value: number): Uint8Array { | ||
const buffer = new ArrayBuffer(8); | ||
const view = new DataView(buffer); | ||
view.setUint32(0, Math.floor(value / 0x100000000)); | ||
view.setUint32(4, value % 0x100000000); | ||
return new Uint8Array(buffer); | ||
const buffer = new ArrayBuffer(8); | ||
const view = new DataView(buffer); | ||
view.setUint32(0, Math.floor(value / 0x100000000)); | ||
view.setUint32(4, value % 0x100000000); | ||
return new Uint8Array(buffer); | ||
} | ||
function byteToHex(byte: number): string { | ||
return byte.toString(16).padStart(2, '0'); | ||
return byte.toString(16).padStart(2, '0'); | ||
} | ||
|
||
function uint16ToBigEndian(value: number): Uint8Array { | ||
const buffer = new ArrayBuffer(2); | ||
const view = new DataView(buffer); | ||
view.setUint16(0, value); | ||
return new Uint8Array(buffer); | ||
const buffer = new ArrayBuffer(2); | ||
const view = new DataView(buffer); | ||
view.setUint16(0, value); | ||
return new Uint8Array(buffer); | ||
} | ||
|
||
export function prepareKeyset(publicKeys: string[], assumedHonest: number): string { | ||
const numberOfMembers = publicKeys.length; | ||
const membersBuffer: Uint8Array[] = []; | ||
const numberOfMembers = publicKeys.length; | ||
const membersBuffer: Uint8Array[] = []; | ||
|
||
// Encode assumed-honest and number of committee members | ||
membersBuffer.push(uint64ToBigEndian(assumedHonest)); | ||
membersBuffer.push(uint64ToBigEndian(numberOfMembers)); | ||
// Encode assumed-honest and number of committee members | ||
membersBuffer.push(uint64ToBigEndian(assumedHonest)); | ||
membersBuffer.push(uint64ToBigEndian(numberOfMembers)); | ||
|
||
for (const key of publicKeys) { | ||
// Decode the base64 public key to get its hexadecimal representation | ||
const keyHex = Buffer.from(key, 'base64').toString('hex'); | ||
for (const key of publicKeys) { | ||
// Decode the base64 public key to get its hexadecimal representation | ||
const keyHex = Buffer.from(key, 'base64').toString('hex'); | ||
|
||
// Calculate the length of the public key in bytes (half the number of characters of its hexadecimal representation) | ||
const keyLength = keyHex.length / 2; | ||
// Calculate the length of the public key in bytes (half the number of characters of its hexadecimal representation) | ||
const keyLength = keyHex.length / 2; | ||
|
||
// Encode the length as uint16 in big endian | ||
membersBuffer.push(uint16ToBigEndian(keyLength)); | ||
// Encode the length as uint16 in big endian | ||
membersBuffer.push(uint16ToBigEndian(keyLength)); | ||
|
||
// Convert the hexadecimal public key to a byte array and add it to the buffer | ||
const keyBytes = Uint8Array.from(Buffer.from(keyHex, 'hex')); | ||
membersBuffer.push(keyBytes); | ||
} | ||
// Convert the hexadecimal public key to a byte array and add it to the buffer | ||
const keyBytes = Uint8Array.from(Buffer.from(keyHex, 'hex')); | ||
membersBuffer.push(keyBytes); | ||
} | ||
|
||
// Concatenate all Uint8Array elements into a single Uint8Array | ||
const totalLength = membersBuffer.reduce((acc, arr) => acc + arr.length, 0); | ||
const result = new Uint8Array(totalLength); | ||
let offset = 0; | ||
// Concatenate all Uint8Array elements into a single Uint8Array | ||
const totalLength = membersBuffer.reduce((acc, arr) => acc + arr.length, 0); | ||
const result = new Uint8Array(totalLength); | ||
let offset = 0; | ||
|
||
for (const arr of membersBuffer) { | ||
result.set(arr, offset); | ||
offset += arr.length; | ||
} | ||
for (const arr of membersBuffer) { | ||
result.set(arr, offset); | ||
offset += arr.length; | ||
} | ||
|
||
return Array.from(result).map(byteToHex).join(''); | ||
return Array.from(result).map(byteToHex).join(''); | ||
} |