-
Notifications
You must be signed in to change notification settings - Fork 2
Setup project integrate shamir #4
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
Open
hadasz
wants to merge
4
commits into
master
Choose a base branch
from
setupProject-integrate-shamir
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is 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) | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.