Skip to content

Commit 3738015

Browse files
Merge pull request #15 from multiparty/ristretto
Use ristretto255 with libsodium-wrappers-sumo instead of elliptic
2 parents 6305e3c + 5cd8ebc commit 3738015

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6424
-8532
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ node_modules/
22
.npm
33
*.log
44
logs
5-
dist/
65
lib/
76
coverage/
87
.nyc_output/
98
.DS_Store
109
.idea/
1110
*.iml
11+
build/

.nycrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"nyc": {
3+
"extension": [
4+
".ts"
5+
],
6+
"include": [
7+
"src/**.ts"
8+
],
9+
"exclude": [
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"**/*.spec.ts"
13+
]
14+
}
15+
}

README.md

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,103 @@
33
[![Build Status](https://travis-ci.org/multiparty/oprf.svg?branch=master)](https://travis-ci.org/multiparty/oprf)
44
[![Coverage Status](https://coveralls.io/repos/github/multiparty/oprf/badge.svg?branch=master)](https://coveralls.io/github/multiparty/oprf?branch=master)
55

6-
#### Oblivious pseudo-random function over an elliptic curve (ED25519)
7-
6+
#### Oblivious pseudo-random function over an elliptic curve (Ristretto255)
87

98
## Installation
10-
```npm install oprf```
9+
For node.js, use:
10+
11+
```bash
12+
npm install oprf
13+
```
14+
15+
For the browser, include a script tag targeting either `dist/oprf.js` or `dist/oprf.slim.js`.
16+
17+
## Bundle vs slim
18+
19+
For browsers, we provide two built files: `dist/oprf.js` and `dist/oprf.slim.js`.
20+
21+
The first includes both OPRF bundled with [libsodium-wrappers-sumo](https://github.com/jedisct1/libsodium.js) version 0.7.6. The second includes only OPRF.
22+
23+
You can use the slim version for cases where your browser-side code uses a more recent version of libsodium, or if you want
24+
to load libsodium asynchronously to reduce page load time.
25+
26+
The API for both versions is identical, except that the slim OPRF constructor expects a sodium instance to be passed in
27+
as a parameter, while the bundled constructor does not expect any parameters.
28+
29+
In node.js, the slim OPRF is not exposed.
30+
31+
```javascript
32+
const OPRF = require('oprf');
33+
const oprf = new OPRF(); // will require('libsodium-wrappers-sumo');
34+
```
1135

1236
## Initialization
13-
The sumo version of libsodium must be used
37+
OPRF is not safe to use until sodium is done loading.
38+
1439
```Typescript
15-
await _sodium.ready;
16-
const oprf = new OPRF(_sodium);
40+
const oprf = new OPRF();
41+
await oprf.ready; // wait for dependencies to load
1742
```
1843

1944
## Security Guarantees
2045
A client has input _x_ while a server holds key _k_. The client receives the output of *f<sub>k</sub>(x)* for some pseudorandom function family *f<sub>k</sub>*. The server learns nothing.
2146

22-
23-
## Dependencies
24-
* [elliptic](https://github.com/indutny/elliptic)
25-
* [libsodium.js](https://github.com/jedisct1/libsodium.js)
47+
The implementation uses [Ristretto255](https://libsodium.gitbook.io/doc/advanced/point-arithmetic/ristretto), and does not suffer from small cofactor attacks.
2648

2749
## Public Interface
2850
Contains a masked point and the mask that was applied to it
2951
```Typescript
3052
export interface IMaskedData {
31-
readonly point: number[];
32-
readonly mask: BN; // big number
53+
readonly point: Uint8Array;
54+
readonly mask: Uint8Array;
3355
}
3456
```
3557

3658
## Public Functions
59+
3760
**hashToPoint**: maps string input to a point on the elliptic curve
3861
```Typescript
39-
public hashToPoint(input: string): number[]
62+
public hashToPoint(input: string): Uint8Array
63+
```
64+
65+
**isValidPoint**: returns whether the given point exists on the elliptic curve
66+
```Typescript
67+
public isValidPoint(point: Uint8Array): boolean
4068
```
69+
4170
**maskInput**: hashes string input as a point on an elliptic curve and applies a random mask to it
4271
```Typescript
4372
public maskInput(input: string): IMaskedData
4473
```
45-
**generateRandomScalar**: generates a random 32-byte array of numbers
74+
75+
**maskPoint**: applies a random mask to an elliptic curve point
4676
```Typescript
47-
public generateRandomScalar(): BN
77+
public maskPoint(point: Uint8Array): IMaskedData
4878
```
49-
**isValidPoint**: returns whether the given point exists on the elliptic curve
79+
80+
**unmaskInput**: applies the multiplicative inverse of the mask to the masked point
5081
```Typescript
51-
public isValidPoint(point: number[]): number
82+
public unmaskPoint(maskedPoint: Uint8Array, mask: Uint8Array): Uint8Array
5283
```
53-
**encodePoint**: converts an elliptic.js point representation to number array representation
84+
85+
**generateRandomScalar**: generates a uniform random 32-byte number in [1, order of curve)
5486
```Typescript
55-
public encodePoint(point: any): number[]
87+
public generateRandomScalar(): Uint8Array
5688
```
57-
**decodePoint**: converts a number array to elliptic.js point object representation
89+
90+
**scalarMult**: salts a point using a key as a scalar
5891
```Typescript
59-
public decodePoint(point: number[]): any
92+
public scalarMult(point: Uint8Array, key: Uint8Array): Uint8Array
6093
```
61-
**unmaskInput**: applies the multiplicative inverse of the mask to the masked point
94+
95+
**encodePoint**: encodes a point representation to a string with either 'ASCII' or 'UTF-8' encoding
6296
```Typescript
63-
public unmaskInput(maskedPoint: number[], mask: BN): number[]
97+
public encodePoint(point: Uint8Array, encoding: string): string
98+
```
99+
100+
**decodePoint**: Decode elliptic curve point from a string
101+
```Typescript
102+
public decodePoint(code: string, encoding: string): Uint8Array
64103
```
65104

66105
## OPRF Steps
@@ -69,22 +108,30 @@ public unmaskInput(maskedPoint: number[], mask: BN): number[]
69108
const input = 'hello world';
70109
const masked = oprf.maskInput(input);
71110

72-
// Send masked.point to server. Do not send masked.mask to the server since it can easily unmask your original input.
111+
// Send masked.point to server,
112+
// Do not send masked.mask to the server.
113+
send(oprf.encodePoint(masked.point, 'UTF-8'));
73114
```
74115

75116
2.) **Server**: salt the masked point using a secret key
76117
```Typescript
77-
// Note: your actual secret key should be a static 32-byte Uint8Array. Do not generate a new scalar for each OPRF unless you have a specific use case for doing so.
78-
const secretKey = oprf.generateRandomScalar();
118+
// Note: your actual secret key should be fixed.
119+
// Do not generate a new scalar for each OPRF
120+
// application unless you have a specific use case for doing so.
121+
const secretKey = oprf.generateRandomScalar();
122+
123+
const maskedPoint = oprf.decodePoint(receive(), 'UTF-8');
79124
const salted = oprf.scalarMult(maskedPoint, secretKey);
80125

81126
// Send salted back to the client
127+
send(oprf.encodePoint(salted, 'UTF-8'));
82128
```
83129

84130
3.) **Client**: unmask the salted point from the server to get a high-entropy output
85131
```Typescript
86132
// Make sure that masked.mask corresponds to the original mask used.
87-
// Otherwise, this will not give you the correct output.
133+
// Otherwise, this will not give you the correct output.
134+
const salted = oprf.decodePoint(receive(), 'UTF-8');
88135
const unmasked = oprf.unmaskInput(salted, masked.mask);
89136
```
90137

dist-web/oprf.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist-web/types/oprf.d.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

dist-web/types/tools.d.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

dist/oprf.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/oprf.slim.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)