ADDED: ed25519_keypair_public_key/2, relating a key pair to its public key

This commit is contained in:
Markus Triska
2020-05-20 23:32:46 +02:00
parent e254d84710
commit 2845f55157
3 changed files with 42 additions and 18 deletions

View File

@@ -294,7 +294,8 @@ pub enum SystemClauseType {
CryptoDataDecrypt, CryptoDataDecrypt,
Ed25519Sign, Ed25519Sign,
Ed25519Verify, Ed25519Verify,
Ed25519NewKeyPair Ed25519NewKeyPair,
Ed25519KeyPairPublicKey
} }
impl SystemClauseType { impl SystemClauseType {
@@ -485,7 +486,8 @@ impl SystemClauseType {
&SystemClauseType::CryptoDataDecrypt => clause_name!("$crypto_data_decrypt"), &SystemClauseType::CryptoDataDecrypt => clause_name!("$crypto_data_decrypt"),
&SystemClauseType::Ed25519Sign => clause_name!("$ed25519_sign"), &SystemClauseType::Ed25519Sign => clause_name!("$ed25519_sign"),
&SystemClauseType::Ed25519Verify => clause_name!("$ed25519_verify"), &SystemClauseType::Ed25519Verify => clause_name!("$ed25519_verify"),
&SystemClauseType::Ed25519NewKeyPair => clause_name!("$ed25519_new_keypair") &SystemClauseType::Ed25519NewKeyPair => clause_name!("$ed25519_new_keypair"),
&SystemClauseType::Ed25519KeyPairPublicKey => clause_name!("$ed25519_keypair_public_key")
} }
} }
@@ -657,6 +659,7 @@ impl SystemClauseType {
("$ed25519_sign", 3) => Some(SystemClauseType::Ed25519Sign), ("$ed25519_sign", 3) => Some(SystemClauseType::Ed25519Sign),
("$ed25519_verify", 3) => Some(SystemClauseType::Ed25519Verify), ("$ed25519_verify", 3) => Some(SystemClauseType::Ed25519Verify),
("$ed25519_new_keypair", 1) => Some(SystemClauseType::Ed25519NewKeyPair), ("$ed25519_new_keypair", 1) => Some(SystemClauseType::Ed25519NewKeyPair),
("$ed25519_keypair_public_key", 2) => Some(SystemClauseType::Ed25519KeyPairPublicKey),
_ => None, _ => None,
} }
} }

View File

@@ -13,21 +13,22 @@
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(crypto, :- module(crypto,
[hex_bytes/2, % ?Hex, ?Bytes [hex_bytes/2, % ?Hex, ?Bytes
crypto_n_random_bytes/2, % +N, -Bytes crypto_n_random_bytes/2, % +N, -Bytes
crypto_data_hash/3, % +Data, -Hash, +Options crypto_data_hash/3, % +Data, -Hash, +Options
crypto_data_hkdf/4, % +Data, +Length, -Bytes, +Options crypto_data_hkdf/4, % +Data, +Length, -Bytes, +Options
crypto_password_hash/2, % +Password, ?Hash crypto_password_hash/2, % +Password, ?Hash
crypto_password_hash/3, % +Password, -Hash, +Options crypto_password_hash/3, % +Password, -Hash, +Options
crypto_data_encrypt/6, % +PlainText, +Algorithm, +Key, +IV, -CipherText, +Options crypto_data_encrypt/6, % +PlainText, +Algorithm, +Key, +IV, -CipherText, +Options
crypto_data_decrypt/6, % +CipherText, +Algorithm, +Key, +IV, -PlainText, +Options crypto_data_decrypt/6, % +CipherText, +Algorithm, +Key, +IV, -PlainText, +Options
ed25519_new_keypair/1, % -KeyPair ed25519_new_keypair/1, % -KeyPair
ed25519_sign/4, % +PrivateKey, +Data, -Signature, +Options ed25519_keypair_public_key/2, % +KeyPair, +PublicKey
ed25519_verify/4, % +PublicKey, +Data, -Signature, +Options ed25519_sign/4, % +PrivateKey, +Data, -Signature, +Options
crypto_name_curve/2, % +Name, -Curve ed25519_verify/4, % +PublicKey, +Data, -Signature, +Options
crypto_curve_order/2, % +Curve, -Order crypto_name_curve/2, % +Name, -Curve
crypto_curve_generator/2, % +Curve, -Generator crypto_curve_order/2, % +Curve, -Order
crypto_curve_scalar_mult/4 % +Curve, +Scalar, +Point, -Result crypto_curve_generator/2, % +Curve, -Generator
crypto_curve_scalar_mult/4 % +Curve, +Scalar, +Point, -Result
]). ]).
:- use_module(library(error)). :- use_module(library(error)).
@@ -663,6 +664,10 @@ encoding_bytes(utf8, Cs, Bs) :-
ed25519_new_keypair(Pair) :- ed25519_new_keypair(Pair) :-
'$ed25519_new_keypair'(Pair). '$ed25519_new_keypair'(Pair).
ed25519_keypair_public_key(Pair0, PublicKey) :-
encoding_bytes(octet, Pair0, Pair),
'$ed25519_keypair_public_key'(Pair, PublicKey).
ed25519_sign(Key0, Data0, Signature, Options) :- ed25519_sign(Key0, Data0, Signature, Options) :-
options_data_bytes(Options, Data0, Data), options_data_bytes(Options, Data0, Data),
encoding_bytes(octet, Key0, Key), encoding_bytes(octet, Key0, Key),

View File

@@ -40,7 +40,7 @@ use crate::crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode}; use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode};
use ring::rand::{SecureRandom, SystemRandom}; use ring::rand::{SecureRandom, SystemRandom};
use ring::{digest,hkdf,pbkdf2,aead,signature}; use ring::{digest,hkdf,pbkdf2,aead,signature::{self,KeyPair}};
use ripemd160::{Ripemd160, Digest}; use ripemd160::{Ripemd160, Digest};
use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512};
use blake2::{Blake2s, Blake2b}; use blake2::{Blake2s, Blake2b};
@@ -5457,6 +5457,22 @@ impl MachineState {
self.unify(self[temp_v!(1)], complete_string); self.unify(self[temp_v!(1)], complete_string);
} }
&SystemClauseType::Ed25519KeyPairPublicKey => {
let stub1 = MachineError::functor_stub(clause_name!("ed25519_keypair_public_key"), 2);
let bytes = self.integers_to_bytevec(temp_v!(1), stub1);
let key_pair = match signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(&bytes) {
Ok(kp) => { kp }
_ => { self.fail = true; return Ok(()); }
};
let complete_string = {
let buffer = String::from_iter(key_pair.public_key().as_ref().iter().map(|b| *b as char));
self.heap.put_complete_string(&buffer)
};
self.unify(self[temp_v!(2)], complete_string);
}
&SystemClauseType::Ed25519Sign => { &SystemClauseType::Ed25519Sign => {
let stub1 = MachineError::functor_stub(clause_name!("ed25519_sign"), 4); let stub1 = MachineError::functor_stub(clause_name!("ed25519_sign"), 4);
let key = self.integers_to_bytevec(temp_v!(1), stub1); let key = self.integers_to_bytevec(temp_v!(1), stub1);