Giter Club home page Giter Club logo

csprng's Introduction

PyTorch/CSPRNG

IMPORTANT NOTE: this is a fork of the original pytorch/csprng repo with some new useful features (in particular, reproducible CSPRNG).

This library has not received an audit, and you should think carefully before using it in a production system.

Installation

This fork of torchcsprng requires libsodium to work correctly; you should install it before installing this library.

Once you have libsodium installed, run:

pip install git+https://github.com/kernelmethod/csprng.git@main

Alternatively, add the following to your requirements.txt:

torchcsprng @ git+https://github.com/kernelmethod/csprng.git@main

Usage

This library exports one function, create_generator, which creates a new PyTorch Generator object with cryptographically secure pseudo-random number generation (CSPRNG).

>>> import torchcsprng as csprng
>>> gen = csprng.create_generator()

Note that the current implementation uses JIT-compiled extensions, so it may take a moment to compile the module the first time that you call it.

Once you've created your Generator instance, you can feed it to PyTorch's RNG functions, e.g.

>>> import torch
>>> torch.randn(3, 3, generator=gen)
tensor([[ 0.5859, -0.6674, -0.2723],
        [ 0.9075,  2.0364,  0.0331],
        [-0.2692, -1.1712, -1.5100]])

When you call create_generator without any arguments, it will automatically generate an secret key from the operating system's random stream. You can explicitly generate a key and pass it in to PyTorch in order to make RNG deterministic:

>>> import secrets
>>> key_bytes = secrets.token_bytes(32)
>>> key = torch.tensor([b for b in key_bytes], dtype=torch.uint8)
>>> gen = csprng.create_generator(key)

csprng's People

Contributors

pbelevich avatar kernelmethod avatar jspisak avatar seemethere avatar facebook-github-bot avatar amyreese avatar kumatea avatar malfet avatar lqf96 avatar smessmer avatar thatch avatar timrc avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.