Skip to content

Commit

Permalink
Bump starknet-types-core version + use the lib's poseidon hash (#1734)
Browse files Browse the repository at this point in the history
* bump starknet-types-core to 0.1.1

* Use core types poseidon

* Fix

* Add changelog entry

* Update core types version
  • Loading branch information
fmoletta authored May 30, 2024
1 parent 88031c7 commit 0f4cfc2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* Bump `starknet-types-core` version + Use the lib's pedersen hash [#1734](https://github.com/lambdaclass/cairo-vm/pull/1734)

* refactor: Add boolean method Cairo1RunConfig::copy_to_output + Update Doc [#1778](https://github.com/lambdaclass/cairo-vm/pull/1778)

* feat: Filter implicit arguments from return value in cairo1-run crate [#1775](https://github.com/lambdaclass/cairo-vm/pull/1775)
Expand Down
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ keccak = { workspace = true }
hashbrown = { workspace = true }
anyhow = { workspace = true }
thiserror-no-std = { workspace = true }
starknet-types-core = { version = "0.1.0", default-features = false, features = ["serde", "curve", "num-traits", "hash"] }
starknet-types-core = { version = "0.1.2", default-features = false, features = ["serde", "curve", "num-traits", "hash"] }

# only for std
num-prime = { version = "0.4.3", features = ["big-int"], optional = true }
Expand Down
30 changes: 12 additions & 18 deletions vm/src/vm/runners/builtin_runner/poseidon.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::air_private_input::{PrivateInput, PrivateInputPoseidonState};
use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*};
use crate::types::builtin_name::BuiltinName;
use crate::types::errors::math_errors::MathError;
use crate::types::instance_definitions::poseidon_instance_def::{
CELLS_PER_POSEIDON, INPUT_CELLS_PER_POSEIDON,
};
Expand All @@ -12,7 +11,7 @@ use crate::vm::vm_memory::memory::Memory;
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use crate::Felt252;
use num_integer::div_ceil;
use starknet_crypto::{poseidon_permute_comp, FieldElement};
use starknet_types_core::hash::Poseidon;

#[derive(Debug, Clone)]
pub struct PoseidonBuiltinRunner {
Expand Down Expand Up @@ -76,28 +75,23 @@ impl PoseidonBuiltinRunner {
for i in 0..INPUT_CELLS_PER_POSEIDON as usize {
let m_index = (first_input_addr + i)?;
let val = match memory.get(&m_index) {
Some(value) => {
let num = value
.get_int_ref()
.ok_or(RunnerError::BuiltinExpectedInteger(Box::new((
BuiltinName::poseidon,
(first_input_addr + i)?,
))))?;
FieldElement::from_bytes_be(&num.to_bytes_be())
.map_err(|_| MathError::ByteConversionError)?
}
Some(value) => *value
.get_int_ref()
.ok_or(RunnerError::BuiltinExpectedInteger(Box::new((
BuiltinName::poseidon,
m_index,
))))?,
_ => return Ok(None),
};
input_felts.push(val)
}
// n_input_cells is fixed to 3, so this try_into will never fail
let mut poseidon_state: [FieldElement; 3] = input_felts.try_into().unwrap();
poseidon_permute_comp(&mut poseidon_state);
let mut poseidon_state: [Felt252; 3] = input_felts.try_into().unwrap();
Poseidon::hades_permutation(&mut poseidon_state);
for (i, elem) in poseidon_state.iter().enumerate() {
self.cache.borrow_mut().insert(
(first_output_addr + i)?,
Felt252::from_bytes_be(&elem.to_bytes_be()),
);
self.cache
.borrow_mut()
.insert((first_output_addr + i)?, *elem);
}

Ok(self.cache.borrow().get(&address).map(|x| x.into()))
Expand Down

0 comments on commit 0f4cfc2

Please sign in to comment.