Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup project integrate shamir #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions ExampleGrinder/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
*/
/*eslint no-unused-vars: "warn"*/
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import { generateSecureRandom } from '../RNSecureRandom/index';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu'
});
import { StyleSheet, Text, View } from 'react-native';
import { randomBitGenerator } from '../lib/randomBitGenerator.js';

type Props = {};

export default class App extends Component<Props> {
state = { randomBits: '' };
async componentDidMount() {
let randomNumber = await randomBitGenerator(14);
this.setState({ randomBits: randomNumber });
}
render() {
return (
<View style={styles.container}>
<Text testID="welcome" style={styles.welcome}>
Welcome to React Native!
{this.state.randomBits}
</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
<Text testID="randombits" style={styles.instructions}>
{this.state.randomBits.length}
</Text>
</View>
);
}
Expand Down
4 changes: 4 additions & 0 deletions e2e/firstTest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ describe('Example', () => {
it('should have welcome screen', async () => {
await expect(element(by.id('welcome'))).toBeVisible();
});

it('should have the correct length', async () => {
await expect(element(by.id('randombits'))).toHaveText('14');
});
});
46 changes: 46 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const config = {
bits: 8, // default number of bits
radix: 16, // work with hex by default
minbits: 3,
maxbits: 20, // this permits 1,048,575 shares, though going this high is not recommended in js!
bytesperchar: 2,
maxbytesperchar: 6, // math.pow(256,7) > math.pow(2,53)

// primitive polynomials (in decimal form) for galois fields gf(2^n), for 2 <= n <= 30
// the index of each term in the array corresponds to the n for that polynomial
// i.e. to get the polynomial for n=16, use primitivepolynomials[16]
primitivepolynomials: [
null,
null,
1,
3,
3,
5,
3,
3,
29,
17,
9,
5,
83,
27,
43,
3,
45,
9,
39,
39,
9,
5,
3,
33,
27,
9,
71,
39,
9,
5,
83
]
};
export default config;
87 changes: 87 additions & 0 deletions lib/galoisField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import config from './config';

//class that represents polynomials as bit arrays
export class Polynomial {
constructor(value) {
if (value % 1 !== 0) {
throw new Error('value must be an integer, got ' + value);
}
this.value = value;
barlock marked this conversation as resolved.
Show resolved Hide resolved
}
getValue() {
return this.value;
}
setValue(value) {
this.value = value;
}
plus(rightHandPolynomial) {
this.value = this.value ^ rightHandPolynomial;
return this;
}
timesMonomialOfDegree(n) {
this.value = this.value << n;
return this;
}
subtractTermsAboveDegree(n) {
this.value = this.value & (Math.pow(2, n + 1) - 1);
return this;
}
static toIntegerForm(bitArray) {
return parseInt(bitArray, 2);
}
static toPolynomialForm(integer) {
return new Number(integer).toString(2);
}
}

export class CharacteristicTwoGaloisField {
constructor(numElements) {
//this.n represents n in GF(2^n)
this.n = Math.log2(numElements);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is n and what does it represent? From looking at the code you copied from, is it bits?

Should we make it more clear what it is?

if (
this.n &&
(this.n % 1 !== 0 || this.n < config.minBits || this.n > config.maxBits)
) {
throw new Error(
'Number of n must be an integer between ' +
config.minBits +
' and ' +
config.maxBits +
', inclusive.'
);
}
this.numberOfElementsInField = numElements;
this.computeLogAndExpTables();
}
fieldContains(value) {
return value < this.numberOfElementsInField;
}
getExponentOfNextDegree(expOfCurrentDegree) {
let primitivePolynomial = config.primitivepolynomials[this.n];
let polynomial = expOfCurrentDegree.timesMonomialOfDegree(1);
if (!this.fieldContains(polynomial.getValue())) {
polynomial = polynomial
.plus(primitivePolynomial)
.subtractTermsAboveDegree(this.n - 1);
}
return polynomial;
}
computeLogAndExpTables() {
this.exps = [];
this.logs = [];
let polynomial = new Polynomial(1);
for (let i = 0; i < this.numberOfElementsInField; i++) {
this.exps[i] = polynomial.getValue();
this.logs[polynomial.getValue()] = i;
polynomial = this.getExponentOfNextDegree(polynomial);
}
}
multiply(polynomialOne, polynomialTwo) {
return new Polynomial(
this.exps[
(this.logs[polynomialOne] + this.logs[polynomialTwo]) %
(this.numberOfElementsInField - 1)
]
);
}
}
65 changes: 65 additions & 0 deletions lib/galoisField.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Polynomial, CharacteristicTwoGaloisField } from './galoisField';
//Testing Polynomial Class
describe('adds two polynomials in galois field of characteristic two', () => {
it('should output the correct XOR value of two polynomials represened as bit arrays', () => {
var polynomialOne = Polynomial.toIntegerForm('1010'); //equivelent of x^3 + x (^ is exponent in this context)
var polynomialTwo = Polynomial.toIntegerForm('0110'); //equivelent of x^2 + x
var expectedResult = Polynomial.toIntegerForm('1100'); //equivelent of x^3 + x^2
var actualResult = new Polynomial(polynomialOne).plus(polynomialTwo);
expect(expectedResult).toBe(actualResult.getValue());
expect(new Polynomial(1).plus(5).getValue()).toBe(4);
});
});

describe('multiplies a polynomial by a monomial of the given degree', () => {
it('should multiply the valueToMultiply by monomial', () => {
var valueToMultiply = Polynomial.toIntegerForm('11100'); //x^4 + x^3 + x^2 in polynomial form, and 28 in integer form
var monomial = '10'; //equivelently x^1 in polynomial form and 2 in integer form
var degreeOfMonomial = Math.log2(Polynomial.toIntegerForm(monomial)); //degree is 1
var expectedResult = Polynomial.toIntegerForm('111000');
var actualResult = new Polynomial(valueToMultiply).timesMonomialOfDegree(
degreeOfMonomial
);
expect(expectedResult).toBe(actualResult.getValue());
});
});

describe('subtract terms above degree', () => {
it('bitwise AND with bit array containing all 1 values up to given degree', () => {
var polynomialWithBigDegree = Polynomial.toIntegerForm('11100');
var expectedResult = Polynomial.toIntegerForm('100');
var actualResult = new Polynomial(
polynomialWithBigDegree
).subtractTermsAboveDegree(2);
expect(expectedResult).toBe(actualResult.getValue());
});
});

//Testing ChracteristicTwoGaloisField class
describe('determines if an element is contained within the field', () => {
it('should return false if the degree of the element is above n in 2^n', () => {
var polynomial = Polynomial.toIntegerForm('1111110');
var gf = new CharacteristicTwoGaloisField(Math.pow(2, 5));
var expectedResult = false;
var actualResult = gf.fieldContains(polynomial);
expect(expectedResult).toBe(actualResult);
});
});
describe('create exponent and log tables for the field', () => {
it('should output the correct exp and log tables for GF(2^3)', () => {
var gf = new CharacteristicTwoGaloisField(Math.pow(2, 3));
expect(gf.exps[0]).toEqual(1);
expect(gf.exps[1]).toEqual(2);
expect(gf.exps[2]).toEqual(4);
expect(gf.exps[3]).toEqual(3);
expect(gf.exps[4]).toEqual(6);
expect(gf.exps[5]).toEqual(7);
expect(gf.exps[6]).toEqual(5);
expect(gf.exps[7]).toEqual(1);
for (var i = 1; i < Math.pow(2, 3); i++) {
expect(gf.exps[gf.logs[i]]).toEqual(i);
}
expect(gf.logs[1]).toBe(7);
expect(gf.logs[0]).toBeFalsy();
});
});
15 changes: 15 additions & 0 deletions lib/randomBitGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { generateSecureRandom } from '../RNSecureRandom/index';
import { bytesToBits } from './utils';

export async function randomBitGenerator(numBits) {
var numBytes,
str = null;

numBytes = Math.ceil(numBits / 8);
while (str === null) {
let uIntByteArr = await generateSecureRandom(numBytes);
str = bytesToBits(uIntByteArr);
}
str = str.substr(-numBits);
return str;
}
Loading