Skip to content

Commit

Permalink
feat: Adds (sha256, sha512, sha3, blake2b, keccak) hash expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Mar 8, 2022
1 parent 52b4b34 commit 1f5675f
Show file tree
Hide file tree
Showing 7 changed files with 740 additions and 144 deletions.
6 changes: 6 additions & 0 deletions src/core/enums/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export enum ExpressionAtom {
isVariant = 'isVariant',
cases_arg = 'cases_arg',
variant_arg = 'variant_arg',
// Crypto
blake2b = 'blake2b',
sha256 = 'sha256',
sha512 = 'sha512',
sha3 = 'sha3',
keccak = 'keccak',
// Generic
isNat = 'isNat',
contains = 'contains',
Expand Down
93 changes: 93 additions & 0 deletions src/expression/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { LineInfo } from '../misc/utils';
import { IExpression } from '../typings/expression';
import { Expression } from '../core/expression';
import ExpressionAtom from '../core/enums/expression';

/**
* Compute a Blake2B cryptographic hash
*
* ```typescript
* BLAKE2B(Bytes("0x01"));
* ```
*
* @category | Crypto
*
* @param bytes An expression that evaluates to a bytes value.
* @param {LineInfo} line Source code line information (Used in error messages)
*
* @returns {IExpression} An expression
*/
export const BLAKE2B = (bytes: IExpression, line = new LineInfo()) =>
new Expression(ExpressionAtom.blake2b, bytes, line);

/**
* Compute a SHA-256 cryptographic hash
*
* ```typescript
* SHA256(Bytes("0x01"));
* ```
*
* @category | Crypto
*
* @param bytes An expression that evaluates to a bytes value.
* @param {LineInfo} line Source code line information (Used in error messages)
*
* @returns {IExpression} An expression
*/
export const SHA256 = (bytes: IExpression, line = new LineInfo()) => new Expression(ExpressionAtom.sha256, bytes, line);

/**
* Compute a SHA-512 cryptographic hash
*
* ```typescript
* SHA512(Bytes("0x01"));
* ```
*
* @category | Crypto
*
* @param bytes An expression that evaluates to a bytes value.
* @param {LineInfo} line Source code line information (Used in error messages)
*
* @returns {IExpression} An expression
*/
export const SHA512 = (bytes: IExpression, line = new LineInfo()) => new Expression(ExpressionAtom.sha512, bytes, line);

/**
* Compute a SHA3-256 cryptographic hash
*
* ```typescript
* SHA3(Bytes("0x01"));
* ```
*
* @category | Crypto
*
* @param bytes An expression that evaluates to a bytes value.
* @param {LineInfo} line Source code line information (Used in error messages)
*
* @returns {IExpression} An expression
*/
export const SHA3 = (bytes: IExpression, line = new LineInfo()) => new Expression(ExpressionAtom.sha3, bytes, line);

/**
* Compute a Keccak-256 cryptographic hash
*
* ```typescript
* KECCAK(Bytes("0x01"));
* ```
*
* @category | Crypto
*
* @param bytes An expression that evaluates to a bytes value.
* @param {LineInfo} line Source code line information (Used in error messages)
*
* @returns {IExpression} An expression
*/
export const KECCAK = (bytes: IExpression, line = new LineInfo()) => new Expression(ExpressionAtom.keccak, bytes, line);

export const Crypto = {
BLAKE2B,
SHA256,
SHA512,
SHA3,
KECCAK,
};
1 change: 1 addition & 0 deletions src/expression/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './blockchain_properties';
export * from './comparison';
export * from './contract';
export * from './crypto';
export * from './equality';
export * from './list';
export * from './literal';
Expand Down
169 changes: 97 additions & 72 deletions tests/distributables/commonjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,112 @@ const contract = new Contract().setStorage(Nat(0)).addEntrypoint(
]),
);

assert.deepEqual(SmartML.compileContract(contract), [
{
prim: 'storage',
args: [
{
prim: 'nat',
},
],
},
{
prim: 'parameter',
args: [
{
prim: 'nat',
annots: ['%ep1'],
},
],
},
{
prim: 'code',
args: [
[
assert.deepEqual(SmartML.compileContract(contract), {
micheline:
'parameter (nat %ep1);\n' +
'storage nat;\n' +
'code\n' +
' {\n' +
' CAR; # @parameter\n' +
' # == ep1 ==\n' +
` # some_address = sp.local("some_address", sp.address('tz1')) # @parameter\n` +
' PUSH address "tz1"; # address : @parameter\n' +
" # sp.verify(some_address.value == sp.sender, 'Not Admin!') # address : @parameter\n" +
' SENDER; # @sender : address : @parameter\n' +
' COMPARE; # int : @parameter\n' +
' EQ; # bool : @parameter\n' +
' IF\n' +
' {}\n' +
' {\n' +
' PUSH string "Not Admin!"; # string : @parameter\n' +
' FAILWITH; # FAILED\n' +
' }; # @parameter\n' +
' # self.data = params # @parameter\n' +
' NIL operation; # list operation : @parameter\n' +
' PAIR; # pair (list operation) @parameter\n' +
' };',
json: [
{
prim: 'storage',
args: [
{
prim: 'CAR',
},
{
prim: 'PUSH',
args: [
{
prim: 'address',
},
{
string: 'tz1',
},
],
},
{
prim: 'SENDER',
},
{
prim: 'COMPARE',
prim: 'nat',
},
],
},
{
prim: 'parameter',
args: [
{
prim: 'EQ',
prim: 'nat',
annots: ['%ep1'],
},
{
prim: 'IF',
args: [
[],
[
],
},
{
prim: 'code',
args: [
[
{
prim: 'CAR',
},
{
prim: 'PUSH',
args: [
{
prim: 'PUSH',
args: [
{
prim: 'string',
},
{
string: 'Not Admin!',
},
],
prim: 'address',
},
{
prim: 'FAILWITH',
string: 'tz1',
},
],
],
},
{
prim: 'NIL',
args: [
{
prim: 'operation',
},
],
},
{
prim: 'PAIR',
},
},
{
prim: 'SENDER',
},
{
prim: 'COMPARE',
},
{
prim: 'EQ',
},
{
prim: 'IF',
args: [
[],
[
{
prim: 'PUSH',
args: [
{
prim: 'string',
},
{
string: 'Not Admin!',
},
],
},
{
prim: 'FAILWITH',
},
],
],
},
{
prim: 'NIL',
args: [
{
prim: 'operation',
},
],
},
{
prim: 'PAIR',
},
],
],
],
},
]);
},
],
});

console.info('[Passes] - CommonJS');
Loading

0 comments on commit 1f5675f

Please sign in to comment.