Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

as-chain

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

as-chain - npm Package Compare versions

Comparing version 0.0.54 to 0.0.55

269

assembly/crypto.ts

@@ -6,2 +6,3 @@ import { check } from "./system";

import * as env from "./env";
import { U256 } from "./bignum";

@@ -12,3 +13,3 @@ export class Checksum160 implements Packer {

pack(): u8[] {
return this.data;
return this.data.slice(0);
}

@@ -59,3 +60,3 @@

pack(): u8[] {
return this.data;
return this.data.slice(0);
}

@@ -92,3 +93,3 @@

pack(): u8[] {
return this.data;
return this.data.slice(0);
}

@@ -121,2 +122,34 @@

export class Checksum1024 implements Packer {
data!: u8[];
pack(): u8[] {
return this.data.slice(0);
}
unpack(data: u8[]): usize {
let dec = new Decoder(data);
this.data = dec.unpackBytes(128);
return dec.getPos();
}
getSize(): usize {
return 128;
}
toString(): string {
return Utils.bytesToHex(this.data);
}
@inline @operator('==')
static eq(a: Checksum1024, b: Checksum1024): bool {
return Utils.bytesCmp(a.data, b.data) == 0;
}
@inline @operator('!=')
static neq(a: Checksum1024, b: Checksum1024): bool {
return Utils.bytesCmp(a.data, b.data) != 0;
}
}
export class ECCPublicKey implements Packer {

@@ -364,3 +397,3 @@ data: Array<u8> | null;

pack(): u8[] {
return this.data;
return this.data.slice(0);
}

@@ -395,2 +428,6 @@

/**
* Key Recovery
*/
export function recoverKey(digest: Checksum256, sig: Signature): PublicKey {

@@ -414,18 +451,26 @@ let rawDigest = digest.pack();

export function assertSha256(data: u8[], hash: Checksum256): void {
env.assert_sha256(data.dataStart, data.length, hash.pack().dataStart);
export function k1Recover(sig: Signature, digest: Checksum256): PublicKey | null {
let rawSig = sig.pack();
let rawDigest = digest.pack();
let rawPub = new Array<u8>(34);
let ret = env.recover_key(rawSig.dataStart, rawDigest.dataStart, rawDigest.length, rawPub.dataStart, rawPub.length);
if (ret == -1) {
return null
}
let pub = new PublicKey();
pub.unpack(rawPub);
return pub;
}
export function assertSha1(data: u8[], hash: Checksum160): void {
env.assert_sha1(data.dataStart, data.length, hash.pack().dataStart);
/**
* Hashing
*/
export function ripemd160(data: u8[]): Checksum160 {
let hash = new Checksum160();
let rawHash = new Array<u8>(20);
env.ripemd160(data.dataStart, data.length, rawHash.dataStart);
hash.data = rawHash;
return hash;
}
export function assertSha512(data: u8[], hash: Checksum512): void {
env.assert_sha512(data.dataStart, data.length, hash.pack().dataStart);
}
export function assertRipemd160(data: u8[], hash: Checksum160): void {
env.assert_ripemd160(data.dataStart, data.length, hash.pack().dataStart);
}
export function sha256(data: u8[]): Checksum256 {

@@ -447,3 +492,3 @@ let hash = new Checksum256();

export function sha512(data: u8[]): Checksum512 {
export function sha512(data: u8[]): Checksum512 {
let hash = new Checksum512();

@@ -456,6 +501,6 @@ let rawHash = new Array<u8>(64);

export function ripemd160(data: u8[]): Checksum160 {
let hash = new Checksum160();
let rawHash = new Array<u8>(20);
env.ripemd160(data.dataStart, data.length, rawHash.dataStart);
function sha3Helper(data: u8[], keccak: boolean): Checksum256 {
let hash = new Checksum256();
let rawHash = new Array<u8>(64);
env.sha3(data.dataStart, data.length, rawHash.dataStart, rawHash.length, +keccak);
hash.data = rawHash;

@@ -465,1 +510,183 @@ return hash;

export function keccak(data: u8[]): Checksum256 {
return sha3Helper(data, true);
}
export function sha3(data: u8[]): Checksum256 {
return sha3Helper(data, false);
}
export function blake2(rounds: u32, state: Checksum512, msg: Checksum1024, t0_offset: u8[], t1_offset: u8[], final: boolean): Checksum1024 | null {
check(t0_offset.length == 8, "Blake2 t0_offset must be 8 bytes");
check(t1_offset.length == 8, "Blake2 t1_offset must be 8 bytes");
let hash = new Checksum1024();
let rawHash = new Array<u8>(1024);
const ret = env.blake2_f(rounds, state.data.dataStart, state.data.length, msg.data.dataStart, msg.data.length, t0_offset.dataStart, t0_offset.length, t1_offset.dataStart, t1_offset.length, +final, rawHash.dataStart, rawHash.length);
if (ret == -1) {
return null
}
hash.data = rawHash;
return hash;
}
/**
* Assert Hashing
*/
export function assertRipemd160(data: u8[], hash: Checksum160): void {
env.assert_ripemd160(data.dataStart, data.length, hash.pack().dataStart);
}
export function assertSha256(data: u8[], hash: Checksum256): void {
env.assert_sha256(data.dataStart, data.length, hash.pack().dataStart);
}
export function assertSha1(data: u8[], hash: Checksum160): void {
env.assert_sha1(data.dataStart, data.length, hash.pack().dataStart);
}
export function assertSha512(data: u8[], hash: Checksum512): void {
env.assert_sha512(data.dataStart, data.length, hash.pack().dataStart);
}
export function assertSha3(data: u8[], hash: Checksum256): void {
const actualHash = sha3Helper(data, false);
check(hash == actualHash, "SHA3 hash of `data` does not match given `hash`");
}
export function assertKeccak(data: u8[], hash: Checksum256): void {
const actualHash = sha3Helper(data, true);
check(hash == actualHash, "Keccak hash of `data` does not match given `hash`");
}
/**
* BN Curve
*/
export class AltBn128G1 implements Packer {
constructor(
public x: U256 = new U256(),
public y: U256 = new U256()
) {}
pack(): u8[] {
const rawX = this.x.pack();
const rawY = this.x.pack();
return rawX.concat(rawY);
}
unpack(data: u8[]): usize {
let dec = new Decoder(data);
this.x.unpack(dec.unpackBytes(32))
this.y.unpack(dec.unpackBytes(32))
return dec.getPos();
}
getSize(): usize {
return 64;
}
toString(): string {
return `${this.x}${this.y}`;
}
@inline @operator('==')
static eq(a: AltBn128G1, b: AltBn128G1): bool {
return a.x == b.x && a.y == b.y;
}
@inline @operator('!=')
static neq(a: AltBn128G1, b: AltBn128G1): bool {
return a.x != b.x || a.y != b.y;
}
}
export class AltBn128G2 implements Packer {
constructor(
public x1: U256 = new U256(),
public x2: U256 = new U256(),
public y1: U256 = new U256(),
public y2: U256 = new U256()
) {}
pack(): u8[] {
const rawX1 = this.x1.pack();
const rawX2 = this.x2.pack();
const rawY1 = this.x1.pack();
const rawY2 = this.x2.pack();
return rawX1.concat(rawX2).concat(rawY1).concat(rawY2);
}
unpack(data: u8[]): usize {
let dec = new Decoder(data);
this.x1.unpack(dec.unpackBytes(32))
this.x2.unpack(dec.unpackBytes(32))
this.y1.unpack(dec.unpackBytes(32))
this.y2.unpack(dec.unpackBytes(32))
return dec.getPos();
}
getSize(): usize {
return 128;
}
toString(): string {
return `${this.x1}${this.x2}${this.y1}${this.y2}`;
}
@inline @operator('==')
static eq(a: AltBn128G2, b: AltBn128G2): bool {
return a.x1 == b.x1 && a.x2 == b.x2 &&
a.y1 == b.y1 && a.y2 == b.y2;
}
@inline @operator('!=')
static neq(a: AltBn128G2, b: AltBn128G2): bool {
return a.x1 != b.x1 || a.x2 != b.x2 ||
a.y1 != b.y1 || a.y2 != b.y2;
}
}
export function bn128Add(op1: AltBn128G1, op2: AltBn128G1): AltBn128G1 {
const rawOp1 = op1.pack();
const rawOp2 = op2.pack();
const rawResult = new Array<u8>(32);
const ret = env.alt_bn128_add(rawOp1.dataStart, rawOp1.length, rawOp2.dataStart, rawOp2.length, rawResult.dataStart, rawResult.length)
check(ret == 0, "bn128Add error");
const result = new AltBn128G1();
result.unpack(rawResult);
return result;
}
export function bn128Mul(g1: AltBn128G1, scalar: U256): AltBn128G1 {
const rawG1 = g1.pack();
const rawScalar = scalar.pack();
const rawResult = new Array<u8>(32);
const ret = env.alt_bn128_mul(rawG1.dataStart, rawG1.length, rawScalar.dataStart, rawScalar.length, rawResult.dataStart, rawResult.length)
check(ret == 0, "bn128Mul error");
const result = new AltBn128G1();
result.unpack(rawResult);
return result;
}
export function bn128Pair(g1: AltBn128G1, g2: AltBn128G2): boolean {
const rawG1 = g1.pack();
const rawG2 = g2.pack();
const input = rawG1.concat(rawG2)
const ret = env.alt_bn128_pair(input.dataStart, input.length)
check(ret != -1, "bn128Pair error");
return ret == 0;
}
export function modExp(base: U256, exp: U256, mod: U256): U256 {
const rawBase = base.pack();
const rawExp = exp.pack();
const rawMod = mod.pack();
const rawResult = new Array<u8>(32);
const ret = env.mod_exp(rawBase.dataStart, rawBase.length, rawExp.dataStart, rawExp.length, rawMod.dataStart, rawMod.length, rawResult.dataStart, rawResult.length)
check(ret == 0, "modExp error");
const result = new U256();
result.unpack(rawResult);
return result
}

@@ -114,7 +114,15 @@ export declare function memcpy (destination: usize, source: usize, num: usize): usize;

export declare function sha512(data_ptr: usize, length: u32, hash_ptr: usize): void;
export declare function sha3(data_ptr: usize, length: u32, hash_ptr: usize, hashlen: u32, keccak: i32): void;
export declare function ripemd160(data_ptr: usize, length: u32, hash_ptr: usize): void;
export declare function blake2_f(rounds: u32, state_ptr: usize, statelen: u32, msg_ptr: usize, msglen: u32, t0_offset_ptr: usize, t0len: u32, t1_offset_ptr: usize, t1len: u32, final: i32, result_ptr: usize, resultlen: u32): i32;
export declare function recover_key(digest_ptr: usize, sig_ptr: usize, siglen: u32, pub_ptr: usize, publen: u32): i32
export declare function assert_recover_key(digest_ptr: usize, sig_ptr: usize, siglen: u32, pub_ptr: usize, publen: u32): void
export declare function k1_recover(sig_ptr: usize, siglen: u32, dig_ptr: usize, diglen: u32, pub_ptr: usize, publen: u32): i32
export declare function alt_bn128_add(op1_ptr: usize, op1len: u32, op2_ptr: usize, op2len: u32, result_ptr: usize, resultlen: u32): i32
export declare function alt_bn128_mul(g1_ptr: usize, g1len: u32, scalar_ptr: usize, scalarlen: u32, result_ptr: usize, resultlen: u32): i32
export declare function alt_bn128_pair(pairs_ptr: usize, pairslen: u32): i32
export declare function mod_exp(base_ptr: usize, baselen: u32, exp_ptr: usize, explen: u32, mod_ptr: usize, modlen: u32, result_ptr: usize, resultlen: u32): i32
//transaction

@@ -121,0 +129,0 @@ export declare function send_deferred(sender_id_u128_ptr: usize, payer: u64, serialized_transaction_ptr: usize, size: u32, replace_existing: u32): void;

2

package.json
{
"name": "as-chain",
"version": "0.0.54",
"version": "0.0.55",
"description": "chain module for assemblyscript",

@@ -5,0 +5,0 @@ "main": "js/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc