Skip to content

Commit e1c5b42

Browse files
committed
2 parents f05aa80 + 63a9c32 commit e1c5b42

File tree

4 files changed

+28
-46
lines changed

4 files changed

+28
-46
lines changed

src/evaluate.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
const label = require('./data/label');
99
const gate = require('./data/gate');
10-
const circuit = require('./data/circuit');
1110
const assignment = require('./data/assignment');
1211
const crypto = require('./util/crypto');
1312

@@ -57,7 +56,7 @@ function processMessages(circuit, messages) {
5756
* @param {Object} gateGarbled - Garbled gate to evaluate
5857
* @param {Object} wireToLabels - Mapping from each wire index to two labels
5958
*/
60-
function evaluateGate(gate, gateGarbled, wireToLabels) {
59+
function evaluateGate(gate_id, gate, gateGarbled, wireToLabels) {
6160
const i = gate.wire_in_index[0];
6261
const j = (gate.wire_in_index.length === 2) ? gate.wire_in_index[1] : i;
6362
const k = (gate.wire_out_index != null) ? gate.wire_out_index[0] : 0; // If null, just return decrypted.
@@ -68,27 +67,12 @@ function evaluateGate(gate, gateGarbled, wireToLabels) {
6867
} else if (gate.operation === 'not') {
6968
wireToLabels.set(k, [wireToLabels.get(i)[0]]); // Already inverted.
7069
} else if (gate.operation === 'and') {
71-
wireToLabels.set(k, [crypto.decrypt(wireToLabels.get(i)[0], wireToLabels.get(j)[0], k, label.Label(gateGarbled.get(l)))]);
70+
wireToLabels.set(k, [crypto.decrypt(wireToLabels.get(i)[0], wireToLabels.get(j)[0], gate_id, label.Label(gateGarbled.get(l)))]);
7271
}
7372
}
7473

75-
/**
76-
* Evaluate all the gates (stateless version).
77-
* @param {Object} circuit - Circuit in which to garble the gates
78-
* @param {Object} gatesGarbled - Ordered collection of garbled gates
79-
* @param {Object} wireToLabels - Labeled wire data structure
80-
* @returns {Object} Mapping from each wire index to two labels
81-
*/
82-
function evaluateGates(circuit, gatesGarbled, wireToLabels) {
83-
for (var i = 0; i < circuit.gate_count; i++) {
84-
this.evaluateGate(circuit.gate[i], gatesGarbled.get(i), wireToLabels);
85-
}
86-
return wireToLabels;
87-
}
88-
8974
module.exports = {
9075
receiveMessages: receiveMessages,
9176
processMessages: processMessages,
9277
evaluateGate: evaluateGate,
93-
evaluateGates: evaluateGates
9478
};

src/garble.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function generateWireToLabelsMap(circuit) {
7070
* @param {Object} wToLs - Mapping from each wire index to two labels
7171
* @returns {Object} Garbled gate
7272
*/
73-
function garbleGate(gateFromCircuit, wToLs) {
73+
function garbleGate(gate_id, gateFromCircuit, wToLs) {
7474
const i = gateFromCircuit.wire_in_index[0];
7575
const j = (gateFromCircuit.wire_in_index.length === 2) ? gateFromCircuit.wire_in_index[1] : i;
7676
const k = gateFromCircuit.wire_out_index[0];
@@ -82,16 +82,16 @@ function garbleGate(gateFromCircuit, wToLs) {
8282
} else if (gateFromCircuit.operation === 'and') {
8383
var t = [0,0,0,1];
8484
var values = [
85-
[crypto.encrypt(wToLs.get(i)[0], wToLs.get(j)[0], k, wToLs.get(k)[t[0]])
85+
[crypto.encrypt(wToLs.get(i)[0], wToLs.get(j)[0], gate_id, wToLs.get(k)[t[0]])
8686
.toJSON(),
8787
(2 * wToLs.get(i)[0].pointer()) + wToLs.get(j)[0].pointer()],
88-
[crypto.encrypt(wToLs.get(i)[0], wToLs.get(j)[1], k, wToLs.get(k)[t[1]])
88+
[crypto.encrypt(wToLs.get(i)[0], wToLs.get(j)[1], gate_id, wToLs.get(k)[t[1]])
8989
.toJSON(),
9090
(2 * wToLs.get(i)[0].pointer()) + wToLs.get(j)[1].pointer()],
91-
[crypto.encrypt(wToLs.get(i)[1], wToLs.get(j)[0], k, wToLs.get(k)[t[2]])
91+
[crypto.encrypt(wToLs.get(i)[1], wToLs.get(j)[0], gate_id, wToLs.get(k)[t[2]])
9292
.toJSON(),
9393
(2 * wToLs.get(i)[1].pointer()) + wToLs.get(j)[0].pointer()],
94-
[crypto.encrypt(wToLs.get(i)[1], wToLs.get(j)[1], k, wToLs.get(k)[t[3]])
94+
[crypto.encrypt(wToLs.get(i)[1], wToLs.get(j)[1], gate_id, wToLs.get(k)[t[3]])
9595
.toJSON(),
9696
(2 * wToLs.get(i)[1].pointer()) + wToLs.get(j)[1].pointer()]
9797
];
@@ -105,20 +105,6 @@ function garbleGate(gateFromCircuit, wToLs) {
105105
// Define cases for any other gate operations here.
106106
}
107107

108-
/**
109-
* Garble all the gates (stateless version).
110-
* @param {Object} circuit - Circuit in which to garble the gates
111-
* @param {Object} wireToLabels - Mapping from each wire index to two labels
112-
* @returns {Object} Ordered collection of garbled gates
113-
*/
114-
function garbleGates(circuit, wireToLabels) {
115-
var gatesGarbled = new gate.GatesGarbled();
116-
for (var i = 0; i < circuit.gate_count; i++) {
117-
gatesGarbled.set(i, garbleGate(circuit.gate[i], wireToLabels));
118-
}
119-
return gatesGarbled;
120-
}
121-
122108
/**
123109
* Send mapping from input wires to their label pairs.
124110
* @param {Object} channel - Communication channel to use
@@ -176,7 +162,6 @@ function outputLabelsToBits(circuit, wireToLabels, outputWireToLabels) {
176162
module.exports = {
177163
generateWireToLabelsMap: generateWireToLabelsMap,
178164
garbleGate: garbleGate,
179-
garbleGates: garbleGates,
180165
sendInputWireToLabelsMap: sendInputWireToLabelsMap,
181166
outputLabelsToBits: outputLabelsToBits
182167
};

src/jigg.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ Agent.prototype.loadCircuit = function () {
9999
*/
100100
Agent.prototype.gatesThrottled = function (circuit, gatesGarbled, wireToLabels, index) {
101101
for (var i = index; i < index + this.parallel && i < circuit.gate_count; i++) {
102-
if (this.role == 'Garbler')
103-
gatesGarbled.set(i, garble.garbleGate(circuit.gate[i], wireToLabels));
104-
else if (this.role == 'Evaluator')
105-
evaluate.evaluateGate(circuit.gate[i], gatesGarbled.get(i), wireToLabels);
102+
if (this.role === 'Garbler')
103+
gatesGarbled.set(i, garble.garbleGate(i, circuit.gate[i], wireToLabels));
104+
else if (this.role === 'Evaluator')
105+
evaluate.evaluateGate(i, circuit.gate[i], gatesGarbled.get(i), wireToLabels);
106106
}
107107

108108
index += this.parallel;
109109
this.progress(Math.min(index, circuit.gate_count), circuit.gate_count);
110110

111111
if (index >= circuit.gate_count) {
112-
if (this.role == 'Garbler')
112+
if (this.role === 'Garbler')
113113
this.finishGarbler(circuit, gatesGarbled, wireToLabels);
114-
else if (this.role == 'Evaluator')
114+
else if (this.role === 'Evaluator')
115115
this.finishEvaluator(circuit, wireToLabels);
116116
return;
117117
}

src/util/crypto.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,29 @@ function encrypt(a, b, t, m) {
2424
return m.xor(k).xor(randomOracle(k, t));
2525
}
2626

27+
function longToByteArray(long) {
28+
// we want to represent the input as a 8-bytes array
29+
var byteArray = new Uint8Array(sodium.crypto_secretbox_NONCEBYTES);
30+
31+
for ( var index = 0; index < byteArray.length; index ++ ) {
32+
var byte = long & 0xff;
33+
byteArray [ index ] = byte;
34+
long = (long - byte) / 256 ;
35+
}
36+
37+
return byteArray;
38+
}
39+
2740
/**
2841
* Fixed-key 1-block cipher as the Random Oracle.
2942
* @param {string} m - Message
3043
* @param {string} t - Tweak
3144
* @returns {string} Pseudorandom bytes for ephemeral OTP key
3245
*/
3346
function randomOracle(m, t = 0) {
34-
return sodium.crypto_secretbox_easy(
47+
return sodium.crypto_secretbox_easy(
3548
m,
36-
new Uint8Array(24).fill(t), // Nonce 24 bytes because this sodium uses 192 bit blocks.
49+
longToByteArray(t), // Nonce 24 bytes because this sodium uses 192 bit blocks.
3750
sodium.from_hex('da5698be17b9b46962335799779fbeca8ce5d491c0d26243bafef9ea1837a9d8') // SHA(0).
3851
).subarray(0, bytes+1); // Prune back to the correct number of bytes.
3952
}

0 commit comments

Comments
 (0)