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

Storage Dynamic Arrays #1035

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
41 changes: 17 additions & 24 deletions src/cairoUtilFuncGen/storage/dynArrayIndexAccess.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import endent from 'endent';
import {
DataLocation,
FunctionCall,
Expand All @@ -10,7 +11,6 @@ import {
import { AST } from '../../ast/ast';
import { CairoType, TypeConversionContext } from '../../utils/cairoTypeSystem';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { U128_FROM_FELT, UINT256_LT } from '../../utils/importPaths';
import { createUint256TypeName } from '../../utils/nodeTemplates';
import { isDynamicArray, safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { typeNameFromTypeNode } from '../../utils/utils';
Expand Down Expand Up @@ -71,29 +71,22 @@ export class DynArrayIndexAccessGen extends StringIndexedFuncGen {
const funcName = `${arrayName}_IDX`;
return {
name: funcName,
code: [
`func ${funcName}{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(ref: felt, index: Uint256) -> (res: felt){`,
` alloc_locals;`,
` let (length) = ${lengthName}.read(ref);`,
` let (inRange) = uint256_lt(index, length);`,
` assert inRange = 1;`,
` let (existing) = ${arrayName}.read(ref, index);`,
` if (existing == 0){`,
` let (used) = WARP_USED_STORAGE.read();`,
` WARP_USED_STORAGE.write(used + ${valueCairoType.width});`,
` ${arrayName}.write(ref, index, used);`,
` return (used,);`,
` }else{`,
` return (existing,);`,
` }`,
`}`,
].join('\n'),
functionsCalled: [
this.requireImport(...U128_FROM_FELT),
this.requireImport(...UINT256_LT),
arrayDef,
arrayLength,
],
code: endent`
fn ${funcName}(ref: felt252, index: u256) -> felt252 {
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
let length = ${lengthName}::read(ref);
assert(index < length, 'Index out of bounds');
let existing = ${arrayName}::read(ref, index);
if (existing == 0) {
let used = WARP_USED_STORAGE::read();
WARP_USED_STORAGE::write(used + ${valueCairoType.width});
${arrayName}::write((ref, index), used);
return used;
} else {
return existing;
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
}
}
`,
functionsCalled: [arrayDef, arrayLength],
};
}
}
39 changes: 18 additions & 21 deletions src/cairoUtilFuncGen/storage/dynArrayPop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import endent from 'endent';
import {
ArrayType,
BytesType,
Expand All @@ -14,7 +15,7 @@ import { AST } from '../../ast/ast';
import { CairoFunctionDefinition } from '../../export';
import { CairoType, TypeConversionContext } from '../../utils/cairoTypeSystem';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { U128_FROM_FELT, UINT256_EQ, UINT256_SUB } from '../../utils/importPaths';
import { U256_FROM_FELTS } from '../../utils/importPaths';
import {
getElementType,
isDynamicArray,
Expand Down Expand Up @@ -88,31 +89,27 @@ export class DynArrayPopGen extends StringIndexedFuncGen {

const getElemLoc =
isDynamicArray(elementType) || isMapping(elementType)
? [
`let (elem_loc) = ${arrayName}.read(loc, newLen);`,
`let (elem_loc) = readId(elem_loc);`,
].join('\n')
: `let (elem_loc) = ${arrayName}.read(loc, newLen);`;
? endent`
let elem_loc = ${arrayName}::read((loc, newLen));
let elem_loc = readId(elem_loc);
`
piwonskp marked this conversation as resolved.
Show resolved Hide resolved
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
: `let elem_loc = ${arrayName}::read((loc, newLen));`;

const funcName = `${arrayName}_POP`;
return {
name: funcName,
code: [
`func ${funcName}{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(loc: felt) -> (){`,
` alloc_locals;`,
` let (len) = ${lengthName}.read(loc);`,
` let (isEmpty) = uint256_eq(len, Uint256(0,0));`,
` assert isEmpty = 0;`,
` let (newLen) = uint256_sub(len, Uint256(1,0));`,
` ${lengthName}.write(loc, newLen);`,
` ${getElemLoc}`,
` return ${deleteFunc.name}(elem_loc);`,
`}`,
].join('\n'),
code: endent`
fn ${funcName}(loc: felt252) {
let len = ${lengthName}::read(loc);
assert(len > u256_from_felts(0,0), 'Pop of empty list');
let newLen = len - u256_from_felts(1,0);
${lengthName}::write(loc, newLen);
${getElemLoc}
return ${deleteFunc.name}(elem_loc);
}
`,
functionsCalled: [
this.requireImport(...U128_FROM_FELT),
this.requireImport(...UINT256_EQ),
this.requireImport(...UINT256_SUB),
this.requireImport(...U256_FROM_FELTS),
deleteFunc,
dynArray,
dynArrayLength,
Expand Down
51 changes: 23 additions & 28 deletions src/cairoUtilFuncGen/storage/dynArrayPushWithArg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
specializeType,
} from '../../utils/nodeTypeProcessing';
import { ImplicitArrayConversion } from '../calldata/implicitArrayConversion';
import { U128_FROM_FELT, UINT256_ADD } from '../../utils/importPaths';
import { U256_FROM_FELTS } from '../../utils/importPaths';
import endent from 'endent';

export class DynArrayPushWithArgGen extends StringIndexedFuncGen {
public constructor(
Expand Down Expand Up @@ -144,40 +145,34 @@ export class DynArrayPushWithArgGen extends StringIndexedFuncGen {
const arrayName = dynArray.name;
const lengthName = dynArrayLength.name;
const funcName = `${arrayName}_PUSHV${this.generatedFunctionsDef.size}`;
const implicits =
argLoc === DataLocation.Memory
? '{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt, warp_memory: DictAccess*}'
: '{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt, bitwise_ptr: BitwiseBuiltin*}';
const implicit = argLoc === DataLocation.Memory ? '#[implicit(warp_memory)]' : '';
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved

const callWriteFunc = (cairoVar: string) =>
isDynamicArray(argType) || argType instanceof MappingType
? [`let (elem_id) = readId(${cairoVar});`, `${elementWriteDef.name}(elem_id, value);`]
: [`${elementWriteDef.name}(${cairoVar}, value);`];
? endent`
let elem_id = readId(${cairoVar});
${elementWriteDef.name}(elem_id, value);`
: `${elementWriteDef.name}(${cairoVar}, value);`;

return {
name: funcName,
code: [
`func ${funcName}${implicits}(loc: felt, value: ${inputType}) -> (){`,
` alloc_locals;`,
` let (len) = ${lengthName}.read(loc);`,
` let (newLen, carry) = uint256_add(len, Uint256(1,0));`,
` assert carry = 0;`,
` ${lengthName}.write(loc, newLen);`,
` let (existing) = ${arrayName}.read(loc, len);`,
` if (existing == 0){`,
` let (used) = WARP_USED_STORAGE.read();`,
` WARP_USED_STORAGE.write(used + ${allocationCairoType.width});`,
` ${arrayName}.write(loc, len, used);`,
...callWriteFunc('used'),
` }else{`,
...callWriteFunc('existing'),
` }`,
` return ();`,
`}`,
].join('\n'),
code: endent`
${implicit}
fn ${funcName}(loc: felt252, value: ${inputType}) {
let len = ${lengthName}::read(loc);
${lengthName}::write(loc, len + u256_from_felts(1,0));
let existing = ${arrayName}::read((loc, len));
if (existing == 0) {
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
let used = WARP_USED_STORAGE::read();
WARP_USED_STORAGE::write(used + ${allocationCairoType.width});
${arrayName}::write((loc, len), used);
${callWriteFunc('used')}
} else {
${callWriteFunc('existing')}
}
}`,
functionsCalled: [
this.requireImport(...U128_FROM_FELT),
this.requireImport(...UINT256_ADD),
this.requireImport(...U256_FROM_FELTS),
elementWriteDef,
dynArray,
dynArrayLength,
Expand Down
43 changes: 18 additions & 25 deletions src/cairoUtilFuncGen/storage/dynArrayPushWithoutArg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import endent from 'endent';
import {
ArrayType,
BytesType,
Expand All @@ -13,7 +14,7 @@ import { AST } from '../../ast/ast';
import { printTypeNode } from '../../export';
import { CairoType, TypeConversionContext } from '../../utils/cairoTypeSystem';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { U128_FROM_FELT, UINT256_ADD } from '../../utils/importPaths';
import { U256_FROM_FELTS } from '../../utils/importPaths';
import { getElementType, safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { typeNameFromTypeNode } from '../../utils/utils';
import { GeneratedFunctionInfo, StringIndexedFuncGen } from '../base';
Expand Down Expand Up @@ -71,30 +72,22 @@ export class DynArrayPushWithoutArgGen extends StringIndexedFuncGen {
const funcName = `${arrayName}_PUSH`;
return {
name: funcName,
code: [
`func ${funcName}{syscall_ptr : felt*, pedersen_ptr : HashBuiltin*, range_check_ptr : felt}(loc: felt) -> (newElemLoc: felt){`,
` alloc_locals;`,
` let (len) = ${lengthName}.read(loc);`,
` let (newLen, carry) = uint256_add(len, Uint256(1,0));`,
` assert carry = 0;`,
` ${lengthName}.write(loc, newLen);`,
` let (existing) = ${arrayName}.read(loc, len);`,
` if ((existing) == 0){`,
` let (used) = WARP_USED_STORAGE.read();`,
` WARP_USED_STORAGE.write(used + ${cairoElementType.width});`,
` ${arrayName}.write(loc, len, used);`,
` return (used,);`,
` }else{`,
` return (existing,);`,
` }`,
`}`,
].join('\n'),
functionsCalled: [
this.requireImport(...U128_FROM_FELT),
this.requireImport(...UINT256_ADD),
dynArray,
dynArrayLength,
],
code: endent`
fn ${funcName}(loc: felt252) -> felt252 {
let len = ${lengthName}::read(loc);
${lengthName}::write(loc, len + u256_from_felts(1,0));
let existing = ${arrayName}::read((loc, len));
if (existing == 0) {
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
let used = WARP_USED_STORAGE::read();
WARP_USED_STORAGE::write(used + ${cairoElementType.width});
${arrayName}::write((loc, len), used);
return used;
} else {
return existing;
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
}
}
`,
functionsCalled: [this.requireImport(...U256_FROM_FELTS), dynArray, dynArrayLength],
};
}
}
14 changes: 8 additions & 6 deletions src/cairoUtilFuncGen/storage/storageDelete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import endent from 'endent';
import {
ArrayType,
BytesType,
Expand Down Expand Up @@ -126,12 +127,13 @@ export class StorageDeleteGen extends StringIndexedFuncGen {
const cairoType = CairoType.fromSol(type, this.ast);
return {
name: funcName,
code: [
`func ${funcName}${IMPLICITS}(loc: felt){`,
...mapRange(cairoType.width, (n) => ` WARP_STORAGE.write(${add('loc', n)}, 0);`),
` return ();`,
`}`,
].join('\n'),
code: endent`
fn ${funcName}(loc: felt252){
${mapRange(cairoType.width, (n) => `WARP_STORAGE::write(${add('loc', n)}, 0);`).join(
'\n',
)}
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
}
`,
AlejandroLabourdette marked this conversation as resolved.
Show resolved Hide resolved
functionsCalled: [],
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/cairoWriter/writers/cairoContractWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class CairoContractWriter extends CairoASTNodeWriter {
WARP_STORAGE: LegacyMap::<felt252, felt252>,
WARP_USED_STORAGE: felt252,
WARP_NAMEGEN: felt252,
${otherStorageVars.map((v) => `${writer.write(v)}`).join('\n')}
${otherStorageVars.map((v) => `${writer.write(v)},`).join('\n')}
piwonskp marked this conversation as resolved.
Show resolved Hide resolved
}

fn readId(loc: felt252) -> felt252 {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/importFuncGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ import {
WARP_MEMORY,
MEMORY_TRAIT,
CONTRACT_ADDRESS,
WM_NEW,
WM_DYN_ARRAY_LENGTH,
WM_INDEX_DYN,
} from './importPaths';

export function createImport(
Expand Down Expand Up @@ -251,6 +254,9 @@ export function createImport(
case encodePath(ARRAY_TRAIT):
case encodePath(WARP_MEMORY):
case encodePath(MEMORY_TRAIT):
case encodePath(WM_NEW):
case encodePath(WM_DYN_ARRAY_LENGTH):
case encodePath(WM_INDEX_DYN):
return createFuncImport();
default:
throw new TranspileFailedError(`Import ${name} from ${path} is not defined.`);
Expand Down
14 changes: 14 additions & 0 deletions warplib/integer.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use integer::u128_try_from_felt252;
use integer::u128_to_felt252;
use integer::u256_from_felt252;
use option::OptionTrait;

fn u256_from_felts(low_felt: felt252, high_felt: felt252) -> u256 {
Expand All @@ -13,3 +15,15 @@ fn get_u128_try_from_felt_result(value: felt252) -> u128 {
return resp.unwrap();
}

fn u256_to_felt252_safe(val: u256) -> felt252 {
let low_felt252 = u128_to_felt252(val.low);
let high_felt252 = u128_to_felt252(val.high);

let two_pow_128 = 0x100000000000000000000000000000000;
let value_felt252 = low_felt252 + high_felt252 * two_pow_128;

let unsafe_value = u256_from_felt252(value_felt252);
assert(val == unsafe_value, 'Value too large for felt');

value_felt252
}
1 change: 1 addition & 0 deletions warplib/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
mod integer;
mod maths;
mod warp_memory;
mod memory;
Loading