Skip to content

Commit

Permalink
updated hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Aug 14, 2024
1 parent 8b3a6e2 commit 650f242
Show file tree
Hide file tree
Showing 23 changed files with 232 additions and 193 deletions.
21 changes: 14 additions & 7 deletions packages/onchain/src/IR/IRNodes/IRApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { blake2b_128 } from "@harmoniclabs/crypto";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { floatAsBytes, isMurmurHash, murmurHash } from "../murmur";

export interface IRAppMeta extends BaseIRMetadata {
__src__?: string | undefined
Expand All @@ -20,7 +21,7 @@ export class IRApp
fn!: IRTerm;
arg!: IRTerm;

readonly hash!: Uint8Array;
readonly hash!: number;
markHashAsInvalid!: () => void;
isHashPresent: () => boolean;

Expand All @@ -32,7 +33,7 @@ export class IRApp
_fn_: IRTerm,
_arg_: IRTerm,
meta: IRAppMeta = {},
_unsafeHash?: Uint8Array
_unsafeHash?: number
)
{
if( !isIRTerm( _fn_ ) )
Expand Down Expand Up @@ -63,18 +64,24 @@ export class IRApp
fn.parent = this;
arg.parent = this;

let hash: Uint8Array | undefined = _unsafeHash;
let hash: number | undefined = isMurmurHash( _unsafeHash ) ? _unsafeHash : undefined;
Object.defineProperty(
this, "hash",
{
get: () => {
if(!( hash instanceof Uint8Array ))
if( !isMurmurHash( hash ) )
{
// basically a merkle tree
hash = blake2b_128( concatUint8Arr( IRApp.tag, fn.hash, arg.hash ) );
hash = murmurHash(
concatUint8Arr(
IRApp.tag,
floatAsBytes( fn.hash ),
floatAsBytes( arg.hash )
)
);
}
// return a copy
return hash.slice()
return hash;
},
set: () => {},
enumerable: true,
Expand All @@ -84,7 +91,7 @@ export class IRApp

Object.defineProperty(
this, "isHashPresent", {
value: () => hash instanceof Uint8Array,
value: () => isMurmurHash( hash ),
writable: false,
enumerable: true,
configurable: false
Expand Down
21 changes: 12 additions & 9 deletions packages/onchain/src/IR/IRNodes/IRCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { mapArrayLike } from "./utils/mapArrayLike";
import { isIRTerm } from "../utils";
import { makeArrayLikeProxy } from "./utils/makeArrayLikeProxy";
import { MutArrayLike } from "../utils/MutArrayLike";
import { floatAsBytes, isMurmurHash, murmurHash } from "../murmur";

export interface IRCaseMeta extends BaseIRMetadata {}

Expand All @@ -19,7 +20,7 @@ export class IRCase
constrTerm!: IRTerm;
continuations!: MutArrayLike<IRTerm>;

readonly hash!: Uint8Array;
readonly hash!: number;
markHashAsInvalid!: () => void;
isHashPresent: () => boolean;

Expand All @@ -33,7 +34,7 @@ export class IRCase
constrTerm: IRTerm,
continuations: ArrayLike<IRTerm>,
meta: IRCaseMeta = {},
_unsafeHash?: Uint8Array
_unsafeHash?: number
)
{
const self = this;
Expand Down Expand Up @@ -93,24 +94,26 @@ export class IRCase
}
);

let hash: Uint8Array | undefined = undefined;
let hash: number | undefined = isMurmurHash( _unsafeHash ) ? _unsafeHash : undefined;
Object.defineProperty(
this, "hash",
{
get: () => {
if(!( hash instanceof Uint8Array ))
if(!isMurmurHash( hash ))
{
// basically a merkle tree
hash = blake2b_128(
hash = murmurHash(
concatUint8Arr(
IRCase.tag,
self.constrTerm.hash,
...mapArrayLike( self.continuations, f => f.hash )
floatAsBytes(
self.constrTerm.hash
),
...mapArrayLike( self.continuations, f => floatAsBytes( f.hash ) )
)
);
}
// return a copy
return hash.slice()
return hash;
},
set: () => {},
enumerable: true,
Expand All @@ -119,7 +122,7 @@ export class IRCase
);
Object.defineProperty(
this, "isHashPresent", {
value: () => hash instanceof Uint8Array,
value: () => isMurmurHash( hash ),
writable: false,
enumerable: true,
configurable: false
Expand Down
15 changes: 8 additions & 7 deletions packages/onchain/src/IR/IRNodes/IRConst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { termTypeToString } from "../../pluts/type_system/utils";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { isMurmurHash, murmurHash } from "../murmur";

export type IRConstValue
= CanBeUInteger
Expand All @@ -40,7 +41,7 @@ export interface IRConstMetadata extends BaseIRMetadata {}
export class IRConst
implements Cloneable<IRConst>, IHash, IIRParent, ToJson
{
readonly hash: Uint8Array;
readonly hash: number;
markHashAsInvalid: () => void;
isHashPresent: () => boolean;

Expand All @@ -51,7 +52,7 @@ export class IRConst

parent: IRParentTerm | undefined;

constructor( t: TermType, v: IRConstValue, _unsafeHash?: Uint8Array )
constructor( t: TermType, v: IRConstValue, _unsafeHash?: number )
{
if(
!isWellFormedType( t ) ||
Expand Down Expand Up @@ -127,21 +128,21 @@ export class IRConst
}
);

let hash: Uint8Array | undefined = _unsafeHash;
let hash: number | undefined = isMurmurHash( _unsafeHash ) ? _unsafeHash : undefined;
Object.defineProperty(
this, "hash", {
get: () => {
if(!( hash instanceof Uint8Array ))
if(!isMurmurHash( hash ) )
{
hash = blake2b_128(
hash = murmurHash(
concatUint8Arr(
IRConst.tag,
new Uint8Array( termTyToConstTy( this.type ) ),
serializeIRConstValue( this.value, this.type )
)
)
}
return hash.slice();
return hash;
},
set: () => {},
enumerable: true,
Expand All @@ -150,7 +151,7 @@ export class IRConst
);
Object.defineProperty(
this, "isHashPresent", {
value: () => hash instanceof Uint8Array,
value: () => isMurmurHash( hash ),
writable: false,
enumerable: true,
configurable: false
Expand Down
17 changes: 9 additions & 8 deletions packages/onchain/src/IR/IRNodes/IRConstr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { mapArrayLike } from "./utils/mapArrayLike";
import { isIRTerm } from "../utils";
import { makeArrayLikeProxy } from "./utils/makeArrayLikeProxy";
import { MutArrayLike } from "../utils/MutArrayLike";
import { floatAsBytes, isMurmurHash, murmurHash } from "../murmur";

export interface IRConstrMeta extends BaseIRMetadata {}

Expand All @@ -21,7 +22,7 @@ export class IRConstr
readonly index!: bigint;
fields!: MutArrayLike<IRTerm>;

readonly hash!: Uint8Array;
readonly hash!: number;
markHashAsInvalid!: () => void;
isHashPresent: () => boolean;

Expand All @@ -35,7 +36,7 @@ export class IRConstr
index: number | bigint,
fields: ArrayLike<IRTerm>,
meta: IRConstrMeta = {},
_unsafeHash?: Uint8Array
_unsafeHash?: number
)
{
const self = this;
Expand Down Expand Up @@ -84,24 +85,24 @@ export class IRConstr
}
);

let hash: Uint8Array | undefined = _unsafeHash;
let hash: number | undefined = isMurmurHash( _unsafeHash ) ? _unsafeHash : undefined;
Object.defineProperty(
this, "hash",
{
get: () => {
if(!( hash instanceof Uint8Array ))
if(!isMurmurHash( hash ))
{
// basically a merkle tree
hash = blake2b_128(
hash = murmurHash(
concatUint8Arr(
IRConstr.tag,
positiveIntAsBytes( this.index ),
...mapArrayLike( this.fields, f => f.hash )
...mapArrayLike( this.fields, f => floatAsBytes( f.hash ) )
)
);
}
// return a copy
return hash.slice()
return hash;
},
set: () => {},
enumerable: true,
Expand All @@ -110,7 +111,7 @@ export class IRConstr
);
Object.defineProperty(
this, "isHashPresent", {
value: () => hash instanceof Uint8Array,
value: () => isMurmurHash( hash ),
writable: false,
enumerable: true,
configurable: false
Expand Down
19 changes: 11 additions & 8 deletions packages/onchain/src/IR/IRNodes/IRDelayed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import { isIRTerm } from "../utils/isIRTerm";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { floatAsBytes, isMurmurHash, murmurHash } from "../murmur";

export interface IRDelayedMetadata extends BaseIRMetadata {}

export class IRDelayed
implements Cloneable<IRDelayed>, IHash, IIRParent, ToJson
{
delayed!: IRTerm
readonly hash!: Uint8Array
readonly hash!: number
markHashAsInvalid!: () => void;
isHashPresent: () => boolean;

readonly meta: IRDelayedMetadata

parent: IRParentTerm | undefined;

constructor( delayed: IRTerm, _unsafeHash?: Uint8Array )
constructor( delayed: IRTerm, _unsafeHash?: number )
{
Object.defineProperty(
this, "meta", {
Expand All @@ -36,27 +37,29 @@ export class IRDelayed
}
);

let hash: Uint8Array | undefined = _unsafeHash;
let hash: number | undefined = isMurmurHash( _unsafeHash ) ? _unsafeHash : undefined;
Object.defineProperty(
this, "hash",
{
get: () => {
if(!(hash instanceof Uint8Array))
if(!isMurmurHash( hash ))
{
hash = blake2b_128(
hash = murmurHash(
concatUint8Arr(
IRDelayed.tag,
this.delayed.hash
floatAsBytes(
this.delayed.hash
)
)
);
}
return hash.slice();
return hash;
}
}
);
Object.defineProperty(
this, "isHashPresent", {
value: () => hash instanceof Uint8Array,
value: () => isMurmurHash( hash ),
writable: false,
enumerable: true,
configurable: false
Expand Down
7 changes: 4 additions & 3 deletions packages/onchain/src/IR/IRNodes/IRError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import { isIRTerm } from "../utils/isIRTerm";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { murmurHash } from "../murmur";

const irErrorBitTag = new Uint8Array([ 0b0000_0111 ]);
const errorHash = blake2b_128( irErrorBitTag.slice() )
const errorHash = murmurHash( irErrorBitTag.slice() )

export interface IRErrorMetadata extends BaseIRMetadata {}

export class IRError
implements Cloneable<IRError>, IHash, IIRParent, ToJson
{
readonly hash!: Uint8Array;
readonly hash!: number;
markHashAsInvalid!: () => void;
isHashPresent: () => boolean;

Expand Down Expand Up @@ -80,7 +81,7 @@ export class IRError

Object.defineProperty(
this, "hash", {
get: () => errorHash.slice(),
get: () => errorHash,
set: () => {},
enumerable: true,
configurable: false,
Expand Down
Loading

0 comments on commit 650f242

Please sign in to comment.