Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MRP_01: MariaDB Ristretto255 Plugin

This package provides all the functionalities of libsodium ristretto255 methods in the form of user-defined functions (UDFs) for MySQL.

Installation

Install the mrp_01 package:

yum install mrp_01.rpm

Install the user defined functions via the included SQL script:

mariadb < /opt/teragrep/mrp_01/share/install_ristretto.sql

Or alternatively, install the user defined functions manually via queries:

USE mysql;

DROP FUNCTION IF EXISTS ristretto255_is_valid_point;
CREATE FUNCTION ristretto255_is_valid_point RETURNS INTEGER SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_random;
CREATE FUNCTION ristretto255_random RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_from_hash;
CREATE FUNCTION ristretto255_from_hash RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS scalarmult_ristretto255;
CREATE FUNCTION scalarmult_ristretto255 RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS scalarmult_ristretto255_base;
CREATE FUNCTION scalarmult_ristretto255_base RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_add;
CREATE FUNCTION ristretto255_add RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_sub;
CREATE FUNCTION ristretto255_sub RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_random;
CREATE FUNCTION ristretto255_scalar_random RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_reduce;
CREATE FUNCTION ristretto255_scalar_reduce RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_invert;
CREATE FUNCTION ristretto255_scalar_invert RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_negate;
CREATE FUNCTION ristretto255_scalar_negate RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_complement;
CREATE FUNCTION ristretto255_scalar_complement RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_add;
CREATE FUNCTION ristretto255_scalar_add RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_sub;
CREATE FUNCTION ristretto255_scalar_sub RETURNS STRING SONAME 'lib_sodium_ristretto.so';
DROP FUNCTION IF EXISTS ristretto255_scalar_mul;
CREATE FUNCTION ristretto255_scalar_mul RETURNS STRING SONAME 'lib_sodium_ristretto.so';

SQL Functions

Encoded element validation

ristretto255_is_valid_point(BINARY(32) p)

The function checks that the input argument p is a valid ristretto255-encoded element. Returns 1 on success, 0 if the check didn’t pass.

Random group element

ristretto255_random()

The function returns a random ristretto255 group element in binary string format.

Hash-to-group

ristretto255_from_hash(BINARY(64) a)

The function maps a 64 bytes vector that given as input argument a to a ristretto255 group element and returns it.

Scalar multiplication

scalarmult_ristretto255(BINARY(32) a, BINARY(32) b);

The function multiplies a ristretto element b by a scalar a and returns the resulting element.

scalarmult_ristretto255_base(BINARY(32) a);

The function multiplies the randomly generated ristretto element by a scalar a and returns the resulting element.

Element addition and subtraction

ristretto255_add(BINARY(32) a, BINARY(32) b)

The function adds the ristretto element a to ristretto element b and returns the resulting element.

ristretto255_sub(BINARY(32) a, BINARY(32) b)

The function subtracts the ristretto element a to the ristretto element b and returns the resulting element.

Scalar arithmetic over L

The ristrettoscalar*() function set operates over scalars in the [0..L[ interval, L being the order of the ristretto255 group: (2^252 + 27742317777372353535851937790883648493).

ristretto255_scalar_random()

The function returns a random scalar in binary string format.

ristretto255_scalar_reduce(BINARY(64) s)

The function reduces a larger scalar value s to s mod L and returns the resulting 32 byte integer in binary string format.

ristretto255_scalar_invert(BINARY(32) a)

The function computes the multiplicative inverse of a over L and returns the result in binary string format.

ristretto255_scalar_negate(BINARY(32) a)

The function returns neg so that a + neg = 0 (mod L).

ristretto255_scalar_complement(BINARY(32) a)

The function returns comp so that a + comp = 1 (mod L).

ristretto255_scalar_add(BINARY(32) a, BINARY(32) b)

The function returns the result of a + b (mod L).

ristretto255_scalar_sub(BINARY(32) a, BINARY(32) b)

The function returns the result of a - b (mod L).

ristretto255_scalar_mul(BINARY(32) a, BINARY(32) b)

The function returns the result of a * b (mod L).

Query examples

A short introduction on how to use the new user defined functions in SQL queries.

Random group element

SELECT ristretto255_random();

Hash-to-group

# The SHA2() function produces a nonbinary string, UNHEX() is used to convert it to binary string.
SELECT ristretto255_from_hash(UNHEX(SHA2('Maria', 512)));

Encoded element validation

SELECT ristretto255_is_valid_point(ristretto255_from_hash(UNHEX(SHA2('Maria', 512))));
SELECT ristretto255_is_valid_point(0x1077D7247296C4E9364498EC436E3A9C9F7CFAC6AC08FEC11C351AEC4E4C1A45);

Scalar multiplication

SELECT scalarmult_ristretto255(0x34ABF3B816C051FE1BA7315760B5854800959D776AF87ACF540CC55F3701A905, 0x1077D7247296C4E9364498EC436E3A9C9F7CFAC6AC08FEC11C351AEC4E4C1A45);
SELECT scalarmult_ristretto255_base(0x34ABF3B816C051FE1BA7315760B5854800959D776AF87ACF540CC55F3701A905);

Element addition and subtraction

SELECT ristretto255_add(0x1077D7247296C4E9364498EC436E3A9C9F7CFAC6AC08FEC11C351AEC4E4C1A45, 0x1077D7247296C4E9364498EC436E3A9C9F7CFAC6AC08FEC11C351AEC4E4C1A45);
SELECT ristretto255_sub(0x1077D7247296C4E9364498EC436E3A9C9F7CFAC6AC08FEC11C351AEC4E4C1A45, 0x1077D7247296C4E9364498EC436E3A9C9F7CFAC6AC08FEC11C351AEC4E4C1A45);

Scalar arithmetic over L

SELECT ristretto255_scalar_random();
SELECT ristretto255_scalar_reduce(RANDOM_BYTES(64));
SELECT ristretto255_scalar_invert(ristretto255_scalar_random());
SELECT ristretto255_scalar_negate(ristretto255_scalar_random());
SELECT ristretto255_scalar_complement(ristretto255_scalar_random());
SELECT ristretto255_scalar_add(ristretto255_scalar_random(), ristretto255_scalar_random());
SELECT ristretto255_scalar_sub(ristretto255_scalar_random(), ristretto255_scalar_random());
SELECT ristretto255_scalar_mul(ristretto255_scalar_random(), ristretto255_scalar_random());

Development

Compiling this project requires at least the following packages to be installed:

  • make

  • automake

  • autoconf

  • libtool

  • mariadb-devel

  • libsodium-devel

To install, run the following commands in project root directory:

autoreconf -fvi

./configure
make
sudo make install
sudo make installdb

And to uninstall, run the following commands:

sudo make uninstalldb
sudo make uninstall

Tests can be run with:

make check

If more test files are added to the Makefile.am:

tests_{test_file_name}_SOURCES = tests/{test_file_name}.c
tests_{test_file_name}_LDADD = -lsodium lib_sodium_ristretto.la

and add the new test file after the rest of the RISTRETTO_BUILT_TESTS:

RISTRETTO_BUILT_TESTS = tests/tests_ristretto_isvalidpoint tests/tests_ristretto_random tests/{test_file_name}

C-code formatting for all source code files can be done using astyle:

dnf install astyle

astyle --options=astyle-options.ini --mode=c --recursive "./*.c, *.h"

Contributing

You can involve yourself with our project by opening an issue or submitting a pull request.

Contribution requirements:

  1. All changes must be accompanied by a new or changed test. If you think testing is not required in your pull request, include a sufficient explanation as why you think so.

  2. Security checks must pass

  3. Pull requests must align with the principles and values of extreme programming.

  4. Pull requests must follow the principles of Object Thinking and Elegant Objects (EO).

Read more in our Contributing Guideline.

Contributor License Agreement

Contributors must sign Teragrep Contributor License Agreement before a pull request is accepted to organization’s repositories.

You need to submit the CLA only once. After submitting the CLA you can contribute to all Teragrep’s repositories.