PHP Classes

How to Implement PHP Type Safety Using Ristretto PHP: Manipulate values in type safe way using classes

Recommend this page to a friend!

  Author Author  
Picture of Scott Arciszewski
Name: Scott Arciszewski <contact>
Classes: 35 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 27x

Winner: 1x


  Detailed description   Download Download .zip .tar.gz  
This package can manipulate values in type safe way using classes.

It implements a base class named Ristretto that implements essential functions like comparison of values of the same type and hexadecimal encoding of a value.

The package also provides sub-classes that implement specific functions of some data types, like generating random values, value hashing, and arithmetic operations.

Currently, it provides the following data type classes:

- Group element

- Public Key

- Scalar Value

- Secret Key

Details

Ristretto (PHP)

Build Status Latest Stable Version Latest Unstable Version License Downloads

Implements a type-safe API for working with the Ristretto Group in PHP projects.

Requirements

  • PHP 8.1 or newer

Installing

composer require paragonie/ristretto

Documentation

There are two basic types: ScalarValue and GroupElement.

The ScalarValue object wraps a big integer between 0 and the order of the Ristretto Group, L.

The GroupElement object wraps a group element of the Ristretto Group.

If an analogy helps, in the world of Ed25519 and X25519, the ScalarValue is your secret key, and GroupElement is your public key.

For that reason, there are also a SecretKey and PublicKey class, which contains some basic helper methods for ease-of-use.

Usage

You can convert from scalars to group elements with multBase(), and then use scalarPointMultiply() to perform a commutative group action (e.g. Diffie-Hellman).

<?php
use ParagonIE\Ristretto\{GroupElement, ScalarValue};

$aliceSecret = ScalarValue::random();
$alicePublic = $aliceSecret->multBase();
$bobSecret = ScalarValue::random();
$bobPublic = $bobSecret->multBase();

// You can perform a similar commutative group action
$aliceToBob = $aliceSecret->scalarPointMultiply($bobPublic);
$bobToAlice = $bobSecret->scalarPointMultiply($alicePublic);
var_dump($aliceToBob->equals($bobToAlice)); // bool(true)

Otherwise, most operations are within a given type (GroupElement to GroupElement, ScalarValue to ScalarValue).

GroupElement

<?php
use ParagonIE\Ristretto\{GroupElement};

$x = GroupElement::random();
$y = GroupElement::random();

$z = $x->add($y);
$w = $z->sub($y);
var_dump($w->equals($x)); // bool(true)

ScalarValue

Example

This is a PHP implementation of the libsodium example protocol.

> Perform a secure two-party computation of f(x) = p(x)^k. x is the input sent to the second party > by the first party after blinding it using a random invertible scalar r, and k is a secret key > only known by the second party. p(x) is a hash-to-group function.

<?php
use ParagonIE\Ristretto\{GroupElement};

// -------- First party -------- Send blinded p(x)
$x = random_bytes(64);

// Compute px = p(x), a group element derived from x
$px = GroupElement::fromHash($x);

// Compute a = p(x) * g^r
$r = ScalarValue::random();
$gr = $r->multBase();
$a = $px->add($gr);

// -------- Second party -------- Send g^k and a^k
$k = ScalarValue::random();

// Compute v = g^k
$v = $k->multBase();

// Compute b = a^k
$b = $k->scalarPointMultiply($a);

// -------- First party -------- Unblind f(x)
// Compute vir = v^(-r)
$ir = $r->negate();
$vir = $v->scalarPointMultiply($ir);

// Compute f(x) = b v^(-r) = (p(x) g^r)^k * (g^k)^(-r)
//              = (p(x) g)^k g^(-k) = p(x)^k
$fx = $b->add($vir);

// --------- Correctness testing -----------
// If you knew both p(x) and k, you could calculate it directly.

// Directly calculate p(x)^k with both parties' secrets
$pxk = $px->scalarPointMultiply($k);
var_dump($fx->equals($pxk)); // bool(true)


  Classes of Scott Arciszewski  >  How to Implement PHP Type Safety Using Ristretto PHP  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog (1)  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: How to Implement PHP Type Safety Using Ristretto PHP
Base name: ristretto-php
Description: Manipulate values in type safe way using classes
Version: -
PHP version: 5
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Cryptography Encrypting, decrypting and hashing data View top rated classes
Group folder image Data types Modeling and manipulating data types View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (5 files)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (2 files)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Data Auxiliary data
  Accessible without login Plain text file psalm.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Accessible without login Plain text file GroupElement.php Class Class source
  Accessible without login Plain text file PublicKey.php Class Class source
  Accessible without login Plain text file Ristretto.php Class Class source
  Accessible without login Plain text file ScalarValue.php Class Class source
  Accessible without login Plain text file SecretKey.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file RistrettoTest.php Class Class source

Download Download all files: ristretto-php.tar.gz ristretto-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.