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

Conversation

hadasz
Copy link
Contributor

@hadasz hadasz commented Nov 5, 2018

WIP of integrating shamir's but code amount has far exceeded amount appropriate for review. Still need to set up JSDoc.
So far - this is a rewrite of existing shamir javascript implementation (https://github.com/amper5and/secrets.js/ ) that uses react-native-securerandom for the random number generator, and focuses on emphasizing and explaining the concepts of Galois Fields of characteristic 2, the fields from which coefficients are randomly generated.

Copy link
Contributor

@barlock barlock left a comment

Choose a reason for hiding this comment

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

There seems to be a lot of copied code from secrets.js and secrets.js-grempe. There's also a newer version (all off the same base) secret-sharing.js.

I'm not sure what you changed. I see refactoring some names and adding a random number generator, plus some more tests which is great.

The reason you mentioned to rebuild these libraries from scratch was to use a better random number generator. All of those libraries have a setRNG that we could use and remove a lot of this code. Did you have other reasons that I'm not seeing?

Given that the logic seems near exact, I think the community would benefit the most from letting this project be a light wrapper around secret-sharing.js that adds the react-native random number generator with setRNG. Then we can add some test cases to the upstream one, rather than split implementations. It might also be valuable to make some of your refactoring changes on the upstream one as well.

lib/config.js Outdated
@@ -0,0 +1,46 @@
/*eslint no-undef: "off"*/
Copy link
Contributor

Choose a reason for hiding this comment

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

What's your reasoning for turning it off? Likely config is actually undefined. Something like

const config = { ... }

export default config;

Would fix this error and have the benefits of letting config be a const rather than a var

lib/galoisField.js Show resolved Hide resolved

export class CharacteristicTwoGaloisField {
constructor(numElements) {
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?

}
getExponentOfNextDegree(expOfCurrentDegree) {
let primitivePolynomial = config.primitivepolynomials[this.n];
var polynomial = expOfCurrentDegree.timesMonomialOfDegree(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I learned something new! eslint/recommended has no-var turned off. I wouldn't have expected that.

var seems to be used a lot, any reason why? While this one and probably the rest are fine, var can lead to a lot of subtle bugs so I tend to avoid them for let and const.

var expectedResult = Polynomial.toIntegerForm('1100'); //equivelent of x^3 + x^2
var actualResult = new Polynomial(polynomialOne).plus(polynomialTwo);
expect(expectedResult).toBe(actualResult.getValue());
expect(4).toBe(new Polynomial(1).plus(5).getValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

What, 1 + 5 = 4? 😛

Super nitty, it's easier to read things like expect(new Polynomial(1).plus(5).getValue()).toBe(4);. This is what's called a "Yoda Clause".

Mmmmm, 4 I expect a polynomial of 1 plus 5 to be

yoda

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]).toBe(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does .toEqual not work with arrays?

https://jestjs.io/docs/en/expect#toequalvalue

lib/utils.js Outdated
}

// return null so this result can be re-processed if the result is all 0's.
if ((str.match(/0/g) || []).length === str.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just use a regex for this whole expression? /^0+$/g

package.json Outdated
@@ -31,7 +31,8 @@
],
"homepage": "https://github.com/rh389/react-native-securerandom#readme",
"dependencies": {
"base64-js": "*"
"base64-js": "*",
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably lock this down to ^2.4.9 otherwise we could have breaks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

latest version I see is 1.3.0 - but point taken

Copy link
Contributor

Choose a reason for hiding this comment

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

😅 Reading is hard, yes 1.3 is right

lib/sssa.test.js Outdated Show resolved Hide resolved
@@ -50,7 +50,8 @@ export function bytesToBits(byteArray) {
}

// return null so this result can be re-processed if the result is all 0's.
if ((str.match(/0/g) || []).length === str.length) {
const allZeroPattern = /^0+$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine as is. For clarity on my statement about regex being expensive to compile, this regex being defined in the function will be newly declared, created, and compile every time the function is called.

To make sure that it's only compiled once, you can move the declaration outside of the function.

⚠️ This is a micro optimization, no big wins or losses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants