Skip to content

Commit

Permalink
add plutus v3 builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed May 23, 2024
1 parent 2aadb6f commit fdb6ec2
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 19 deletions.
13 changes: 13 additions & 0 deletions packages/onchain/src/pluts/PTypes/PBlsG1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BlsG1 } from "@harmoniclabs/crypto";
import { PType } from "../PType";

export class PBlsG1 extends PType
{
private _pblsg1: BlsG1

constructor( bs: BlsG1 )
{
super();
this._pblsg1 = bs;
}
}
13 changes: 13 additions & 0 deletions packages/onchain/src/pluts/PTypes/PBlsG2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BlsG2 } from "@harmoniclabs/crypto";
import { PType } from "../PType";

export class PBlsG2 extends PType
{
private _pblsg2: BlsG2

constructor( bs: BlsG2 )
{
super();
this._pblsg2 = bs;
}
}
13 changes: 13 additions & 0 deletions packages/onchain/src/pluts/PTypes/PBlsMlRes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BlsResult } from "@harmoniclabs/crypto";
import { PType } from "../PType";

export class PBlsMlRes extends PType
{
private _pblsRes: BlsResult

constructor( bs: BlsResult )
{
super();
this._pblsRes = bs;
}
}
1 change: 0 additions & 1 deletion packages/onchain/src/pluts/PTypes/PByteString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ export class PByteString extends PDataRepresentable
super();
this._pbytestring = bs;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { PBlsG1 } from "../../../PTypes/PBlsG1";
import { blsG1, bool, bs, fn, int } from "../../../type_system/types";
import { IRNative } from "../../../../IR/IRNodes/IRNative";
import { Term } from "../../../Term";
import { PFn } from "../../../PTypes/PFn/PFn";
import { PInt } from "../../../PTypes/PInt";
import type { PByteString } from "../../../PTypes/PByteString";
import { PBool } from "../../../PTypes/PBool";
import { addApplications } from "../addApplications";

export const bls12_381_G1_element_add = addApplications<[ PBlsG1, PBlsG1 ], PBlsG1>(
new Term(
fn([ blsG1, blsG1 ], blsG1 ),
_dbn => {
return IRNative.bls12_381_G1_add;
}
)
);

export const bls12_381_G1_element_neg = addApplications<[ PBlsG1 ], PBlsG1>(
new Term(
fn([ blsG1 ], blsG1 ),
_dbn => {
return IRNative.bls12_381_G1_neg;
}
)
);

export const bls12_381_G1_element_scalarMul = addApplications<[ PInt, PBlsG1 ], PBlsG1>(
new Term(
fn([ int, blsG1 ], blsG1 ),
_dbn => {
return IRNative.bls12_381_G1_scalarMul;
}
)
);

export const bls12_381_G1_element_eq = addApplications<[ PBlsG1, PBlsG1 ], PBool>(
new Term(
fn([ blsG1, blsG1 ], bool ),
_dbn => {
return IRNative.bls12_381_G1_equal;
}
)
);

export const bls12_381_G1_element_hashToGroup = addApplications<[ PByteString, PByteString ], PBlsG1>(
new Term(
fn([ bs, bs ], blsG1 ),
_dbn => {
return IRNative.bls12_381_G1_hashToGroup;
}
)
);

export const bls12_381_G1_element_compress = addApplications<[ PBlsG1 ], PByteString>(
new Term(
fn([ blsG1 ], bs ),
_dbn => {
return IRNative.bls12_381_G1_compress;
}
)
);

export const bls12_381_G1_element_uncompress = addApplications<[ PByteString ], PBlsG1>(
new Term(
fn([ bs ], blsG1 ),
_dbn => {
return IRNative.bls12_381_G1_uncompress;
}
)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { PBlsG2 } from "../../../PTypes/PBlsG2";
import { blsG2, bool, bs, fn, int } from "../../../type_system/types";
import { IRNative } from "../../../../IR/IRNodes/IRNative";
import { Term } from "../../../Term";
import { PFn } from "../../../PTypes/PFn/PFn";
import { PInt } from "../../../PTypes/PInt";
import type { PByteString } from "../../../PTypes/PByteString";
import { PBool } from "../../../PTypes/PBool";
import { addApplications } from "../addApplications";

export const bls12_381_G2_element_add = addApplications<[ PBlsG2, PBlsG2 ], PBlsG2>(
new Term(
fn([ blsG2, blsG2 ], blsG2 ),
_dbn => {
return IRNative.bls12_381_G2_add;
}
)
);

export const bls12_381_G2_element_neg = addApplications<[ PBlsG2 ], PBlsG2>(
new Term(
fn([ blsG2 ], blsG2 ),
_dbn => {
return IRNative.bls12_381_G2_neg;
}
)
);

export const bls12_381_G2_element_scalarMul = addApplications<[ PInt, PBlsG2 ], PBlsG2>(
new Term(
fn([ int, blsG2 ], blsG2 ),
_dbn => {
return IRNative.bls12_381_G2_scalarMul;
}
)
);

export const bls12_381_G2_element_eq = addApplications<[ PBlsG2, PBlsG2 ], PBool>(
new Term(
fn([ blsG2, blsG2 ], bool ),
_dbn => {
return IRNative.bls12_381_G2_equal;
}
)
);

export const bls12_381_G2_element_hashToGroup = addApplications<[ PByteString, PByteString ], PBlsG2>(
new Term(
fn([ bs, bs ], blsG2 ),
_dbn => {
return IRNative.bls12_381_G2_hashToGroup;
}
)
);

export const bls12_381_G2_element_compress = addApplications<[ PBlsG2 ], PByteString>(
new Term(
fn([ blsG2 ], bs ),
_dbn => {
return IRNative.bls12_381_G2_compress;
}
)
);

export const bls12_381_G2_element_uncompress = addApplications<[ PByteString ], PBlsG2>(
new Term(
fn([ bs ], blsG2 ),
_dbn => {
return IRNative.bls12_381_G2_uncompress;
}
)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PBlsG2 } from "../../../PTypes/PBlsG2";
import { blsG1, blsG2, blsResult, bool, fn } from "../../../type_system/types";
import { IRNative } from "../../../../IR/IRNodes/IRNative";
import { Term } from "../../../Term";
import { PBool } from "../../../PTypes/PBool";
import { PBlsMlRes } from "../../../PTypes/PBlsMlRes";
import { addApplications } from "../addApplications";

export const bls12_381_millerLoop = addApplications<[ PBlsG2, PBlsG2 ], PBlsMlRes>(
new Term(
fn([ blsG1, blsG2 ], blsResult ),
_dbn => {
return IRNative.bls12_381_millerLoop;
}
)
);

export const bls12_381_mulMlResult = addApplications<[ PBlsMlRes, PBlsMlRes ], PBlsMlRes>(
new Term(
fn([ blsResult, blsResult ], blsResult ),
_dbn => {
return IRNative.bls12_381_mulMlResult;
}
)
);

export const bls12_381_finalVerify = addApplications<[ PBlsMlRes, PBlsMlRes ], PBool>(
new Term(
fn([ blsResult, blsResult ], bool ),
_dbn => {
return IRNative.bls12_381_finalVerify;
}
)
);
69 changes: 68 additions & 1 deletion packages/onchain/src/pluts/lib/builtins/bs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { defineReadOnlyProperty } from "@harmoniclabs/obj-utils";
import { IRNative } from "../../../../IR/IRNodes/IRNative";
import { IRHoisted } from "../../../../IR/IRNodes/IRHoisted";
import { IRApp } from "../../../../IR/IRNodes/IRApp";
import { IRConst } from "../../../../IR/IRNodes/IRConst";


export type ByteStrBinOPToBS = Term< PLam< PByteString, PLam< PByteString, PByteString>>>
Expand Down Expand Up @@ -279,6 +280,22 @@ export const pblake2b_256: TermFn<[ PByteString ], PByteString> =
)
);

export const pkeccak_256: TermFn<[ PByteString ], PByteString> =
addApplications<[ PByteString ], PByteString>(
new Term(
lam( bs, bs ),
_dbn => IRNative.keccak_256
)
);

export const pblake2b_224: TermFn<[ PByteString ], PByteString> =
addApplications<[ PByteString ], PByteString>(
new Term(
lam( bs, bs ),
_dbn => IRNative.blake2b_224
)
);

/**
* performs cryptographic signature verification using the Ed25519 scheme
*
Expand Down Expand Up @@ -348,4 +365,54 @@ addApplications<[ PByteString, PByteString, PByteString ], PBool>(
fn([ bs, bs, bs ], bool),
_dbn => IRNative.verifySchnorrSecp256k1Signature
)
);
);

// bitwise - plutus v3

export const pencodeIntBE: TermFn<[ PInt, PInt ], PByteString> =
addApplications<[ PInt, PInt ], PByteString>(
new Term(
fn([ int, int ], bs),
_dbn => new IRApp(
IRNative.integerToByteString,
IRConst.bool( true ) // big endian
)
)
);

export const pencodeIntLE: TermFn<[ PInt, PInt ], PByteString> =
addApplications<[ PInt, PInt ], PByteString>(
new Term(
fn([ int, int ], bs),
_dbn => new IRApp(
IRNative.integerToByteString,
IRConst.bool( false ) // big endian
)
)
);

export const pdecodeIntBE: TermFn<[ PByteString ], PInt> =
addApplications<[ PByteString ], PInt>(
new Term(
fn([ bs ], int),
_dbn => new IRHoisted(
new IRApp(
IRNative.byteStringToInteger,
IRConst.bool( true ) // big endian
)
)
)
);

export const pdecodeIntLE: TermFn<[ PByteString ], PInt> =
addApplications<[ PByteString ], PInt>(
new Term(
fn([ bs ], int),
_dbn => new IRHoisted(
new IRApp(
IRNative.byteStringToInteger,
IRConst.bool( false ) // big endian
)
)
)
);
5 changes: 4 additions & 1 deletion packages/onchain/src/pluts/lib/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export * from "./pchooseUnit";
export * from "./ppairData";
export * from "./pprepend";
export * from "./ptrace";
export * from "./str";
export * from "./str";
export * from "./bls12_381_G1_element";
export * from "./bls12_381_G2_element";
export * from "./bls12_381_MlResult";
2 changes: 1 addition & 1 deletion packages/onchain/src/pluts/type_system/cloneTermType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cloneSopDef, cloneStructDef } from "../PTypes/PStruct/cloneStructDef";
import { cloneStructDef } from "../PTypes/PStruct/cloneStructDef";
import { GenericTermType, PrimType, lam, struct, pair, list, delayed, asData, alias, sop } from "./types";

export function cloneTermType<T extends GenericTermType>( t: T ): T
Expand Down
22 changes: 21 additions & 1 deletion packages/onchain/src/pluts/type_system/kinds/isPrimTypeTag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BasePrimType, DataRepPrimType, PrimType } from "../types";
import { BaseDataRepPrimType, BasePrimType, DataRepPrimType, PrimType } from "../types";

export function isPrimTypeTag( t: any ): t is PrimType
{
Expand All @@ -25,6 +25,26 @@ export function isPrimTypeTag( t: any ): t is PrimType
}

export function isBasePrimType( t: any ): t is BasePrimType
{
switch( t as PrimType )
{
case PrimType.BS:
case PrimType.bls12_381_G1_element:
case PrimType.bls12_381_G2_element:
case PrimType.bls12_381_MlResult:
case PrimType.Bool:
case PrimType.Data:
case PrimType.Int:
case PrimType.Str:
case PrimType.Unit:
return true;
default:
return false;
}
}


export function isBaseDataRepPrimType( t: any ): t is BaseDataRepPrimType
{
switch( t as PrimType )
{
Expand Down
Loading

0 comments on commit fdb6ec2

Please sign in to comment.