-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmodulus.test.ts
181 lines (180 loc) · 6.62 KB
/
modulus.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { SEALLibrary } from '../implementation/seal'
import SEAL from '../throws_wasm_node_umd'
let seal: SEALLibrary
beforeAll(async () => {
seal = await SEAL()
})
describe('Modulus', () => {
test('It should be a factory', () => {
expect(seal.Modulus).toBeDefined()
expect(typeof seal.Modulus.constructor).toBe('function')
expect(seal.Modulus).toBeInstanceOf(Object)
expect(seal.Modulus.constructor).toBe(Function)
expect(seal.Modulus.constructor.name).toBe('Function')
})
test('It should construct an instance', () => {
const Constructor = jest.fn(seal.Modulus)
Constructor(BigInt('0'))
expect(Constructor).toHaveBeenCalledWith(BigInt('0'))
})
test('It should construct an instance from a string', () => {
const Constructor = jest.fn(seal.Modulus)
Constructor(BigInt('2'))
expect(Constructor).toHaveBeenCalledWith(BigInt('2'))
})
test('It should fail to construct an instance', () => {
const Constructor = jest.fn(seal.Modulus)
expect(() => Constructor(BigInt('1'))).toThrow()
expect(Constructor).toHaveBeenCalledWith(BigInt('1'))
})
test('It should have properties', () => {
const modulus = seal.Modulus(BigInt('5'))
// Test properties
expect(modulus).toHaveProperty('instance')
expect(modulus).toHaveProperty('inject')
expect(modulus).toHaveProperty('delete')
expect(modulus).toHaveProperty('setValue')
expect(modulus).toHaveProperty('value')
expect(modulus).toHaveProperty('bitCount')
expect(modulus).toHaveProperty('isZero')
expect(modulus).toHaveProperty('isPrime')
expect(modulus).toHaveProperty('save')
expect(modulus).toHaveProperty('saveArray')
expect(modulus).toHaveProperty('load')
expect(modulus).toHaveProperty('loadArray')
})
test('It should have an instance', () => {
const modulus = seal.Modulus(BigInt('4'))
expect(modulus.instance).toBeDefined()
})
test('It should inject', () => {
const modulus = seal.Modulus(BigInt('4'))
const newModulus = seal.Modulus(BigInt('6'))
newModulus.delete()
const spyOn = jest.spyOn(newModulus, 'inject')
newModulus.inject(modulus.instance)
expect(spyOn).toHaveBeenCalledWith(modulus.instance)
})
test('It should delete the old instance and inject', () => {
const modulus = seal.Modulus(BigInt('4'))
const newModulus = seal.Modulus(BigInt('6'))
const spyOn = jest.spyOn(newModulus, 'inject')
newModulus.inject(modulus.instance)
expect(spyOn).toHaveBeenCalledWith(modulus.instance)
})
test("It should delete it's instance", () => {
const modulus = seal.Modulus(BigInt('6'))
const spyOn = jest.spyOn(modulus, 'delete')
modulus.delete()
expect(spyOn).toHaveBeenCalled()
expect(modulus.instance).toBeUndefined()
expect(() => modulus.value).toThrow(TypeError)
})
test('It should skip deleting twice', () => {
const modulus = seal.Modulus(BigInt('6'))
modulus.delete()
const spyOn = jest.spyOn(modulus, 'delete')
modulus.delete()
expect(spyOn).toHaveBeenCalled()
expect(modulus.instance).toBeUndefined()
})
test('It should set a value', () => {
const modulus = seal.Modulus(BigInt('6'))
const spyOn = jest.spyOn(modulus, 'setValue')
modulus.setValue(BigInt('2'))
expect(spyOn).toHaveBeenCalledWith(BigInt('2'))
expect(modulus.value).toEqual(BigInt('2'))
})
test('It should fail to set a value', () => {
const modulus = seal.Modulus(BigInt('2'))
const spyOn = jest.spyOn(modulus, 'setValue')
expect(() => modulus.setValue(BigInt('1'))).toThrow()
expect(spyOn).toHaveBeenCalledWith(BigInt('1'))
})
test('It should have bitCount of 0', () => {
const modulus = seal.Modulus(BigInt('0'))
expect(modulus.bitCount).toBe(0)
})
test('It should have bitCount of 20', () => {
const modulus = seal.Modulus(BigInt('786433'))
expect(modulus.bitCount).toBe(20)
})
test('It should be zero', () => {
const modulus = seal.Modulus(BigInt('0'))
expect(modulus.isZero).toBe(true)
})
test('It should be non zero', () => {
const modulus = seal.Modulus(BigInt('88'))
expect(modulus.isZero).toBe(false)
})
test('It should be prime', () => {
const modulus = seal.Modulus(BigInt('786433'))
expect(modulus.isPrime).toBe(true)
})
test('It should be not prime', () => {
const modulus = seal.Modulus(BigInt('786432'))
expect(modulus.isPrime).toBe(false)
})
test('It should save to a string', () => {
const modulus = seal.Modulus(BigInt('7'))
expect(modulus.value).toBe(BigInt('7'))
const spyOn = jest.spyOn(modulus, 'save')
const str = modulus.save()
expect(spyOn).toHaveBeenCalled()
expect(typeof str).toBe('string')
})
test('It should save to an array', () => {
const modulus = seal.Modulus(BigInt('7'))
expect(modulus.value).toBe(BigInt('7'))
const spyOn = jest.spyOn(modulus, 'saveArray')
const array = modulus.saveArray()
expect(spyOn).toHaveBeenCalled()
expect(array.constructor).toBe(Uint8Array)
})
test('It should load from a string', () => {
const modulus = seal.Modulus(BigInt('7'))
expect(modulus.value).toBe(BigInt('7'))
const str = modulus.save()
modulus.delete()
const newModulus = seal.Modulus(BigInt('9'))
const spyOn = jest.spyOn(newModulus, 'load')
newModulus.load(str)
expect(spyOn).toHaveBeenCalledWith(str)
expect(newModulus.value).toEqual(BigInt('7'))
})
test('It should load from a typed array', () => {
const modulus = seal.Modulus(BigInt('7'))
expect(modulus.value).toBe(BigInt('7'))
const array = modulus.saveArray()
modulus.delete()
const newModulus = seal.Modulus(BigInt('9'))
const spyOn = jest.spyOn(newModulus, 'loadArray')
newModulus.loadArray(array)
expect(spyOn).toHaveBeenCalledWith(array)
expect(newModulus.value).toEqual(BigInt('7'))
})
test('It should fail to load from a string', () => {
const modulus = seal.Modulus(BigInt('0'))
const spyOn = jest.spyOn(modulus, 'load')
expect(() => modulus.load('XqEQAwUBAAAbAAAAAAAAAHicY2eAAAAAQAAA')).toThrow()
expect(spyOn).toHaveBeenCalledWith('XqEQAwUBAAAbAAAAAAAAAHicY2eAAAAAQAAA')
})
test('It should fail to load from a typed array', () => {
const modulus = seal.Modulus(BigInt('0'))
const spyOn = jest.spyOn(modulus, 'loadArray')
expect(() =>
modulus.loadArray(
Uint8Array.from([
94, 161, 16, 3, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 156, 99, 103,
128, 0, 0, 0, 64, 0, 8
])
)
).toThrow()
expect(spyOn).toHaveBeenCalledWith(
Uint8Array.from([
94, 161, 16, 3, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 156, 99, 103,
128, 0, 0, 0, 64, 0, 8
])
)
})
})