Skip to content

Commit

Permalink
Merge branch 'develop' into chore_migrate_to_array_macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Orland0x authored Aug 17, 2023
2 parents cf8c244 + c376414 commit 340e4dd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 39 deletions.
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 @@ -137,10 +137,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 @@ -222,10 +219,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 @@ -75,10 +75,7 @@ mod tests {
space.contract_address, author, vanilla_execution_strategy, array![], array![]
);

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 @@ -178,10 +175,7 @@ mod tests {
space.contract_address, author, vanilla_execution_strategy, array![], array![]
);

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 @@ -231,10 +225,7 @@ mod tests {
space.contract_address, author, vanilla_execution_strategy, array![], array![]
);

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
13 changes: 5 additions & 8 deletions starknet/src/utils/signatures.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,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 @@ -237,14 +237,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
}
}
}

0 comments on commit 340e4dd

Please sign in to comment.