Repository navigation
Expand file tree
/
Copy pathscalar.js
More file actions
115 lines (97 loc) · 2.64 KB
/
Copy pathscalar.js
File metadata and controls
115 lines (97 loc) · 2.64 KB
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
import * as Scalar_bigint from "./scalar_bigint.js";
let Scalar = {};
Object.assign(Scalar, Scalar_bigint);
// Returns a buffer with Little Endian Representation
Scalar.toRprLE = function rprBE(buff, o, e, n8) {
const s = "0000000" + e.toString(16);
const v = new Uint32Array(buff.buffer, o, n8/4);
const l = (((s.length-7)*4 - 1) >> 5)+1; // Number of 32bit words;
for (let i=0; i<l; i++) v[i] = parseInt(s.substring(s.length-8*i-8, s.length-8*i), 16);
for (let i=l; i<v.length; i++) v[i] = 0;
for (let i=v.length*4; i<n8; i++) buff[i] = Scalar.toNumber(Scalar.band(Scalar.shiftRight(e, i*8), 0xFF));
};
// Returns a buffer with Big Endian Representation
Scalar.toRprBE = function rprLEM(buff, o, e, n8) {
const s = "0000000" + e.toString(16);
const v = new DataView(buff.buffer, buff.byteOffset + o, n8);
const l = (((s.length-7)*4 - 1) >> 5)+1; // Number of 32bit words;
for (let i=0; i<l; i++) v.setUint32(n8-i*4 -4, parseInt(s.substring(s.length-8*i-8, s.length-8*i), 16), false);
for (let i=0; i<n8/4-l; i++) v[i] = 0;
};
// Pases a buffer with Little Endian Representation
Scalar.fromRprLE = function rprLEM(buff, o, n8) {
n8 = n8 || buff.byteLength;
o = o || 0;
const v = new Uint32Array(buff.buffer, o, n8/4);
const a = new Array(n8/4);
v.forEach( (ch,i) => a[a.length-i-1] = ch.toString(16).padStart(8,"0") );
return Scalar.fromString(a.join(""), 16);
};
// Pases a buffer with Big Endian Representation
Scalar.fromRprBE = function rprLEM(buff, o, n8) {
n8 = n8 || buff.byteLength;
o = o || 0;
const v = new DataView(buff.buffer, buff.byteOffset + o, n8);
const a = new Array(n8/4);
for (let i=0; i<n8/4; i++) {
a[i] = v.getUint32(i*4, false).toString(16).padStart(8, "0");
}
return Scalar.fromString(a.join(""), 16);
};
Scalar.toString = function toString(a, radix) {
return a.toString(radix);
};
Scalar.toLEBuff = function toLEBuff(a) {
const buff = new Uint8Array(Math.floor((Scalar.bitLength(a) - 1) / 8) +1);
Scalar.toRprLE(buff, 0, a, buff.byteLength);
return buff;
};
Scalar.zero = Scalar.e(0);
Scalar.one = Scalar.e(1);
export let {
toRprLE,
toRprBE,
fromRprLE,
fromRprBE,
toString,
toLEBuff,
zero,
one,
fromString,
e,
fromArray,
bitLength,
isNegative,
isZero,
shiftLeft,
shiftRight,
shl,
shr,
isOdd,
naf,
bits,
toNumber,
toArray,
add,
sub,
neg,
mul,
square,
pow,
exp,
abs,
div,
mod,
eq,
neq,
lt,
gt,
leq,
geq,
band,
bor,
bxor,
land,
lor,
lnot,
} = Scalar;