Skip to content

Instantly share code, notes, and snippets.

const e = B2 >= 10**7 && d * d < 2**53 ? 2 : 1;
const dsP = scalePoint(curve, sP, Math.pow(d, e));
const range = function (n) {
const a = new Array(n);
for (let i = 0; i < n; i += 1) {
a[i] = i;
}
return a;
};
function add(a, b) {
const c = new Array(Math.max(a.length, b.length));
const min = Math.min(a.length, b.length);
for (let i = 0; i < min; i += 1) {
c[i] = a[i] + b[i];
}
for (let i = min; i < a.length; i += 1) {
c[i] = a[i];
}
for (let i = min; i < b.length; i += 1) {
let hex = new Uint8Array(0);
const td = new TextDecoder();
const te = new TextEncoder();
function packBigInt(blocks, blockSize) {
function encode(blocks, length) {
const u32 = new Uint32Array(hex.buffer);
let k = (length >> 2) - 1;//TODO:!?
@Yaffle
Yaffle / jsHelper64.js
Created March 29, 2023 08:11
jsHelper64.js
const uint64 = new BigUint64Array(1);
const uint32 = new Uint32Array(uint64.buffer);
function clz64(bigint) {
uint64[0] = bigint;
let x = Math.clz32(uint32[1]);
let y = Math.clz32(uint32[0]);
return x + (x === 32 ? y : 0);
}
@Yaffle
Yaffle / wast2wasm.js
Last active March 18, 2023 11:13
conversion of wast to wasm (WebAssembley Text Format to WebAssemlby binary-code) - no dependencies
/*jshint esversion:6, bitwise:false*/
// https://webassembly.github.io/wabt/demo/wat2wasm/index.html helps a lot
function wast2wasm(sExpression) {
function sExpressionToJSON(s) {
return JSON.parse(s.trim().replace(/\s*\)/g, ']').replace(/\(\s*/g, '[').replace(/\s+/g, ',').replaceAll('"', '').replace(/([^\[\],]+)/g, '"$1"'));
}
// CPU L2 cache size
function ASMModule1(stdlib, foreign, heap) {
"use asm";
var memory = new stdlib.Uint32Array(heap);
function loop(c) {
c = c | 0;
var k = 0;
var j = 0;
for (j = c; (j | 0) != 0; j = (j - 1) | 0) {
k = memory[k >> 2] << 2;
@Yaffle
Yaffle / continuedFractionForSqrt.js
Created September 6, 2021 10:53
continued fraction for sqrt(n)
function* continuedFractionForSqrt(n) {
// https://trizenx.blogspot.com/2018/10/continued-fraction-factorization-method.html
function floorDiv(a, b) {
return typeof a === "bigint" && typeof b === "bigint" ? a / b : Math.floor(a / b);
}
n = BigInt(n);
if (n < 0n) {
throw new RangeError();
}
const root = BigInt(sqrt(n));
@Yaffle
Yaffle / test.js
Last active September 8, 2024 12:56
get/set caret position in contenteditable element
// Usage:
// var x = document.querySelector('[contenteditable]');
// var caretPosition = getSelectionDirection(x) !== 'forward' ? getSelectionStart(x) : getSelectionEnd(x);
// setSelectionRange(x, caretPosition + 1, caretPosition + 1);
// var value = getValue(x);
// it will not work with "<img /><img />" and, perhaps, in many other cases.
function isAfter(container, offset, node) {
var c = node;
We couldn’t find that file to show.
@Yaffle
Yaffle / decodeURIComponent.js
Last active April 23, 2020 10:01
decodeURIComponent using TextDecoder
// Implementation of the standard decodeURIComponent using TextDecoder.
// The original JavaScript specification does not separate UTF-8 decoding this way.
// The purpose is to show the possible implementation in JavaScript language.
function hexDigit(code) {
if (code >= '0'.charCodeAt(0) && code <= '9'.charCodeAt(0)) {
return code - '0'.charCodeAt(0);
} else if (code >= 'A'.charCodeAt(0) && code <= 'F'.charCodeAt(0)) {
return code + (10 - 'A'.charCodeAt(0));
} else if (code >= 'a'.charCodeAt(0) && code <= 'f'.charCodeAt(0)) {