crypto#randomFillSync TypeScript Examples

The following examples show how to use crypto#randomFillSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sampleGroupElement.ts    From hoprnet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Samples a valid exponent and returns the exponent
 * and the product of exponent and base-point.
 * @dev can be used to derive a secp256k1 keypair
 * @returns [ exponent, groupElement]
 */
export function sampleGroupElement(compressed: boolean = false): [exponent: Uint8Array, groupElement: Uint8Array] {
  let exponent = new Uint8Array(SECP256K1_CONSTANTS.PRIVATE_KEY_LENGTH)
  let groupElement: Uint8Array

  do {
    randomFillSync(exponent, 0, SECP256K1_CONSTANTS.PRIVATE_KEY_LENGTH)

    if (!privateKeyVerify(exponent)) {
      continue
    }

    groupElement = publicKeyCreate(exponent, false)
  } while (!publicKeyVerify(groupElement))

  if (compressed) {
    return [exponent, publicKeyConvert(groupElement)]
  } else {
    return [exponent, groupElement]
  }
}
Example #2
Source File: routingInfo.ts    From hoprnet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates the routing information of the mixnet packet
 * @param maxHops maximal number of hops
 * @param path IDs of the nodes along the path
 * @param secrets shared secrets with the nodes along the path
 * @param additionalDataRelayer additional data for each relayer
 * @param additionalDataLastHop additional data for the final recipient
 * @returns bytestring containing the routing information, and the
 * authentication tag
 */
export function createRoutingInfo(
  maxHops: number,
  path: PeerId[],
  secrets: Uint8Array[],
  additionalDataRelayerLength: number,
  additionalDataRelayer: Uint8Array[],
  additionalDataLastHop?: Uint8Array
): { routingInformation: Uint8Array; mac: Uint8Array } {
  if (
    secrets.some((s) => s.length != SECRET_LENGTH) ||
    additionalDataRelayer.some((r) => r.length != additionalDataRelayerLength) ||
    secrets.length > maxHops
  ) {
    throw Error(`Invalid arguments`)
  }

  const routingInfoLength = additionalDataRelayerLength + MAC_LENGTH + SECP256K1_CONSTANTS.COMPRESSED_PUBLIC_KEY_LENGTH
  const lastHopLength = (additionalDataLastHop?.length ?? 0) + END_PREFIX_LENGTH

  const headerLength = lastHopLength + (maxHops - 1) * routingInfoLength
  const extendedHeaderLength = lastHopLength + maxHops * routingInfoLength

  const extendedHeader = new Uint8Array(extendedHeaderLength)

  let mac: Uint8Array

  for (let index = 0; index < secrets.length; index++) {
    const invIndex = secrets.length - index - 1
    const secret = secrets[invIndex]
    const params = derivePRGParameters(secret)

    if (index == 0) {
      extendedHeader[0] = END_PREFIX

      if (additionalDataLastHop?.length > 0) {
        extendedHeader.set(additionalDataLastHop, END_PREFIX_LENGTH)
      }

      const paddingLength = (maxHops - secrets.length) * routingInfoLength

      if (paddingLength > 0) {
        randomFillSync(extendedHeader, lastHopLength, paddingLength)
      }

      u8aXOR(
        true,
        extendedHeader.subarray(0, lastHopLength + paddingLength),
        PRG.createPRG(params).digest(0, lastHopLength + paddingLength)
      )

      if (secrets.length > 1) {
        extendedHeader.set(
          generateFiller(maxHops, routingInfoLength, lastHopLength, secrets),
          lastHopLength + paddingLength
        )
      }
    } else {
      extendedHeader.copyWithin(routingInfoLength, 0, headerLength)

      // Add pubkey of next downstream node
      extendedHeader.set(path[invIndex + 1].pubKey.marshal())

      extendedHeader.set(mac, SECP256K1_CONSTANTS.COMPRESSED_PUBLIC_KEY_LENGTH)

      extendedHeader.set(additionalDataRelayer[invIndex], SECP256K1_CONSTANTS.COMPRESSED_PUBLIC_KEY_LENGTH + MAC_LENGTH)

      u8aXOR(true, extendedHeader, PRG.createPRG(params).digest(0, extendedHeaderLength))
    }

    mac = createMAC(secret, extendedHeader.subarray(0, headerLength))
  }

  return { routingInformation: extendedHeader.slice(0, headerLength), mac }
}