Skip to content

Commit

Permalink
WIP working
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Mar 12, 2024
1 parent 90585ff commit 23077c4
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 92 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
####
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref/blake2s-ref.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand Down
1 change: 1 addition & 0 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ panic = "abort"

[profile.dev]
panic = "abort"
debug=true

2 changes: 1 addition & 1 deletion app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ parser_error_t from_bytes_wide(const uint8_t input[64], uint8_t output[32]);
// parser_error_t scalar_multiplication2(const uint8_t input[32], constant_key_t key, uint8_t output[160]);
parser_error_t scalar_multiplication(const uint8_t input[32], constant_key_t key, uint8_t output[32]);

parser_error_t scalar_multiplication2(const uint8_t scalar_ptr[32], uint8_t output_ptr[32]);
parser_error_t scalar_multiplication2(const uint8_t scalar_ptr[64], uint8_t output_ptr[32]);

parser_error_t from_bytes_wide2(const uint8_t input[32], uint8_t output[160]);
2 changes: 2 additions & 0 deletions app/rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub const SPENDING_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_unche
)
.to_niels();



pub const PROOF_GENERATION_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_unchecked(
Fq::from_raw([
0x3af2_dbef_b96e_2571,
Expand Down
64 changes: 48 additions & 16 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,81 @@ pub extern "C" fn from_bytes_wide(input: &[u8; 64], output: &mut [u8; 32]) -> Pa
// TODO add checks
let result = Fr::from_bytes_wide(input).to_bytes();
output.copy_from_slice(&result[0..32]);

let key_point = constants::SPENDING_KEY_GENERATOR;
let asd = key_point.multiply_bits(&result);
let mut isEqual = false;
if (asd == asd) {
isEqual = true;
let serialized = asd;
// if (serialized == [0u8; 32]) {
// isEqual = false;
// }
}

ParserError::ParserOk
}


#[no_mangle]
pub extern "C" fn scalar_multiplication2(scalar_ptr: *const [u8; 32], output_ptr: *mut [u8; 32]) {
pub extern "C" fn scalar_multiplication2(scalar_ptr: *const [u8; 64], output_ptr: *mut [u8; 32]) {

let scalar_bytes = unsafe { &*scalar_ptr };
let spend_authorizing_key = Fr::from_bytes_wide(scalar_bytes);
let spend_authorizing_key_bytes = spend_authorizing_key.to_bytes();
let extendedPoint = constants::SPENDING_KEY_GENERATOR.multiply_bits(&spend_authorizing_key_bytes);
let result = AffinePoint::from(&extendedPoint);

unsafe {
let output_slice = &mut *output_ptr;
// output.copy_from_slice(&result.get_u().to_bytes()[0..32]);
output_slice.copy_from_slice(&result.to_bytes());
}


// let scalar = Fr::from_bytes_wide(scalar_ptr);
// let scalar = unsafe { &*scalar_ptr };
// let output = unsafe { &mut *output_ptr };
// let v = constants::SPENDING_KEY_GENERATOR.multiply_bits(scalar);
// // let v = constants::SPENDING_KEY_GENERATOR * scalar;
// let tmp = AffinePoint::from(v).to_bytes();
// output.copy_from_slice(&tmp);

// let spend_authorizing_key =
// jubjub::Fr::from_bytes_wide(&Self::convert_key(spending_key, 0));
}

#[no_mangle]
pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, output: *mut [u8; 32]) -> ParserError {
let key_point = match key {
ConstantKey::SpendingKeyGenerator => &constants::SPENDING_KEY_GENERATOR,
ConstantKey::ProofGenerationKeyGenerator => &constants::PROOF_GENERATION_KEY_GENERATOR,
ConstantKey::SpendingKeyGenerator => constants::SPENDING_KEY_GENERATOR,
ConstantKey::ProofGenerationKeyGenerator => constants::PROOF_GENERATION_KEY_GENERATOR,
};

// let key_point = constants::SPENDING_KEY_GENERATOR;
// let result = AffinePoint::from(key_point.multiply_bits(input)).to_bytes();
// let tmpConstant = constants::SPENDING_KEY_GENERATOR;
// let tmpBool = false;
// if (tmpConstant.isEqual(key_point)) {
// tmpBool = true;
// }
let extendedPoint = key_point.multiply_bits(input);
let result = AffinePoint::from(&extendedPoint);
let u = Fq::from_bytes(&result.get_u().to_bytes()).unwrap();
let v = Fq::from_bytes(&result.get_v().to_bytes());
let result_bytes = result.to_bytes();
let subgroup = SubgroupPoint::from_raw_unchecked(result.get_u(), result.get_v());
let subgroup2 = SubgroupPoint::from_raw_unchecked(result.get_v(), result.get_u());
let mut isEqual = false;
// let tmp_u = result.get_u() + result.get_v();
if (subgroup == subgroup2) {
isEqual == true;
// result_bytes.fill(0);
}
// let u = Fq::from_bytes(&result.get_u().to_bytes()).unwrap();
// let v = Fq::from_bytes(&result.get_v().to_bytes());
// let result_bytes = result.to_bytes();
// let subgroup = SubgroupPoint::from_raw_unchecked(result.get_u(), result.get_v());
// let subgroup2 = SubgroupPoint::from_raw_unchecked(result.get_v(), result.get_u());
// let mut isEqual = false;
// // let tmp_u = result.get_u() + result.get_v();
// if (subgroup == subgroup2) {
// isEqual == true;
// // result_bytes.fill(0);
// }

unsafe {
let output_slice = &mut *output;
// output.copy_from_slice(&result.get_u().to_bytes()[0..32]);
output_slice.copy_from_slice(&u.to_bytes());
output_slice.copy_from_slice(&result.to_bytes());
}

// unsafe {
Expand Down
60 changes: 58 additions & 2 deletions app/src/crypto_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,71 @@ parser_error_t convertKey(const uint8_t spendingKey[32], const uint8_t modifier,
from_bytes_wide(output, outputKey);
swap_endian(outputKey, 32);
} else {
memcpy(output, outputKey, 32);
memcpy(outputKey, output, 32);
}

#endif
return parser_ok;
}

parser_error_t generate_key(const uint8_t spendingKey[32], constant_key_t key, uint8_t output[32]) {
parser_error_t generate_key(uint8_t spendingKey[32], constant_key_t key, uint8_t output[32]) {
swap_endian(spendingKey, 32);
scalar_multiplication(spendingKey, key, output);
return parser_ok;
}

parser_error_t ft_convertKey(const uint8_t spendingKey[32], const uint8_t modifier, uint8_t outputKey[32]) {

blake2b_state state = {0};
uint8_t output[64] = {0};
blake2b_init_with_personalization(&state, BLAKE2B_OUTPUT_LEN, PERSONALIZATION_ASK);
blake2b_update(&state, spendingKey, 32);
blake2b_update(&state, &modifier, 1);
blake2b_final(&state, output, sizeof(output));

// from_bytes_wide(output, outputKey);
// swap_endian(outputKey, 32);

scalar_multiplication2(output, outputKey);

return parser_ok;
}

parser_error_t computeIVK(const ak_t ak, const nk_t nk, ivk_t ivk) {
blake2s_state state;
blake2s_init_with_personalization(&state, 32, CRH_IVK_PERSONALIZATION, 8);
blake2s_update(&state, ak, 32);
blake2s_update(&state, nk, 32);
blake2s_final(&state, ivk, 32);
swap_endian(ivk, 32);
return parser_ok;
}

#if 0
pub fn hash_viewing_key(
authorizing_key: &SubgroupPoint,
nullifier_deriving_key: &SubgroupPoint,
) -> Result<jubjub::Fr, IronfishError> {
let mut view_key_contents = [0; 64];
view_key_contents[0..32].copy_from_slice(&authorizing_key.to_bytes());
view_key_contents[32..64].copy_from_slice(&nullifier_deriving_key.to_bytes());
// let mut hasher = Blake2s::with_params(32, &[], &[], CRH_IVK_PERSONALIZATION);

let mut hash_result = [0; 32];
hash_result.copy_from_slice(
Blake2s::new()
.hash_length(32)
.personal(CRH_IVK_PERSONALIZATION)
.hash(&view_key_contents)
.as_bytes(),
);
// Drop the last five bits, so it can be interpreted as a scalar.
hash_result[31] &= 0b0000_0111;
if hash_result == [0; 32] {
return Err(IronfishError::new(IronfishErrorKind::InvalidViewingKey));
}
let scalar = read_scalar(&hash_result[..])?;
Ok(scalar)
}
#endif

6 changes: 5 additions & 1 deletion app/src/crypto_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ extern "C" {
// parser_error_t computeASK(const spending_key_t spendingKey, const uint8_t modifier, ask_t *ask);
// parser_error_t computeASK(const uint8_t spendingKey[32], const uint8_t modifier, ask_t *ask);
parser_error_t convertKey(const uint8_t spendingKey[32], const uint8_t modifier, uint8_t outputKey[32], bool reduceWideByte);
parser_error_t generate_key(const uint8_t spendingKey[32], constant_key_t key, uint8_t output[160]);
parser_error_t generate_key(uint8_t spendingKey[32], constant_key_t key, uint8_t output[160]);
//parser_error_t generate_key(const uint8_t spendingKey[32], constant_key_t key, uint8_t *output);

parser_error_t ft_convertKey(const uint8_t spendingKey[32], const uint8_t modifier, uint8_t outputKey[32]);

parser_error_t computeIVK(const ak_t ak, const nk_t nk, ivk_t ivk);

#ifdef __cplusplus
}
#endif
3 changes: 3 additions & 0 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ typedef uint8_t nsk_t[32];
typedef uint8_t ovk_t[32];
typedef uint8_t ivk_t[32];

typedef uint8_t ak_t[32];
typedef uint8_t nk_t[32];

typedef uint8_t blake2b_hash_t[64];


Expand Down
1 change: 1 addition & 0 deletions app/src/keys_personalizations.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const uint8_t MODIFIER_NSK = 0x01;
const uint8_t BLAKE2B_OUTPUT_LEN = 64;

const char PERSONALIZATION_ASK[16] = "Iron Fish Money ";
const char CRH_IVK_PERSONALIZATION[8] = "Zcashivk";


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 23077c4

Please sign in to comment.