Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

uintN->uN #967

Merged
merged 19 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/cairoUtilFuncGen/memory/memoryRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { CairoFunctionDefinition, typeNameFromTypeNode } from '../../export';
import {
CairoFelt,
CairoType,
CairoUint256,
CairoUint,
MemoryLocation,
TypeConversionContext,
} from '../../utils/cairoTypeSystem';
import { cloneASTNode } from '../../utils/cloning';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { DICT_READ, WM_READ256, WM_READ_FELT, WM_READ_ID } from '../../utils/importPaths';
import { DICT_READ, WARPLIB_MEMORY, WM_READ_FELT, WM_READ_ID } from '../../utils/importPaths';
import { createNumberLiteral, createNumberTypeName } from '../../utils/nodeTemplates';
import { isDynamicArray, safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { add, GeneratedFunctionInfo, locationIfComplexType, StringIndexedFuncGen } from '../base';
Expand Down Expand Up @@ -83,8 +83,13 @@ export class MemoryReadGen extends StringIndexedFuncGen {
funcDef = this.requireImport(...WM_READ_ID, inputs, outputs);
} else if (resultCairoType instanceof CairoFelt) {
funcDef = this.requireImport(...WM_READ_FELT, inputs, outputs);
} else if (resultCairoType.fullStringRepresentation === CairoUint256.fullStringRepresentation) {
funcDef = this.requireImport(...WM_READ256, inputs, outputs);
} else if (resultCairoType instanceof CairoUint) {
funcDef = this.requireImport(
[...WARPLIB_MEMORY],
`wm_read_${resultCairoType.nBits}`,
inputs,
outputs,
);
} else {
const funcInfo = this.getOrCreate(resultCairoType);
funcDef = createCairoGeneratedFunction(funcInfo, inputs, outputs, this.ast, this.sourceUnit, {
Expand Down
6 changes: 3 additions & 3 deletions src/cairoUtilFuncGen/memory/memoryToCalldata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/
import {
ALLOC,
NARROW_SAFE,
WM_READ256,
U128_FROM_FELT,
U32_FROM_FELT,
WARPLIB_MEMORY,
} from '../../utils/importPaths';
import {
getElementType,
Expand Down Expand Up @@ -204,7 +204,7 @@ export class MemoryToCallDataGen extends StringIndexedFuncGen {
functionsCalled: [
this.requireImport(...ALLOC),
this.requireImport(...NARROW_SAFE),
this.requireImport(...WM_READ256),
this.requireImport([...WARPLIB_MEMORY], 'wm_read_256'),
calldataDynArrayStruct,
...dynArrayReaderInfo.functionsCalled,
],
Expand Down Expand Up @@ -279,7 +279,7 @@ export class MemoryToCallDataGen extends StringIndexedFuncGen {

const memberFeltSize = CairoType.fromSol(type, this.ast).width;
return [
[`let member${index} = *warp_memory.at(u32_from_felt(${add('mem_loc', offset)}));`],
[`let member${index} = *warp_memory.at(u32_from_felt252(${add('mem_loc', offset)}));`],
[this.requireImport(...U32_FROM_FELT)],
offset + memberFeltSize,
];
Expand Down
17 changes: 11 additions & 6 deletions src/cairoUtilFuncGen/memory/memoryWrite.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Expression, FunctionCall, TypeNode, DataLocation, PointerType } from 'solc-typed-ast';
import { CairoFelt, CairoType, CairoUint256 } from '../../utils/cairoTypeSystem';
import { CairoFelt, CairoType, CairoUint } from '../../utils/cairoTypeSystem';
import { cloneASTNode } from '../../utils/cloning';
import {
createCairoGeneratedFunction,
createCallToFunction,
ParameterInfo,
} from '../../utils/functionGeneration';
import { DICT_WRITE, WM_WRITE256, WM_WRITE_FELT } from '../../utils/importPaths';
import { DICT_WRITE, WARPLIB_MEMORY, WM_WRITE_FELT } from '../../utils/importPaths';
import { safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { typeNameFromTypeNode } from '../../utils/utils';
import { add, GeneratedFunctionInfo, StringIndexedFuncGen } from '../base';
Expand Down Expand Up @@ -49,10 +49,15 @@ export class MemoryWriteGen extends StringIndexedFuncGen {
const cairoTypeToWrite = CairoType.fromSol(typeToWrite, this.ast);
if (cairoTypeToWrite instanceof CairoFelt) {
return this.requireImport(...WM_WRITE_FELT, inputs, outputs);
} else if (
cairoTypeToWrite.fullStringRepresentation === CairoUint256.fullStringRepresentation
) {
return this.requireImport(...WM_WRITE256, inputs, outputs);
}

if (cairoTypeToWrite instanceof CairoUint) {
return this.requireImport(
[...WARPLIB_MEMORY],
`wm_write_${cairoTypeToWrite.nBits}`,
inputs,
outputs,
);
}

const funcInfo = this.getOrCreate(typeToWrite);
Expand Down
21 changes: 14 additions & 7 deletions src/cairoUtilFuncGen/serialisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CairoStaticArray,
CairoStruct,
CairoType,
CairoUint,
CairoUint256,
WarpLocation,
} from '../utils/cairoTypeSystem';
Expand Down Expand Up @@ -48,24 +49,30 @@ function producePackExpression(type: CairoType): (string | Read)[] {
')',
];
}
if (type instanceof CairoStruct) {

if (type instanceof CairoUint) {
if (type.fullStringRepresentation === CairoUint256.fullStringRepresentation) {
return [
type.name,
type.toString(),
'{',
...[...type.members.entries()]
...[
['low', new CairoUint(128)],
['high', new CairoUint(128)],
]
.flatMap(([memberName, memberType]) => [
memberName,
memberName as string,
':',
'u128_from_felt252(',
...producePackExpression(memberType),
')',
...producePackExpression(memberType as CairoType),
',',
])
.slice(0, -1),
'}',
];
}
return [`core::integer::${type.toString()}_from_felt252(${Read.Felt})`];
}

if (type instanceof CairoStruct) {
return [
type.name,
'{',
Expand Down
15 changes: 13 additions & 2 deletions src/cairoUtilFuncGen/storage/storageWrite.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import endent from 'endent';
import { Expression, FunctionCall, TypeNode, DataLocation, PointerType } from 'solc-typed-ast';
import { CairoType, CairoUint256, TypeConversionContext } from '../../utils/cairoTypeSystem';
import {
CairoType,
CairoUint,
CairoUint256,
TypeConversionContext,
} from '../../utils/cairoTypeSystem';
import { cloneASTNode } from '../../utils/cloning';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { U128_TO_FELT } from '../../utils/importPaths';
import { safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { typeNameFromTypeNode } from '../../utils/utils';
import { add, GeneratedFunctionInfo, StringIndexedFuncGen } from '../base';
import { toFeltfromuXImport } from '../utils/uNselector';

export class StorageWriteGen extends StringIndexedFuncGen {
public gen(storageLocation: Expression, writeValue: Expression): FunctionCall {
Expand Down Expand Up @@ -55,11 +61,16 @@ export class StorageWriteGen extends StringIndexedFuncGen {
TypeConversionContext.StorageAllocation,
);
const cairoTypeString = cairoTypeToWrite.toString();
const fnsToImport: [string[], string][] = [];
const writeCode = cairoTypeToWrite
.serialiseMembers('value')
.map((name, index) => {
if (cairoTypeToWrite.fullStringRepresentation === CairoUint256.fullStringRepresentation) {
name = `u128_to_felt252(${name})`;
fnsToImport.push(U128_TO_FELT);
} else if (cairoTypeToWrite instanceof CairoUint) {
name = `${cairoTypeString}_to_felt252(${name})`;
fnsToImport.push(toFeltfromuXImport(cairoTypeToWrite));
}
return ` ${write(add('loc', index), name)}`;
})
Expand All @@ -74,7 +85,7 @@ export class StorageWriteGen extends StringIndexedFuncGen {
return value;
}
`,
functionsCalled: [this.requireImport(...U128_TO_FELT)],
functionsCalled: [...fnsToImport.map((imp) => this.requireImport(...imp))],
};
return funcInfo;
}
Expand Down
203 changes: 203 additions & 0 deletions src/cairoUtilFuncGen/utils/uNselector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { CairoUint } from '../../export';
import {
U8_TO_FELT,
U16_TO_FELT,
U24_TO_FELT,
U32_TO_FELT,
U40_TO_FELT,
U48_TO_FELT,
U56_TO_FELT,
U64_TO_FELT,
U72_TO_FELT,
U80_TO_FELT,
U88_TO_FELT,
U96_TO_FELT,
U104_TO_FELT,
U112_TO_FELT,
U120_TO_FELT,
U128_TO_FELT,
U136_TO_FELT,
U144_TO_FELT,
U152_TO_FELT,
U160_TO_FELT,
U168_TO_FELT,
U176_TO_FELT,
U184_TO_FELT,
U192_TO_FELT,
U200_TO_FELT,
U208_TO_FELT,
U216_TO_FELT,
U224_TO_FELT,
U232_TO_FELT,
U240_TO_FELT,
U248_TO_FELT,
U8_FROM_FELT,
U16_FROM_FELT,
U24_FROM_FELT,
U32_FROM_FELT,
U40_FROM_FELT,
U48_FROM_FELT,
U56_FROM_FELT,
U64_FROM_FELT,
U72_FROM_FELT,
U80_FROM_FELT,
U88_FROM_FELT,
U96_FROM_FELT,
U104_FROM_FELT,
U112_FROM_FELT,
U120_FROM_FELT,
U128_FROM_FELT,
U136_FROM_FELT,
U144_FROM_FELT,
U152_FROM_FELT,
U160_FROM_FELT,
U168_FROM_FELT,
U176_FROM_FELT,
U184_FROM_FELT,
U192_FROM_FELT,
U200_FROM_FELT,
U208_FROM_FELT,
U216_FROM_FELT,
U224_FROM_FELT,
U232_FROM_FELT,
U240_FROM_FELT,
U248_FROM_FELT,
} from '../../utils/importPaths';

export const toFeltfromuXImport = (uNtype: CairoUint) => {
switch (uNtype.toString()) {
case 'u8':
return U8_TO_FELT;
case 'u16':
return U16_TO_FELT;
case 'u24':
return U24_TO_FELT;
case 'u32':
return U32_TO_FELT;
case 'u40':
return U40_TO_FELT;
case 'u48':
return U48_TO_FELT;
case 'u56':
return U56_TO_FELT;
case 'u64':
return U64_TO_FELT;
case 'u72':
return U72_TO_FELT;
case 'u80':
return U80_TO_FELT;
case 'u88':
return U88_TO_FELT;
case 'u96':
return U96_TO_FELT;
case 'u104':
return U104_TO_FELT;
case 'u112':
return U112_TO_FELT;
case 'u120':
return U120_TO_FELT;
case 'u128':
return U128_TO_FELT;
case 'u136':
return U136_TO_FELT;
case 'u144':
return U144_TO_FELT;
case 'u152':
return U152_TO_FELT;
case 'u160':
return U160_TO_FELT;
case 'u168':
return U168_TO_FELT;
case 'u176':
return U176_TO_FELT;
case 'u184':
return U184_TO_FELT;
case 'u192':
return U192_TO_FELT;
case 'u200':
return U200_TO_FELT;
case 'u208':
return U208_TO_FELT;
case 'u216':
return U216_TO_FELT;
case 'u224':
return U224_TO_FELT;
case 'u232':
return U232_TO_FELT;
case 'u240':
return U240_TO_FELT;
case 'u248':
return U248_TO_FELT;
default:
throw new Error('Invalid CairoUint type');
}
};

export const getFeltfromuXImport = (uNtype: CairoUint) => {
switch (uNtype.toString()) {
case 'u8':
return U8_FROM_FELT;
case 'u16':
return U16_FROM_FELT;
case 'u24':
return U24_FROM_FELT;
case 'u32':
return U32_FROM_FELT;
case 'u40':
return U40_FROM_FELT;
case 'u48':
return U48_FROM_FELT;
case 'u56':
return U56_FROM_FELT;
case 'u64':
return U64_FROM_FELT;
case 'u72':
return U72_FROM_FELT;
case 'u80':
return U80_FROM_FELT;
case 'u88':
return U88_FROM_FELT;
case 'u96':
return U96_FROM_FELT;
case 'u104':
return U104_FROM_FELT;
case 'u112':
return U112_FROM_FELT;
case 'u120':
return U120_FROM_FELT;
case 'u128':
return U128_FROM_FELT;
case 'u136':
return U136_FROM_FELT;
case 'u144':
return U144_FROM_FELT;
case 'u152':
return U152_FROM_FELT;
case 'u160':
return U160_FROM_FELT;
case 'u168':
return U168_FROM_FELT;
case 'u176':
return U176_FROM_FELT;
case 'u184':
return U184_FROM_FELT;
case 'u192':
return U192_FROM_FELT;
case 'u200':
return U200_FROM_FELT;
case 'u208':
return U208_FROM_FELT;
case 'u216':
return U216_FROM_FELT;
case 'u224':
return U224_FROM_FELT;
case 'u232':
return U232_FROM_FELT;
case 'u240':
return U240_FROM_FELT;
case 'u248':
return U248_FROM_FELT;
default:
throw new Error('Invalid CairoUint type');
}
};
Loading