Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use u256 const syntax #499

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions starknet/src/space/space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod Space {
// TODO: Lots of copying, maybe figure out how to pass snapshots to events/storage writers.
self._proposals.write(proposal_id, proposal);

self._next_proposal_id.write(proposal_id + u256 { low: 1_u128, high: 0_u128 });
self._next_proposal_id.write(proposal_id + 1_u256);

ProposalCreated(
proposal_id, author, snap_proposal, @execution_strategy.params, @metadata_URI
Expand Down Expand Up @@ -269,7 +269,7 @@ mod Space {
proposal.active_voting_strategies
);

assert(voting_power > u256 { low: 0_u128, high: 0_u128 }, 'User has no voting power');
assert(voting_power > 0_u256, 'User has no voting power');
self
._vote_power
.write(
Expand Down Expand Up @@ -510,7 +510,7 @@ mod Space {
_set_proposal_validation_strategy(ref self, _proposal_validation_strategy.clone());
_add_voting_strategies(ref self, _voting_strategies.clone());
_add_authenticators(ref self, _authenticators.clone());
self._next_proposal_id.write(u256 { low: 1_u128, high: 0_u128 });
self._next_proposal_id.write(1_u256);
SpaceCreated(
info::get_contract_address(),
_owner,
Expand Down Expand Up @@ -548,7 +548,7 @@ mod Space {
allowed_strategies: u256
) -> u256 {
user_strategies.assert_no_duplicate_indices();
let mut total_voting_power = u256 { low: 0_u128, high: 0_u128 };
let mut total_voting_power = 0_u256;
let mut i = 0_usize;
loop {
if i >= user_strategies.len() {
Expand Down
10 changes: 2 additions & 8 deletions starknet/src/tests/test_space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ mod tests {
// Create Proposal
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

assert(
space.next_proposal_id() == u256 { low: 2_u128, high: 0_u128 },
'next_proposal_id should be 2'
);
assert(space.next_proposal_id() == 2_u256, 'next_proposal_id should be 2');

let proposal = space.proposals(u256_from_felt252(1));
let timestamp = info::get_block_timestamp().try_into().unwrap();
Expand Down Expand Up @@ -235,10 +232,7 @@ mod tests {
vanilla_execution_strategy_address
);

assert(
space.next_proposal_id() == u256 { low: 1_u128, high: 0_u128 },
'next_proposal_id should be 1'
);
assert(space.next_proposal_id() == 1_u256, 'next_proposal_id should be 1');

// Replace proposal validation strategy with one that always fails
let (strategy_address, _) = deploy_syscall(
Expand Down
15 changes: 3 additions & 12 deletions starknet/src/tests/test_stark_tx_auth.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ mod tests {
ArrayTrait::<felt252>::new()
);

assert(
space.next_proposal_id() == u256 { low: 2_u128, high: 0_u128 },
'next_proposal_id should be 2'
);
assert(space.next_proposal_id() == 2_u256, 'next_proposal_id should be 2');

// Update Proposal

Expand Down Expand Up @@ -202,10 +199,7 @@ mod tests {
ArrayTrait::<felt252>::new()
);

assert(
space.next_proposal_id() == u256 { low: 2_u128, high: 0_u128 },
'next_proposal_id should be 2'
);
assert(space.next_proposal_id() == 2_u256, 'next_proposal_id should be 2');

// Update Proposal

Expand Down Expand Up @@ -264,10 +258,7 @@ mod tests {
ArrayTrait::<felt252>::new()
);

assert(
space.next_proposal_id() == u256 { low: 2_u128, high: 0_u128 },
'next_proposal_id should be 2'
);
assert(space.next_proposal_id() == 2_u256, 'next_proposal_id should be 2');

// Update Proposal

Expand Down
6 changes: 3 additions & 3 deletions starknet/src/types/indexed_strategy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ impl IndexedStrategyImpl of IndexedStrategyTrait {
return ();
}

let mut bit_map = u256 { low: 0_u128, high: 0_u128 };
let mut bit_map = 0_u256;
let mut i = 0_usize;
loop {
if i >= self.len() {
break ();
}
// Check that bit at index `strats[i].index` is not set.
let s = math::pow(u256 { low: 2_u128, high: 0_u128 }, *self.at(i).index);
let s = math::pow(2_u256, *self.at(i).index);

assert((bit_map & s) == u256 { low: 1_u128, high: 0_u128 }, 'Duplicate Found');
assert((bit_map & s) == 1_u256, 'Duplicate Found');
// Update aforementioned bit.
bit_map = bit_map | s;
i += 1;
Expand Down
4 changes: 2 additions & 2 deletions starknet/src/utils/bits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl U256BitSetter of BitSetter<u256> {
/// Sets the bit at the given index to 1.
#[inline(always)]
fn set_bit(ref self: u256, index: u8, bit: bool) {
let mask = pow(u256 { low: 2_u128, high: 0_u128 }, index);
let mask = pow(2_u256, index);
if bit {
self = self | mask;
} else {
Expand All @@ -23,7 +23,7 @@ impl U256BitSetter of BitSetter<u256> {
/// Returns true if the bit at the given index is set to 1.
#[inline(always)]
fn is_bit_set(self: u256, index: u8) -> bool {
let mask = pow(u256 { low: 2_u128, high: 0_u128 }, index);
let mask = pow(2_u256, index);
(self & mask).is_non_zero()
}
}
Expand Down
2 changes: 1 addition & 1 deletion starknet/src/utils/math.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl U64Zeroable of Zeroable<u64> {
}

fn pow(base: u256, mut exp: u8) -> u256 {
let mut res = u256 { low: 1_u128, high: 0_u128 };
let mut res = 1_u256;
loop {
if exp == 0 {
break res;
Expand Down
15 changes: 6 additions & 9 deletions starknet/src/utils/signatures.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn get_domain_hash(name: felt252, version: felt252) -> u256 {
encoded_data.append(name.into());
encoded_data.append(version.into());
// TODO: chain id doesnt seem like its exposed atm, so just dummy value for now
encoded_data.append(u256 { low: 'dummy', high: 0 });
encoded_data.append('dummy'.into());
encoded_data.append(starknet::get_contract_address().into());
keccak::keccak_u256s_le_inputs(encoded_data.span())
}
Expand All @@ -221,7 +221,7 @@ fn _add_prefix_array(input: Array<u256>, mut prefix: u128) -> Array<u256> {
if i >= input.len() {
// left shift so that the prefix is in the high bits
let prefix_u256 = u256 { low: prefix, high: 0_u128 };
let shifted_prefix = prefix_u256 * pow(u256 { low: 2_u128, high: 0_u128 }, 112_u8);
let shifted_prefix = prefix_u256 * pow(2_u256, 112_u8);
out.append(shifted_prefix);
break ();
}
Expand All @@ -238,14 +238,11 @@ fn _add_prefix_array(input: Array<u256>, mut prefix: u128) -> Array<u256> {

// prefixes a 16 bit prefix to a 128 bit input, returning the result and a carry if it overflows 128 bits
fn _add_prefix_u128(input: u128, prefix: u128) -> (u128, u128) {
let prefix_u256 = u256 { low: prefix, high: 0_u128 };
let shifted_prefix = prefix_u256 * pow(u256 { low: 2_u128, high: 0_u128 }, 128_u8);
let with_prefix = u256 { low: input, high: 0_u128 } + shifted_prefix;
let overflow_mask = pow(u256 { low: 2_u128, high: 0_u128 }, 16_u8) - u256 {
low: 1_u128, high: 0_u128
};
let shifted_prefix = prefix.into() * pow(2_u256, 128_u8);
let with_prefix = input.into() + shifted_prefix;
let overflow_mask = pow(2_u256, 16_u8) - 1_u256;
let carry = with_prefix & overflow_mask;
// Removing the carry and shifting back. The result fits in 128 bits.
let out = ((with_prefix - carry) / pow(u256 { low: 2_u128, high: 0_u128 }, 16_u8));
let out = ((with_prefix - carry) / pow(2_u256, 16_u8));
(out.low, carry.low)
}
2 changes: 1 addition & 1 deletion starknet/src/voting_strategies/vanilla.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod VanillaVotingStrategy {
params: Array<felt252>,
user_params: Array<felt252>,
) -> u256 {
u256 { low: 1_u128, high: 0_u128 }
1_u256
}
}
}