index

@buff-beacon-project/curby-client • Docs


@buff-beacon-project/curby-client

This is the official client library for the CURBy Project. It fetches and validates both the regular randomness chain and the DIRNG chain.

Example

import { Client, DIRNGClient } from '@buff-beacon-project/curby-client'

const client = Client.create()
const randomness = await client.randomness()
const myArray = [1, 2, 3, 4, 5]
const shuffled = randomness.shuffled(myArray)
// shuffled = [3, 4, 2, 5, 1]

const dirng = DIRNGClient.create()
const latest = await dirng.latest()
console.log(`Got info for round ${latest.round}. Stage is ${latest.stage}`)

Using random bits

The randomness returned by this library is a ByteHelper object. It provides many utility functions for working with random bits such as ByteHelper.shuffled to shuffle lists.

There is also an acompaning library, rand-utils that provides more utilities for working with random bits. It is particularly useful for getting unbiased random integers from a bitstream.

When using public randomness for the purpose of transparency, such as in a lottery, it is important to first publically commit to using a future round number. This prevents the operator from retroactively choosing a round that benefits them. Additionally, the operator should commit to the specific algorithm that is using the randomness.

A simple example of this would be to use a git commit hash of code to serve as the algorithm. The code should explicitly state the chainCid, round number, and be fully deterministic given the initial seed. There should also be a clear explanation of what to do in the event of an error.

How to use this library

The CURBy client library provides two main classes, Client (for classical randomness) and DIRNGClient (for Quantum DIRNG randomness).

Classical Randomness

The Client class is used to fetch randomness from the main classical CURBy chain which produces randomness every minute. The latest randomness can be fetched by simply calling the Client.randomness method. The client library checks the following when fetching randomness:

  • The chain has a valid signature

  • The pulse and previous pulse have valid signatures

  • The pulse index matches the expected index (round)

  • The randomness has a valid precommitment value

For more information on the randomness generation process, see the CURBy-RNG Chain reference.

Quantum DIRNG Randomness

The DIRNGClient class is used to fetch randomness from the Quantum DIRNG chain, CURBY-Q. The latest randomness can be fetched by calling the DIRNGClient.randomness method and to get randomness from a specific round, you can instead call DIRNGClient.fetchRound and use the .randomness property of the returned object, which is a ByteHelper object.

The client library checks the following when fetching randomness:

  • The chain has a valid signature

  • All pulses have valid signatures

  • The response pulse from the bell data chain has correct causal ordering with the CURBY-Q pulses.

  • The seed pulse on the seed chain has correct causal ordering with the CURBY-Q pulses.

  • The seed was correctly expanded to the correct number of bits. (NOTE: this requires the setting validateSeed to be true)

What is not validated, due to technical limitations:

  • The DIRNG certificate, and randomness extraction.

In order to validate the randomness, a separate utility is required. See [curby_verify docker image)[https://github.com/buff-beacon-project/curby_verify] for more information.

See

https://github.com/buff-beacon-project/curby_verify

Type Aliases

Type alias
Description

A type for arrays of numbers

The options for waiting

Classical RNG

Class, Type alias
Description

A client for fetching randomness from the CURBy RNG chain

The options for the Rng Client

A latest pulse pair

A pair of sequential pulses

A randomness round

Constants

Variable
Description

A constant containing the chain CIDs

The base URL for the CURBy API

DIRNG

Class, Type alias, Function
Description

A client for the Device Independent Randomness Generation (DIRNG) chain

The options for the DIRNG client

A DIRNG round's data

A round's validations

Convert pulses to round data

Add validations to a round

Errors

Class
Description

An error for when a precommitment value is invalid

Utilities

Interface, Function
Description

A helper for working with randomness.

Create a ByteHelper from a set of bytes and a timestamp

Extract randomness from a pulse on an RNG chain

Get the precommitment value for a pulse

Time until next rng pulse in milliseconds

Wait a specified number of milliseconds

Perform an XOR operation on two arrays

zip the provided arrays together

Validations

Type alias, Function
Description

A validation result

Check if a precommitment value is valid

Validate a bell response pulse

Validate the seed value

Validate the ordering of the seed pulse

Last updated