Skip to content

Commit

Permalink
bijs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubelrti committed Jan 25, 2024
1 parent 4b5ff34 commit f56ab49
Show file tree
Hide file tree
Showing 17 changed files with 312 additions and 288 deletions.
1 change: 0 additions & 1 deletion benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as smV2 from '../dist/index.js'
const msg = 'Hello world~!'
const longMsg = msg.repeat(10000)
const keypair = smV2.sm2.generateKeyPairHex('12345678901234567890')

run(async () => {
await smV2.sm2.initRNGPool()
const sig = smV2.sm2.doSignature(msg, keypair.privateKey, { publicKey: keypair.publicKey})
Expand Down
12 changes: 6 additions & 6 deletions src/sm2/asn1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import * as utils from './curves/utils';
import { ONE } from './bn';
import { utf8ToHex } from './utils';
import JSBI from 'jsbi';
import { BigInteger } from 'big-integer';

export function bigintToValue(bigint: JSBI) {
export function bigintToValue(bigint: BigInteger) {
let h = bigint.toString(16)
if (h[0] !== '-') {
// 正数
Expand All @@ -24,7 +24,7 @@ export function bigintToValue(bigint: JSBI) {

// 对绝对值取反,加1

let output = JSBI.add(JSBI.bitwiseXor(mask, bigint), ONE);
let output = mask.xor(bigint).add(ONE);
h = output.toString(16).replace(/^-/, '')
}
return h
Expand Down Expand Up @@ -71,7 +71,7 @@ class ASN1Object {
}

class DERInteger extends ASN1Object {
constructor(bigint: JSBI) {
constructor(bigint: BigInteger) {
super()

this.t = '02' // 整型标签说明
Expand Down Expand Up @@ -142,15 +142,15 @@ function getStartOfV(str: string, start: number) {
/**
* ASN.1 der 编码,针对 sm2 签名
*/
export function encodeDer(r: JSBI, s: JSBI) {
export function encodeDer(r: BigInteger, s: BigInteger) {
const derR = new DERInteger(r)
const derS = new DERInteger(s)
const derSeq = new DERSequence([derR, derS])

return derSeq.getEncodedHex()
}

export function encodeEnc(x: JSBI, y: JSBI, hash: string, cipher: string) {
export function encodeEnc(x: BigInteger, y: BigInteger, hash: string, cipher: string) {
const derX = new DERInteger(x)
const derY = new DERInteger(y)
const derHash = new DEROctetString(hash)
Expand Down
10 changes: 5 additions & 5 deletions src/sm2/bn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSBI from 'jsbi';
import bigInt from 'big-integer';

export const ZERO = JSBI.BigInt(0);
export const ONE = JSBI.BigInt(1);
export const TWO = JSBI.BigInt(2);
export const THREE = JSBI.BigInt(3);
export const ZERO = bigInt(0);
export const ONE = bigInt(1);
export const TWO = bigInt(2);
export const THREE = bigInt(3);
44 changes: 22 additions & 22 deletions src/sm2/curves/curve.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
// Abelian group utilities
import bigInt, { BigInteger } from 'big-integer';
import { IField, validateField, nLength } from './modular.js';
import { validateObject } from './utils.js';
import JSBI from 'jsbi';
const _0n = JSBI.BigInt(0);
const _1n = JSBI.BigInt(1);
const _2n = JSBI.BigInt(2);
const _0n = bigInt(0);
const _1n = bigInt(1);
const _2n = bigInt(2);

export type AffinePoint<T> = {
x: T;
Expand All @@ -18,7 +18,7 @@ export interface Group<T extends Group<T>> {
add(other: T): T;
subtract(other: T): T;
equals(other: T): boolean;
multiply(scalar: JSBI): T;
multiply(scalar: BigInteger): T;
}

export type GroupConstructor<T> = {
Expand Down Expand Up @@ -51,13 +51,13 @@ export function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number) {
return {
constTimeNegate,
// non-const time multiplication ladder
unsafeLadder(elm: T, n: JSBI) {
unsafeLadder(elm: T, n: BigInteger) {
let p = c.ZERO;
let d: T = elm;
while (JSBI.GT(n, _0n)) {
if (JSBI.bitwiseAnd(n, _1n)) p = p.add(d);
let d = elm;
while (n.greater(_0n)) {
if (n.and(_1n).notEquals(_0n)) p = p.add(d);
d = d.double();
n = JSBI.signedRightShift(n, _1n);
n = n.shiftRight(1);
}
return p;
},
Expand Down Expand Up @@ -97,31 +97,31 @@ export function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number) {
* @param n scalar (we don't check here, but should be less than curve order)
* @returns real and fake (for const-time) points
*/
wNAF(W: number, precomputes: T[], n: JSBI): { p: T; f: T } {
wNAF(W: number, precomputes: T[], n: BigInteger): { p: T; f: T } {
// TODO: maybe check that scalar is less than group order? wNAF behavious is undefined otherwise
// But need to carefully remove other checks before wNAF. ORDER == bits here
const { windows, windowSize } = opts(W);

let p = c.ZERO;
let f = c.BASE;

const mask = JSBI.subtract(JSBI.exponentiate(_2n, JSBI.BigInt(W)), _1n); // Create mask with W ones: 0b1111 for W=4 etc.
const mask = bigInt(2).pow(W).minus(_1n); // Create mask with W ones: 0b1111 for W=4 etc.
const maxNumber = 2 ** W;
const shiftBy = JSBI.BigInt(W);

const shiftBy = bigInt(W);
for (let window = 0; window < windows; window++) {
const offset = window * windowSize;
// Extract W bits.
let wbits = JSBI.toNumber(JSBI.bitwiseAnd(n, mask));
let wbits = n.and(mask).toJSNumber();

// Shift number by W bits.
n = JSBI.signedRightShift(n, shiftBy);

n = n.shiftRight(shiftBy);
// If the bits are bigger than max size, we'll split those.
// +224 => 256 - 32
if (wbits > windowSize) {
wbits -= maxNumber;
n = JSBI.add(n, _1n);
n = n.add(_1n);
}

// This code was first written with assumption that 'f' and 'p' will never be infinity point:
Expand Down Expand Up @@ -151,7 +151,7 @@ export function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number) {
return { p, f };
},

wNAFCached(P: T, precomputesMap: Map<T, T[]>, n: JSBI, transform: Mapper<T>): { p: T; f: T } {
wNAFCached(P: T, precomputesMap: Map<T, T[]>, n: BigInteger, transform: Mapper<T>): { p: T; f: T } {
// @ts-ignore
const W: number = P._WINDOW_SIZE || 1;
// Calculate precomputes on a first run, reuse them after
Expand All @@ -171,11 +171,11 @@ export function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number) {
// Though generator can be different (Fp2 / Fp6 for BLS).
export type BasicCurve<T> = {
Fp: IField<T>; // Field over which we'll do calculations (Fp)
n: JSBI; // Curve order, total count of valid points in the field
n: BigInteger; // Curve order, total count of valid points in the field
nBitLength?: number; // bit length of curve order
nByteLength?: number; // byte length of curve order
h: JSBI; // cofactor. we can assign default=1, but users will just ignore it w/o validation
hEff?: JSBI; // Number to multiply to clear cofactor
h: BigInteger; // cofactor. we can assign default=1, but users will just ignore it w/o validation
hEff?: BigInteger; // Number to multiply to clear cofactor
Gx: T; // base point X coordinate
Gy: T; // base point Y coordinate
allowInfinityPoint?: boolean; // bls12-381 requires it. ZERO point is valid, but invalid pubkey
Expand Down
Loading

0 comments on commit f56ab49

Please sign in to comment.